From 0a2a00dabad9cd4d31c3fe11cf1ca776fc435c3e Mon Sep 17 00:00:00 2001 From: Farzin Negahbani Date: Wed, 31 Jul 2024 17:38:29 +0200 Subject: [PATCH 1/6] ENH: check for dropping all channels --- mne/channels/channels.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mne/channels/channels.py b/mne/channels/channels.py index 49890a56e91..0a761f989ce 100644 --- a/mne/channels/channels.py +++ b/mne/channels/channels.py @@ -600,6 +600,9 @@ def drop_channels(self, ch_names, on_missing="raise"): msg = "Channel(s) {0} not found, nothing dropped." _on_missing(on_missing, msg.format(", ".join(missing))) + if set(ch_names) == set(self.ch_names): + raise ValueError("All channel(s) would be dropped.") + bad_idx = [self.ch_names.index(ch) for ch in ch_names if ch in self.ch_names] idx = np.setdiff1d(np.arange(len(self.ch_names)), bad_idx) return self._pick_drop_channels(idx) From b00d547ccdb35be2c72a3fb360ddb4c73cf1a774 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 31 Jul 2024 15:53:08 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/channels/channels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/channels/channels.py b/mne/channels/channels.py index 0a761f989ce..f54689bb142 100644 --- a/mne/channels/channels.py +++ b/mne/channels/channels.py @@ -602,7 +602,7 @@ def drop_channels(self, ch_names, on_missing="raise"): if set(ch_names) == set(self.ch_names): raise ValueError("All channel(s) would be dropped.") - + bad_idx = [self.ch_names.index(ch) for ch in ch_names if ch in self.ch_names] idx = np.setdiff1d(np.arange(len(self.ch_names)), bad_idx) return self._pick_drop_channels(idx) From bc631353a2314de34a5a88a8bc4d8c41aa01392c Mon Sep 17 00:00:00 2001 From: Farzin Negahbani Date: Thu, 1 Aug 2024 09:50:58 +0200 Subject: [PATCH 3/6] Update mne/channels/channels.py reduce redundant calculation for drop all check Co-authored-by: Eric Larson --- mne/channels/channels.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mne/channels/channels.py b/mne/channels/channels.py index f54689bb142..04a2be2b131 100644 --- a/mne/channels/channels.py +++ b/mne/channels/channels.py @@ -600,11 +600,10 @@ def drop_channels(self, ch_names, on_missing="raise"): msg = "Channel(s) {0} not found, nothing dropped." _on_missing(on_missing, msg.format(", ".join(missing))) - if set(ch_names) == set(self.ch_names): - raise ValueError("All channel(s) would be dropped.") - bad_idx = [self.ch_names.index(ch) for ch in ch_names if ch in self.ch_names] idx = np.setdiff1d(np.arange(len(self.ch_names)), bad_idx) + if len(idx) == 0: + raise ValueError("All channel(s) would be dropped.") return self._pick_drop_channels(idx) @verbose From 2b38c24ea345e135f5d24653c7fd9bde31c95caa Mon Sep 17 00:00:00 2001 From: Farzin Negahbani Date: Thu, 1 Aug 2024 10:45:19 +0200 Subject: [PATCH 4/6] add changelog for dropping all channels check #12763 --- doc/changes/devel/12763.bugfix.rst | 1 + doc/changes/names.inc | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 doc/changes/devel/12763.bugfix.rst diff --git a/doc/changes/devel/12763.bugfix.rst b/doc/changes/devel/12763.bugfix.rst new file mode 100644 index 00000000000..405abd19313 --- /dev/null +++ b/doc/changes/devel/12763.bugfix.rst @@ -0,0 +1 @@ +Fix check for dropping all channels in :meth:`mne.io.Raw.drop_channels` and related methods, by :newcontrib:`Farzin Negahbani`. \ No newline at end of file diff --git a/doc/changes/names.inc b/doc/changes/names.inc index 34195e4ce47..929228e2cb7 100644 --- a/doc/changes/names.inc +++ b/doc/changes/names.inc @@ -629,3 +629,5 @@ .. _Seyed Yahya Shirazi: https://neuromechanist.github.io .. _Sammi Chekroud: https://github.com/schekroud + +.. _Farzin Negahbani: https://github.com/Farzin-Negahbani From 58e4e5f82af52afefd8c2b89bfc45b022f995c6b Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Thu, 1 Aug 2024 09:14:53 -0400 Subject: [PATCH 5/6] Update names.inc --- doc/changes/names.inc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/changes/names.inc b/doc/changes/names.inc index 929228e2cb7..97a562bc7fb 100644 --- a/doc/changes/names.inc +++ b/doc/changes/names.inc @@ -168,6 +168,8 @@ .. _Fahimeh Mamashli: https://github.com/fmamashli +.. _Farzin Negahbani: https://github.com/Farzin-Negahbani + .. _Federico Raimondo: https://github.com/fraimondo .. _Federico Zamberlan: https://github.com/fzamberlan @@ -508,6 +510,8 @@ .. _Rotem Falach: https://github.com/Falach +.. _Sammi Chekroud: https://github.com/schekroud + .. _Samu Taulu: https://phys.washington.edu/people/samu-taulu .. _Samuel Deslauriers-Gauthier: https://github.com/sdeslauriers @@ -536,6 +540,8 @@ .. _Senwen Deng: https://snwn.de +.. _Seyed Yahya Shirazi: https://neuromechanist.github.io + .. _Sheraz Khan: https://github.com/SherazKhan .. _Silvia Cotroneo: https://github.com/sfc-neuro @@ -625,9 +631,3 @@ .. _Zhi Zhang: https://github.com/tczhangzhi/ .. _Zvi Baratz: https://github.com/ZviBaratz - -.. _Seyed Yahya Shirazi: https://neuromechanist.github.io - -.. _Sammi Chekroud: https://github.com/schekroud - -.. _Farzin Negahbani: https://github.com/Farzin-Negahbani From 8c603174e3e9ca37e3641ab939319c16a3d9958b Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Thu, 1 Aug 2024 09:25:24 -0400 Subject: [PATCH 6/6] FIX: Test --- mne/channels/channels.py | 2 +- mne/channels/tests/test_channels.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/mne/channels/channels.py b/mne/channels/channels.py index 04a2be2b131..92ddb7a9779 100644 --- a/mne/channels/channels.py +++ b/mne/channels/channels.py @@ -603,7 +603,7 @@ def drop_channels(self, ch_names, on_missing="raise"): bad_idx = [self.ch_names.index(ch) for ch in ch_names if ch in self.ch_names] idx = np.setdiff1d(np.arange(len(self.ch_names)), bad_idx) if len(idx) == 0: - raise ValueError("All channel(s) would be dropped.") + raise ValueError("All channels would be dropped.") return self._pick_drop_channels(idx) @verbose diff --git a/mne/channels/tests/test_channels.py b/mne/channels/tests/test_channels.py index 13ed8f23f08..61fe7277156 100644 --- a/mne/channels/tests/test_channels.py +++ b/mne/channels/tests/test_channels.py @@ -539,6 +539,8 @@ def test_drop_channels(): raw.drop_channels(m_chs, on_missing="warn") # ...or ignored altogether raw.drop_channels(m_chs, on_missing="ignore") + with pytest.raises(ValueError, match="All channels"): + raw.drop_channels(raw.ch_names) def test_pick_channels():