1717from io import BytesIO
1818from ..spatialimages import (SpatialHeader , SpatialImage , HeaderDataError ,
1919 Header , ImageDataError )
20+ from ..imageclasses import spatial_axes_first
2021
2122from unittest import TestCase
2223from nose .tools import (assert_true , assert_false , assert_equal ,
@@ -422,19 +423,19 @@ def test_slicer(self):
422423 in_data = in_data_template .copy ().reshape (dshape )
423424 img = img_klass (in_data , base_affine .copy ())
424425
425- # Detect time axis on first loop (4D image)
426- if t_axis is None and img ._spatial_dims is not None :
427- t_axis = 3 if img ._spatial_dims .start == 0 else 0
426+ if not spatial_axes_first (img ):
427+ with assert_raises (ValueError ):
428+ img .slicer
429+ continue
428430
429431 assert_true (hasattr (img .slicer , '__getitem__' ))
430432
431433 # Note spatial zooms are always first 3, even when
432434 spatial_zooms = img .header .get_zooms ()[:3 ]
433435
434436 # Down-sample with [::2, ::2, ::2] along spatial dimensions
435- sliceobj = [slice (None )] * len (dshape )
436- if img ._spatial_dims is not None :
437- sliceobj [img ._spatial_dims ] = [slice (None , None , 2 )] * 3
437+ sliceobj = [slice (None , None , 2 )] * 3 + \
438+ [slice (None )] * (len (dshape ) - 3 )
438439 downsampled_img = img .slicer [tuple (sliceobj )]
439440 assert_array_equal (downsampled_img .header .get_zooms ()[:3 ],
440441 np .array (spatial_zooms ) * 2 )
@@ -443,19 +444,10 @@ def test_slicer(self):
443444 'dims' in img .header ._structarr .dtype .fields and
444445 img .header ._structarr ['dims' ].shape == (4 ,))
445446 # Check newaxis and single-slice errors
446- if t_axis == 3 :
447- with assert_raises (IndexError ):
448- img .slicer [None ]
449- with assert_raises (IndexError ):
450- img .slicer [0 ]
451- elif len (img .shape ) == 4 :
452- with assert_raises (IndexError ):
453- img .slicer [None ]
454- # 4D Minc to 3D
455- assert_equal (img .slicer [0 ].shape , img .shape [1 :])
456- elif t_axis is not None :
457- # 3D Minc to 4D
458- assert_equal (img .slicer [None ].shape , (1 ,) + img .shape )
447+ with assert_raises (IndexError ):
448+ img .slicer [None ]
449+ with assert_raises (IndexError ):
450+ img .slicer [0 ]
459451 # Axes 1 and 2 are always spatial
460452 with assert_raises (IndexError ):
461453 img .slicer [:, None ]
@@ -465,12 +457,7 @@ def test_slicer(self):
465457 img .slicer [:, :, None ]
466458 with assert_raises (IndexError ):
467459 img .slicer [:, :, 0 ]
468- if t_axis == 0 :
469- with assert_raises (IndexError ):
470- img .slicer [:, :, :, None ]
471- with assert_raises (IndexError ):
472- img .slicer [:, :, :, 0 ]
473- elif len (img .shape ) == 4 :
460+ if len (img .shape ) == 4 :
474461 if max4d :
475462 with assert_raises (ValueError ):
476463 img .slicer [:, :, :, None ]
@@ -481,7 +468,7 @@ def test_slicer(self):
481468 # 4D to 3D using ellipsis or slices
482469 assert_equal (img .slicer [..., 0 ].shape , img .shape [:- 1 ])
483470 assert_equal (img .slicer [:, :, :, 0 ].shape , img .shape [:- 1 ])
484- elif t_axis is not None :
471+ else :
485472 # 3D Analyze/NIfTI/MGH to 4D
486473 assert_equal (img .slicer [:, :, :, None ].shape , img .shape + (1 ,))
487474 if len (img .shape ) == 3 :
@@ -496,17 +483,10 @@ def test_slicer(self):
496483 img .shape + (1 ,))
497484
498485 # Crop by one voxel in each dimension
499- if len (img .shape ) == 3 or t_axis == 3 :
500- sliced_i = img .slicer [1 :]
501- sliced_j = img .slicer [:, 1 :]
502- sliced_k = img .slicer [:, :, 1 :]
503- sliced_ijk = img .slicer [1 :, 1 :, 1 :]
504- elif t_axis is not None :
505- # 4D Minc
506- sliced_i = img .slicer [:, 1 :]
507- sliced_j = img .slicer [:, :, 1 :]
508- sliced_k = img .slicer [:, :, :, 1 :]
509- sliced_ijk = img .slicer [:, 1 :, 1 :, 1 :]
486+ sliced_i = img .slicer [1 :]
487+ sliced_j = img .slicer [:, 1 :]
488+ sliced_k = img .slicer [:, :, 1 :]
489+ sliced_ijk = img .slicer [1 :, 1 :, 1 :]
510490
511491 # No scaling change
512492 assert_array_equal (sliced_i .affine [:3 , :3 ], img .affine [:3 , :3 ])
0 commit comments