Skip to content

Checking shape of CIFTI2 images #773

@MichielCottaar

Description

@MichielCottaar

At present there is no checking of whether the shape of the data array is consistent with the CIFTI2 header. In the example below, I expect two numbers (A and B) for each of my three vertices on the left cortex, so a (2, 3)-shaped array. However, nibabel will happily accept the (7,4)-shaped array provided here. Similarly, when reading the CIFTI file no errors or warnings are raised.

from nibabel import cifti2
import numpy as np
data = np.random.rand(7, 4)
bm = cifti2.BrainModelAxis.from_mask([True, True, True], name='left_cortex')
sc = cifti2.ScalarAxis(['A', 'B'])
img = cifti2.Cifti2Image(data, header=(sc, bm))
img.to_filename('test.dscalar.nii')

I'm happy to implement some code to compare the shape of the array with that implied by the header, but I'm not sure what would be the best time to make this check and whether it should be a warning or an error. The options are:

  1. warning or error when creating the Cifti2Image itself
  2. warning or error when writing the Cifti2Image
  3. warning or error when reading the Cifti2Image

Any suggestions on where to plug this test and whether to make it a warning or an error? We can't simply adjust the header shape to match the data as this would involve creating new labels for the rows or columns and hence would lead to very confusing behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions