Nonlinear Transforms

Nonlinear transforms.

class nitransforms.nonlinear.BSplineFieldTransform(coefficients, reference=None, order=3)[source]

Represent a nonlinear transform parameterized by BSpline basis.

apply(spatialimage, reference=None, order=3, mode='constant', cval=0.0, prefilter=True, output_dtype=None)[source]

Apply a B-Spline transform on input data.

map(x, inverse=False)[source]

Apply the transformation to a list of physical coordinate points.

\[\mathbf{y} = \mathbf{x} + \Psi^3(\mathbf{k}, \mathbf{x}), \label{eq:1}\tag{1}\]
Parameters
  • x (N x D numpy.ndarray) – Input RAS+ coordinates (i.e., physical coordinates).

  • inverse (bool) – If True, apply the inverse transform \(x = f^{-1}(y)\).

Returns

y – Transformed (mapped) RAS+ coordinates (i.e., physical coordinates).

Return type

N x D numpy.ndarray

Examples

>>> xfm = BSplineFieldTransform(test_dir / "someones_bspline_coefficients.nii.gz")
>>> xfm.reference = test_dir / "someones_anatomy.nii.gz"
>>> xfm.map([-6.5, -36., -19.5]).tolist()
[[-6.5, -31.476097418406784, -19.5]]
>>> xfm.map([[-6.5, -36., -19.5], [-1., -41.5, -11.25]]).tolist()
[[-6.5, -31.476097418406784, -19.5], [-1.0, -3.8072675377121996, -11.25]]
to_field(reference=None, dtype='float32')[source]

Generate a displacements deformation field from this B-Spline field.

class nitransforms.nonlinear.DenseFieldTransform(field=None, is_deltas=True, reference=None)[source]

Represents dense field (voxel-wise) transforms.

map(x, inverse=False)[source]

Apply the transformation to a list of physical coordinate points.

\[\mathbf{y} = \mathbf{x} + \Delta(\mathbf{x}), \label{eq:2}\tag{2}\]

where \(\Delta(\mathbf{x})\) is the value of the discrete field of displacements \(\Delta\) interpolated at the location \(\mathbf{x}\).

Parameters
  • x (N x D numpy.array_like) – Input RAS+ coordinates (i.e., physical coordinates).

  • inverse (bool) – If True, apply the inverse transform \(x = f^{-1}(y)\).

Returns

y – Transformed (mapped) RAS+ coordinates (i.e., physical coordinates).

Return type

N x D numpy.array_like

Examples

>>> xfm = DenseFieldTransform(
...     test_dir / "someones_displacement_field.nii.gz",
...     is_deltas=False,
... )
>>> xfm.map([-6.5, -36., -19.5]).tolist()
[[0.0, -0.47516798973083496, 0.0]]
>>> xfm.map([[-6.5, -36., -19.5], [-1., -41.5, -11.25]]).tolist()
[[0.0, -0.47516798973083496, 0.0], [0.0, -0.538356602191925, 0.0]]
>>> np.array_str(
...     xfm.map([[-6.7, -36.3, -19.2], [-1., -41.5, -11.25]]),
...     precision=3,
...     suppress_small=True,
... )
'[[ 0.    -0.482  0.   ]\n [ 0.    -0.538  0.   ]]'
>>> xfm = DenseFieldTransform(
...     test_dir / "someones_displacement_field.nii.gz",
...     is_deltas=True,
... )
>>> xfm.map([[-6.5, -36., -19.5], [-1., -41.5, -11.25]]).tolist()
[[-6.5, -36.47516632080078, -19.5], [-1.0, -42.03835678100586, -11.25]]
>>> np.array_str(
...     xfm.map([[-6.7, -36.3, -19.2], [-1., -41.5, -11.25]]),
...     precision=3,
...     suppress_small=True,
... )
'[[ -6.7   -36.782 -19.2  ]\n [ -1.    -42.038 -11.25 ]]'