6
6
# copyright and license terms.
7
7
#
8
8
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9
- ''' Read / write access to CIfTI2 image format
9
+ ''' Read / write access to CIFTI-2 image format
10
10
11
11
Format of the NIFTI2 container format described here:
12
12
13
13
http://www.nitrc.org/forum/message.php?msg_id=3738
14
14
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:
17
16
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
21
18
'''
22
19
from __future__ import division , print_function , absolute_import
23
20
import re
@@ -42,7 +39,7 @@ def _float_01(val):
42
39
43
40
44
41
class Cifti2HeaderError (Exception ):
45
- """ Error in CIFTI2 header
42
+ """ Error in CIFTI-2 header
46
43
"""
47
44
48
45
@@ -178,7 +175,7 @@ def _to_xml_element(self):
178
175
179
176
180
177
class Cifti2LabelTable (xml .XmlSerializable , MutableMapping ):
181
- """ CIFTI2 label table: a sequence of ``Cifti2Label``s
178
+ """ CIFTI-2 label table: a sequence of ``Cifti2Label``s
182
179
183
180
* Description - Used by NamedMap when IndicesMapToDataType is
184
181
"CIFTI_INDEX_TYPE_LABELS" in order to associate names and display colors
@@ -236,7 +233,7 @@ def _to_xml_element(self):
236
233
237
234
238
235
class 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
240
237
241
238
For all color components, value is floating point with range 0.0 to 1.0.
242
239
@@ -314,7 +311,7 @@ def _to_xml_element(self):
314
311
315
312
316
313
class 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
318
315
319
316
Associates a name, optional metadata, and possibly a LabelTable with an
320
317
index in a map.
@@ -432,7 +429,7 @@ def _to_xml_element(self):
432
429
433
430
434
431
class 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
436
433
437
434
* Description - Identifies the voxels that model a brain structure, or
438
435
participate in a parcel. Note that when this is a child of BrainModel,
@@ -514,7 +511,7 @@ def _to_xml_element(self):
514
511
515
512
516
513
class 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
518
515
519
516
* Description - Contains a BrainStructure type and a list of vertex indices
520
517
within a Parcel.
@@ -580,7 +577,7 @@ def _to_xml_element(self):
580
577
581
578
582
579
class 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
584
581
585
582
* Description - Associates a name, plus vertices and/or voxels, with an
586
583
index.
@@ -695,7 +692,7 @@ def _to_xml_element(self):
695
692
696
693
697
694
class 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
699
696
700
697
* Description - Provides information about the volume for any mappings that
701
698
use voxels.
@@ -738,7 +735,7 @@ def _to_xml_element(self):
738
735
739
736
740
737
class 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
742
739
743
740
The vertex indices (which are independent for each surface, and
744
741
zero-based) that are used in this brain model[.] The parent
@@ -1081,7 +1078,7 @@ def _to_xml_element(self):
1081
1078
1082
1079
1083
1080
class Cifti2Matrix (xml .XmlSerializable , MutableSequence ):
1084
- """ CIFTI2 Matrix object
1081
+ """ CIFTI-2 Matrix object
1085
1082
1086
1083
This is a list-like container where the elements are instances of
1087
1084
:class:`Cifti2MatrixIndicesMap`.
@@ -1213,7 +1210,7 @@ def _to_xml_element(self):
1213
1210
1214
1211
1215
1212
class Cifti2Header (FileBasedHeader , xml .XmlSerializable ):
1216
- ''' Class for CIFTI2 header extension '''
1213
+ ''' Class for CIFTI-2 header extension '''
1217
1214
1218
1215
def __init__ (self , matrix = None , version = "2.0" ):
1219
1216
FileBasedHeader .__init__ (self )
@@ -1268,9 +1265,43 @@ def get_index_map(self, index):
1268
1265
'''
1269
1266
return self .matrix .get_index_map (index )
1270
1267
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
+
1271
1302
1272
1303
class Cifti2Image (DataobjImage ):
1273
- """ Class for single file CIFTI2 format image
1304
+ """ Class for single file CIFTI-2 format image
1274
1305
"""
1275
1306
header_class = Cifti2Header
1276
1307
valid_exts = Nifti2Image .valid_exts
@@ -1297,15 +1328,19 @@ def __init__(self,
1297
1328
Object containing image data. It should be some object that
1298
1329
returns an array from ``np.asanyarray``. It should have a
1299
1330
``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.
1302
1335
nifti_header : None or mapping or NIfTI2 header instance, optional
1303
1336
Metadata for NIfTI2 component of this format.
1304
1337
extra : None or mapping
1305
1338
Extra metadata not captured by `header` or `nifti_header`.
1306
1339
file_map : mapping, optional
1307
1340
Mapping giving file information for this image format.
1308
1341
'''
1342
+ if not isinstance (header , Cifti2Header ) and header :
1343
+ header = Cifti2Header .from_axes (header )
1309
1344
super (Cifti2Image , self ).__init__ (dataobj , header = header ,
1310
1345
extra = extra , file_map = file_map )
1311
1346
self ._nifti_header = Nifti2Header .from_header (nifti_header )
@@ -1321,7 +1356,7 @@ def nifti_header(self):
1321
1356
1322
1357
@classmethod
1323
1358
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
1325
1360
1326
1361
Parameters
1327
1362
----------
@@ -1341,7 +1376,7 @@ def from_file_map(klass, file_map):
1341
1376
cifti_header = item .get_content ()
1342
1377
break
1343
1378
else :
1344
- raise ValueError ('NIfTI2 header does not contain a CIFTI2 '
1379
+ raise ValueError ('NIfTI2 header does not contain a CIFTI-2 '
1345
1380
'extension' )
1346
1381
1347
1382
# Construct cifti image.
@@ -1400,7 +1435,7 @@ def to_file_map(self, file_map=None):
1400
1435
img .to_file_map (file_map or self .file_map )
1401
1436
1402
1437
def update_headers (self ):
1403
- ''' Harmonize CIFTI2 and NIfTI headers with image data
1438
+ ''' Harmonize CIFTI-2 and NIfTI headers with image data
1404
1439
1405
1440
>>> import numpy as np
1406
1441
>>> data = np.zeros((2,3,4))
0 commit comments