class_docstr: Class to parse an XML string into a CIFTI-2 header object
class_docstr_tok: ["Class","to","parse","an","XML","string","into","a","CIFTI-2","header","object"]
code: def StartElementHandler(self, name, attrs):
      self.flush_chardata()
      if self.verbose > 0:
          print("Start element:\n\t", repr(name), attrs)
  
      if name == "CIFTI":
          # create cifti2 image
          self.header = Cifti2Header()
          self.header.version = ver = attrs["Version"]
          if parse(ver) < Version("2"):
              raise ValueError(
                  f"Only CIFTI-2 files are supported; found version {ver}"
              )
          self.fsm_state.append("CIFTI")
          self.struct_state.append(self.header)
  
      elif name == "Matrix":
          self.fsm_state.append("Matrix")
          matrix = Cifti2Matrix()
          parent = self.struct_state[-1]
          if not isinstance(parent, Cifti2Header):
              raise Cifti2HeaderError(
                  "Matrix element can only be a child of the CIFTI-2 Header element"
              )
          parent.matrix = matrix
          self.struct_state.append(matrix)
  
      elif name == "MetaData":
          self.fsm_state.append("MetaData")
          meta = Cifti2MetaData()
          parent = self.struct_state[-1]
          if not isinstance(parent, (Cifti2Matrix, Cifti2NamedMap)):
              raise Cifti2HeaderError(
                  "MetaData element can only be a child of the CIFTI-2 Matrix "
                  "or NamedMap elements"
              )
  
          self.struct_state.append(meta)
  
      elif name == "MD":
          pair = ["", ""]
          self.fsm_state.append("MD")
          self.struct_state.append(pair)
  
      elif name == "Name":
          self.write_to = "Name"
  
      elif name == "Value":
          self.write_to = "Value"
  
      elif name == "MatrixIndicesMap":
          self.fsm_state.append("MatrixIndicesMap")
          dimensions = [
              int(value)
              for value in attrs["AppliesToMatrixDimension"].split(",")
          ]
          mim = Cifti2MatrixIndicesMap(
              applies_to_matrix_dimension=dimensions,
              indices_map_to_data_type=attrs["IndicesMapToDataType"],
          )
          for key, dtype in (
              ("NumberOfSeriesPoints", int),
              ("SeriesExponent", int),
              ("SeriesStart", float),
              ("SeriesStep", float),
              ("SeriesUnit", str),
          ):
              if key in attrs:
                  setattr(mim, _underscore(key), dtype(attrs[key]))
          matrix = self.struct_state[-1]
          if not isinstance(matrix, Cifti2Matrix):
              raise Cifti2HeaderError(
                  "MatrixIndicesMap element can only be a child of the CIFTI-2 Matrix element"
              )
          matrix.append(mim)
          self.struct_state.append(mim)
  
      elif name == "NamedMap":
          self.fsm_state.append("NamedMap")
          named_map = Cifti2NamedMap()
          mim = self.struct_state[-1]
          if not isinstance(mim, Cifti2MatrixIndicesMap):
              raise Cifti2HeaderError(
                  "NamedMap element can only be a child of the CIFTI-2 MatrixIndicesMap element"
              )
          self.struct_state.append(named_map)
          mim.append(named_map)
  
      elif name == "LabelTable":
          named_map = self.struct_state[-1]
          mim = self.struct_state[-2]
          if mim.indices_map_to_data_type != "CIFTI_INDEX_TYPE_LABELS":
              raise Cifti2HeaderError(
                  "LabelTable element can only be a child of a MatrixIndicesMap "
                  "with CIFTI_INDEX_TYPE_LABELS type"
              )
          lata = Cifti2LabelTable()
          if not isinstance(named_map, Cifti2NamedMap):
              raise Cifti2HeaderError(
                  "LabelTable element can only be a child of the CIFTI-2 NamedMap element"
              )
          self.fsm_state.append("LabelTable")
          self.struct_state.append(lata)
          named_map.label_table = lata
  
      elif name == "Label":
          lata = self.struct_state[-1]
          if not isinstance(lata, Cifti2LabelTable):
              raise Cifti2HeaderError(
                  "Label element can only be a child of the CIFTI-2 LabelTable element"
              )
          label = Cifti2Label()
          label.key = int(attrs["Key"])
          label.red = float(attrs["Red"])
          label.green = float(attrs["Green"])
          label.blue = float(attrs["Blue"])
          label.alpha = float(attrs["Alpha"])
          self.write_to = "Label"
          self.fsm_state.append("Label")
          self.struct_state.append(label)
  
      elif name == "MapName":
          named_map = self.struct_state[-1]
          if not isinstance(named_map, Cifti2NamedMap):
              raise Cifti2HeaderError(
                  "MapName element can only be a child of the CIFTI-2 NamedMap element"
              )
  
          self.fsm_state.append("MapName")
          self.write_to = "MapName"
  
      elif name == "Surface":
          surface = Cifti2Surface()
          mim = self.struct_state[-1]
          if not isinstance(mim, Cifti2MatrixIndicesMap):
              raise Cifti2HeaderError(
                  "Surface element can only be a child of the CIFTI-2 MatrixIndicesMap element"
              )
          if mim.indices_map_to_data_type != "CIFTI_INDEX_TYPE_PARCELS":
              raise Cifti2HeaderError(
                  "Surface element can only be a child of a MatrixIndicesMap "
                  "with CIFTI_INDEX_TYPE_PARCELS type"
              )
          surface.brain_structure = attrs["BrainStructure"]
          surface.surface_number_of_vertices = int(
              attrs["SurfaceNumberOfVertices"]
          )
          mim.append(surface)
  
      elif name == "Parcel":
          parcel = Cifti2Parcel()
          mim = self.struct_state[-1]
          if not isinstance(mim, Cifti2MatrixIndicesMap):
              raise Cifti2HeaderError(
                  "Parcel element can only be a child of the CIFTI-2 MatrixIndicesMap element"
              )
          parcel.name = attrs["Name"]
          mim.append(parcel)
          self.fsm_state.append("Parcel")
          self.struct_state.append(parcel)
  
      elif name == "Vertices":
          vertices = Cifti2Vertices()
          parcel = self.struct_state[-1]
          if not isinstance(parcel, Cifti2Parcel):
              raise Cifti2HeaderError(
                  "Vertices element can only be a child of the CIFTI-2 Parcel element"
              )
          vertices.brain_structure = attrs["BrainStructure"]
          if vertices.brain_structure not in CIFTI_BRAIN_STRUCTURES:
              raise Cifti2HeaderError(
                  "BrainStructure for this Vertices element is not valid"
              )
          parcel.append_cifti_vertices(vertices)
          self.fsm_state.append("Vertices")
          self.struct_state.append(vertices)
          self.write_to = "Vertices"
  
      elif name == "VoxelIndicesIJK":
          parent = self.struct_state[-1]
          if not isinstance(parent, (Cifti2Parcel, Cifti2BrainModel)):
              raise Cifti2HeaderError(
                  "VoxelIndicesIJK element can only be a child of the CIFTI-2 "
                  "Parcel or BrainModel elements"
              )
          parent.voxel_indices_ijk = Cifti2VoxelIndicesIJK()
          self.write_to = "VoxelIndices"
  
      elif name == "Volume":
          mim = self.struct_state[-1]
          if not isinstance(mim, Cifti2MatrixIndicesMap):
              raise Cifti2HeaderError(
                  "Volume element can only be a child of the CIFTI-2 MatrixIndicesMap element"
              )
          dimensions = tuple(
              int(val) for val in attrs["VolumeDimensions"].split(",")
          )
          volume = Cifti2Volume(volume_dimensions=dimensions)
          mim.append(volume)
          self.fsm_state.append("Volume")
          self.struct_state.append(volume)
  
      elif name == "TransformationMatrixVoxelIndicesIJKtoXYZ":
          volume = self.struct_state[-1]
          if not isinstance(volume, Cifti2Volume):
              raise Cifti2HeaderError(
                  "TransformationMatrixVoxelIndicesIJKtoXYZ element can only be a child "
                  "of the CIFTI-2 Volume element"
              )
          transform = Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ()
          transform.meter_exponent = int(attrs["MeterExponent"])
          volume.transformation_matrix_voxel_indices_ijk_to_xyz = (
              transform
          )
          self.fsm_state.append("TransformMatrix")
          self.struct_state.append(transform)
          self.write_to = "TransformMatrix"
  
      elif name == "BrainModel":
          model = Cifti2BrainModel()
          mim = self.struct_state[-1]
          if not isinstance(mim, Cifti2MatrixIndicesMap):
              raise Cifti2HeaderError(
                  "BrainModel element can only be a child "
                  "of the CIFTI-2 MatrixIndicesMap element"
              )
          if (
              mim.indices_map_to_data_type
              != "CIFTI_INDEX_TYPE_BRAIN_MODELS"
          ):
              raise Cifti2HeaderError(
                  "BrainModel element can only be a child of a MatrixIndicesMap "
                  "with CIFTI_INDEX_TYPE_BRAIN_MODELS type"
              )
          for key, dtype in (
              ("IndexOffset", int),
              ("IndexCount", int),
              ("ModelType", str),
              ("BrainStructure", str),
              ("SurfaceNumberOfVertices", int),
          ):
              if key in attrs:
                  setattr(model, _underscore(key), dtype(attrs[key]))
          if model.brain_structure not in CIFTI_BRAIN_STRUCTURES:
              raise Cifti2HeaderError(
                  "BrainStructure for this BrainModel element is not valid"
              )
          if model.model_type not in CIFTI_MODEL_TYPES:
              raise Cifti2HeaderError(
                  "ModelType for this BrainModel element is not valid"
              )
          mim.append(model)
          self.fsm_state.append("BrainModel")
          self.struct_state.append(model)
  
      elif name == "VertexIndices":
          index = Cifti2VertexIndices()
          model = self.struct_state[-1]
          if not isinstance(model, Cifti2BrainModel):
              raise Cifti2HeaderError(
                  "VertexIndices element can only be a child "
                  "of the CIFTI-2 BrainModel element"
              )
          self.fsm_state.append("VertexIndices")
          model.vertex_indices = index
          self.struct_state.append(index)
          self.write_to = "VertexIndices"
  
imports: from io import BytesIO
  import numpy
  from packaging.version import Version, parse
  from ..None import xmlutils
  from ..batteryrunners import Report
  from ..nifti1 import Nifti1Extension, extension_codes, intent_codes
  from ..nifti2 import Nifti2Header, Nifti2Image
  from ..spatialimages import HeaderDataError
  from .cifti2 import CIFTI_BRAIN_STRUCTURES, CIFTI_MODEL_TYPES, Cifti2BrainModel, Cifti2Header, Cifti2HeaderError, Cifti2Label, Cifti2LabelTable, Cifti2Matrix, Cifti2MatrixIndicesMap, Cifti2MetaData, Cifti2NamedMap, Cifti2Parcel, Cifti2Surface, Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ, Cifti2VertexIndices, Cifti2Vertices, Cifti2Volume, Cifti2VoxelIndicesIJK, _underscore
label_desc: Use image node_id 2 for calling the Cifti2Parser obj's underlying member method code with example usage: obj.StartElementHandler(name, attrs) without return types