Skip to content

Commit

Permalink
add tests and fix handling of immutable case
Browse files Browse the repository at this point in the history
  • Loading branch information
mscheltienne committed Jan 6, 2024
1 parent 8e20449 commit d4b3da5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
14 changes: 12 additions & 2 deletions mne/channels/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def pick(self, picks=None, exclude=(), *, verbose=None):
try:
picks = [_ensure_int(picks)]
except TypeError:
picks = list(deepcopy(picks))
picks = list(picks) if isinstance(picks, tuple) else deepcopy(picks)
apply_exclude = False
if apply_exclude:
if isinstance(exclude, str):
Expand All @@ -193,7 +193,11 @@ def pick(self, picks=None, exclude=(), *, verbose=None):
try:
exclude = [_ensure_int(exclude)]
except TypeError:
exclude = list(deepcopy(exclude))
exclude = (
list(exclude)
if isinstance(exclude, tuple)
else deepcopy(exclude)
)
for var, var_name in ((picks, "picks"), (exclude, "exclude")):
if var_name == "exclude" and not apply_exclude:
continue
Expand Down Expand Up @@ -230,6 +234,12 @@ def pick(self, picks=None, exclude=(), *, verbose=None):
)
if apply_exclude:
picks = np.array(list(set(picks) - set(exclude)))
if len(picks) == 0:
raise RuntimeError(
"The channel selection yielded no remaining channels. Please edit "
"the arguments 'picks' and 'exclude' to include at least one "
"channel."
)
else:
picks = np.array(list(set(picks)))
self.pos = self.pos[picks]
Expand Down
27 changes: 26 additions & 1 deletion mne/channels/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,29 @@ def test_layout_pick_more(layout):

def test_layout_pick_errors(layout):
"""Test validation of layout.pick."""
pass
with pytest.raises(TypeError, match="must be a list, tuple, set or ndarray"):
layout.pick(lambda x: x)
with pytest.raises(TypeError, match="must be a list, tuple, set or ndarray"):
layout.pick(None, lambda x: x)
with pytest.raises(TypeError, match="must be integers or strings"):
layout.pick([0, lambda x: x])
with pytest.raises(TypeError, match="must be integers or strings"):
layout.pick(None, [0, lambda x: x])
with pytest.raises(ValueError, match="does not match any channels"):
layout.pick("foo")
with pytest.raises(ValueError, match="does not match any channels"):
layout.pick(None, "foo")
with pytest.raises(ValueError, match="does not match any channels"):
layout.pick(101)
with pytest.raises(ValueError, match="does not match any channels"):
layout.pick(None, 101)
with pytest.warns(RuntimeWarning, match="has duplicates which will be ignored"):
layout.copy().pick(["0", "0"])
with pytest.warns(RuntimeWarning, match="has duplicates which will be ignored"):
layout.copy().pick(["0", 0])
with pytest.warns(RuntimeWarning, match="has duplicates which will be ignored"):
layout.copy().pick(None, ["0", "0"])
with pytest.warns(RuntimeWarning, match="has duplicates which will be ignored"):
layout.copy().pick(None, ["0", 0])
with pytest.raises(RuntimeError, match="selection yielded no remaining channels"):
layout.copy().pick(None, ["0", "1", "2"])

0 comments on commit d4b3da5

Please sign in to comment.