Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
484bd9c
ENH: Add on_drop_all parameter to Epochs.drop
shruti423 Dec 21, 2025
114fccb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 21, 2025
4d199d5
TEST : Add test cases for on_drop_all
shruti423 Dec 22, 2025
621a2dc
Merge branch 'warn-epochs-drop' of https://github.com/shruti423/mne-p…
shruti423 Dec 22, 2025
1c84d61
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 22, 2025
6128160
TEST: Use RandomState and reduce number of epochs
shruti423 Dec 23, 2025
6c196c3
Merge fix: Accept RandomState changes and epochs changes
shruti423 Dec 23, 2025
79c169f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 23, 2025
c6ca9cf
Merge branch 'main' into warn-epochs-drop
larsoner Dec 24, 2025
ca37daa
DOC: Add changelog entry
shruti423 Dec 24, 2025
8d6c0bc
Merge branch 'warn-epochs-drop' of https://github.com/shruti423/mne-p…
shruti423 Dec 24, 2025
c01f933
DOC: Fix rst syntax in changelog
shruti423 Dec 24, 2025
b100def
Merge branch 'main' into warn-epochs-drop
shruti423 Dec 26, 2025
a06aafe
Fix RuntimeError: check len(events) instead of len(self) during drop
shruti423 Dec 29, 2025
c708d92
Merge branch 'warn-epochs-drop' of https://github.com/shruti423/mne-p…
shruti423 Dec 29, 2025
ebb3272
Merge branch 'main' into warn-epochs-drop
shruti423 Jan 2, 2026
d13639a
Merge branch 'main' into warn-epochs-drop
shruti423 Jan 5, 2026
65a81cb
Merge branch 'main' into warn-epochs-drop
shruti423 Jan 6, 2026
c294520
Trigger CI to retry Windows tests
shruti423 Jan 6, 2026
c462435
Merge branch 'warn-epochs-drop' of https://github.com/shruti423/mne-p…
shruti423 Jan 6, 2026
8d4bf3f
Fix on_drop_all default to ignore and update docstring
shruti423 Jan 6, 2026
235b26e
Update test_drop_all_epochs to match ignore default
shruti423 Jan 6, 2026
a3b752b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/dev/13556.new.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``on_drop_all`` parameter to :meth:`mne.Epochs.drop` to control behavior when all epochs are dropped.
20 changes: 19 additions & 1 deletion mne/epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="ignore"):
"""Drop epochs based on indices or boolean mask.

.. note:: The indices refer to the current set of undropped epochs
Expand All @@ -1537,6 +1537,12 @@ def drop(self, indices, reason="USER", verbose=None):
Reason(s) for dropping the epochs ('ECG', 'timeout', 'blink' etc).
Reason(s) are applied to all indices specified.
Default: 'USER'.
on_drop_all : 'ignore' | 'warn' | 'raise'
Behavior when all epochs are dropped.
If 'ignore', no error is raised and the epochs object is empty.
If 'warn', a RuntimeWarning is emitted.
If 'raise', a ValueError is raised.
Default: 'ignore'.
%(verbose)s

Returns
Expand Down Expand Up @@ -1569,6 +1575,18 @@ def drop(self, indices, reason="USER", verbose=None):
", ".join(map(str, np.sort(try_idx))),
)

if len(self.events) == 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

def _get_epoch_from_raw(self, idx, verbose=None):
Expand Down
30 changes: 30 additions & 0 deletions mne/tests/test_epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5273,3 +5273,33 @@ 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.RandomState(0).randn(1, 1, 10)
info = create_info(["ch1"], 1000.0, "eeg")
epochs = EpochsArray(data, info)

# 1. Test 'ignore' (default)
epochs.copy().drop([0])

# 2. Test 'warn' explicitly
# We expect a warning when dropping all epochs
with pytest.warns(RuntimeWarning, match="All epochs dropped"):
epochs.copy().drop([0], on_drop_all="warn")

# 3. 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")

# 4. Test 'ignore' explicitly
# Should run silently (no warning, no error)
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")