Skip to content

Commit 23fdf81

Browse files
ENH: Define Cifti2 Axes describing the rows/columns of the Cifti2 data
1 parent ad6b890 commit 23fdf81

File tree

6 files changed

+1704
-1
lines changed

6 files changed

+1704
-1
lines changed

nibabel/cifti2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ,
2626
Cifti2Vertices, Cifti2Volume, CIFTI_BRAIN_STRUCTURES,
2727
Cifti2HeaderError, CIFTI_MODEL_TYPES, load, save)
28+
from .cifti2_axes import (Axis, BrainModel, Parcels, Series, Label, Scalar)

nibabel/cifti2/cifti2.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,40 @@ def get_index_map(self, index):
12681268
'''
12691269
return self.matrix.get_index_map(index)
12701270

1271+
def get_axis(self, index):
1272+
'''
1273+
Generates the Cifti2 axis for a given dimension
1274+
1275+
Parameters
1276+
----------
1277+
index : int
1278+
Dimension for which we want to obtain the mapping.
1279+
1280+
Returns
1281+
-------
1282+
axis : cifti2_axes.Axis
1283+
'''
1284+
from . import cifti2_axes
1285+
return cifti2_axes.from_mapping(self.matrix.get_index_map(index))
1286+
1287+
@classmethod
1288+
def from_axes(cls, axes):
1289+
'''
1290+
Creates a new Cifti2 header based on the Cifti2 axes
1291+
1292+
Parameters
1293+
----------
1294+
axes : Tuple[cifti2_axes.Axis]
1295+
sequence of Cifti2 axes describing each row/column of the matrix to be stored
1296+
1297+
Returns
1298+
-------
1299+
header : Cifti2Header
1300+
new header describing the rows/columns in a format consistent with Cifti2
1301+
'''
1302+
from . import cifti2_axes
1303+
return cifti2_axes.to_header(axes)
1304+
12711305

12721306
class Cifti2Image(DataobjImage):
12731307
""" Class for single file CIFTI2 format image
@@ -1297,15 +1331,19 @@ def __init__(self,
12971331
Object containing image data. It should be some object that
12981332
returns an array from ``np.asanyarray``. It should have a
12991333
``shape`` attribute or property.
1300-
header : Cifti2Header instance
1334+
header : Cifti2Header instance or Sequence[cifti2_axes.Axis]
13011335
Header with data for / from XML part of CIFTI2 format.
1336+
Alternatively a sequence of cifti2_axes.Axis objects can be provided
1337+
describing each dimension of the array.
13021338
nifti_header : None or mapping or NIfTI2 header instance, optional
13031339
Metadata for NIfTI2 component of this format.
13041340
extra : None or mapping
13051341
Extra metadata not captured by `header` or `nifti_header`.
13061342
file_map : mapping, optional
13071343
Mapping giving file information for this image format.
13081344
'''
1345+
if not isinstance(header, Cifti2Header) and header:
1346+
header = Cifti2Header.from_axes(header)
13091347
super(Cifti2Image, self).__init__(dataobj, header=header,
13101348
extra=extra, file_map=file_map)
13111349
self._nifti_header = Nifti2Header.from_header(nifti_header)

0 commit comments

Comments
 (0)