Skip to content

ENH: Update NIfTI header dimensions for CIFTI-2 compliance #929

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions nibabel/cifti2/cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import re
from collections.abc import MutableSequence, MutableMapping, Iterable
from collections import OrderedDict
import numpy as np
from .. import xmlutils as xml
from ..filebasedimages import FileBasedHeader
from ..dataobj_images import DataobjImage
Expand Down Expand Up @@ -1484,8 +1485,14 @@ def update_headers(self):
>>> img.update_headers()
>>> img.nifti_header.get_data_shape() == (2, 3, 4)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> img.nifti_header.get_data_shape() == (2, 3, 4)
>>> img.nifti_header.get_data_shape() == (1, 1, 1, 1, 2, 3, 4)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this breaks the current API, is that fine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what sense? Does it change img.shape? If so, we should divorce Cifti2Image.shape from Cifti2Image.nifti_header.get_data_shape().

True
>>> np.array_equal(img.nifti_header['dim'], [7, 1, 1, 1, 1, 2, 3, 4])
True
"""
self._nifti_header.set_data_shape(self._dataobj.shape)
_dims = np.ones((8), dtype=int)
_dims[0] = 7 if len(self._dataobj.shape) == 3 else 6
_dims[5:8] = (self._dataobj.shape + (1,))[:3]
self._nifti_header['dim'] = _dims
Comment on lines 1491 to +1495
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating the input to set_data_shape() ought to do the trick.

Suggested change
self._nifti_header.set_data_shape(self._dataobj.shape)
_dims = np.ones((8), dtype=int)
_dims[0] = 7 if len(self._dataobj.shape) == 3 else 6
_dims[5:8] = (self._dataobj.shape + (1,))[:3]
self._nifti_header['dim'] = _dims
self._nifti_header.set_data_shape((1, 1, 1, 1) + self._dataobj.shape)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't equivalent though? dim[0] should be either 6 or 7

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dim0 indicates the length of dim[1:8]. This wlll be determined by this tuple.


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