17
17
from io import BytesIO
18
18
from ..spatialimages import (SpatialHeader , SpatialImage , HeaderDataError ,
19
19
Header , ImageDataError )
20
+ from ..imageclasses import spatial_axes_first
20
21
21
22
from unittest import TestCase
22
23
from nose .tools import (assert_true , assert_false , assert_equal ,
@@ -422,19 +423,19 @@ def test_slicer(self):
422
423
in_data = in_data_template .copy ().reshape (dshape )
423
424
img = img_klass (in_data , base_affine .copy ())
424
425
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
428
430
429
431
assert_true (hasattr (img .slicer , '__getitem__' ))
430
432
431
433
# Note spatial zooms are always first 3, even when
432
434
spatial_zooms = img .header .get_zooms ()[:3 ]
433
435
434
436
# 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 )
438
439
downsampled_img = img .slicer [tuple (sliceobj )]
439
440
assert_array_equal (downsampled_img .header .get_zooms ()[:3 ],
440
441
np .array (spatial_zooms ) * 2 )
@@ -443,19 +444,10 @@ def test_slicer(self):
443
444
'dims' in img .header ._structarr .dtype .fields and
444
445
img .header ._structarr ['dims' ].shape == (4 ,))
445
446
# 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 ]
459
451
# Axes 1 and 2 are always spatial
460
452
with assert_raises (IndexError ):
461
453
img .slicer [:, None ]
@@ -465,12 +457,7 @@ def test_slicer(self):
465
457
img .slicer [:, :, None ]
466
458
with assert_raises (IndexError ):
467
459
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 :
474
461
if max4d :
475
462
with assert_raises (ValueError ):
476
463
img .slicer [:, :, :, None ]
@@ -481,7 +468,7 @@ def test_slicer(self):
481
468
# 4D to 3D using ellipsis or slices
482
469
assert_equal (img .slicer [..., 0 ].shape , img .shape [:- 1 ])
483
470
assert_equal (img .slicer [:, :, :, 0 ].shape , img .shape [:- 1 ])
484
- elif t_axis is not None :
471
+ else :
485
472
# 3D Analyze/NIfTI/MGH to 4D
486
473
assert_equal (img .slicer [:, :, :, None ].shape , img .shape + (1 ,))
487
474
if len (img .shape ) == 3 :
@@ -496,17 +483,10 @@ def test_slicer(self):
496
483
img .shape + (1 ,))
497
484
498
485
# 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 :]
510
490
511
491
# No scaling change
512
492
assert_array_equal (sliced_i .affine [:3 , :3 ], img .affine [:3 , :3 ])
0 commit comments