@@ -428,10 +428,12 @@ def test_slicer(self):
428
428
max4d = (hasattr (img .header , '_header_data' ) and
429
429
'dims' in img .header ._header_data .dtype .fields and
430
430
img .header ._header_data ['dims' ].shape == (4 ,))
431
- # Check newaxis errors
431
+ # Check newaxis and single-slice errors
432
432
if t_axis == 3 :
433
433
with assert_raises (IndexError ):
434
434
img .slicer [None ]
435
+ with assert_raises (IndexError ):
436
+ img .slicer [0 ]
435
437
elif len (img .shape ) == 4 :
436
438
with assert_raises (IndexError ):
437
439
img .slicer [None ]
@@ -443,11 +445,17 @@ def test_slicer(self):
443
445
# Axes 1 and 2 are always spatial
444
446
with assert_raises (IndexError ):
445
447
img .slicer [:, None ]
448
+ with assert_raises (IndexError ):
449
+ img .slicer [:, 0 ]
446
450
with assert_raises (IndexError ):
447
451
img .slicer [:, :, None ]
452
+ with assert_raises (IndexError ):
453
+ img .slicer [:, :, 0 ]
448
454
if t_axis == 0 :
449
455
with assert_raises (IndexError ):
450
456
img .slicer [:, :, :, None ]
457
+ with assert_raises (IndexError ):
458
+ img .slicer [:, :, :, 0 ]
451
459
elif len (img .shape ) == 4 :
452
460
if max4d :
453
461
with assert_raises (ValueError ):
@@ -456,6 +464,9 @@ def test_slicer(self):
456
464
# Reorder non-spatial axes
457
465
assert_equal (img .slicer [:, :, :, None ].shape ,
458
466
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 ])
459
470
else :
460
471
# 3D Analyze/NIfTI/MGH to 4D
461
472
assert_equal (img .slicer [:, :, :, None ].shape , img .shape + (1 ,))
@@ -503,11 +514,19 @@ def test_slicer(self):
503
514
with assert_raises (ValueError ):
504
515
img ._slice_affine ((slice (None ), slice (None , None , 0 )))
505
516
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
+
506
525
# Check data is consistent with slicing numpy arrays
507
526
slice_elems = (None , Ellipsis , 0 , 1 , - 1 , [0 ], [1 ], [- 1 ],
508
527
slice (None ), slice (1 ), slice (- 1 ), slice (1 , - 1 ))
509
528
for n_elems in range (6 ):
510
- for _ in range (10 ):
529
+ for _ in range (1 if n_elems == 0 else 10 ):
511
530
sliceobj = tuple (
512
531
np .random .choice (slice_elems , n_elems ).tolist ())
513
532
try :
0 commit comments