Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Enhancements
- Add ``starting_affine`` keyword argument to :func:`mne.transforms.compute_volume_registration` to initialize an alignment with an affine (:gh:`11020` by `Alex Rockhill`_)
- The ``trans`` parameter in :func:`mne.make_field_map` now accepts a :class:`~pathlib.Path` object, and uses standardised loading logic (:gh:`10784` by :newcontrib:`Andrew Quinn`)
- Allow :func:`mne.beamformer.make_dics` to take ``pick_ori='vector'`` to compute vector source estimates (:gh:`19080` by `Alex Rockhill`_)
- Add ``on_missing`` functionality to all of our classes that have a ``drop_channels`` method, to control what happens when channel names are not in the object (:gh:`11077` by `Andrew Quinn`_)

Bugs
~~~~
Expand Down
9 changes: 6 additions & 3 deletions mne/channels/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from ..defaults import HEAD_SIZE_DEFAULT, _handle_default
from ..utils import (verbose, logger, warn,
_check_preload, _validate_type, fill_doc, _check_option,
_get_stim_channel, _check_fname, _check_dict_keys)
_get_stim_channel, _check_fname, _check_dict_keys,
_on_missing)
from ..io.constants import FIFF
from ..io.meas_info import (anonymize_info, Info, MontageMixin, create_info,
_rename_comps)
Expand Down Expand Up @@ -735,13 +736,15 @@ def reorder_channels(self, ch_names):
idx.append(ii)
return self._pick_drop_channels(idx)

def drop_channels(self, ch_names):
@fill_doc
def drop_channels(self, ch_names, on_missing='raise'):
"""Drop channel(s).

Parameters
----------
ch_names : iterable or str
Iterable (e.g. list) of channel name(s) or channel name to remove.
%(on_missing_ch_names)s

Returns
-------
Expand Down Expand Up @@ -774,7 +777,7 @@ def drop_channels(self, ch_names):
missing = [ch for ch in ch_names if ch not in self.ch_names]
if len(missing) > 0:
msg = "Channel(s) {0} not found, nothing dropped."
raise ValueError(msg.format(", ".join(missing)))
_on_missing(on_missing, msg.format(", ".join(missing)))

bad_idx = [self.ch_names.index(ch) for ch in ch_names
if ch in self.ch_names]
Expand Down
10 changes: 10 additions & 0 deletions mne/channels/tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,16 @@ def test_drop_channels():
pytest.raises(ValueError, raw.drop_channels, ["MEG 0111", 5])
pytest.raises(ValueError, raw.drop_channels, 5) # must be list or str

# by default, drop channels raises a ValueError if a channel can't be found
m_chs = ["MEG 0111", "MEG blahblah"]
with pytest.raises(ValueError, match='not found, nothing dropped'):
raw.drop_channels(m_chs)
# ...but this can be turned to a warning
with pytest.raises(RuntimeWarning, match='not found, nothing dropped'):
raw.drop_channels(m_chs, on_missing='warn')
# ...or ignored altogether
raw.drop_channels(m_chs, on_missing='ignore')


def test_pick_channels():
"""Test if picking channels works with various arguments."""
Expand Down