Skip to content

Commit e9c86eb

Browse files
committed
Fix pick channels crash when _orig_units is None (#11314)
Picking/reordering channels built {k: v for ... in self._orig_units.items()} unconditionally, raising AttributeError when _orig_units is None (as produced by some readers / RawArray flows). Guard for a falsy _orig_units. Adds a test.
1 parent 1a442f0 commit e9c86eb

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

doc/changes/dev/11314.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a crash (``AttributeError`` on ``NoneType``) when picking channels on a :class:`~mne.io.Raw` whose ``_orig_units`` is ``None``, by `Cedric Conday`_.

mne/channels/channels.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,10 @@ def _pick_drop_channels(self, idx, *, verbose=None):
646646

647647
if isinstance(self, BaseRaw):
648648
self.annotations._prune_ch_names(self.info, on_missing="ignore")
649-
self._orig_units = {
650-
k: v for k, v in self._orig_units.items() if k in self.ch_names
651-
}
649+
if self._orig_units:
650+
self._orig_units = {
651+
k: v for k, v in self._orig_units.items() if k in self.ch_names
652+
}
652653

653654
self._pick_projs()
654655
return self

mne/channels/tests/test_channels.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,17 @@ def test_combine_channels_metadata():
696696
good = dict(foo=[0, 1, 3, 4], bar=[5, 2]) # good grad and mag
697697
combined_epochs = combine_channels(epochs, good)
698698
pd.testing.assert_frame_equal(epochs.metadata, combined_epochs.metadata)
699+
700+
701+
def test_pick_channels_orig_units_none():
702+
"""Picking channels must not crash when _orig_units is None (gh-11314)."""
703+
import numpy as np
704+
705+
import mne
706+
707+
info = mne.create_info(["Fp1", "Fp2", "F3", "F4"], 100.0, "eeg")
708+
raw = mne.io.RawArray(np.zeros((4, 100)), info)
709+
raw._orig_units = None # the state reported by some readers / RawArray flows
710+
raw.pick(["Fp1", "Fp2"]) # previously raised AttributeError on None.items()
711+
assert raw.ch_names == ["Fp1", "Fp2"]
712+
assert raw._orig_units is None

0 commit comments

Comments
 (0)