diff --git a/examples/preprocessing/movement_compensation.py b/examples/preprocessing/movement_compensation.py index 146a8b6f290..4577a7e0a51 100644 --- a/examples/preprocessing/movement_compensation.py +++ b/examples/preprocessing/movement_compensation.py @@ -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)") diff --git a/examples/preprocessing/otp.py b/examples/preprocessing/otp.py index 4f2d7619ab8..a7d45d4cbc7 100644 --- a/examples/preprocessing/otp.py +++ b/examples/preprocessing/otp.py @@ -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 # diff --git a/mne/preprocessing/maxwell.py b/mne/preprocessing/maxwell.py index 2e71219c127..47c5b52e0c6 100644 --- a/mne/preprocessing/maxwell.py +++ b/mne/preprocessing/maxwell.py @@ -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"] @@ -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( diff --git a/mne/preprocessing/tests/test_maxwell.py b/mne/preprocessing/tests/test_maxwell.py index 2f279440d1c..961dbaadcd3 100644 --- a/mne/preprocessing/tests/test_maxwell.py +++ b/mne/preprocessing/tests/test_maxwell.py @@ -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() @@ -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 diff --git a/tutorials/inverse/85_brainstorm_phantom_ctf.py b/tutorials/inverse/85_brainstorm_phantom_ctf.py index 6d9db3408ee..1c0312e4f29 100644 --- a/tutorials/inverse/85_brainstorm_phantom_ctf.py +++ b/tutorials/inverse/85_brainstorm_phantom_ctf.py @@ -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()