Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

utils.nifti

utils.nifti

Functions

NameDescription
apply_half_voxel_shiftShift an affine by half a voxel in x/y (and optionally z). Deprecated, always raises.
get_display_affineReturn the affine mapping display voxel indices (after orient_nifti) to world mm.
get_display_zoomsReturn the (row, col) physical voxel edge lengths of the displayed grid, in mm.
orient_niftiReorient and rotate a NIfTI image’s data array for display.
resample_and_orient_niftiResample source_nii onto target_nii’s grid, then orient both for display.
resample_niftiResample source_nii into target_nii’s voxel grid via nibabel.processing.

apply_half_voxel_shift

utils.nifti.apply_half_voxel_shift(affine, shift_z_axis=False)

Shift an affine by half a voxel in x/y (and optionally z). Deprecated, always raises.

Parameters

NameTypeDescriptionDefault
affinendarrayThe affine to shift.required
shift_z_axisboolWhether to also shift the z axis. Defaults to False.False

Returns

NameTypeDescription
ndarrayThe shifted affine. Unreachable, since this function always raises.

Raises

NameTypeDescription
DeprecationWarningAlways. Half voxel shifts are usually a symptom of a bad affine definition; use dcm2nii to convert DICOMs and use its affine instead.

get_display_affine

utils.nifti.get_display_affine(nii, orientation=None, display_plane=None)

Return the affine mapping display voxel indices (after orient_nifti) to world mm.

orient_nifti applies apply_orientation then flipud(rot90(k=1)) which equals a transpose of axes 0 and 1. This function encodes both steps as an affine. Use inv(get_display_affine(t1)) @ mrsi_affine to get mrsi→display mapping.

Parameters

NameTypeDescriptionDefault
niinibabel.spatialimages.SpatialImage or GESeries.NiiBaseThe image to compute the display affine for.required
orientationtuple[str, str, str]Target axis codes. Defaults to rcParams["nifti.orientation"] if neither this nor display_plane is given.None
display_plane('axial', 'coronal', 'sagittal')If given, overrides orientation with a preset for the named plane, matching orient_nifti."axial"

get_display_zooms

utils.nifti.get_display_zooms(nii, orientation=None, display_plane=None)

Return the (row, col) physical voxel edge lengths of the displayed grid, in mm.

The column norms of get_display_affine’s first two columns give the physical spacing along the two in-plane axes orient_nifti puts at positions 0 and 1 -- correct even for oblique/rotated affines, unlike reading nii.header.get_zooms() and guessing which entries survived the reorientation/transpose. Meant to be passed straight through as display_images(..., zooms=...).

orient_nifti

utils.nifti.orient_nifti(
    nii,
    orientation=None,
    display_plane=None,
    caching=True,
)

Reorient and rotate a NIfTI image’s data array for display.

Applies the requested axis orientation (or one derived from display_plane), then transposes axes 0 and 1 to match the display convention used elsewhere in this module.

Parameters

NameTypeDescriptionDefault
niispatialimages.SpatialImage or GESeries.NiiBaseThe image to orient.required
orientationtuple[str, str, str]Target axis codes. Defaults to rcParams["nifti.orientation"] if neither this nor display_plane is given.None
display_plane('axial', 'coronal', 'sagittal')If given, overrides orientation with a preset for the named plane."axial"
cachingboolWhether to cache the underlying image data (more memory, faster repeat access) or not (less memory, slower repeat access). Defaults to True.True

Returns

NameTypeDescription
ndarrayThe reoriented image data array.

resample_and_orient_nifti

utils.nifti.resample_and_orient_nifti(
    source_nii,
    target_nii,
    orientation=None,
    display_plane=None,
    **kwargs,
)

Resample source_nii onto target_nii’s grid, then orient both for display.

Parameters

NameTypeDescriptionDefault
source_niispatialimages.SpatialImage or GESeries.NiiBaseThe image to resample.required
target_niispatialimages.SpatialImage or GESeries.NiiBaseThe image whose grid to resample onto.required
orientationtuple[str, str, str]Target axis codes passed to orient_nifti. Defaults to rcParams["nifti.orientation"].None
display_plane('axial', 'coronal', 'sagittal')If given, overrides orientation with a preset for the named plane."axial"
**kwargsAnyAdditional keyword arguments passed to resample_nifti.{}

Returns

NameTypeDescription
tuple[ndarray, ndarray]The oriented target image data and the oriented resampled data.

resample_nifti

utils.nifti.resample_nifti(source_nii, target_nii, **kwargs)

Resample source_nii into target_nii’s voxel grid via nibabel.processing.

Parameters

NameTypeDescriptionDefault
source_niispatialimages.SpatialImage or GESeries.NiiBaseThe image to resample.required
target_niispatialimages.SpatialImage or GESeries.NiiBaseThe image whose grid to resample onto.required
**kwargsAnyOverrides for the nifti.resample.* rcParams, passed to nibabel.processing.resample_from_to.{}

Returns

NameTypeDescription
spatialimages.SpatialImageThe resampled image.