Skip to content

Commit

Permalink
improve slice handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Scheltienne committed Jan 9, 2024
1 parent c32faeb commit aadcf70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 9 additions & 3 deletions mne/channels/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,22 @@ def pick(self, picks=None, exclude=(), *, verbose=None):
-----
.. versionadded:: 1.7
"""
# sadly, all the picking functions operates on an 'info' object which is missing
# for a layout, thus we have to do the extra work here.
# TODO: all the picking functions operates on an 'info' object which is missing
# for a layout, thus we have to do the extra work here. The logic below can be
# replaced when https://github.com/mne-tools/mne-python/issues/11913 is solved.
if (isinstance(picks, str) and picks == "all") or (picks is None):
picks = deepcopy(self.names)
apply_exclude = True
elif isinstance(picks, str):
picks = [picks]
apply_exclude = False
elif isinstance(picks, slice):
picks = np.arange(len(self.names))[picks]
try:
picks = np.arange(len(self.names))[picks]
except TypeError:
raise TypeError(
"If a slice is provided, it must be a slice of integers."
)
apply_exclude = False
else:
try:
Expand Down
2 changes: 2 additions & 0 deletions mne/channels/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,5 @@ def test_layout_pick_errors(layout):
layout.copy().pick(None, ["0", "1", "2"])
with pytest.raises(ValueError, match="must be a 1D array-like"):
layout.copy().pick(None, np.array([[0, 1]]))
with pytest.raises(TypeError, match="slice of integers"):
layout.copy().pick(slice("2342342342", 0, 3), ())

0 comments on commit aadcf70

Please sign in to comment.