Nonlinear Transforms

Nonlinear transforms.

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

Represent a nonlinear transform parameterized by BSpline basis.

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, -36.475114..., -19.5]]
>>> xfm.map([[-6.5, -36., -19.5], [-1., -41.5, -11.25]]).tolist()  
[[-6.5, -36.475114..., -19.5], [-1.0, -42.03878957..., -11.25]]
property ndim

Get the dimensions of the transform.

to_field(reference=None, dtype='float32')[source]

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

to_x5(metadata=None)[source]

Return an X5Transform representation.

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

Represents dense field (voxel-wise) transforms.

property is_deltas

Check whether this is a displacements (True) or a deformation (False) field.

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.475..., -19.5], [-1.0, -42.038..., -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 ]]'
property ndim

Get the dimensions of the transform.

to_filename(filename, fmt='X5', moving=None, x5_inverse=False)[source]

Store the transform in the designated format.

to_x5(metadata=None)[source]

Return an X5Transform representation.

nitransforms.nonlinear.from_x5(x5_list, x5_position=0)[source]

Create a transform from a list of X5Transform objects.