Skip to content

Commit 20e2c25

Browse files
authored
Merge pull request #279 from nipy/fix/finalize-270
FIX: Respect singleton 4th-D
2 parents 13b0a26 + 0857139 commit 20e2c25

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

nitransforms/resampling.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def apply(
234234
if isinstance(spatialimage, (str, Path)):
235235
spatialimage = _nbload(str(spatialimage))
236236

237+
singleton_4d = spatialimage.ndim == 4 and spatialimage.shape[-1] == 1
237238
spatialimage = squeeze_image(spatialimage)
238239

239240
# Avoid opening the data array just yet
@@ -370,7 +371,8 @@ def apply(
370371
with suppress(ValueError):
371372
resampled = np.squeeze(resampled, axis=3)
372373

373-
moved = spatialimage.__class__(resampled, _ref.affine, hdr)
374+
moved = spatialimage.__class__(
375+
resampled[..., None] if singleton_4d else resampled, _ref.affine, hdr)
374376
return moved
375377

376378
output_dtype = output_dtype or input_dtype

nitransforms/tests/test_resampling.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ def test_apply_singleton_time_dimension():
5656

5757
data = np.reshape(np.arange(27, dtype=np.uint8), (3, 3, 3, 1))
5858
nii = nb.Nifti1Image(data, np.eye(4))
59-
xfm = nitl.Affine(np.eye(4), reference=nii)
60-
apply(xfm, nii)
59+
ref = nb.Nifti1Image(np.zeros((4, 4, 4)), np.eye(4))
60+
xfm = nitl.Affine(np.eye(4), reference=ref)
61+
movednii = apply(xfm, nii)
62+
assert movednii.shape == ref.shape + (1, )
63+
64+
movednii = apply(xfm, nii, reference=ref)
65+
assert movednii.shape == ref.shape + (1, )
6166

6267

6368
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)