Skip to content

Commit 554129d

Browse files
committed
fix: ensure ndcoords and ndindex are NxD
1 parent c71a127 commit 554129d

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

nitransforms/base.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,30 +202,24 @@ def inverse(self):
202202
def ndindex(self):
203203
"""List the indexes corresponding to the space grid."""
204204
if self._ndindex is None:
205-
indexes = tuple([np.arange(s) for s in self._shape])
206-
self._ndindex = np.array(np.meshgrid(*indexes, indexing="ij")).reshape(
207-
self._ndim, self._npoints
208-
)
205+
indexes = np.mgrid[0:self._shape[0], 0:self._shape[1], 0:self._shape[2]]
206+
self._ndindex = indexes.reshape((indexes.shape[0], -1)).T
209207
return self._ndindex
210208

211209
@property
212210
def ndcoords(self):
213211
"""List the physical coordinates of this gridded space samples."""
214212
if self._coords is None:
215-
self._coords = np.tensordot(
216-
self._affine,
217-
np.vstack((self.ndindex, np.ones((1, self._npoints)))),
218-
axes=1,
219-
)[:3, ...]
213+
self._coords = self.ras(self.ndindex)
220214
return self._coords
221215

222216
def ras(self, ijk):
223217
"""Get RAS+ coordinates from input indexes."""
224-
return _apply_affine(ijk, self._affine, self._ndim)
218+
return _apply_affine(ijk, self._affine, self._ndim).T
225219

226220
def index(self, x):
227221
"""Get the image array's indexes corresponding to coordinates."""
228-
return _apply_affine(x, self._inverse, self._ndim)
222+
return _apply_affine(x, self._inverse, self._ndim).T
229223

230224
def _to_hdf5(self, group):
231225
group.attrs["Type"] = "image"

nitransforms/resampling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def apply(
253253
serialize_4d = n_resamplings >= serialize_nvols
254254

255255
targets = None
256-
ref_ndcoords = _ref.ndcoords.T
256+
ref_ndcoords = _ref.ndcoords
257257
if hasattr(transform, "to_field") and callable(transform.to_field):
258258
targets = ImageGrid(spatialimage).index(
259259
_as_homogeneous(
@@ -332,7 +332,7 @@ def apply(
332332

333333
resampled = ndi.map_coordinates(
334334
data,
335-
targets,
335+
targets.T,
336336
order=order,
337337
mode=mode,
338338
cval=cval,

nitransforms/tests/test_base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,24 @@ def test_ImageGrid(get_testdata, image_orientation):
5555

5656
assert np.allclose(np.squeeze(img.ras(ijk[0])), xyz[0])
5757
assert np.allclose(np.round(img.index(xyz[0])), ijk[0])
58-
assert np.allclose(img.ras(ijk).T, xyz)
59-
assert np.allclose(np.round(img.index(xyz)).T, ijk)
58+
assert np.allclose(img.ras(ijk), xyz)
59+
assert np.allclose(np.round(img.index(xyz)), ijk)
6060

6161
# nd index / coords
6262
idxs = img.ndindex
6363
coords = img.ndcoords
6464
assert len(idxs.shape) == len(coords.shape) == 2
65-
assert idxs.shape[0] == coords.shape[0] == img.ndim == 3
66-
assert idxs.shape[1] == coords.shape[1] == img.npoints == np.prod(im.shape)
65+
assert idxs.shape[1] == coords.shape[1] == img.ndim == 3
66+
assert idxs.shape[0] == coords.shape[0] == img.npoints == np.prod(im.shape)
6767

6868
img2 = ImageGrid(img)
6969
assert img2 == img
7070
assert (img2 != img) is False
7171

72+
# Test indexing round trip
73+
np.testing.assert_allclose(img.ndcoords, img.ras(img.ndindex))
74+
np.testing.assert_allclose(img.ndindex, np.round(img.index(img.ndcoords)))
75+
7276

7377
def test_ImageGrid_utils(tmpdir, testdata_path, get_testdata):
7478
"""Check that images can be objects or paths and equality."""

0 commit comments

Comments
 (0)