From be5ddd628aede5979f06a7209c8f9bbab083c02c Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Thu, 14 Aug 2025 10:42:38 +0200 Subject: [PATCH 1/2] test: reproduce 4d singleton issue --- nitransforms/tests/test_resampling.py | 9 +++++++++ 1 file changed, 9 insertions(+) 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", [ From 01cb7cda1bd5dd1722540c9af38e1823dae609a4 Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Thu, 14 Aug 2025 14:08:47 +0200 Subject: [PATCH 2/2] fix: squeeze last dimension of moving image in ``apply()`` Resolves: #270. --- nitransforms/resampling.py | 3 +++ 1 file changed, 3 insertions(+) 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)