From f35685abf9c0aabc342a6e32afe253c2be06c91b Mon Sep 17 00:00:00 2001 From: Thomas Samuel Binns Date: Sat, 14 Dec 2024 20:23:44 +0000 Subject: [PATCH 1/5] Fix EpochsTFRArray default drop log initialisation --- mne/time_frequency/tests/test_tfr.py | 28 ++++++++++++++++------------ mne/time_frequency/tfr.py | 8 +++++++- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/mne/time_frequency/tests/test_tfr.py b/mne/time_frequency/tests/test_tfr.py index cd3a97ab90a..59e3ef8da91 100644 --- a/mne/time_frequency/tests/test_tfr.py +++ b/mne/time_frequency/tests/test_tfr.py @@ -1218,8 +1218,8 @@ def test_averaging_freqsandtimes_epochsTFR(): avgpower = power.average(method=lambda x: np.mean(x, axis=2), **kwargs) -@pytest.mark.parametrize("n_drop", (0, 2)) -def test_epochstfr_getitem(epochs_full, n_drop): +@pytest.mark.parametrize("n_drop, as_array", ((0, False), (0, True), (2, False))) +def test_epochstfr_getitem(epochs_full, n_drop, as_array): """Test EpochsTFR.__getitem__().""" pd = pytest.importorskip("pandas") from pandas.testing import assert_frame_equal @@ -1227,16 +1227,20 @@ def test_epochstfr_getitem(epochs_full, n_drop): epochs_full.metadata = pd.DataFrame(dict(foo=list("aaaabbb"), bar=np.arange(7))) epochs_full.drop(np.arange(n_drop)) tfr = epochs_full.compute_tfr(method="morlet", freqs=freqs_linspace) - # check that various attributes are preserved - assert_frame_equal(tfr.metadata, epochs_full.metadata) - assert epochs_full.drop_log == tfr.drop_log - for attr in ("events", "selection", "times"): - assert_array_equal(getattr(epochs_full, attr), getattr(tfr, attr)) - # test pandas query - foo_a = tfr["foo == 'a'"] - bar_3 = tfr["bar <= 3"] - assert foo_a == bar_3 - assert foo_a.shape[0] == 4 - n_drop + if not as_array: # check that various attributes are preserved + assert_frame_equal(tfr.metadata, epochs_full.metadata) + assert epochs_full.drop_log == tfr.drop_log + for attr in ("events", "selection", "times"): + assert_array_equal(getattr(epochs_full, attr), getattr(tfr, attr)) + # test pandas query + foo_a = tfr["foo == 'a'"] + bar_3 = tfr["bar <= 3"] + assert foo_a == bar_3 + assert foo_a.shape[0] == 4 - n_drop + else: # repackage to check __getitem__ also works with unspecified events, etc... + tfr = EpochsTFRArray( + info=tfr.info, data=tfr.data, times=tfr.times, freqs=tfr.freqs + ) # test integer and slice subset_ints = tfr[[0, 1, 2]] subset_slice = tfr[:3] diff --git a/mne/time_frequency/tfr.py b/mne/time_frequency/tfr.py index eaf173092bb..1fb9f3f2e07 100644 --- a/mne/time_frequency/tfr.py +++ b/mne/time_frequency/tfr.py @@ -3105,8 +3105,14 @@ def __setstate__(self, state): ).squeeze(axis=0) self.events = state.get("events", _ensure_events(fake_events)) self.event_id = state.get("event_id", _check_event_id(None, self.events)) - self.drop_log = state.get("drop_log", tuple()) self.selection = state.get("selection", np.arange(n_epochs)) + self.drop_log = state.get( + "drop_log", + tuple( + () if k in self.selection else ("IGNORED",) + for k in range(max(len(self.events), max(self.selection) + 1)) + ), + ) self._bad_dropped = True # always true, need for `equalize_event_counts()` def __next__(self, return_event_id=False): From 71beeb45e7caacf15ac8204efd4a706aade8aadd Mon Sep 17 00:00:00 2001 From: Thomas Samuel Binns Date: Thu, 19 Dec 2024 12:27:09 +0000 Subject: [PATCH 2/5] Add changelog entry --- doc/changes/devel/13028.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changes/devel/13028.bugfix.rst diff --git a/doc/changes/devel/13028.bugfix.rst b/doc/changes/devel/13028.bugfix.rst new file mode 100644 index 00000000000..13e34189eaf --- /dev/null +++ b/doc/changes/devel/13028.bugfix.rst @@ -0,0 +1 @@ +Fix epoch indexing in :class:`mne.time_frequency.EpochsTFRArray` when initialising the class with the default ``drop_log`` parameter, by `Thomas Binns`_. \ No newline at end of file From 4e61cd336d94435fbb75fa98397ed2b876bbb1a6 Mon Sep 17 00:00:00 2001 From: Thomas Samuel Binns Date: Thu, 19 Dec 2024 12:27:20 +0000 Subject: [PATCH 3/5] Update test param name --- mne/time_frequency/tests/test_tfr.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mne/time_frequency/tests/test_tfr.py b/mne/time_frequency/tests/test_tfr.py index 59e3ef8da91..e68ea9e6e18 100644 --- a/mne/time_frequency/tests/test_tfr.py +++ b/mne/time_frequency/tests/test_tfr.py @@ -1218,8 +1218,8 @@ def test_averaging_freqsandtimes_epochsTFR(): avgpower = power.average(method=lambda x: np.mean(x, axis=2), **kwargs) -@pytest.mark.parametrize("n_drop, as_array", ((0, False), (0, True), (2, False))) -def test_epochstfr_getitem(epochs_full, n_drop, as_array): +@pytest.mark.parametrize("n_drop, as_tfr_array", ((0, False), (0, True), (2, False))) +def test_epochstfr_getitem(epochs_full, n_drop, as_tfr_array): """Test EpochsTFR.__getitem__().""" pd = pytest.importorskip("pandas") from pandas.testing import assert_frame_equal @@ -1227,7 +1227,7 @@ def test_epochstfr_getitem(epochs_full, n_drop, as_array): epochs_full.metadata = pd.DataFrame(dict(foo=list("aaaabbb"), bar=np.arange(7))) epochs_full.drop(np.arange(n_drop)) tfr = epochs_full.compute_tfr(method="morlet", freqs=freqs_linspace) - if not as_array: # check that various attributes are preserved + if not as_tfr_array: # check that various attributes are preserved assert_frame_equal(tfr.metadata, epochs_full.metadata) assert epochs_full.drop_log == tfr.drop_log for attr in ("events", "selection", "times"): From 261b1beb04dbd3588c86b5c5194946940529b6bf Mon Sep 17 00:00:00 2001 From: Thomas Samuel Binns Date: Thu, 19 Dec 2024 12:38:13 +0000 Subject: [PATCH 4/5] Fix pre-commit compliance --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 50e0daaa52c..cfa7488324f 100644 --- a/README.rst +++ b/README.rst @@ -72,7 +72,7 @@ The minimum required dependencies to run MNE-Python are: .. ↓↓↓ BEGIN CORE DEPS LIST. DO NOT EDIT! HANDLED BY PRE-COMMIT HOOK ↓↓↓ -- `Python `__ ≥ 3.9 +- `Python `__ ≥ 3.10 - `NumPy `__ ≥ 1.23 - `SciPy `__ ≥ 1.9 - `Matplotlib `__ ≥ 3.6 From eb1b2937a1fc31736b3d8612c516f50c25ceb1e6 Mon Sep 17 00:00:00 2001 From: Thomas Samuel Binns Date: Fri, 20 Dec 2024 11:40:35 +0000 Subject: [PATCH 5/5] Revert "Fix pre-commit compliance" This reverts commit 261b1beb04dbd3588c86b5c5194946940529b6bf. --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index cfa7488324f..50e0daaa52c 100644 --- a/README.rst +++ b/README.rst @@ -72,7 +72,7 @@ The minimum required dependencies to run MNE-Python are: .. ↓↓↓ BEGIN CORE DEPS LIST. DO NOT EDIT! HANDLED BY PRE-COMMIT HOOK ↓↓↓ -- `Python `__ ≥ 3.10 +- `Python `__ ≥ 3.9 - `NumPy `__ ≥ 1.23 - `SciPy `__ ≥ 1.9 - `Matplotlib `__ ≥ 3.6