Skip to content

Commit ddf6edb

Browse files
committed
RF+TST: add test for 4D MINC to processing + test
Processing routines should barf on 4D MINC because they do not know how to work out the spatial axes. Use new `spatial_axes_first` function to check for problem images, raise. Test errors raised.
1 parent e9500fd commit ddf6edb

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

nibabel/processing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from .affines import AffineError, to_matvec, from_matvec, append_diag
2626
from .spaces import vox2out_vox
2727
from .nifti1 import Nifti1Image
28+
from .imageclasses import spatial_axes_first
2829

2930
SIGMA2FWHM = np.sqrt(8 * np.log(2))
3031

@@ -153,6 +154,9 @@ def resample_from_to(from_img,
153154
resampling `from_img` into axes aligned to the output space of
154155
``from_img.affine``
155156
"""
157+
if not spatial_axes_first(from_img):
158+
raise ValueError('Cannot predict position of spatial axes for Image '
159+
'type ' + str(type(from_img)))
156160
try:
157161
to_shape, to_affine = to_vox_map.shape, to_vox_map.affine
158162
except AttributeError:
@@ -280,6 +284,9 @@ def smooth_image(img,
280284
Image of instance specified by `out_class`, containing data output from
281285
smoothing `img` data by given FWHM kernel.
282286
"""
287+
if not spatial_axes_first(img):
288+
raise ValueError('Cannot predict position of spatial axes for Image '
289+
'type ' + str(type(img)))
283290
if out_class is None:
284291
out_class = img.__class__
285292
n_dim = len(img.shape)

nibabel/tests/test_processing.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
from nibabel.nifti1 import Nifti1Image
2525
from nibabel.nifti2 import Nifti2Image
2626
from nibabel.orientations import flip_axis, inv_ornt_aff
27-
from nibabel.affines import AffineError, from_matvec, to_matvec, apply_affine
27+
from nibabel.affines import (AffineError, from_matvec, to_matvec, apply_affine,
28+
voxel_sizes)
2829
from nibabel.eulerangles import euler2mat
2930

3031
from numpy.testing import (assert_almost_equal,
@@ -41,6 +42,15 @@
4142

4243
DATA_DIR = pjoin(dirname(__file__), 'data')
4344

45+
# 3D MINC work correctly with processing, but not 4D MINC
46+
from .test_imageclasses import MINC_3DS, MINC_4DS
47+
48+
# Filenames of other images that should work correctly with processing
49+
OTHER_IMGS = ('anatomical.nii', 'functional.nii',
50+
'example4d.nii.gz', 'example_nifti2.nii.gz',
51+
'phantom_EPI_asc_CLEAR_2_1.PAR')
52+
53+
4454
def test_sigma2fwhm():
4555
# Test from constant
4656
assert_almost_equal(sigma2fwhm(1), 2.3548200)
@@ -346,6 +356,25 @@ def test_smooth_image():
346356
Nifti2Image)
347357

348358

359+
def test_spatial_axes_check():
360+
for fname in MINC_3DS + OTHER_IMGS:
361+
img = nib.load(pjoin(DATA_DIR, fname))
362+
s_img = smooth_image(img, 0)
363+
assert_array_equal(img.dataobj, s_img.dataobj)
364+
out = resample_from_to(img, img, mode='nearest')
365+
assert_almost_equal(img.dataobj, out.dataobj)
366+
if len(img.shape) > 3:
367+
continue
368+
# Resample to output does not raise an error
369+
out = resample_to_output(img, voxel_sizes(img.affine))
370+
for fname in MINC_4DS:
371+
img = nib.load(pjoin(DATA_DIR, fname))
372+
assert_raises(ValueError, smooth_image, img, 0)
373+
assert_raises(ValueError, resample_from_to, img, img, mode='nearest')
374+
assert_raises(ValueError,
375+
resample_to_output, img, voxel_sizes(img.affine))
376+
377+
349378
def assert_spm_resampling_close(from_img, our_resampled, spm_resampled):
350379
""" Assert our resampling is close to SPM's, allowing for edge effects
351380
"""

0 commit comments

Comments
 (0)