Skip to content

FIX: Set CIFTI-2 dimensions in the last three entries of dim #930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions nibabel/cifti2/cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,16 +1476,28 @@ def to_file_map(self, file_map=None):
def update_headers(self):
''' Harmonize NIfTI headers with image data

Ensures that the NIfTI-2 header records the data shape in the last three
``dim`` fields. Per the spec:

Because the first four dimensions in NIfTI are reserved for space and time, the CIFTI
dimensions are stored in the NIfTI header in dim[5] and up, where dim[5] is the length
of the first CIFTI dimension (number of values in a row), dim[6] is the length of the
second CIFTI dimension, and dim[7] is the length of the third CIFTI dimension, if
applicable. The fields dim[1] through dim[4] will be 1; dim[0] will be 6 or 7,
depending on whether a third matrix dimension exists.

>>> import numpy as np
>>> data = np.zeros((2,3,4))
>>> img = Cifti2Image(data)
>>> img.shape == (2, 3, 4)
True
>>> img.update_headers()
>>> img.nifti_header.get_data_shape() == (2, 3, 4)
>>> img.nifti_header.get_data_shape() == (1, 1, 1, 1, 2, 3, 4)
True
>>> img.shape == (2, 3, 4)
True
'''
self._nifti_header.set_data_shape(self._dataobj.shape)
self._nifti_header.set_data_shape((1, 1, 1, 1) + self._dataobj.shape)

def get_data_dtype(self):
return self._nifti_header.get_data_dtype()
Expand Down