Skip to content

Commit f663530

Browse files
authored
Merge pull request #278 from nipy/codex/investigate-issue-#270-and-propose-tests
FIX: ``apply()`` fails with 4D singleton images
2 parents 7772372 + 01cb7cd commit f663530

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

nitransforms/resampling.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from nibabel.loadsave import load as _nbload
2020
from nibabel.arrayproxy import get_obj_dtype
2121
from nibabel.spatialimages import SpatialImage
22+
from nibabel.funcs import squeeze_image
2223
from scipy import ndimage as ndi
2324

2425
from nitransforms.base import (
@@ -233,6 +234,8 @@ def apply(
233234
if isinstance(spatialimage, (str, Path)):
234235
spatialimage = _nbload(str(spatialimage))
235236

237+
spatialimage = squeeze_image(spatialimage)
238+
236239
# Avoid opening the data array just yet
237240
input_dtype = cap_dtype(get_obj_dtype(spatialimage.dataobj), dtype_width)
238241

nitransforms/tests/test_resampling.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@
5151
}
5252

5353

54+
def test_apply_singleton_time_dimension():
55+
"""Resampling fails when the input image has a trailing singleton dimension (gh-270)."""
56+
57+
data = np.reshape(np.arange(27, dtype=np.uint8), (3, 3, 3, 1))
58+
nii = nb.Nifti1Image(data, np.eye(4))
59+
xfm = nitl.Affine(np.eye(4), reference=nii)
60+
apply(xfm, nii)
61+
62+
5463
@pytest.mark.parametrize(
5564
"image_orientation",
5665
[

0 commit comments

Comments
 (0)