Skip to content

Commit 9f58548

Browse files
committed
TEST: Test single-slice and fancy indexing raise errors
1 parent 2ebc4eb commit 9f58548

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

nibabel/tests/test_spatialimages.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,12 @@ def test_slicer(self):
440440
max4d = (hasattr(img.header, '_header_data') and
441441
'dims' in img.header._header_data.dtype.fields and
442442
img.header._header_data['dims'].shape == (4,))
443-
# Check newaxis errors
443+
# Check newaxis and single-slice errors
444444
if t_axis == 3:
445445
with assert_raises(IndexError):
446446
img.slicer[None]
447+
with assert_raises(IndexError):
448+
img.slicer[0]
447449
elif len(img.shape) == 4:
448450
with assert_raises(IndexError):
449451
img.slicer[None]
@@ -455,11 +457,17 @@ def test_slicer(self):
455457
# Axes 1 and 2 are always spatial
456458
with assert_raises(IndexError):
457459
img.slicer[:, None]
460+
with assert_raises(IndexError):
461+
img.slicer[:, 0]
458462
with assert_raises(IndexError):
459463
img.slicer[:, :, None]
464+
with assert_raises(IndexError):
465+
img.slicer[:, :, 0]
460466
if t_axis == 0:
461467
with assert_raises(IndexError):
462468
img.slicer[:, :, :, None]
469+
with assert_raises(IndexError):
470+
img.slicer[:, :, :, 0]
463471
elif len(img.shape) == 4:
464472
if max4d:
465473
with assert_raises(ValueError):
@@ -468,6 +476,9 @@ def test_slicer(self):
468476
# Reorder non-spatial axes
469477
assert_equal(img.slicer[:, :, :, None].shape,
470478
img.shape[:3] + (1,) + img.shape[3:])
479+
# 4D to 3D using ellipsis or slices
480+
assert_equal(img.slicer[..., 0].shape, img.shape[:-1])
481+
assert_equal(img.slicer[:, :, :, 0].shape, img.shape[:-1])
471482
else:
472483
# 3D Analyze/NIfTI/MGH to 4D
473484
assert_equal(img.slicer[:, :, :, None].shape, img.shape + (1,))
@@ -515,11 +526,19 @@ def test_slicer(self):
515526
with assert_raises(ValueError):
516527
img._slice_affine((slice(None), slice(None, None, 0)))
517528

529+
# No fancy indexing
530+
with assert_raises(IndexError):
531+
img.slicer[[0]]
532+
with assert_raises(IndexError):
533+
img.slicer[[-1]]
534+
with assert_raises(IndexError):
535+
img.slicer[[0], [-1]]
536+
518537
# Check data is consistent with slicing numpy arrays
519538
slice_elems = (None, Ellipsis, 0, 1, -1, [0], [1], [-1],
520539
slice(None), slice(1), slice(-1), slice(1, -1))
521540
for n_elems in range(6):
522-
for _ in range(10):
541+
for _ in range(1 if n_elems == 0 else 10):
523542
sliceobj = tuple(
524543
np.random.choice(slice_elems, n_elems).tolist())
525544
try:

0 commit comments

Comments
 (0)