nifreeze.registration.base module

The interface of every registration backend, and the factory that builds them by name.

nifreeze.registration.base.AFFINE = PosixPath('/home/runner/work/nifreeze/nifreeze/src/nifreeze/registration/config/affine.yaml')

Affine registration, started from the rigid estimate (eddy currents).

nifreeze.registration.base.BACKENDS = {'ants': 'nifreeze.registration.ants:Ants', 'elastix': 'nifreeze.registration.elastix:Elastix', 'itk': 'nifreeze.registration.itk_backend:ITK'}

The backends by name, as module:class (imported when first built).

nifreeze.registration.base.CLIPPING = ('sample', 'predicted', 'both')

The values of clip that clip an image to the window.

nifreeze.registration.base.CONFIG = PosixPath('/home/runner/work/nifreeze/nifreeze/src/nifreeze/registration/config')

The packaged settings, with a section per backend.

nifreeze.registration.base.RIGID = PosixPath('/home/runner/work/nifreeze/nifreeze/src/nifreeze/registration/config/rigid.yaml')

Rigid registration, the default (head motion).

class nifreeze.registration.base.Registration(self, align_config: 'Mapping | str | PathLike | None' = None, *, seed: 'int | None' = None, num_threads: 'int | None' = None, clip: 'str | bool | None' = 'both', debug_dir: 'str | Path | None' = None, phase_encoding: 'str | None' = None) → 'None'[source]

Bases: ABC

Estimate the transform that maps each point of a fixed image into a moving one.

The fixed image is the model’s prediction; the moving image is the sample, on the same grid. Calls run one at a time (elastix sets ITK’s process-wide thread count).

Parameters:
  • align_config (Mapping or os.pathlike, optional) – The backend’s settings, or a YAML file with a section per backend, such as RIGID (the default) and AFFINE (see load_config()).

  • seed (int, optional) – Seed of the backend’s random sampling.

  • num_threads (int, optional) – Threads of each registration.

  • clip (str, optional) – Clip "sample", "predicted", "both" (default) or "none" of the images to the sample’s intensity window, estimated within the mask.

  • debug_dir (os.pathlike, optional) – Keep predicted_<index>.nii and sample_<index>.nii, the values registered, and aligned_<index>.nii, the sample with its affine corrected by the estimate (a viewer shows it on top of the prediction).

  • phase_encoding (str, optional) – The images’ phase-encoding direction, as BIDS writes PhaseEncodingDirection (i, j or k, with an optional -). Only backends whose settings restrict a field to it use it (see split()).

__call__(fixed: ndarray, moving: ndarray, affine: ndarray, mask: ndarray | None = None, index: int = 0, init: ndarray | None = None) → Affine[source]

Register one volume.

Parameters:
  • fixed, moving (ndarray) – The fixed image (the prediction) and the moving one (the sample).

  • affine (ndarray) – The voxel-to-RAS+ affine of their grid.

  • mask (ndarray, optional) – The mask of the fixed image (the moving one is never masked).

  • index (int, optional) – The volume’s index, which names its files.

  • init (ndarray, optional) – The initial estimate, a RAS+ fixed-to-moving matrix.

Returns:

The fixed-to-moving transform (RAS+), the initialization included.

Return type:

Affine

align_config: Any
split(matrix: ndarray, affine: ndarray) → tuple[ndarray, ndarray] | None[source]

Split an estimate into head motion and a field, when the backend models both.

A backend whose settings restrict the field to the phase-encoding direction estimates each mapping as head motion, then that field, and returns the two (see split_phase_encoding()). The others return None, and the estimate is stored whole.

Parameters:
  • matrix (ndarray) – An estimate of this backend, a 4 x 4 RAS+ matrix.

  • affine (ndarray) – The voxel-to-RAS+ affine of the images it was estimated on.

Returns:

The head motion and the field, each a 4 x 4 RAS+ matrix; or None.

Return type:

tuple or None

class nifreeze.registration.base.RegistrationFactory(self, /, *args, **kwargs)[source]

Bases: object

Build a registration backend by name, as models are built by name.

static init(name: str | None = None, **kwargs: Any) → Registration[source]

Build the backend name (of BACKENDS, or any "module:class").

Without a name, the backend is that of default_backend().

>>> type(RegistrationFactory.init("nifreeze.registration.ants:Ants")).__name__
'Ants'
nifreeze.registration.base.WINDOW_LEVELS = 4095

Highest code of the 12-bit encoding of floating-point registration inputs.

nifreeze.registration.base.default_backend() → str[source]

Elastix where itk-elastix is installed (as #672 found it), ANTs otherwise.

nifreeze.registration.base.load_config(align_config: Mapping | str | PathLike | None, backend: str) → dict[source]

Read one backend’s settings.

Parameters:
  • align_config (Mapping or os.pathlike, optional) – The backend’s settings, or a YAML file with a section per backend (default: RIGID).

  • backend (str) – The section of a file to read.

Returns:

A copy of the settings.

Return type:

dict

Examples

>>> load_config(AFFINE, "elastix")["stages"][0]["parameters"]["Transform"]
['AffineTransform']
>>> load_config(None, "ants") == load_config(RIGID, "ants")
True
>>> load_config(RIGID, "flirt")
Traceback (most recent call last):
ValueError: .../rigid.yaml has no settings for flirt (it has: ants, elastix).