Skip to content

Commit 997cbac

Browse files
oestebaneffigies
andauthored
Apply suggestions from code review
Co-authored-by: Chris Markiewicz <[email protected]>
1 parent 52a68e5 commit 997cbac

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

nitransforms/interp/bspline.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ def grid_bspline_weights(target_nii, ctrl_nii):
7575
shape = target_nii.shape[:3]
7676
ctrl_sp = nb.affines.voxel_sizes(ctrl_nii.affine)[:3]
7777
ras2ijk = np.linalg.inv(ctrl_nii.affine)
78+
# IJK index in the control point image of the first index in the target image
7879
origin = nb.affines.apply_affine(ras2ijk, [tuple(target_nii.affine[:3, 3])])[0]
7980

8081
wd = []
8182
for i, (o, n, sp) in enumerate(
8283
zip(origin, shape, nb.affines.voxel_sizes(target_nii.affine)[:3])
8384
):
85+
# Locations of voxels in target image in control point image
8486
locations = np.arange(0, n, dtype="float16") * sp / ctrl_sp[i] + o
8587
knots = np.arange(0, ctrl_nii.shape[i], dtype="float16")
8688
distance = np.abs(locations[np.newaxis, ...] - knots[..., np.newaxis])

nitransforms/nonlinear.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, field, reference=None):
3737
Example
3838
-------
3939
>>> DisplacementsFieldTransform(test_dir / "someones_displacement_field.nii.gz")
40-
<(57, 67, 56) field of 3D displacements>
40+
<DisplacementFieldTransform[3D] (57, 67, 56)>
4141
4242
"""
4343
super().__init__()
@@ -59,7 +59,7 @@ def __init__(self, field, reference=None):
5959

6060
def __repr__(self):
6161
"""Beautify the python representation."""
62-
return f"<{self._field.shape[:3]} field of {self._field.shape[-1]}D displacements>"
62+
return f"<DisplacementFieldTransform[{self._field.shape[-1]}D] {self._field.shape[:3]}>"
6363

6464
def map(self, x, inverse=False):
6565
r"""
@@ -132,7 +132,7 @@ def __init__(self, coefficients, reference=None, order=3):
132132
coefficients = _ensure_image(coefficients)
133133

134134
self._coeffs = np.asanyarray(coefficients.dataobj)
135-
self._knots = ImageGrid(four_to_three(coefficients)[0])
135+
self._knots = ImageGrid(coefficients)
136136
self._weights = None
137137
if reference is not None:
138138
self.reference = reference
@@ -158,9 +158,11 @@ def to_field(self, reference=None, dtype="float32"):
158158
field = np.zeros((_ref.npoints, ndim))
159159

160160
for d in range(ndim):
161+
# 1 x Nvox : (1 x K) @ (K x Nvox)
161162
field[:, d] = self._coeffs[..., d].reshape(-1) @ self._weights
162163

163-
return field.astype(dtype)
164+
return DisplacementsFieldTransform(
165+
field.astype(dtype).reshape(*_ref.shape, -1), reference=_ref)
164166

165167
def apply(
166168
self,
@@ -197,10 +199,7 @@ def apply(
197199
)
198200

199201
# If locations to be interpolated are on a grid, generate a displacements field
200-
return DisplacementsFieldTransform(
201-
self.to_field().reshape((*(_ref.shape), -1)),
202-
reference=_ref,
203-
).apply(
202+
return self.to_field().apply(
204203
spatialimage,
205204
reference=reference,
206205
order=order,
@@ -216,7 +215,7 @@ def map(self, x, inverse=False):
216215
217216
.. math::
218217
\mathbf{y} = \mathbf{x} + \Psi^3(\mathbf{k}, \mathbf{x}),
219-
\label{eq:2}\tag{2}
218+
\label{eq:1}\tag{1}
220219
221220
Parameters
222221
----------

0 commit comments

Comments
 (0)