Skip to content

Nibabel creates invalid GIfTI files #1198

@neurolabusc

Description

@neurolabusc

The GIfTI format specification is available from here. Section 2.3.4.2 DataType notes This required attribute describes the numeric type of the data contained in a Data Array and are limited to the types displayed in the table.

Value Description
NIFTI_TYPE_UINT8 Unsigned, 8-bit bytes.
NIFTI_TYPE_INT32 Signed, 32-bit integers.
NIFTI_TYPE_FLOAT32 32-bit single precision floating point.

However, the following code creates an invalid GIfTI file which popular tools can't read, using DataType="NIFTI_TYPE_FLOAT64"

# %% make_surface.py
import pathlib

import nibabel as nib

from nilearn import datasets, surface

fsaverage = datasets.fetch_surf_fsaverage("fsaverage7")

motor_images = datasets.fetch_neurovault_motor_task()
stat_img = motor_images.images[0]
surface_map = surface.vol_to_surf(stat_img, fsaverage.pial_left)

surface_map_path = "./surface_map.gii"

img = nib.gifti.gifti.GiftiImage()
img.add_gifti_data_array(
    nib.gifti.gifti.GiftiDataArray(
        surface_map,
        intent="NIFTI_INTENT_ZSCORE",
    )
)
nib.save(img, surface_map_path)

For example, Connectome Workbench is unable to view this image:

connectome

This is similar to previous issues where nibabel used excessive integer precision for GIfTI and NIfTI images. However, it is easier to establish the optimal data type for discrete integers rather than floating point data. For science, it is common to use float64 for internal calculations (15-17 significant decimal places) to avoid rounding errors, but for storage float32 is typically more than sufficient (about 7 decimal places). If there is a strong rationale for increasing the precision for GIfTI, the specification should be updated and implementation support provided to the smaller teams that create popular tools that support this format.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions