@@ -1114,9 +1114,67 @@ def metadata(self, meta):
11141114 """
11151115 self ._meta = _value_if_klass (meta , Cifti2MetaData )
11161116
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+
11171174 def __setitem__ (self , key , value ):
11181175 if not isinstance (value , Cifti2MatrixIndicesMap ):
11191176 raise TypeError ("Not a valid Cifti2MatrixIndicesMap instance" )
1177+ self ._validate_new_mim (value )
11201178 self ._mims [key ] = value
11211179
11221180 def __getitem__ (self , key ):
@@ -1131,6 +1189,7 @@ def __len__(self):
11311189 def insert (self , index , value ):
11321190 if not isinstance (value , Cifti2MatrixIndicesMap ):
11331191 raise TypeError ("Not a valid Cifti2MatrixIndicesMap instance" )
1192+ self ._validate_new_mim (value )
11341193 self ._mims .insert (index , value )
11351194
11361195 def _to_xml_element (self ):
@@ -1151,7 +1210,9 @@ class Cifti2Header(FileBasedHeader, xml.XmlSerializable):
11511210 def __init__ (self , matrix = None , version = "2.0" ):
11521211 FileBasedHeader .__init__ (self )
11531212 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
11551216 self .version = version
11561217
11571218 def _to_xml_element (self ):
@@ -1167,6 +1228,38 @@ def may_contain_header(klass, binaryblock):
11671228 from .parse_cifti2 import _Cifti2AsNiftiHeader
11681229 return _Cifti2AsNiftiHeader .may_contain_header (binaryblock )
11691230
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+
11701263
11711264class Cifti2Image (DataobjImage ):
11721265 """ Class for single file CIFTI2 format image
0 commit comments