Skip to content

Commit 62bbc3c

Browse files
committed
A more principled approach to avoiding the matplotlib problem - x[:, None] adds an empty dimension to a NumPy array x. This doesn't make sense for AnalogSignal, which can only be 2D, so we raise an IndexError.
The alternative would be to cast the object to a 3D Quantities array, but I think that would be unexpected behaviour.
1 parent 3f328cf commit 62bbc3c

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

neo/core/analogsignal.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,11 @@ def __getitem__(self, i):
265265
raise TypeError("%s not supported" % type(j))
266266
if isinstance(k, (int, np.integer)):
267267
obj = obj.reshape(-1, 1)
268-
if k is not None: # matplotlib _check_1d() calls__getitem__ with (:, None)
269-
obj.array_annotate(**deepcopy(self.array_annotations_at_index(k)))
268+
elif k is None:
269+
# matplotlib _check_1d() calls__getitem__ with (:, None) and
270+
# reacts appropriately if an IndexError or ValueError is raised
271+
raise IndexError("Cannot add dimensions to an AnalogSignal")
272+
obj.array_annotate(**deepcopy(self.array_annotations_at_index(k)))
270273
elif isinstance(i, slice):
271274
obj = super().__getitem__(i)
272275
if i.start:

neo/test/coretest/test_analogsignal.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,7 @@ def test__getitem_should_return_single_quantity(self):
640640
self.assertRaises(IndexError, self.signal1.__getitem__, (99, 73))
641641

642642
def test__getitem__with_tuple_slice_none(self):
643-
item = self.signal1[:, None]
644-
# we're just testing no error is raised
643+
self.assertRaises(IndexError, self.signal1.__getitem__, (slice(None), None))
645644

646645
def test__time_index(self):
647646
# scalar arguments

0 commit comments

Comments
 (0)