Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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 :class:`mne.channels.channels.UpdateChannelsMixin` to tune drop_channels behaviour when specified channel names are not found(:gh:`11043` 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
8 changes: 8 additions & 0 deletions mne/channels/tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,14 @@ 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"]
pytest.raises(ValueError, raw.drop_channels, m_chs)
# ...but this can be turned to a warning
pytest.warns(RuntimeWarning, raw.drop_channels, m_chs, on_missing='warn')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I didn't catch this before, but ideally these should use the newer context-manager style of raises or warns. git grep "with pytest.raises" will show you some examples of the syntax; the advantage is (1) easier to read what's being tested and (2) checks that the actual error/warning message matches rather than just the error/warning type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - I've updated in 761d719

# ...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