Skip to content

Commit b3b4a22

Browse files
committed
TEST: Handle MGHFormat (max: 4D) correctly
1 parent 0718701 commit b3b4a22

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

nibabel/tests/test_spatialimages.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ def test_slicer(self):
439439
assert_array_equal(downsampled_img.header.get_zooms()[:3],
440440
np.array(spatial_zooms) * 2)
441441

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,))
442445
# Check newaxis errors
443446
if t_axis == 3:
444447
with assert_raises(IndexError):
@@ -460,17 +463,26 @@ def test_slicer(self):
460463
with assert_raises(IndexError):
461464
img.slicer[:, :, :, None]
462465
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:])
465473
else:
466474
# 3D Analyze/NIfTI/MGH to 4D
467475
assert_equal(img.slicer[:, :, :, None].shape, img.shape + (1,))
468476
if len(img.shape) == 3:
469477
# Slices exceed dimensions
470478
with assert_raises(IndexError):
471479
img.slicer[:, :, :, :, None]
480+
elif max4d:
481+
with assert_raises(ValueError):
482+
img.slicer[:, :, :, :, None]
472483
else:
473-
assert_equal(img.slicer[:, :, :, :, None].shape, img.shape + (1,))
484+
assert_equal(img.slicer[:, :, :, :, None].shape,
485+
img.shape + (1,))
474486

475487
# Crop by one voxel in each dimension
476488
if len(img.shape) == 3 or t_axis == 3:
@@ -508,7 +520,7 @@ def test_slicer(self):
508520
np.random.choice(slice_elems, n_elems).tolist())
509521
try:
510522
sliced_img = img.slicer[sliceobj]
511-
except IndexError:
523+
except (IndexError, ValueError):
512524
# Only checking valid slices
513525
pass
514526
else:

0 commit comments

Comments
 (0)