Skip to content

Commit cc3b21e

Browse files
Julien Marabottooesteban
authored andcommitted
Updated outsource Apply
outsourced Apply(); fixed resampled.py (line 101); implemented np.tensordor to _apply_affine() in main.py (line 287). Left to do: fix test_linear RunTime error (line 358)
1 parent 3ff7407 commit cc3b21e

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

nitransforms/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,11 @@ def _as_homogeneous(xyz, dtype="float32", dim=3):
283283

284284
return np.hstack((xyz, np.ones((xyz.shape[0], 1), dtype=dtype)))
285285

286-
286+
#import pdb; pdb.set_trace()
287287
def _apply_affine(x, affine, dim):
288288
"""Get the image array's indexes corresponding to coordinates."""
289-
return affine.dot(_as_homogeneous(x, dim=dim).T)[:dim, ...].T
289+
return np.tensordot(
290+
affine,
291+
_as_homogeneous(x, dim=dim).T,
292+
axes=1,
293+
)[:dim, ...]

nitransforms/resampling.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,36 @@ def apply(
8787
spatialimage = _nbload(str(spatialimage))
8888

8989
data = np.asanyarray(spatialimage.dataobj)
90+
9091
targets = ImageGrid(spatialimage).index( # data should be an image
9192
_as_homogeneous(transform.map(_ref.ndcoords.T), dim=_ref.ndim)
9293
)
9394

95+
if data.ndim < targets.shape[-1]:
96+
data = data[..., np.newaxis]
97+
98+
import pdb; pdb.set_trace()
99+
100+
#import pdb; pdb.set_trace()
94101
resampled = ndi.map_coordinates(
95102
data,
96-
targets.T,
103+
#targets.T,
104+
#Reshape targets (516096, 3, 8) --> (4, 4128768) :
105+
_as_homogeneous(targets.reshape(-2, targets.shape[0])).T,
97106
output=output_dtype,
98107
order=order,
99108
mode=mode,
100109
cval=cval,
101110
prefilter=prefilter,
102111
)
103-
112+
104113
if isinstance(_ref, ImageGrid): # If reference is grid, reshape
105114
hdr = None
106115
if _ref.header is not None:
107116
hdr = _ref.header.copy()
108117
hdr.set_data_dtype(output_dtype or spatialimage.get_data_dtype())
109118
moved = spatialimage.__class__(
110-
resampled.reshape(_ref.shape),
119+
resampled.reshape(_ref.shape if data.ndim < 4 else _ref.shape + (-1, )),
111120
_ref.affine,
112121
hdr,
113122
)

nitransforms/tests/test_linear.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,11 @@ def test_LinearTransformsMapping_apply(tmp_path, data_path, testdata_path):
353353
hmcinv = nitl.LinearTransformsMapping(
354354
np.linalg.inv(hmc.matrix), reference=testdata_path / "func.nii.gz"
355355
)
356-
nii = apply(hmcinv, testdata_path / "fmap.nii.gz", order=1)
356+
357+
import pdb; pdb.set_trace()
358+
nii = apply(
359+
hmcinv, testdata_path / "fmap.nii.gz", order=1
360+
)
357361
assert nii.dataobj.shape[-1] == len(hmc)
358362

359363
# Ensure a ValueError is issued when trying to do weird stuff

0 commit comments

Comments
 (0)