From 484bd9c7c5d87c88ee739cc3ef8d5c58de5196e1 Mon Sep 17 00:00:00 2001 From: Shruti Bhale Date: Sun, 21 Dec 2025 22:56:26 +0530 Subject: [PATCH 1/8] ENH: Add on_drop_all parameter to Epochs.drop --- mne/epochs.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mne/epochs.py b/mne/epochs.py index 6c3935097bf..914ced9ccde 100644 --- a/mne/epochs.py +++ b/mne/epochs.py @@ -1515,7 +1515,7 @@ def plot_image( ) @verbose - def drop(self, indices, reason="USER", verbose=None): + def drop(self, indices, reason="USER", verbose=None, on_drop_all="warn"): """Drop epochs based on indices or boolean mask. .. note:: The indices refer to the current set of undropped epochs @@ -1568,6 +1568,16 @@ def drop(self, indices, reason="USER", verbose=None): _pl(count), ", ".join(map(str, np.sort(try_idx))), ) + + if len(self) == 0: + msg = "All epochs dropped" + if on_drop_all == 'raise': + raise ValueError(msg) + elif on_drop_all == 'warn': + warn(msg) + elif on_drop_all != 'ignore': + raise ValueError('on_drop_all must be "warn", "raise" or "ignore", ' + f'got {on_drop_all}') return self From 114fccbea0d3452e1f0bb02fe3affcfcbbc82774 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 21 Dec 2025 17:31:06 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/epochs.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mne/epochs.py b/mne/epochs.py index 914ced9ccde..9e5ae97bafe 100644 --- a/mne/epochs.py +++ b/mne/epochs.py @@ -1568,16 +1568,18 @@ def drop(self, indices, reason="USER", verbose=None, on_drop_all="warn"): _pl(count), ", ".join(map(str, np.sort(try_idx))), ) - + if len(self) == 0: msg = "All epochs dropped" - if on_drop_all == 'raise': + if on_drop_all == "raise": raise ValueError(msg) - elif on_drop_all == 'warn': + elif on_drop_all == "warn": warn(msg) - elif on_drop_all != 'ignore': - raise ValueError('on_drop_all must be "warn", "raise" or "ignore", ' - f'got {on_drop_all}') + elif on_drop_all != "ignore": + raise ValueError( + 'on_drop_all must be "warn", "raise" or "ignore", ' + f"got {on_drop_all}" + ) return self From 4d199d59b2c6b94996defe54ad973a11b0089be3 Mon Sep 17 00:00:00 2001 From: Shruti Bhale Date: Mon, 22 Dec 2025 20:51:48 +0530 Subject: [PATCH 3/8] TEST : Add test cases for on_drop_all --- mne/tests/test_epochs.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/mne/tests/test_epochs.py b/mne/tests/test_epochs.py index 91c5f902ac8..7e3072de4dd 100644 --- a/mne/tests/test_epochs.py +++ b/mne/tests/test_epochs.py @@ -5273,3 +5273,29 @@ def test_empty_error(method, epochs_empty): pytest.importorskip("pandas") with pytest.raises(RuntimeError, match="is empty."): getattr(epochs_empty.copy(), method[0])(**method[1]) + +def test_drop_all_epochs(): + """Test on_drop_all parameter in Epochs.drop.""" + # Create tiny dummy data (3 epochs) + data = np.random.randn(3, 1, 10) + info = create_info(['ch1'], 1000., 'eeg') + epochs = EpochsArray(data, info) + + # 1. Test 'warn' (default) + # We expect a warning when dropping all epochs + with pytest.warns(RuntimeWarning, match='All epochs dropped'): + epochs.copy().drop([0, 1, 2]) + + # 2. Test 'raise' + # We expect a ValueError when dropping all epochs + with pytest.raises(ValueError, match='All epochs dropped'): + epochs.copy().drop([0, 1, 2], on_drop_all='raise') + + # 3. Test 'ignore' + # Should run silently (no warning, no error) + epochs.copy().drop([0, 1, 2], on_drop_all='ignore') + + # 4. Test Typo + # We expect a ValueError because 'wrn' is not valid + with pytest.raises(ValueError, match='on_drop_all must be'): + epochs.copy().drop([0, 1, 2], on_drop_all='wrn') \ No newline at end of file From 1c84d61d01abb09a29837ad21f4200c7eec084a6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Dec 2025 16:02:01 +0000 Subject: [PATCH 4/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/tests/test_epochs.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/mne/tests/test_epochs.py b/mne/tests/test_epochs.py index 7e3072de4dd..53f7cfdbcfa 100644 --- a/mne/tests/test_epochs.py +++ b/mne/tests/test_epochs.py @@ -5274,28 +5274,29 @@ def test_empty_error(method, epochs_empty): with pytest.raises(RuntimeError, match="is empty."): getattr(epochs_empty.copy(), method[0])(**method[1]) + def test_drop_all_epochs(): """Test on_drop_all parameter in Epochs.drop.""" # Create tiny dummy data (3 epochs) data = np.random.randn(3, 1, 10) - info = create_info(['ch1'], 1000., 'eeg') + info = create_info(["ch1"], 1000.0, "eeg") epochs = EpochsArray(data, info) # 1. Test 'warn' (default) # We expect a warning when dropping all epochs - with pytest.warns(RuntimeWarning, match='All epochs dropped'): + with pytest.warns(RuntimeWarning, match="All epochs dropped"): epochs.copy().drop([0, 1, 2]) # 2. Test 'raise' # We expect a ValueError when dropping all epochs - with pytest.raises(ValueError, match='All epochs dropped'): - epochs.copy().drop([0, 1, 2], on_drop_all='raise') + with pytest.raises(ValueError, match="All epochs dropped"): + epochs.copy().drop([0, 1, 2], on_drop_all="raise") # 3. Test 'ignore' # Should run silently (no warning, no error) - epochs.copy().drop([0, 1, 2], on_drop_all='ignore') + epochs.copy().drop([0, 1, 2], on_drop_all="ignore") # 4. Test Typo # We expect a ValueError because 'wrn' is not valid - with pytest.raises(ValueError, match='on_drop_all must be'): - epochs.copy().drop([0, 1, 2], on_drop_all='wrn') \ No newline at end of file + with pytest.raises(ValueError, match="on_drop_all must be"): + epochs.copy().drop([0, 1, 2], on_drop_all="wrn") From 612816021eac320124bed46544a8aedfec2c90ef Mon Sep 17 00:00:00 2001 From: Shruti Bhale Date: Tue, 23 Dec 2025 17:42:45 +0530 Subject: [PATCH 5/8] TEST: Use RandomState and reduce number of epochs --- mne/tests/test_epochs.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mne/tests/test_epochs.py b/mne/tests/test_epochs.py index 7e3072de4dd..dbba57dab80 100644 --- a/mne/tests/test_epochs.py +++ b/mne/tests/test_epochs.py @@ -5277,25 +5277,25 @@ def test_empty_error(method, epochs_empty): def test_drop_all_epochs(): """Test on_drop_all parameter in Epochs.drop.""" # Create tiny dummy data (3 epochs) - data = np.random.randn(3, 1, 10) + data = np.random.RandomState(0).randn(1, 1, 10) info = create_info(['ch1'], 1000., 'eeg') epochs = EpochsArray(data, info) # 1. Test 'warn' (default) # We expect a warning when dropping all epochs with pytest.warns(RuntimeWarning, match='All epochs dropped'): - epochs.copy().drop([0, 1, 2]) + epochs.copy().drop([0]) # 2. Test 'raise' # We expect a ValueError when dropping all epochs with pytest.raises(ValueError, match='All epochs dropped'): - epochs.copy().drop([0, 1, 2], on_drop_all='raise') + epochs.copy().drop([0], on_drop_all='raise') # 3. Test 'ignore' # Should run silently (no warning, no error) - epochs.copy().drop([0, 1, 2], on_drop_all='ignore') + epochs.copy().drop([0], on_drop_all='ignore') # 4. Test Typo # We expect a ValueError because 'wrn' is not valid with pytest.raises(ValueError, match='on_drop_all must be'): - epochs.copy().drop([0, 1, 2], on_drop_all='wrn') \ No newline at end of file + epochs.copy().drop([0], on_drop_all='wrn') \ No newline at end of file From 79c169fa4e82423daf6dfc43f1b668478bb2e2b3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 23 Dec 2025 12:26:03 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/tests/test_epochs.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mne/tests/test_epochs.py b/mne/tests/test_epochs.py index 2dc36926bf9..5235b728d7e 100644 --- a/mne/tests/test_epochs.py +++ b/mne/tests/test_epochs.py @@ -5279,24 +5279,24 @@ def test_drop_all_epochs(): """Test on_drop_all parameter in Epochs.drop.""" # Create tiny dummy data (3 epochs) data = np.random.RandomState(0).randn(1, 1, 10) - info = create_info(['ch1'], 1000., 'eeg') + info = create_info(["ch1"], 1000.0, "eeg") epochs = EpochsArray(data, info) # 1. Test 'warn' (default) # We expect a warning when dropping all epochs - with pytest.warns(RuntimeWarning, match='All epochs dropped'): + with pytest.warns(RuntimeWarning, match="All epochs dropped"): epochs.copy().drop([0]) # 2. Test 'raise' # We expect a ValueError when dropping all epochs - with pytest.raises(ValueError, match='All epochs dropped'): - epochs.copy().drop([0], on_drop_all='raise') + with pytest.raises(ValueError, match="All epochs dropped"): + epochs.copy().drop([0], on_drop_all="raise") # 3. Test 'ignore' # Should run silently (no warning, no error) - epochs.copy().drop([0], on_drop_all='ignore') + epochs.copy().drop([0], on_drop_all="ignore") # 4. Test Typo # We expect a ValueError because 'wrn' is not valid - with pytest.raises(ValueError, match='on_drop_all must be'): - epochs.copy().drop([0], on_drop_all='wrn') + with pytest.raises(ValueError, match="on_drop_all must be"): + epochs.copy().drop([0], on_drop_all="wrn") From ca37daaba239f4a504fd0ade2301d405cafaa883 Mon Sep 17 00:00:00 2001 From: Shruti Bhale Date: Wed, 24 Dec 2025 22:11:28 +0530 Subject: [PATCH 7/8] DOC: Add changelog entry --- doc/changes/dev/13556.new.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changes/dev/13556.new.rst diff --git a/doc/changes/dev/13556.new.rst b/doc/changes/dev/13556.new.rst new file mode 100644 index 00000000000..78667b34e52 --- /dev/null +++ b/doc/changes/dev/13556.new.rst @@ -0,0 +1 @@ +Add :param:`on_drop_all` parameter to :meth:`mne.Epochs.drop` to control behavior when all epochs are dropped. \ No newline at end of file From c01f93334f6fa92d1aabf02e5d672e0fefc69c57 Mon Sep 17 00:00:00 2001 From: Shruti Bhale Date: Wed, 24 Dec 2025 22:34:49 +0530 Subject: [PATCH 8/8] DOC: Fix rst syntax in changelog --- doc/changes/dev/13556.new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/dev/13556.new.rst b/doc/changes/dev/13556.new.rst index 78667b34e52..e3a6f53cb1c 100644 --- a/doc/changes/dev/13556.new.rst +++ b/doc/changes/dev/13556.new.rst @@ -1 +1 @@ -Add :param:`on_drop_all` parameter to :meth:`mne.Epochs.drop` to control behavior when all epochs are dropped. \ No newline at end of file +Add ``on_drop_all`` parameter to :meth:`mne.Epochs.drop` to control behavior when all epochs are dropped. \ No newline at end of file