66# copyright and license terms.
77#
88### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9- ''' Read / write access to CIfTI2 image format
9+ ''' Read / write access to CIFTI-2 image format
1010
1111Format of the NIFTI2 container format described here:
1212
1313 http://www.nitrc.org/forum/message.php?msg_id=3738
1414
15- Definition of the CIFTI2 header format and file extensions attached to this
16- email:
15+ Definition of the CIFTI-2 header format and file extensions can be found at:
1716
18- http://www.nitrc.org/forum/forum.php?thread_id=4380&forum_id=1955
19-
20- Filename is ``CIFTI-2_Main_FINAL_1March2014.pdf``.
17+ http://www.nitrc.org/projects/cifti
2118'''
2219from __future__ import division , print_function , absolute_import
2320import re
@@ -42,7 +39,7 @@ def _float_01(val):
4239
4340
4441class Cifti2HeaderError (Exception ):
45- """ Error in CIFTI2 header
42+ """ Error in CIFTI-2 header
4643 """
4744
4845
@@ -178,7 +175,7 @@ def _to_xml_element(self):
178175
179176
180177class Cifti2LabelTable (xml .XmlSerializable , MutableMapping ):
181- """ CIFTI2 label table: a sequence of ``Cifti2Label``s
178+ """ CIFTI-2 label table: a sequence of ``Cifti2Label``s
182179
183180 * Description - Used by NamedMap when IndicesMapToDataType is
184181 "CIFTI_INDEX_TYPE_LABELS" in order to associate names and display colors
@@ -236,7 +233,7 @@ def _to_xml_element(self):
236233
237234
238235class Cifti2Label (xml .XmlSerializable ):
239- """ CIFTI2 label: association of integer key with a name and RGBA values
236+ """ CIFTI-2 label: association of integer key with a name and RGBA values
240237
241238 For all color components, value is floating point with range 0.0 to 1.0.
242239
@@ -314,7 +311,7 @@ def _to_xml_element(self):
314311
315312
316313class Cifti2NamedMap (xml .XmlSerializable ):
317- """CIFTI2 named map: association of name and optional data with a map index
314+ """CIFTI-2 named map: association of name and optional data with a map index
318315
319316 Associates a name, optional metadata, and possibly a LabelTable with an
320317 index in a map.
@@ -432,7 +429,7 @@ def _to_xml_element(self):
432429
433430
434431class Cifti2VoxelIndicesIJK (xml .XmlSerializable , MutableSequence ):
435- """CIFTI2 VoxelIndicesIJK: Set of voxel indices contained in a structure
432+ """CIFTI-2 VoxelIndicesIJK: Set of voxel indices contained in a structure
436433
437434 * Description - Identifies the voxels that model a brain structure, or
438435 participate in a parcel. Note that when this is a child of BrainModel,
@@ -514,7 +511,7 @@ def _to_xml_element(self):
514511
515512
516513class Cifti2Vertices (xml .XmlSerializable , MutableSequence ):
517- """CIFTI2 vertices - association of brain structure and a list of vertices
514+ """CIFTI-2 vertices - association of brain structure and a list of vertices
518515
519516 * Description - Contains a BrainStructure type and a list of vertex indices
520517 within a Parcel.
@@ -580,7 +577,7 @@ def _to_xml_element(self):
580577
581578
582579class Cifti2Parcel (xml .XmlSerializable ):
583- """CIFTI2 parcel: association of a name with vertices and/or voxels
580+ """CIFTI-2 parcel: association of a name with vertices and/or voxels
584581
585582 * Description - Associates a name, plus vertices and/or voxels, with an
586583 index.
@@ -695,7 +692,7 @@ def _to_xml_element(self):
695692
696693
697694class Cifti2Volume (xml .XmlSerializable ):
698- """CIFTI2 volume: information about a volume for mappings that use voxels
695+ """CIFTI-2 volume: information about a volume for mappings that use voxels
699696
700697 * Description - Provides information about the volume for any mappings that
701698 use voxels.
@@ -738,7 +735,7 @@ def _to_xml_element(self):
738735
739736
740737class Cifti2VertexIndices (xml .XmlSerializable , MutableSequence ):
741- """CIFTI2 vertex indices: vertex indices for an associated brain model
738+ """CIFTI-2 vertex indices: vertex indices for an associated brain model
742739
743740 The vertex indices (which are independent for each surface, and
744741 zero-based) that are used in this brain model[.] The parent
@@ -1081,7 +1078,7 @@ def _to_xml_element(self):
10811078
10821079
10831080class Cifti2Matrix (xml .XmlSerializable , MutableSequence ):
1084- """ CIFTI2 Matrix object
1081+ """ CIFTI-2 Matrix object
10851082
10861083 This is a list-like container where the elements are instances of
10871084 :class:`Cifti2MatrixIndicesMap`.
@@ -1213,7 +1210,7 @@ def _to_xml_element(self):
12131210
12141211
12151212class Cifti2Header (FileBasedHeader , xml .XmlSerializable ):
1216- ''' Class for CIFTI2 header extension '''
1213+ ''' Class for CIFTI-2 header extension '''
12171214
12181215 def __init__ (self , matrix = None , version = "2.0" ):
12191216 FileBasedHeader .__init__ (self )
@@ -1268,9 +1265,43 @@ def get_index_map(self, index):
12681265 '''
12691266 return self .matrix .get_index_map (index )
12701267
1268+ def get_axis (self , index ):
1269+ '''
1270+ Generates the Cifti2 axis for a given dimension
1271+
1272+ Parameters
1273+ ----------
1274+ index : int
1275+ Dimension for which we want to obtain the mapping.
1276+
1277+ Returns
1278+ -------
1279+ axis : :class:`.cifti2_axes.Axis`
1280+ '''
1281+ from . import cifti2_axes
1282+ return cifti2_axes .from_index_mapping (self .matrix .get_index_map (index ))
1283+
1284+ @classmethod
1285+ def from_axes (cls , axes ):
1286+ '''
1287+ Creates a new Cifti2 header based on the Cifti2 axes
1288+
1289+ Parameters
1290+ ----------
1291+ axes : tuple of :class`.cifti2_axes.Axis`
1292+ sequence of Cifti2 axes describing each row/column of the matrix to be stored
1293+
1294+ Returns
1295+ -------
1296+ header : Cifti2Header
1297+ new header describing the rows/columns in a format consistent with Cifti2
1298+ '''
1299+ from . import cifti2_axes
1300+ return cifti2_axes .to_header (axes )
1301+
12711302
12721303class Cifti2Image (DataobjImage ):
1273- """ Class for single file CIFTI2 format image
1304+ """ Class for single file CIFTI-2 format image
12741305 """
12751306 header_class = Cifti2Header
12761307 valid_exts = Nifti2Image .valid_exts
@@ -1297,15 +1328,19 @@ def __init__(self,
12971328 Object containing image data. It should be some object that
12981329 returns an array from ``np.asanyarray``. It should have a
12991330 ``shape`` attribute or property.
1300- header : Cifti2Header instance
1301- Header with data for / from XML part of CIFTI2 format.
1331+ header : Cifti2Header instance or sequence of :class:`cifti2_axes.Axis`
1332+ Header with data for / from XML part of CIFTI-2 format.
1333+ Alternatively a sequence of cifti2_axes.Axis objects can be provided
1334+ describing each dimension of the array.
13021335 nifti_header : None or mapping or NIfTI2 header instance, optional
13031336 Metadata for NIfTI2 component of this format.
13041337 extra : None or mapping
13051338 Extra metadata not captured by `header` or `nifti_header`.
13061339 file_map : mapping, optional
13071340 Mapping giving file information for this image format.
13081341 '''
1342+ if not isinstance (header , Cifti2Header ) and header :
1343+ header = Cifti2Header .from_axes (header )
13091344 super (Cifti2Image , self ).__init__ (dataobj , header = header ,
13101345 extra = extra , file_map = file_map )
13111346 self ._nifti_header = Nifti2Header .from_header (nifti_header )
@@ -1321,7 +1356,7 @@ def nifti_header(self):
13211356
13221357 @classmethod
13231358 def from_file_map (klass , file_map ):
1324- """ Load a CIFTI2 image from a file_map
1359+ """ Load a CIFTI-2 image from a file_map
13251360
13261361 Parameters
13271362 ----------
@@ -1341,7 +1376,7 @@ def from_file_map(klass, file_map):
13411376 cifti_header = item .get_content ()
13421377 break
13431378 else :
1344- raise ValueError ('NIfTI2 header does not contain a CIFTI2 '
1379+ raise ValueError ('NIfTI2 header does not contain a CIFTI-2 '
13451380 'extension' )
13461381
13471382 # Construct cifti image.
@@ -1400,7 +1435,7 @@ def to_file_map(self, file_map=None):
14001435 img .to_file_map (file_map or self .file_map )
14011436
14021437 def update_headers (self ):
1403- ''' Harmonize CIFTI2 and NIfTI headers with image data
1438+ ''' Harmonize CIFTI-2 and NIfTI headers with image data
14041439
14051440 >>> import numpy as np
14061441 >>> data = np.zeros((2,3,4))
0 commit comments