Skip to content

Commit 2175a6a

Browse files
committed
TEST: Test single-slice and fancy indexing raise errors
1 parent 613d038 commit 2175a6a

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
@@ -428,10 +428,12 @@ def test_slicer(self):
428428
max4d = (hasattr(img.header, '_header_data') and
429429
'dims' in img.header._header_data.dtype.fields and
430430
img.header._header_data['dims'].shape == (4,))
431-
# Check newaxis errors
431+
# Check newaxis and single-slice errors
432432
if t_axis == 3:
433433
with assert_raises(IndexError):
434434
img.slicer[None]
435+
with assert_raises(IndexError):
436+
img.slicer[0]
435437
elif len(img.shape) == 4:
436438
with assert_raises(IndexError):
437439
img.slicer[None]
@@ -443,11 +445,17 @@ def test_slicer(self):
443445
# Axes 1 and 2 are always spatial
444446
with assert_raises(IndexError):
445447
img.slicer[:, None]
448+
with assert_raises(IndexError):
449+
img.slicer[:, 0]
446450
with assert_raises(IndexError):
447451
img.slicer[:, :, None]
452+
with assert_raises(IndexError):
453+
img.slicer[:, :, 0]
448454
if t_axis == 0:
449455
with assert_raises(IndexError):
450456
img.slicer[:, :, :, None]
457+
with assert_raises(IndexError):
458+
img.slicer[:, :, :, 0]
451459
elif len(img.shape) == 4:
452460
if max4d:
453461
with assert_raises(ValueError):
@@ -456,6 +464,9 @@ def test_slicer(self):
456464
# Reorder non-spatial axes
457465
assert_equal(img.slicer[:, :, :, None].shape,
458466
img.shape[:3] + (1,) + img.shape[3:])
467+
# 4D to 3D using ellipsis or slices
468+
assert_equal(img.slicer[..., 0].shape, img.shape[:-1])
469+
assert_equal(img.slicer[:, :, :, 0].shape, img.shape[:-1])
459470
else:
460471
# 3D Analyze/NIfTI/MGH to 4D
461472
assert_equal(img.slicer[:, :, :, None].shape, img.shape + (1,))
@@ -503,11 +514,19 @@ def test_slicer(self):
503514
with assert_raises(ValueError):
504515
img._slice_affine((slice(None), slice(None, None, 0)))
505516

517+
# No fancy indexing
518+
with assert_raises(IndexError):
519+
img.slicer[[0]]
520+
with assert_raises(IndexError):
521+
img.slicer[[-1]]
522+
with assert_raises(IndexError):
523+
img.slicer[[0], [-1]]
524+
506525
# Check data is consistent with slicing numpy arrays
507526
slice_elems = (None, Ellipsis, 0, 1, -1, [0], [1], [-1],
508527
slice(None), slice(1), slice(-1), slice(1, -1))
509528
for n_elems in range(6):
510-
for _ in range(10):
529+
for _ in range(1 if n_elems == 0 else 10):
511530
sliceobj = tuple(
512531
np.random.choice(slice_elems, n_elems).tolist())
513532
try:

0 commit comments

Comments
 (0)