@@ -439,6 +439,9 @@ def test_slicer(self):
439
439
assert_array_equal (downsampled_img .header .get_zooms ()[:3 ],
440
440
np .array (spatial_zooms ) * 2 )
441
441
442
+ max4d = (hasattr (img .header , '_header_data' ) and
443
+ 'dims' in img .header ._header_data .dtype .fields and
444
+ img .header ._header_data ['dims' ].shape == (4 ,))
442
445
# Check newaxis errors
443
446
if t_axis == 3 :
444
447
with assert_raises (IndexError ):
@@ -460,17 +463,26 @@ def test_slicer(self):
460
463
with assert_raises (IndexError ):
461
464
img .slicer [:, :, :, None ]
462
465
elif len (img .shape ) == 4 :
463
- # Reorder non-spatial axes
464
- assert_equal (img .slicer [:, :, :, None ].shape , img .shape [:3 ] + (1 ,) + img .shape [3 :])
466
+ if max4d :
467
+ with assert_raises (ValueError ):
468
+ img .slicer [:, :, :, None ]
469
+ else :
470
+ # Reorder non-spatial axes
471
+ assert_equal (img .slicer [:, :, :, None ].shape ,
472
+ img .shape [:3 ] + (1 ,) + img .shape [3 :])
465
473
else :
466
474
# 3D Analyze/NIfTI/MGH to 4D
467
475
assert_equal (img .slicer [:, :, :, None ].shape , img .shape + (1 ,))
468
476
if len (img .shape ) == 3 :
469
477
# Slices exceed dimensions
470
478
with assert_raises (IndexError ):
471
479
img .slicer [:, :, :, :, None ]
480
+ elif max4d :
481
+ with assert_raises (ValueError ):
482
+ img .slicer [:, :, :, :, None ]
472
483
else :
473
- assert_equal (img .slicer [:, :, :, :, None ].shape , img .shape + (1 ,))
484
+ assert_equal (img .slicer [:, :, :, :, None ].shape ,
485
+ img .shape + (1 ,))
474
486
475
487
# Crop by one voxel in each dimension
476
488
if len (img .shape ) == 3 or t_axis == 3 :
@@ -508,7 +520,7 @@ def test_slicer(self):
508
520
np .random .choice (slice_elems , n_elems ).tolist ())
509
521
try :
510
522
sliced_img = img .slicer [sliceobj ]
511
- except IndexError :
523
+ except ( IndexError , ValueError ) :
512
524
# Only checking valid slices
513
525
pass
514
526
else :
0 commit comments