diff --git a/doc/changes/devel/bugfix.rst b/doc/changes/devel/bugfix.rst new file mode 100644 index 00000000000..3afe215c57e --- /dev/null +++ b/doc/changes/devel/bugfix.rst @@ -0,0 +1 @@ +Fix bug in :func:`~mne.preprocessing.maxwell_filter_prepare_emptyroom` where a difference in sampling frequencies between data and emptyroom files was ignored, by `Daniel McCloy`_. \ No newline at end of file diff --git a/mne/preprocessing/maxwell.py b/mne/preprocessing/maxwell.py index 1b2c7c459ef..36a58535b16 100644 --- a/mne/preprocessing/maxwell.py +++ b/mne/preprocessing/maxwell.py @@ -181,7 +181,8 @@ def maxwell_filter_prepare_emptyroom( # handle first_samp raw_er_prepared.annotations.onset += raw.first_time - raw_er_prepared.first_time - raw_er_prepared._cropped_samp = raw._cropped_samp + # don't copy _cropped_samp directly, as sfreqs may differ + raw_er_prepared._cropped_samp = raw_er_prepared.time_as_index(raw.first_time).item() # handle annotations if annotations != "keep": diff --git a/mne/preprocessing/tests/test_maxwell.py b/mne/preprocessing/tests/test_maxwell.py index 85b8a153c13..34f64797a27 100644 --- a/mne/preprocessing/tests/test_maxwell.py +++ b/mne/preprocessing/tests/test_maxwell.py @@ -1820,8 +1820,9 @@ def test_prepare_emptyroom_bads(bads): @pytest.mark.parametrize("set_annot_when", ("before", "after")) @pytest.mark.parametrize("raw_meas_date", ("orig", None)) @pytest.mark.parametrize("raw_er_meas_date", ("orig", None)) +@pytest.mark.parametrize("equal_sfreq", (False, True)) def test_prepare_emptyroom_annot_first_samp( - set_annot_when, raw_meas_date, raw_er_meas_date + set_annot_when, raw_meas_date, raw_er_meas_date, equal_sfreq ): """Test prepare_emptyroom.""" raw = read_raw_fif(raw_fname, allow_maxshield="yes", verbose=False) @@ -1861,12 +1862,15 @@ def test_prepare_emptyroom_annot_first_samp( assert set_annot_when == "after" meas_date = "from_raw" want_date = raw.info["meas_date"] + if not equal_sfreq: + with raw_er.info._unlock(): + raw_er.info["sfreq"] -= 100 raw_er_prepared = maxwell_filter_prepare_emptyroom( raw_er=raw_er, raw=raw, meas_date=meas_date, emit_warning=True ) assert raw_er.first_samp == raw_er_first_samp_orig assert raw_er_prepared.info["meas_date"] == want_date - assert raw_er_prepared.first_samp == raw.first_samp + assert raw_er_prepared.first_time == raw.first_time # Ensure (movement) annotations carry over regardless of whether they're # set before or after preparation @@ -1878,7 +1882,8 @@ def test_prepare_emptyroom_annot_first_samp( prop_bad = np.isnan(raw.get_data([0], reject_by_annotation="nan")).mean() assert 0.3 < prop_bad < 0.4 assert len(raw_er_prepared.annotations) == want_annot - prop_bad_er = np.isnan( - raw_er_prepared.get_data([0], reject_by_annotation="nan") - ).mean() - assert_allclose(prop_bad, prop_bad_er) + if equal_sfreq: + prop_bad_er = np.isnan( + raw_er_prepared.get_data([0], reject_by_annotation="nan") + ).mean() + assert_allclose(prop_bad, prop_bad_er)