Skip to content

Commit e50d565

Browse files
mistraubelarsoner
andauthored
ENH: Add on_missing for combine_channels (mne-tools#13463)
Co-authored-by: Eric Larson <[email protected]>
1 parent 6ec537f commit e50d565

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The ``combine_channels`` method now has an ``on_missing`` parameter to control behavior on missing event ids, by :newcontrib:`Michael Straube`.

doc/changes/names.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
.. _Matti Hämäläinen: https://research.aalto.fi/en/persons/matti-h%C3%A4m%C3%A4l%C3%A4inen/
208208
.. _Matti Toivonen: https://github.com/mattitoi
209209
.. _Mauricio Cespedes Tenorio: https://github.com/mcespedes99
210+
.. _Michael Straube: https://github.com/mistraube
210211
.. _Michal Žák: https://github.com/michalrzak
211212
.. _Michiru Kaneda: https://github.com/rcmdnk
212213
.. _Mikołaj Magnuski: https://github.com/mmagnuski

mne/channels/channels.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2020,7 +2020,14 @@ def make_1020_channel_selections(info, midline="z", *, return_ch_names=False):
20202020

20212021
@verbose
20222022
def combine_channels(
2023-
inst, groups, method="mean", keep_stim=False, drop_bad=False, verbose=None
2023+
inst,
2024+
groups,
2025+
method="mean",
2026+
keep_stim=False,
2027+
drop_bad=False,
2028+
*,
2029+
on_missing="raise",
2030+
verbose=None,
20242031
):
20252032
"""Combine channels based on specified channel grouping.
20262033
@@ -2059,6 +2066,8 @@ def combine_channels(
20592066
drop_bad : bool
20602067
If ``True``, drop channels marked as bad before combining. Defaults to
20612068
``False``.
2069+
%(on_missing_epochs)s
2070+
.. versionadded:: 1.11.0
20622071
%(verbose)s
20632072
20642073
Returns
@@ -2174,6 +2183,7 @@ def combine_channels(
21742183
event_id=inst.event_id,
21752184
tmin=inst.times[0],
21762185
baseline=inst.baseline,
2186+
on_missing=on_missing,
21772187
)
21782188
if inst.metadata is not None:
21792189
combined_inst.metadata = inst.metadata.copy()

mne/channels/tests/test_channels.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Copyright the MNE-Python contributors.
44

55
import hashlib
6+
from contextlib import nullcontext
67
from copy import deepcopy
78
from functools import partial
89
from pathlib import Path
@@ -673,6 +674,16 @@ def test_combine_channels():
673674
combine_channels(raw_ch_bad, warn3, drop_bad=True)
674675
assert len(record) == 3
675676

677+
# Test on_missing
678+
event_id = [1, 100] # 100 does not exist
679+
epochs1 = Epochs(raw, read_events(eve_fname), event_id, on_missing="ignore")
680+
with pytest.raises(ValueError, match="No matching events found"):
681+
combine_channels(epochs1, groups={"foo": [0, 1]})
682+
with pytest.warns(RuntimeWarning, match="No matching events found"):
683+
combine_channels(epochs1, groups={"foo": [0, 1]}, on_missing="warn")
684+
with nullcontext():
685+
combine_channels(epochs1, groups={"foo": [0, 1]}, on_missing="ignore")
686+
676687

677688
def test_combine_channels_metadata():
678689
"""Test if metadata is correctly retained in combined object."""

0 commit comments

Comments
 (0)