diff --git a/nitransforms/resampling.py b/nitransforms/resampling.py index 6166386d..2428a769 100644 --- a/nitransforms/resampling.py +++ b/nitransforms/resampling.py @@ -19,6 +19,7 @@ from nibabel.loadsave import load as _nbload from nibabel.arrayproxy import get_obj_dtype from nibabel.spatialimages import SpatialImage +from nibabel.funcs import squeeze_image from scipy import ndimage as ndi from nitransforms.base import ( @@ -233,6 +234,8 @@ def apply( if isinstance(spatialimage, (str, Path)): spatialimage = _nbload(str(spatialimage)) + spatialimage = squeeze_image(spatialimage) + # Avoid opening the data array just yet input_dtype = cap_dtype(get_obj_dtype(spatialimage.dataobj), dtype_width) diff --git a/nitransforms/tests/test_resampling.py b/nitransforms/tests/test_resampling.py index d7538ea4..3ff8fb36 100644 --- a/nitransforms/tests/test_resampling.py +++ b/nitransforms/tests/test_resampling.py @@ -51,6 +51,15 @@ } +def test_apply_singleton_time_dimension(): + """Resampling fails when the input image has a trailing singleton dimension (gh-270).""" + + data = np.reshape(np.arange(27, dtype=np.uint8), (3, 3, 3, 1)) + nii = nb.Nifti1Image(data, np.eye(4)) + xfm = nitl.Affine(np.eye(4), reference=nii) + apply(xfm, nii) + + @pytest.mark.parametrize( "image_orientation", [