Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/13463.newfeature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ``combine_channels`` method now has an ``on_missing`` parameter to control behavior on missing event ids, by :newcontrib:`Michael Straube`.
1 change: 1 addition & 0 deletions doc/changes/names.inc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
.. _Matti Hämäläinen: https://research.aalto.fi/en/persons/matti-h%C3%A4m%C3%A4l%C3%A4inen/
.. _Matti Toivonen: https://github.com/mattitoi
.. _Mauricio Cespedes Tenorio: https://github.com/mcespedes99
.. _Michael Straube: https://github.com/mistraube
.. _Michal Žák: https://github.com/michalrzak
.. _Michiru Kaneda: https://github.com/rcmdnk
.. _Mikołaj Magnuski: https://github.com/mmagnuski
Expand Down
12 changes: 11 additions & 1 deletion mne/channels/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,14 @@ def make_1020_channel_selections(info, midline="z", *, return_ch_names=False):

@verbose
def combine_channels(
inst, groups, method="mean", keep_stim=False, drop_bad=False, verbose=None
inst,
groups,
method="mean",
keep_stim=False,
drop_bad=False,
*,
on_missing="raise",
verbose=None,
):
"""Combine channels based on specified channel grouping.

Expand Down Expand Up @@ -2059,6 +2066,8 @@ def combine_channels(
drop_bad : bool
If ``True``, drop channels marked as bad before combining. Defaults to
``False``.
%(on_missing_epochs)s
.. versionadded:: 1.11.0
%(verbose)s

Returns
Expand Down Expand Up @@ -2174,6 +2183,7 @@ def combine_channels(
event_id=inst.event_id,
tmin=inst.times[0],
baseline=inst.baseline,
on_missing=on_missing,
)
if inst.metadata is not None:
combined_inst.metadata = inst.metadata.copy()
Expand Down
11 changes: 11 additions & 0 deletions mne/channels/tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright the MNE-Python contributors.

import hashlib
from contextlib import nullcontext
from copy import deepcopy
from functools import partial
from pathlib import Path
Expand Down Expand Up @@ -673,6 +674,16 @@ def test_combine_channels():
combine_channels(raw_ch_bad, warn3, drop_bad=True)
assert len(record) == 3

# Test on_missing
event_id = [1, 100] # 100 does not exist
epochs1 = Epochs(raw, read_events(eve_fname), event_id, on_missing="ignore")
with pytest.raises(ValueError, match="No matching events found"):
combine_channels(epochs1, groups={"foo": [0, 1]})
with pytest.warns(RuntimeWarning, match="No matching events found"):
combine_channels(epochs1, groups={"foo": [0, 1]}, on_missing="warn")
with nullcontext():
combine_channels(epochs1, groups={"foo": [0, 1]}, on_missing="ignore")


def test_combine_channels_metadata():
"""Test if metadata is correctly retained in combined object."""
Expand Down
Loading