nifti
nifti
¶
NIfTI file loading and saving for osipy.
This module provides functions for loading and saving NIfTI files, including PerfusionDataset containers and ParameterMap objects.
References
NIfTI-1 Data Format: https://nifti.nimh.nih.gov/
load_nifti
¶
Load NIfTI file as PerfusionDataset.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to NIfTI file (.nii or .nii.gz).
TYPE:
|
modality
|
Perfusion modality. Defaults to :class:
TYPE:
|
acquisition_params
|
Acquisition parameters. When provided, takes precedence over any
values derived from the sidecar JSON. When None and a sidecar is
available, parameters are mapped via
TYPE:
|
sidecar_json
|
Optional BIDS-style sidecar JSON. If None, a file sharing the
NIfTI's stem with a
TYPE:
|
interactive
|
Forwarded to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PerfusionDataset
|
Loaded imaging data with metadata. |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the NIfTI file does not exist. |
IOError
|
If the file is not a valid NIfTI. |
DataValidationError
|
If the data is not 3D or 4D. |
Examples:
>>> from osipy.common.io.nifti import load_nifti
>>> from osipy.common.types import Modality
>>> dataset = load_nifti("dce_data.nii.gz", modality=Modality.DCE)
>>> print(dataset.shape)
(64, 64, 20, 30)
Load with an explicit sidecar:
save_nifti
¶
Save data as a NIfTI file with proper orientation preservation.
This function ensures the affine matrix is correctly applied to preserve spatial orientation when saving parameter maps or datasets.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Data to save. Can be: - A numpy array (requires affine parameter) - A ParameterMap (uses embedded affine) - A PerfusionDataset (uses embedded affine)
TYPE:
|
path
|
Output path. Will add .nii.gz extension if not present.
TYPE:
|
affine
|
4x4 affine transformation matrix. Required if data is a numpy array. Ignored if data is a ParameterMap or PerfusionDataset.
TYPE:
|
dtype
|
Output data type. Defaults to float32 for parameter maps.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Path
|
Path to the saved file. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If data is a numpy array and affine is not provided. |
IOError
|
If saving fails. |
Examples: