Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix bug with interval calculation #13062

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ stages:
PYTHONIOENCODING: 'utf-8'
AZURE_CI_WINDOWS: 'true'
PYTHON_ARCH: 'x64'
timeoutInMinutes: 75
timeoutInMinutes: 80
strategy:
maxParallel: 4
matrix:
Expand Down
1 change: 1 addition & 0 deletions doc/changes/devel/13062.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix computation of time intervals in :func:`mne.preprocessing.compute_fine_calibration` by `Eric Larson`_.
15 changes: 9 additions & 6 deletions mne/preprocessing/_fine_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ def compute_fine_calibration(
# 1. Rotate surface normals using magnetometer information (if present)
#
cals = np.ones(len(info["ch_names"]))
time_idxs = raw.time_as_index(np.arange(0.0, raw.times[-1], t_window))
if len(time_idxs) <= 1:
time_idxs = np.array([0, len(raw.times)], int)
else:
time_idxs[-1] = len(raw.times)
end = len(raw.times) + 1
time_idxs = np.arange(0, end, int(round(t_window * raw.info["sfreq"])))
if len(time_idxs) == 1:
time_idxs = np.concatenate([time_idxs, [end]])
if time_idxs[-1] != end:
time_idxs[-1] = end
count = 0
locs = np.array([ch["loc"] for ch in info["chs"]])
zs = locs[mag_picks, -3:].copy()
Expand Down Expand Up @@ -388,9 +389,11 @@ def _adjust_mag_normals(info, data, origin, ext_order, *, angle_limit, err_limit
each_err = _data_err(data, S_tot, cals, axis=-1)[picks_mag]
n_bad = (each_err > err_limit).sum()
if n_bad:
bad_max = np.argmax(each_err)
reason.append(
f"{n_bad} residual{_pl(n_bad)} > {err_limit:0.1f}% "
f"(max: {each_err.max():0.2f}%)"
f"(max: {each_err[bad_max]:0.2f}% @ "
f"{info['ch_names'][picks_mag[bad_max]]})"
)
reason = ", ".join(reason)
if reason:
Expand Down
14 changes: 13 additions & 1 deletion mne/preprocessing/tests/test_fine_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from mne.preprocessing.tests.test_maxwell import _assert_shielding
from mne.transforms import _angle_dist_between_rigid
from mne.utils import object_diff
from mne.utils import catch_logging, object_diff

# Define fine calibration filepaths
data_path = testing.data_path(download=False)
Expand Down Expand Up @@ -289,3 +289,15 @@ def test_fine_cal_systems(system, tmp_path):
got_corrs = np.corrcoef([raw_data, raw_sss_data, raw_sss_cal_data])
got_corrs = got_corrs[np.triu_indices(3, 1)]
assert_allclose(got_corrs, corrs, atol=corr_tol)
if system == "fil":
with catch_logging(verbose=True) as log:
compute_fine_calibration(
raw.copy().crop(0, 0.12).pick(raw.ch_names[:12]),
t_window=0.06, # 2 segments
angle_limit=angle_limit,
err_limit=err_limit,
ext_order=2,
verbose=True,
)
log = log.getvalue()
assert "(averaging over 2 time intervals)" in log, log
Loading