IO
Reading and writing of transform files.
Base I/O
Read/write linear transforms.
- class nitransforms.io.base.BaseLinearTransformList(xforms=None, binaryblock=None, endianness=None, check=True)[source]
A string-based structure for series of linear transforms.
- property xforms
Get the list of internal transforms.
- class nitransforms.io.base.DisplacementsField[source]
A data structure representing displacements fields.
- class nitransforms.io.base.LinearParameters(parameters=None)[source]
A string-based structure for linear transforms.
Examples
>>> lp = LinearParameters() >>> np.array_equal(lp.structarr['parameters'], np.eye(4)) True
>>> p = np.diag([2., 2., 2., 1.]) >>> lp = LinearParameters(p) >>> np.array_equal(lp.structarr['parameters'], p) True
- class nitransforms.io.base.LinearTransformStruct(binaryblock=None, endianness=None, check=True)[source]
File data structure from linear transforms.
- class nitransforms.io.base.StringBasedStruct(binaryblock=None, endianness=None, check=True)[source]
File data structure from text files.
Tool Specific I/O
AFNI
Read/write AFNI’s transforms.
- class nitransforms.io.afni.AFNIDisplacementsField[source]
A data structure representing displacements fields.
- class nitransforms.io.afni.AFNILinearTransform(parameters=None)[source]
A string-based structure for AFNI linear transforms.
- classmethod from_ras(ras, moving=None, reference=None)[source]
Create an AFNI affine from a nitransform’s RAS+ matrix.
AFNI implicitly de-obliques image affine matrices before applying transforms, so for consistency we update the transform to account for the obliquity of the images.
>>> moving.affine == ras @ reference.affine
We can decompose the affines into oblique and de-obliqued components:
>>> moving.affine == m_obl @ m_deobl >>> reference.affine == r_obl @ r_deobl
To generate an equivalent AFNI transform, we need an effective transform (
e_ras):>>> m_obl @ m_deobl == ras @ r_obl @ r_deobl >>> m_deobl == inv(m_obl) @ ras @ r_obl @ r_deobl
Hence,
>>> m_deobl == e_ras @ r_deobl >>> e_ras == inv(m_obl) @ ras @ r_obl
- class nitransforms.io.afni.AFNILinearTransformArray(xforms=None, binaryblock=None, endianness=None, check=True)[source]
A string-based structure for series of AFNI linear transforms.
BIDS’ X5
Data structures for the X5 transform format.
Implements what’s drafted in the BIDS X5 specification draft.
- class nitransforms.io.x5.X5Domain(grid: bool, size: Sequence[int], mapping: ndarray | None, coordinates: str | None = None)[source]
Domain information of a transform representing reference/moving spaces.
- coordinates: str | None = None
Indexing type of the Mapping field (for example, “cartesian”, “spherical” or “index”).
- class nitransforms.io.x5.X5Transform(type: str, transform: ndarray, subtype: str | None = None, representation: str | None = None, metadata: Dict[str, Any] | None = None, dimension_kinds: Sequence[str] | None = None, domain: X5Domain | None = None, inverse: ndarray | None = None, jacobian: ndarray | None = None, additional_parameters: ndarray | None = None, array_length: int = 1)[source]
Represent one transform entry of an X5 file.
- additional_parameters: ndarray | None = None
An OPTIONAL field to store additional parameters, depending on the SubType of the transform.
- array_length: int = 1
Undocumented field in the draft to enable a single transform group for 4D transforms.
- dimension_kinds: Sequence[str] | None = None
Identifies what “kind” of information is represented by the samples along each axis.
- domain: X5Domain | None = None
A dataset specifying the reference manifold for the transform (either a regularly gridded 3D space or a surface/sphere). REQUIRED for nonlinear Type, RECOMMENDED for linear Type.
- jacobian: ndarray | None = None
A RECOMMENDED data array to keep cached the determinant of Jacobian of the transform in case tools have calculated it. For parametric models it is generally possible to obtain it analytically, so this dataset could not be as useful in that case.
- representation: str | None = None
A string specifiying the transform representation or model, REQUIRED only for nonlinear Type.
- subtype: str | None = None
An OPTIONAL extension of type to drive the interpretation of AdditionalParameters.
- nitransforms.io.x5.from_filename(fname: str | Path) List[X5Transform][source]
Read a list of
X5Transformobjects from an X5 HDF5 file.
- nitransforms.io.x5.to_filename(fname: str | Path, x5_list: List[X5Transform])[source]
Write a list of
X5Transformobjects to an X5 HDF5 file.- Parameters:
fname (
os.PathLike) – The file name (preferably with the “.x5” extension) in which transforms will be stored.x5_list (
list) – The list of transforms to be stored in the output dataset.
- Returns:
fname – File containing the transform(s).
- Return type:
FreeSurfer/LTA
Read/write linear transforms.
- class nitransforms.io.lta.FSLinearTransform(binaryblock=None, endianness=None, check=True)[source]
Represents a single LTA’s transform structure.
- classmethod from_ras(ras, moving=None, reference=None)[source]
Create an affine from a nitransform’s RAS+ matrix.
- to_ras(moving=None, reference=None)[source]
Return a nitransforms’ internal RAS+ array.
Seemingly, the matrix of an LTA is defined such that it maps coordinates from the
dest volumeto thesrc volume. Therefore, without inversion, the LTA matrix is appropiate to move the information fromsrc volumeinto thedest volume’s grid.Important
The
movingandreferenceparameters are dismissed becauseVOX2VOXLTAs are converted toRAS2RAStype before returning the RAS+ matrix, using thedestandsrccontained in the LTA. Both arguments are kept for API compatibility.- Parameters:
moving (dismissed) – The spatial reference of moving images.
reference (dismissed) – The spatial reference of moving images.
- Returns:
matrix – The RAS+ affine matrix corresponding to the LTA.
- Return type:
- class nitransforms.io.lta.FSLinearTransformArray(xforms=None, binaryblock=None, endianness=None, check=True)[source]
A list of linear transforms generated by FreeSurfer.
FSL
Read/write FSL’s transforms.
- class nitransforms.io.fsl.FSLDisplacementsField[source]
A data structure representing displacements fields.
- class nitransforms.io.fsl.FSLLinearTransform(parameters=None)[source]
A string-based structure for FSL linear transforms.
- class nitransforms.io.fsl.FSLLinearTransformArray(xforms=None, binaryblock=None, endianness=None, check=True)[source]
A string-based structure for series of FSL linear transforms.
- classmethod from_filename(filename)[source]
Read the struct from a file given its path.
If the file does not exist, then indexed names with the zero-padded suffix
.NNNare attempted, following FSL’s MCFLIRT conventions.
ITK
Read/write ITK transforms.
- class nitransforms.io.itk.ITKCompositeH5[source]
A data structure for ITK’s HDF5 files.
- class nitransforms.io.itk.ITKDisplacementsField[source]
A data structure representing displacements fields.
- class nitransforms.io.itk.ITKLinearTransform(parameters=None, offset=None)[source]
A string-based structure for ITK linear transforms.
- class nitransforms.io.itk.ITKLinearTransformArray(xforms=None, binaryblock=None, endianness=None, check=True)[source]
A string-based structure for series of ITK linear transforms.
- classmethod from_ras(ras, moving=None, reference=None)[source]
Create an ITK affine from a nitransform’s RAS+ matrix.
The moving and reference parameters are included in this method’s signature for a consistent API, but they have no effect on this particular method because ITK already uses RAS+ coordinates to describe transfroms internally.
- property xforms
Get the list of internal ITKLinearTransforms.