nifreeze.data.base module

Four-dimensional data representation in hard-disk and memory.

nifreeze.data.base.AFFINE_ABSENCE_ERROR_MSG = "BaseDataset 'affine' may not be None"

BaseDataset initialization affine absence error message.

nifreeze.data.base.AFFINE_NDIM_ERROR_MSG = "BaseDataset 'affine' must be a 2D array"

Affine dimensionality error message.

nifreeze.data.base.AFFINE_OBJECT_ERROR_MSG = "BaseDataset 'affine' must be a numpy array."

BaseDataset initialization affine object error message.

nifreeze.data.base.AFFINE_SHAPE_ERROR_MSG = "BaseDataset 'affine' must be a 2D numpy array (4 x 4)"

BaseDataset initialization affine shape error message.

nifreeze.data.base.BRAINMASK_SHAPE_MISMATCH_ERROR_MSG = "BaseDataset 'brainmask' shape ({brainmask_shape}) does not match dataset volumes ({data_shape})."

BaseDataset brainmask shape mismatch error message.

class nifreeze.data.base.BaseDataset(self, dataobj: 'Any' = None, affine: 'np.ndarray' = None, brainmask: 'np.ndarray | None' = None, motion_affines: 'np.ndarray | None' = None, datahdr: 'nb.Nifti1Header | None' = None, filepath: 'Path' = NOTHING) → None[source]

Bases: Generic[Unpack[Ts]]

Base dataset representation structure.

A general data structure to represent 4D images and the necessary metadata for head motion estimation (that is, potentially a brain mask and the head motion estimates).

Volumes are stored on the first axis: dataobj has shape (N, X, Y, Z) for N volumes, and dataset[i] selects dataobj[i]. The array is kept C-contiguous, so each volume is a contiguous block in memory and on disk.

The data structure has a direct HDF5 mapping to facilitate memory efficiency. For modalities requiring additional metadata such as DWI (which requires the gradient table and potentially a b=0 reference), this class may be derived to override certain behaviors (in the case of DWIs, the indexed access should also return the corresponding gradient specification).

Method generated by attrs for class BaseDataset.

__len__() → int[source]

Obtain the number of volumes/frames in the dataset.

affine: ndarray

Best affine for RAS-to-voxel conversion of coordinates (NIfTI header).

brainmask: ndarray | None

A boolean ndarray object containing a corresponding brainmask.

datahdr: Nifti1Header | None

A Nifti1Header header corresponding to the data.

dataobj: ndarray

N volumes on the first axis.

Type:

A ndarray of shape (N, X, Y, Z)

classmethod from_filename(filename: Path | str) → Self[source]

Read an nfHDF5 file from disk and create a BaseDataset.

Parameters:

filename (os.pathlike) – The HDF5 file path to read.

Returns:

The constructed dataset with data loaded from the file.

Return type:

BaseDataset

get_filename() → Path[source]

Get the filepath of the HDF5 file.

motion_affines: ndarray | None

Array of Affine realigning the dataset.

realign_into(view: Self, index: int, order: int = 3) → None[source]

Write volume index into a realigned view, moved by its current estimate.

The original volume is resampled once with the estimate in motion_affines (the same mapping as to_nifti()). A new estimate replaces the realigned volume: the realigned volume is never resampled again. Volumes without a motion estimate are copied as they are.

Parameters:
  • view (BaseDataset) – The realigned view (see realigned()).

  • index (int) – The volume to realign.

  • order (int, optional) – The interpolation order used to resample the volume.

realigned(order: int = 3) → Self[source]

Create the realigned view of the dataset, which models train on.

The view is a shallow copy whose volumes are a disk-backed copy of this dataset’s volumes. Volumes with a motion estimate are moved into the reference frame (see realign_into()). This dataset is not modified.

Parameters:

order (int, optional) – The interpolation order used to resample the volumes.

Returns:

The realigned view.

Return type:

BaseDataset

set_transform(index: int, affine: ndarray) → None[source]

Set an affine transform for a particular index and update the data object.

