utils.nifti
utils.nifti
Functions ¶ Name Description apply_half_voxel_shift Shift an affine by half a voxel in x/y (and optionally z). Deprecated, always raises. get_display_affine Return the affine mapping display voxel indices (after orient_nifti) to world mm. get_display_zooms Return the (row, col) physical voxel edge lengths of the displayed grid, in mm. orient_nifti Reorient and rotate a NIfTI image’s data array for display. resample_and_orient_nifti Resample source_nii onto target_nii’s grid, then orient both for display. resample_nifti Resample 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 ¶ Name Type Description Default affine ndarray The affine to shift. required shift_z_axis bool Whether to also shift the z axis. Defaults to False. False
Returns ¶ Name Type Description ndarray The shifted affine. Unreachable, since this function always raises.
Raises ¶ Name Type Description DeprecationWarning Always. 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 ¶ Name Type Description Default nii nibabel.spatialimages.SpatialImage or GESeries.NiiBase The image to compute the display affine for. required orientation tuple[str, str, str] Target axis codes. Defaults to rcParams["nifti.orientation"] if neither this nor display_plane is given. Nonedisplay_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 ¶ Name Type Description Default nii spatialimages.SpatialImage or GESeries.NiiBase The image to orient. required orientation tuple[str, str, str] Target axis codes. Defaults to rcParams["nifti.orientation"] if neither this nor display_plane is given. Nonedisplay_plane ('axial', 'coronal', 'sagittal') If given, overrides orientation with a preset for the named plane. "axial"caching bool Whether to cache the underlying image data (more memory, faster repeat access) or not (less memory, slower repeat access). Defaults to True. True
Returns ¶ Name Type Description ndarray The 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 ¶ Name Type Description Default source_nii spatialimages.SpatialImage or GESeries.NiiBase The image to resample. required target_nii spatialimages.SpatialImage or GESeries.NiiBase The image whose grid to resample onto. required orientation tuple[str, str, str] Target axis codes passed to orient_nifti. Defaults to rcParams["nifti.orientation"]. Nonedisplay_plane ('axial', 'coronal', 'sagittal') If given, overrides orientation with a preset for the named plane. "axial"**kwargs Any Additional keyword arguments passed to resample_nifti. {}
Returns ¶ Name Type Description 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 ¶ Name Type Description Default source_nii spatialimages.SpatialImage or GESeries.NiiBase The image to resample. required target_nii spatialimages.SpatialImage or GESeries.NiiBase The image whose grid to resample onto. required **kwargs Any Overrides for the nifti.resample.* rcParams, passed to nibabel.processing.resample_from_to. {}
Returns ¶ Name Type Description spatialimages.SpatialImage The resampled image.