Skip to content

Commit

Permalink
BUG: Fix bug with movecomp and t=0 (#13132)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner authored Feb 26, 2025
1 parent c0f1665 commit 7166946
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/preprocessing/movement_compensation.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

# %%
# Third, use raw movement compensation (restores pattern).
raw_sss = maxwell_filter(raw, head_pos=head_pos)
raw_sss = maxwell_filter(raw, head_pos=head_pos, mc_interp="hann")
evoked_raw_mc = mne.Epochs(raw_sss, events, 1, -0.2, 0.8).average()
fig = evoked_raw_mc.plot_topomap(**topo_kwargs)
fig.suptitle("Moving: movement compensated (raw)")
Expand Down
4 changes: 2 additions & 2 deletions examples/preprocessing/otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Plot sensor denoising using oversampled temporal projection
===========================================================
This demonstrates denoising using the OTP algorithm :footcite:`LarsonTaulu2018`
on data with with sensor artifacts (flux jumps) and random noise.
This demonstrates denoising using the OTP algorithm :footcite:`LarsonTaulu2018` on data
with with sensor artifacts (flux jumps) and random noise.
"""
# Author: Eric Larson <[email protected]>
#
Expand Down
4 changes: 3 additions & 1 deletion mne/preprocessing/maxwell.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ def _check_pos(pos, coord_frame, raw, st_fixed):
f"first sample offset, but found {t[0]:0.4f} < {raw._first_time:0.4f}"
)
t = t - raw._first_time
if len(t) == 0 or t[0] > 0:
if len(t) == 0 or t[0] >= 0.5 / raw.info["sfreq"]:
# Prepend the existing dev_head_t to make movecomp easier
t = np.concatenate([[0.0], t])
trans = raw.info["dev_head_t"]
Expand All @@ -1258,6 +1258,8 @@ def _check_pos(pos, coord_frame, raw, st_fixed):
[t[[0]], rot_to_quat(trans[:3, :3]), trans[:3, 3], [0, 0, 0]]
)
pos = np.concatenate([dev_head_pos[np.newaxis], pos])
# now that we've either prepended dev_head_t or know it's zero-like, make it zero
t[0] = 0
max_dist = np.sqrt(np.sum(pos[:, 4:7] ** 2, axis=1)).max()
if max_dist > 1.0:
warn(
Expand Down
7 changes: 6 additions & 1 deletion mne/preprocessing/tests/test_maxwell.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def read_crop(fname, lims=(0, None)):

@pytest.mark.slowtest
@testing.requires_testing_data
def test_movement_compensation(tmp_path):
def test_movement_compensation_basic(tmp_path):
"""Test movement compensation."""
lims = (0, 4)
raw = read_crop(raw_fname, lims).load_data()
Expand Down Expand Up @@ -303,6 +303,11 @@ def test_movement_compensation(tmp_path):
assert_meg_snr(
raw_sss_tweak, raw_sss.copy().crop(0, 0.05), 1.4, 8.0, chpi_med_tol=5
)
# smoke test a zero-like t[0]
head_pos_bad[0, 0] = raw._first_time + 0.1 / raw.info["sfreq"]
maxwell_filter(
raw.copy().crop(0, 0.05), head_pos=head_pos_bad, origin=mf_head_origin
)


@pytest.mark.slowtest
Expand Down
2 changes: 1 addition & 1 deletion tutorials/inverse/85_brainstorm_phantom_ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
# Maxwell filtering:

raw.apply_gradient_compensation(0) # must un-do software compensation first
mf_kwargs = dict(origin=(0.0, 0.0, 0.0), st_duration=10.0)
mf_kwargs = dict(origin=(0.0, 0.0, 0.0), st_duration=10.0, st_overlap=True)
raw = mne.preprocessing.maxwell_filter(raw, **mf_kwargs)
raw.plot()

Expand Down

0 comments on commit 7166946

Please sign in to comment.