@@ -1114,9 +1114,67 @@ def metadata(self, meta):
1114
1114
"""
1115
1115
self ._meta = _value_if_klass (meta , Cifti2MetaData )
1116
1116
1117
+ def _get_indices_from_mim (self , mim ):
1118
+ applies_to_matrix_dimension = mim .applies_to_matrix_dimension
1119
+ if not isinstance (
1120
+ applies_to_matrix_dimension ,
1121
+ collections .Iterable
1122
+ ):
1123
+ applies_to_matrix_dimension = (int (applies_to_matrix_dimension ),)
1124
+ return applies_to_matrix_dimension
1125
+
1126
+ @property
1127
+ def mapped_indices (self ):
1128
+ '''
1129
+ List of matrix indices that are mapped
1130
+ '''
1131
+ mapped_indices = []
1132
+ for v in self :
1133
+ a2md = self ._get_indices_from_mim (v )
1134
+ mapped_indices += a2md
1135
+ return mapped_indices
1136
+
1137
+ def get_index_map (self , index ):
1138
+ '''
1139
+ Cifti2 Mapping class for a given index
1140
+
1141
+ Parameters
1142
+ ----------
1143
+ index : int
1144
+ Index for which we want to obtain the mapping.
1145
+ Must be in the mapped_indices sequence.
1146
+
1147
+ Returns
1148
+ -------
1149
+ cifti2_map : Cifti2MatrixIndicesMap
1150
+ Returns the Cifti2MatrixIndicesMap corresponding to
1151
+ the given index.
1152
+ '''
1153
+
1154
+ for v in self :
1155
+ a2md = self ._get_indices_from_mim (v )
1156
+ if index in a2md :
1157
+ return v
1158
+ else :
1159
+ raise Cifti2HeaderError ("Index not mapped" )
1160
+
1161
+ def _validate_new_mim (self , value ):
1162
+ if value .applies_to_matrix_dimension is None :
1163
+ raise Cifti2HeaderError (
1164
+ "Cifti2MatrixIndicesMap needs to have "
1165
+ "the applies_to_matrix_dimension attribute set"
1166
+ )
1167
+ a2md = self ._get_indices_from_mim (value )
1168
+ if not set (self .mapped_indices ).isdisjoint (a2md ):
1169
+ raise Cifti2HeaderError (
1170
+ "Indices in this Cifti2MatrixIndicesMap "
1171
+ "already mapped in this matrix"
1172
+ )
1173
+
1117
1174
def __setitem__ (self , key , value ):
1118
1175
if not isinstance (value , Cifti2MatrixIndicesMap ):
1119
1176
raise TypeError ("Not a valid Cifti2MatrixIndicesMap instance" )
1177
+ self ._validate_new_mim (value )
1120
1178
self ._mims [key ] = value
1121
1179
1122
1180
def __getitem__ (self , key ):
@@ -1131,6 +1189,7 @@ def __len__(self):
1131
1189
def insert (self , index , value ):
1132
1190
if not isinstance (value , Cifti2MatrixIndicesMap ):
1133
1191
raise TypeError ("Not a valid Cifti2MatrixIndicesMap instance" )
1192
+ self ._validate_new_mim (value )
1134
1193
self ._mims .insert (index , value )
1135
1194
1136
1195
def _to_xml_element (self ):
@@ -1151,7 +1210,9 @@ class Cifti2Header(FileBasedHeader, xml.XmlSerializable):
1151
1210
def __init__ (self , matrix = None , version = "2.0" ):
1152
1211
FileBasedHeader .__init__ (self )
1153
1212
xml .XmlSerializable .__init__ (self )
1154
- self .matrix = Cifti2Matrix () if matrix is None else Cifti2Matrix ()
1213
+ if matrix is None :
1214
+ matrix = Cifti2Matrix ()
1215
+ self .matrix = matrix
1155
1216
self .version = version
1156
1217
1157
1218
def _to_xml_element (self ):
@@ -1167,6 +1228,38 @@ def may_contain_header(klass, binaryblock):
1167
1228
from .parse_cifti2 import _Cifti2AsNiftiHeader
1168
1229
return _Cifti2AsNiftiHeader .may_contain_header (binaryblock )
1169
1230
1231
+ @property
1232
+ def number_of_mapped_indices (self ):
1233
+ '''
1234
+ Number of mapped indices
1235
+ '''
1236
+ return len (self .matrix )
1237
+
1238
+ @property
1239
+ def mapped_indices (self ):
1240
+ '''
1241
+ List of matrix indices that are mapped
1242
+ '''
1243
+ return self .matrix .mapped_indices
1244
+
1245
+ def get_index_map (self , index ):
1246
+ '''
1247
+ Cifti2 Mapping class for a given index
1248
+
1249
+ Parameters
1250
+ ----------
1251
+ index : int
1252
+ Index for which we want to obtain the mapping.
1253
+ Must be in the mapped_indices sequence.
1254
+
1255
+ Returns
1256
+ -------
1257
+ cifti2_map : Cifti2MatrixIndicesMap
1258
+ Returns the Cifti2MatrixIndicesMap corresponding to
1259
+ the given index.
1260
+ '''
1261
+ return self .matrix .get_index_map (index )
1262
+
1170
1263
1171
1264
class Cifti2Image (DataobjImage ):
1172
1265
""" Class for single file CIFTI2 format image
0 commit comments