@@ -1473,16 +1473,28 @@ def to_file_map(self, file_map=None):
1473
1473
def update_headers (self ):
1474
1474
""" Harmonize NIfTI headers with image data
1475
1475
1476
+ Ensures that the NIfTI-2 header records the data shape in the last three
1477
+ ``dim`` fields. Per the spec:
1478
+
1479
+ Because the first four dimensions in NIfTI are reserved for space and time, the CIFTI
1480
+ dimensions are stored in the NIfTI header in dim[5] and up, where dim[5] is the length
1481
+ of the first CIFTI dimension (number of values in a row), dim[6] is the length of the
1482
+ second CIFTI dimension, and dim[7] is the length of the third CIFTI dimension, if
1483
+ applicable. The fields dim[1] through dim[4] will be 1; dim[0] will be 6 or 7,
1484
+ depending on whether a third matrix dimension exists.
1485
+
1476
1486
>>> import numpy as np
1477
1487
>>> data = np.zeros((2,3,4))
1478
1488
>>> img = Cifti2Image(data)
1479
1489
>>> img.shape == (2, 3, 4)
1480
1490
True
1481
1491
>>> img.update_headers()
1482
- >>> img.nifti_header.get_data_shape() == (2, 3, 4)
1492
+ >>> img.nifti_header.get_data_shape() == (1, 1, 1, 1, 2, 3, 4)
1493
+ True
1494
+ >>> img.shape == (2, 3, 4)
1483
1495
True
1484
1496
"""
1485
- self ._nifti_header .set_data_shape (self ._dataobj .shape )
1497
+ self ._nifti_header .set_data_shape (( 1 , 1 , 1 , 1 ) + self ._dataobj .shape )
1486
1498
1487
1499
def get_data_dtype (self ):
1488
1500
return self ._nifti_header .get_data_dtype ()
0 commit comments