nifreeze.registration.utils module

Utilities to aid in performing and evaluating image registration.

This module provides functions to compute displacements of image coordinates under a transformation, useful for assessing the accuracy of image registration processes.

nifreeze.registration.utils.RADIUS = 50.0

Typical radius (in mm) of a sphere mimicking the size of a typical human brain.

nifreeze.registration.utils.compute_fd_from_motion(motion_parameters: ndarray, radius: float = 50.0) → ndarray[source]

Compute framewise displacement (FD) from motion parameters.

Each row in the motion parameters represents one frame, and columns represent each coordinate axis x, y`, and z. Translation parameters are followed by rotation parameters column-wise.

Parameters:
  • motion_parameters (ndarray) – Motion parameters.

  • radius (float, optional) – Radius (in mm) of a sphere mimicking the size of a typical human brain.

Returns:

The framewise displacement (FD) as the sum of absolute differences between consecutive frames.

Return type:

ndarray

nifreeze.registration.utils.compute_fd_from_transform(img: SpatialImage, test_xfm: TransformBase, radius: float = 50.0) → float[source]

Compute the framewise displacement (FD) for a given transformation.

Parameters:
  • img (SpatialImage) – The reference image. Used to extract the center coordinates.

  • test_xfm (TransformBase) – The transformation to test. Applied to coordinates around the image center.

  • radius (float, optional) – The radius (in mm) of the spherical neighborhood around the center of the image.

Returns:

The average framewise displacement (FD) for the test transformation.

Return type:

float

nifreeze.registration.utils.displacements_within_mask(mask_img: SpatialImage, test_xfm: TransformBase, reference_xfm: TransformBase | None = None) → ndarray[source]

Compute the distance between voxel coordinates mapped through two transforms.

Parameters:
  • mask_img (SpatialImage) – A mask image that defines the region of interest. Voxel coordinates within the mask are transformed.

  • test_xfm (TransformBase) – The transformation to test. This transformation is applied to the voxel coordinates.

  • reference_xfm (TransformBase, optional) – A reference transformation to compare with. If None, the identity transformation is assumed (no transformation).

Returns:

An array of displacements (in mm) for each voxel within the mask.

Return type:

ndarray

nifreeze.registration.utils.extract_motion_parameters(affine: ndarray) → Tuple[ndarray, ndarray][source]

Extract translation (mm) and rotation (degrees) parameters from an affine matrix.

Parameters:

affine (ndarray) – The affine transformation matrix.

Returns:

Extracted translation and rotation parameters.

Return type:

tuple

nifreeze.registration.utils.phase_encoding_vector(phase_encoding: str, affine: ndarray) → ndarray[source]

The phase-encoding direction in world (RAS+) coordinates, as a unit vector.

Parameters:
  • phase_encoding (str) – The direction as BIDS writes PhaseEncodingDirection: an axis of the data array (i, j or k) and, with a trailing -, the reverse sign.

  • affine (ndarray) – The voxel-to-RAS+ affine of the data.

Returns:

The direction, shape (3,).

Return type:

ndarray

Examples

>>> affine = np.diag([-2.0, 2.0, 2.0, 1.0])
>>> phase_encoding_vector("j-", affine).tolist(), phase_encoding_vector("i", affine).tolist()
([0.0, -1.0, 0.0], [-1.0, 0.0, 0.0])
nifreeze.registration.utils.split_phase_encoding(matrix: ndarray, direction: ndarray) → tuple[ndarray, ndarray][source]

Split a mapping into head motion, then a field along phase encoding.

A field of eddy currents linear in position moves points only along the phase-encoding direction \(u\), so a volume’s mapping is \(W = E M\), with \(M\) rigid and \(E = I - u s^T\) (a scaling along \(u\) and two shears; a translation along \(u\) belongs to \(M\)). The split is unique. Every \(v \perp u\) has \(v^T A = v^T R\) (\(A\), \(R\) the linear parts of \(W\), \(M\)), so \(R\)’s rows across \(u\) are \(A\)’s, and its row along \(u\) is their cross product; then \(s^T = (u^T R - u^T A) R^T\), and \(M\)’s translation is \(E^{-1} t_W\). A mapping outside this family is projected: its part across \(u\) is taken as rigid.

Parameters:
  • matrix (ndarray) – The mapping \(W\), a 4 x 4 RAS+ matrix.

  • direction (ndarray) – The phase-encoding direction \(u\) in world coordinates (its sign does not matter).

Returns:

motion, field – \(M\) and \(E\), each a 4 x 4 RAS+ matrix, with field @ motion equal to matrix when it is in the family.

Return type:

ndarray

Examples

>>> motion = np.eye(4)
>>> motion[:3, :3] = [[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]
>>> motion[:3, 3] = [1.0, 2.0, 3.0]
>>> field = np.eye(4)
>>> field[1, :3] = [0.02, 0.97, -0.01]
>>> found = split_phase_encoding(field @ motion, np.array([0.0, 1.0, 0.0]))
>>> np.allclose(found[0], motion), np.allclose(found[1], field)
(True, True)