@@ -440,10 +440,12 @@ def test_slicer(self):
440
440
max4d = (hasattr (img .header , '_header_data' ) and
441
441
'dims' in img .header ._header_data .dtype .fields and
442
442
img .header ._header_data ['dims' ].shape == (4 ,))
443
- # Check newaxis errors
443
+ # Check newaxis and single-slice errors
444
444
if t_axis == 3 :
445
445
with assert_raises (IndexError ):
446
446
img .slicer [None ]
447
+ with assert_raises (IndexError ):
448
+ img .slicer [0 ]
447
449
elif len (img .shape ) == 4 :
448
450
with assert_raises (IndexError ):
449
451
img .slicer [None ]
@@ -455,11 +457,17 @@ def test_slicer(self):
455
457
# Axes 1 and 2 are always spatial
456
458
with assert_raises (IndexError ):
457
459
img .slicer [:, None ]
460
+ with assert_raises (IndexError ):
461
+ img .slicer [:, 0 ]
458
462
with assert_raises (IndexError ):
459
463
img .slicer [:, :, None ]
464
+ with assert_raises (IndexError ):
465
+ img .slicer [:, :, 0 ]
460
466
if t_axis == 0 :
461
467
with assert_raises (IndexError ):
462
468
img .slicer [:, :, :, None ]
469
+ with assert_raises (IndexError ):
470
+ img .slicer [:, :, :, 0 ]
463
471
elif len (img .shape ) == 4 :
464
472
if max4d :
465
473
with assert_raises (ValueError ):
@@ -468,6 +476,9 @@ def test_slicer(self):
468
476
# Reorder non-spatial axes
469
477
assert_equal (img .slicer [:, :, :, None ].shape ,
470
478
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 ])
471
482
else :
472
483
# 3D Analyze/NIfTI/MGH to 4D
473
484
assert_equal (img .slicer [:, :, :, None ].shape , img .shape + (1 ,))
@@ -515,11 +526,19 @@ def test_slicer(self):
515
526
with assert_raises (ValueError ):
516
527
img ._slice_affine ((slice (None ), slice (None , None , 0 )))
517
528
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
+
518
537
# Check data is consistent with slicing numpy arrays
519
538
slice_elems = (None , Ellipsis , 0 , 1 , - 1 , [0 ], [1 ], [- 1 ],
520
539
slice (None ), slice (1 ), slice (- 1 ), slice (1 , - 1 ))
521
540
for n_elems in range (6 ):
522
- for _ in range (10 ):
541
+ for _ in range (1 if n_elems == 0 else 10 ):
523
542
sliceobj = tuple (
524
543
np .random .choice (slice_elems , n_elems ).tolist ())
525
544
try :
0 commit comments