Parameters:
  • index (int) – The volume index to transform.

  • affine (ndarray) – The 4x4 affine matrix to be applied.

property shape3d: tuple[int, ...]

Get the shape of the 3D volume.

property size3d

Get the number of voxels in the 3D volume.

to_filename(filename: Path | str, compression: str | None = None, compression_opts: Any = None) → None[source]

Write the dataset to an nfHDF5 file.

Parameters:
  • filename (os.pathlike) – The HDF5 file path to write to.

  • compression (str, optional) – Compression strategy. See create_dataset documentation.

  • compression_opts (typing.Any, optional) – Parameters for compression filters.

to_nifti(filename: Path | str | None = None, write_hmxfms: bool = False, order: int = 3) → Nifti1Image[source]

Write a NIfTI file to disk.

Volumes are resampled to the reference affine if motion affines have been set, otherwise the original data are written.

Parameters:
  • filename (os.pathlike, optional) – The output NIfTI file path.

  • write_hmxfms (bool, optional) – If True, the head motion affines will be written out to filesystem with BIDS’ X5 format.

  • order (int, optional) – The interpolation order to use when resampling the data. Defaults to 3 (cubic interpolation).

Returns:

NIfTI image written to disk.

Return type:

Nifti1Image

nifreeze.data.base.DATAOBJ_ABSENCE_ERROR_MSG = "BaseDataset 'dataobj' may not be None"

BaseDataset initialization dataobj absence error message.

nifreeze.data.base.DATAOBJ_NDIM_ERROR_MSG = "BaseDataset 'dataobj' must be a 4-D numpy array"

BaseDataset initialization dataobj dimensionality error message.

nifreeze.data.base.DATAOBJ_OBJECT_ERROR_MSG = "BaseDataset 'dataobj' must be a numpy array."

BaseDataset initialization dataobj object error message.

class nifreeze.data.base.ImageGrid(self, /, *args, **kwargs)

Bases: tuple

Create new instance of ImageGrid(shape, affine)

affine

Alias for field number 1

shape

Alias for field number 0

nifreeze.data.base.to_nifti(dataset: BaseDataset, filename: Path | str | None = None, write_hmxfms: bool = False, order: int = 3) → Nifti1Image[source]

Write a NIfTI file to disk.

Volumes are resampled to the reference affine if motion affines have been set, otherwise the original data are written.

Parameters:
  • dataset (BaseDataset) – The dataset to serialize.

  • filename (os.pathlike, optional) – The output NIfTI file path.

  • write_hmxfms (bool, optional) – If True, the head motion affines will be written out to filesystem with BIDS’ X5 format.

  • order (int, optional) – The interpolation order to use when resampling the data. Defaults to 3 (cubic interpolation).

Returns:

NIfTI image written to disk.

Return type:

Nifti1Image

nifreeze.data.base.validate_affine(inst: BaseDataset, attr: Attribute, value: Any) → None[source]

Strict validator for affine matrices.

Enforces that value is present and is a 4x4 NumPy array.

This function is intended for use as an attrs-style validator.

Parameters:
  • inst (BaseDataset) – The instance being validated (unused, present for validator signature).

  • attr (Attribute) – The attribute being validated (unused, present for validator signature).

  • value (Any) – The value to validate.

:raises exc:TypeError: If the input cannot be converted to a float ndarray. :raises exc:ValueError: If the value is None, or not shaped (4, 4).

nifreeze.data.base.validate_dataobj(inst: BaseDataset, attr: Attribute, value: Any) → None[source]

Strict validator for data objects.

Enforces that value is present and is a NumPy array with exactly 4 dimensions (ndim == 4).

This function is intended for use as an attrs-style validator.

Parameters:
  • inst (BaseDataset) – The instance being validated (unused, present for validator signature).

  • attr (Attribute) – The attribute being validated (unused, present for validator signature).

  • value (Any) – The value to validate.

:raises exc:TypeError: If the input cannot be converted to a float ndarray. :raises exc:ValueError: If the value is None, or not 4-dimensional.