Skip to content

Commit

Permalink
BUG: Fix bug with interval calculation (#13062)
Browse files Browse the repository at this point in the history
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
larsoner and autofix-ci[bot] authored Jan 14, 2025
1 parent 5fec4e0 commit c0da91d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
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

0 comments on commit c0da91d

Please sign in to comment.