diff --git a/doc/changes/dev/14006.bugfix.rst b/doc/changes/dev/14006.bugfix.rst new file mode 100644 index 00000000000..8a30fec7520 --- /dev/null +++ b/doc/changes/dev/14006.bugfix.rst @@ -0,0 +1 @@ +Fix a crash (``AttributeError`` on ``NoneType``) when picking channels on a :class:`~mne.io.Raw` whose ``_orig_units`` is ``None``, by `Cedric Conday`_. diff --git a/mne/channels/channels.py b/mne/channels/channels.py index c1a3d462525..c18ac8d253c 100644 --- a/mne/channels/channels.py +++ b/mne/channels/channels.py @@ -646,9 +646,10 @@ def _pick_drop_channels(self, idx, *, verbose=None): if isinstance(self, BaseRaw): self.annotations._prune_ch_names(self.info, on_missing="ignore") - self._orig_units = { - k: v for k, v in self._orig_units.items() if k in self.ch_names - } + if self._orig_units: + self._orig_units = { + k: v for k, v in self._orig_units.items() if k in self.ch_names + } self._pick_projs() return self diff --git a/mne/channels/tests/test_channels.py b/mne/channels/tests/test_channels.py index 138d2b325c7..2c2bfc6db6f 100644 --- a/mne/channels/tests/test_channels.py +++ b/mne/channels/tests/test_channels.py @@ -14,6 +14,7 @@ from numpy.testing import assert_allclose, assert_array_equal, assert_equal from scipy.io import savemat +import mne from mne import ( Epochs, EpochsArray, @@ -696,3 +697,13 @@ def test_combine_channels_metadata(): good = dict(foo=[0, 1, 3, 4], bar=[5, 2]) # good grad and mag combined_epochs = combine_channels(epochs, good) pd.testing.assert_frame_equal(epochs.metadata, combined_epochs.metadata) + + +def test_pick_channels_orig_units_none(): + """Picking channels must not crash when _orig_units is None (gh-11314).""" + info = mne.create_info(["Fp1", "Fp2", "F3", "F4"], 100.0, "eeg") + raw = mne.io.RawArray(np.zeros((4, 100)), info) + raw._orig_units = None # the state reported by some readers / RawArray flows + raw.pick(["Fp1", "Fp2"]) # previously raised AttributeError on None.items() + assert raw.ch_names == ["Fp1", "Fp2"] + assert raw._orig_units is None