Skip to content

Commit a362e5a

Browse files
fix first_samp matching in maxwell_filter_prepare_emptyroom (#12760)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent dd6e155 commit a362e5a

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

doc/changes/devel/12760.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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`_.

mne/preprocessing/maxwell.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ def maxwell_filter_prepare_emptyroom(
181181

182182
# handle first_samp
183183
raw_er_prepared.annotations.onset += raw.first_time - raw_er_prepared.first_time
184-
raw_er_prepared._cropped_samp = raw._cropped_samp
184+
# don't copy _cropped_samp directly, as sfreqs may differ
185+
raw_er_prepared._cropped_samp = raw_er_prepared.time_as_index(raw.first_time).item()
185186

186187
# handle annotations
187188
if annotations != "keep":

mne/preprocessing/tests/test_maxwell.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,8 +1820,9 @@ def test_prepare_emptyroom_bads(bads):
18201820
@pytest.mark.parametrize("set_annot_when", ("before", "after"))
18211821
@pytest.mark.parametrize("raw_meas_date", ("orig", None))
18221822
@pytest.mark.parametrize("raw_er_meas_date", ("orig", None))
1823+
@pytest.mark.parametrize("equal_sfreq", (False, True))
18231824
def test_prepare_emptyroom_annot_first_samp(
1824-
set_annot_when, raw_meas_date, raw_er_meas_date
1825+
set_annot_when, raw_meas_date, raw_er_meas_date, equal_sfreq
18251826
):
18261827
"""Test prepare_emptyroom."""
18271828
raw = read_raw_fif(raw_fname, allow_maxshield="yes", verbose=False)
@@ -1861,12 +1862,15 @@ def test_prepare_emptyroom_annot_first_samp(
18611862
assert set_annot_when == "after"
18621863
meas_date = "from_raw"
18631864
want_date = raw.info["meas_date"]
1865+
if not equal_sfreq:
1866+
with raw_er.info._unlock():
1867+
raw_er.info["sfreq"] -= 100
18641868
raw_er_prepared = maxwell_filter_prepare_emptyroom(
18651869
raw_er=raw_er, raw=raw, meas_date=meas_date, emit_warning=True
18661870
)
18671871
assert raw_er.first_samp == raw_er_first_samp_orig
18681872
assert raw_er_prepared.info["meas_date"] == want_date
1869-
assert raw_er_prepared.first_samp == raw.first_samp
1873+
assert raw_er_prepared.first_time == raw.first_time
18701874

18711875
# Ensure (movement) annotations carry over regardless of whether they're
18721876
# set before or after preparation
@@ -1878,7 +1882,8 @@ def test_prepare_emptyroom_annot_first_samp(
18781882
prop_bad = np.isnan(raw.get_data([0], reject_by_annotation="nan")).mean()
18791883
assert 0.3 < prop_bad < 0.4
18801884
assert len(raw_er_prepared.annotations) == want_annot
1881-
prop_bad_er = np.isnan(
1882-
raw_er_prepared.get_data([0], reject_by_annotation="nan")
1883-
).mean()
1884-
assert_allclose(prop_bad, prop_bad_er)
1885+
if equal_sfreq:
1886+
prop_bad_er = np.isnan(
1887+
raw_er_prepared.get_data([0], reject_by_annotation="nan")
1888+
).mean()
1889+
assert_allclose(prop_bad, prop_bad_er)

0 commit comments

Comments
 (0)