Skip to content

Nifti header

linc_convert.utils.nifti_header

Utilities for loading, creating, and manipulating NIfTI headers.

load_nifti_header_from_file

load_nifti_header_from_file(path)

Load a NIfTI header from a file path.

Supports: - Standard NIfTI files: .nii, .nii.gz - NIfTI-Zarr array containing a NIfTI header: .nii.zarr

Parameters:

Name Type Description Default
path str | Path

Path to a NIfTI file or Zarr directory.

required

Returns:

Type Description
NiftiHeader

A nibabel NIfTI header object.

Raises:

Type Description
FileNotFoundError

If the path does not exist.

ValueError

If the path exists but is not a recognized NIfTI file or Zarr directory.

Exception

If Zarr opening fails for all supported versions.

load_nifti_header_from_zarray

load_nifti_header_from_zarray(path)

Load a NIfTI header from a Zarr array storing the raw NIfTI header bytes.

This function attempts Zarr format v2 first, then v3 (or vice versa if you change the order in _try_open_zarr_array). It reads the header bytes once, infers the correct nibabel header class via get_nibabel_klass, and constructs the header with from_fileobj(check=False) (since the buffer contains exactly the header bytes, not a full image).

Parameters:

Name Type Description Default
path str | Path

Path to the Zarr array (directory) containing the NIfTI header bytes.

required

Returns:

Type Description
NiftiHeader

A nibabel NIfTI header object parsed from the stored bytes.

Raises:

Type Description
Exception

If the Zarr array cannot be opened using supported versions.

ValueError

If the Zarr array does not yield any bytes.

build_nifti_header

build_nifti_header(*, zgroup, voxel_size_zyx, unit, nii_config)

Build a NIfTI header for the data in zgroup.

Behavior: - If nii_config.nifti_header is provided and loadable, use it as a template. - Otherwise, start from default_nifti_header(...) using OME multiscales. - In either case, we always align the header to the actual data: * set shape / dtype to match the stored array * pick affine based on either the loaded header or the requested orientation/centering * set units with to_nifti_unit(unit)

Notes
  • The data array is taken as zgroup["0"], and we follow your existing convention: NIfTI shape is reversed (X, Y, Z) compared to stored (Z, Y, X).