File: CiftiXMLReader.cxx

package info (click to toggle)
caret 5.6.4~dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 31,904 kB
  • ctags: 28,901
  • sloc: cpp: 378,050; python: 6,718; ansic: 5,507; makefile: 333; sh: 46
file content (455 lines) | stat: -rw-r--r-- 18,876 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <stdio.h>
#include <QtCore>
#include "CiftiXMLElements.h"
#include "iostream"
#include "CiftiXMLReader.h"

void parseCiftiXML(QXmlStreamReader &xml, CiftiRootElement &rootElement)
{
   while (!xml.atEnd()  && !xml.hasError()) {
      xml.readNext();
         QString test = xml.name().toString();
      if(xml.isStartElement())
      {
         QString elementName = xml.name().toString();
         if(elementName == "CIFTI") {
            QXmlStreamAttributes attributes = xml.attributes();
            if(attributes.hasAttribute("Version")) rootElement.m_version = attributes.value("Version").toString();
            else xml.raiseError("Cifti XML Header missing Version String.");
            if(attributes.hasAttribute("NumberOfMatrices")) rootElement.m_numberOfMatrices = attributes.value("NumberOfMatrices").toString().toULong();
            else xml.raiseError("Cifti XML Header missing number of matrices.");            
         }
         else if(elementName == "Matrix") {
            rootElement.m_matrices.push_back(CiftiMatrixElement());
            parseMatrixElement(xml,rootElement.m_matrices.back());
         }
         else std::cout << "unknown element: " << elementName.toAscii().data() << std::endl;             
      }
   }

   if(xml.hasError())
   {
      std::cout << "XML error: " << xml.errorString().toAscii().data() << std::endl;
   }
   else if(xml.atEnd())
   {
      ;//std::cout << "Reached end, done" << std::endl;
   }
}

void parseMatrixElement(QXmlStreamReader &xml, CiftiMatrixElement &matrixElement)
{
   QString test = xml.name().toString();
   
   while (!(xml.isEndElement() && (xml.name().toString() == "Matrix")) && !xml.hasError()) {// && xml.name() == "Matrix") {     
      xml.readNext();
      QString test2 = xml.name().toString();
      if(xml.isStartElement())
      {
         QString elementName = xml.name().toString();
         if(elementName == "MetaData") 
         {
            parseMetaData(xml,matrixElement.m_userMetaData);
         }
         else if(elementName == "LabelTable") {            
            parseLabelTable(xml,matrixElement.m_labelTable);
         }
         else if(elementName == "MatrixIndicesMap")
         {
            matrixElement.m_matrixIndicesMap.push_back(CiftiMatrixIndicesMapElement());
            parseMatrixIndicesMap(xml,matrixElement.m_matrixIndicesMap.back());
         }
         else if(elementName == "Volume")
         {
            matrixElement.m_volume.push_back(CiftiVolume());
            parseVolume(xml,matrixElement.m_volume.back());
         }
         else std::cout << "unknown element: " << elementName.toAscii().data() << std::endl;             
      }      
   }

   QString test2=xml.name().toString();
   //check end element
   if(!xml.hasError()) if(!xml.isEndElement() || (xml.name().toString() != "Matrix"))
      xml.raiseError("Matrix end tag not found.");
}

void parseMetaData(QXmlStreamReader &xml, QHash<QString, QString> &userMetaData)
{
   while (!(xml.isEndElement()  && (xml.name().toString() == "MetaData")) && !xml.hasError()) {// && xml.name() == "MetaData") {      
      xml.readNext();
      if(xml.isStartElement())
      {
         QString elementName = xml.name().toString();
         if(elementName == "MD") 
         {
            parseMetaDataElement(xml,userMetaData);
         }
         else std::cout << "unknown element: " << elementName.toAscii().data() << std::endl;             
      }
   }
   //check for end element
   if(!xml.isEndElement() || (xml.name().toString() != "MetaData"))
      xml.raiseError("MetaData end tag not found.");
}

void parseMetaDataElement(QXmlStreamReader &xml, QHash<QString,QString> &userMetaData)
{
   QString name;
   QString value;
   QString test;
   while (!(xml.isEndElement() && (xml.name().toString() == "MD")) && !xml.hasError()) {  
      test = xml.name().toString();
      xml.readNext();
      
      if(xml.isStartElement())
      {
         QString elementName = xml.name().toString();
         if(elementName == "Name") 
         {
            xml.readNext();
            if(xml.tokenType() != QXmlStreamReader::Characters) {
               return;
            }
            name = xml.text().toString();
            xml.readNext();
            if(!xml.isEndElement())
               xml.raiseError("End element for meta data name tag not found.");           
         }
         else if(elementName == "Value")
         {
            xml.readNext();
            if(xml.tokenType() != QXmlStreamReader::Characters) {
               return;
            }
            value = xml.text().toString();
            xml.readNext();
            if(!xml.isEndElement())
               xml.raiseError("End element for meta data value tag not found.");            
         }
         else std::cout << "unknown element: " << elementName.toAscii().data() << std::endl;                      
      }

   }
   userMetaData.insert(name,value);   
   if(!xml.isEndElement() || (xml.name().toString() != "MD"))
      xml.raiseError("End element for MD tag not found");  
}

void parseLabelTable(QXmlStreamReader &xml, std::vector<CiftiLabelElement> &labelTable)
{
   while (!(xml.isEndElement() && (xml.name().toString() == "LabelTable"))&& !xml.hasError()) {// && xml.name() == "Matrix") {
      xml.readNext();
      if(xml.isStartElement())
      {
         QString elementName = xml.name().toString();
         if(elementName == "Label") 
         {
            labelTable.push_back(CiftiLabelElement());
            parseLabel(xml,labelTable.back());
         }
         else std::cout << "unknown element: " << elementName.toAscii().data() << std::endl;             
      }
   }
   //check end element   
   if(!xml.isEndElement() || (xml.name().toString() != "LabelTable"))
   {
      xml.raiseError("End element for label table not found.");
   }
   
}

void parseLabel(QXmlStreamReader &xml, CiftiLabelElement &label)
{
   if(!(xml.name().toString() == "Label")) xml.raiseError("Error parsing Label\n");
   QXmlStreamAttributes attributes = xml.attributes();

   //get attribute values
   if(attributes.hasAttribute("Key")) label.m_key = attributes.value("Key").toString().toULongLong();
   else xml.raiseError("Label does not contain Key value\n");
   
   if(attributes.hasAttribute("Red")) label.m_red = attributes.value("Red").toString().toFloat();
   else xml.raiseError("Label does not contain Red value\n");
   
   if(attributes.hasAttribute("Green")) label.m_green = attributes.value("Green").toString().toFloat();
   else xml.raiseError("Label does not contain Green value\n");
   
   if(attributes.hasAttribute("Blue")) label.m_blue = attributes.value("Blue").toString().toFloat();
   else xml.raiseError("Label does not contain Blue value\n");
   
   if(attributes.hasAttribute("Alpha")) label.m_alpha = attributes.value("Alpha").toString().toFloat();
   else xml.raiseError("Label does not contain Alpha value\n");
   
   if(attributes.hasAttribute("X")) label.m_x = attributes.value("X").toString().toFloat();
   else xml.raiseError("Label does not contain X value\n");
   
   if(attributes.hasAttribute("Y")) label.m_y = attributes.value("Y").toString().toFloat();
   else xml.raiseError("Label does not contain Y value\n");
   
   if(attributes.hasAttribute("Z")) label.m_z = attributes.value("Z").toString().toFloat();
   else xml.raiseError("Label does not contain Z value\n");

   //get Label Text
   xml.readNext();
   if(xml.tokenType() != QXmlStreamReader::Characters) {
      return;
   }
   label.m_text = xml.text().toString();

   //get end element
   xml.readNext();
   if(!xml.isEndElement())
   {
      xml.raiseError("End element for label not found.");
   }   
}

void parseMatrixIndicesMap(QXmlStreamReader &xml, CiftiMatrixIndicesMapElement &matrixIndicesMap)
{
   QXmlStreamAttributes attributes = xml.attributes();
   
   //get attribute values
   if(attributes.hasAttribute("AppliesToMatrixDimension")) 
   {
       QStringList values = attributes.value("AppliesToMatrixDimension").toString().split(',');
       
       for(int i = 0;i<values.size();i++)
          matrixIndicesMap.m_appliesToMatrixDimension.push_back(values.at(i).toInt());
   }
   else xml.raiseError("MatrixIndicesMap does not contain AppliesToMatrixDimension value\n");

   if(attributes.hasAttribute("IndicesMapToDataType")) {
      QString indicesMapToDataType = attributes.value("IndicesMapToDataType").toString();
      if(indicesMapToDataType == "CIFTI_INDEX_TYPE_BRAIN_MODELS") matrixIndicesMap.m_indicesMapToDataType = CIFTI_INDEX_TYPE_BRAIN_MODELS;
      else if(indicesMapToDataType == "CIFTI_TYPE_FIBERS") matrixIndicesMap.m_indicesMapToDataType = CIFTI_INDEX_TYPE_FIBERS;
      else if(indicesMapToDataType == "CIFTI_INDEX_TYPE_PARCELS") matrixIndicesMap.m_indicesMapToDataType = CIFTI_INDEX_TYPE_PARCELS;
      else if(indicesMapToDataType == "CIFTI_INDEX_TYPE_TIME_POINTS") matrixIndicesMap.m_indicesMapToDataType = CIFTI_INDEX_TYPE_TIME_POINTS;
      else xml.raiseError("Error, unrecognized value for BrainModel, indicesMapToDataType.");
   }
   else xml.raiseError("MatrixIndicesMap does not contain IndicesMapToDataType value\n");
   
   if(attributes.hasAttribute("TimeStep")) matrixIndicesMap.m_timeStep = attributes.value("TimeStep").toString().toFloat();
   //else xml.raiseError("MatrixIndicesMap does not contain timeStep Value.");
   
   if(attributes.hasAttribute("TimeStepUnits")) 
   {   
      QString timeStepUnits = attributes.value("TimeStepUnits").toString();
      if(timeStepUnits == "NIFTI_UNITS_SEC")
      {
         matrixIndicesMap.m_timeStepUnits = NIFTI_UNITS_SEC;
      }
      else if(timeStepUnits == "NIFTI_UNITS_MSEC")
      {
         matrixIndicesMap.m_timeStepUnits = NIFTI_UNITS_MSEC;
      }
      else if(timeStepUnits == "NIFTI_UNITS_USEC")
      {
         matrixIndicesMap.m_timeStepUnits = NIFTI_UNITS_USEC;
      }
   }   

   while (!xml.isEndElement()  && !xml.hasError()) {
      xml.readNext();
      if(xml.isStartElement())
      {
         QString elementName = xml.name().toString();
         if(elementName == "BrainModel")
         {
            matrixIndicesMap.m_brainModels.push_back(CiftiBrainModelElement());
            parseBrainModel(xml,matrixIndicesMap.m_brainModels.back());
            xml.readNext();//read next element after brainmodel end element
         }         
         else std::cout << "unknown element: " << elementName.toAscii().data() << std::endl;
      }
   }
}

void parseBrainModel(QXmlStreamReader &xml, CiftiBrainModelElement &brainModel)
{
   QXmlStreamAttributes attributes = xml.attributes();
   
   //get attribute values
   if(attributes.hasAttribute("IndexOffset")) brainModel.m_indexOffset = attributes.value("IndexOffset").toString().toULongLong();
   else xml.raiseError("BrainModel does not contain required IndexOffset attribute\n");

   if(attributes.hasAttribute("IndexCount")) brainModel.m_indexCount = attributes.value("IndexCount").toString().toULongLong();
   else xml.raiseError("BrainModel does not contain required IndexCount attribute\n");

   if(attributes.hasAttribute("ModelType")) 
   {
      QString modelType = attributes.value("ModelType").toString();
      if(modelType == "CIFTI_MODEL_TYPE_SURFACE")
      {
         brainModel.m_modelType = CIFTI_MODEL_TYPE_SURFACE;
      }
      else if(modelType == "CIFTI_MODEL_TYPE_VOXELS")
      {
         brainModel.m_modelType = CIFTI_MODEL_TYPE_VOXELS;
      }
      else xml.raiseError("BrainModel contains unrecognized model type");
   }
   else xml.raiseError("BrainModel does not contain required ModelType attribute\n");

   if(attributes.hasAttribute("BrainStructure"))
   {
      QString brainStructure = attributes.value("BrainStructure").toString();
      brainModel.m_brainStructure = attributes.value("BrainStructure").toString();
   }
   
   if(attributes.hasAttribute("SurfaceNumberOfNodes")) brainModel.m_surfaceNumberOfNodes = attributes.value("SurfaceNumberOfNodes").toString().toULongLong();
   else brainModel.m_surfaceNumberOfNodes = 0;
   //else xml.raiseError("BrainModel does not contain require SurfaceNumberOfNodes attribute\n");

   while (!(xml.isEndElement() && (xml.name().toString() == "BrainModel")) && !xml.hasError()) {
      xml.readNext();
      if(xml.isStartElement())
      {
         QString elementName = xml.name().toString();
         if(elementName == "NodeIndices")
         {
            //get node Indices
            xml.readNext();
            if(xml.tokenType() != QXmlStreamReader::Characters) {
               return;
            }
            QString nodeIndices = xml.text().toString();

            QStringList list = nodeIndices.split(QRegExp("\\D+"),QString::SkipEmptyParts);
            for(int i = 0;i<list.count();i++)
            {
               brainModel.m_nodeIndices.push_back(list[i].toULongLong());
            }
            //get end element
            xml.readNext();
            if(!xml.isEndElement())
            {
               xml.raiseError("End element for NodeIndices not found.");
            } 
         }
         else if(elementName == "VoxelIndicesIJK")
         {
            //get voxel Indices IJK
            xml.readNext();
            if(xml.tokenType() != QXmlStreamReader::Characters) {
               return;
            }
            QString voxelIndicesIJK = xml.text().toString();
         
            QStringList list = voxelIndicesIJK.split(QRegExp("\\D+"),QString::SkipEmptyParts);
            if(list.count()%3) std::cout << voxelIndicesIJK.toAscii().data() << std::endl << list[list.count()-3].toULongLong() << list[list.count()-2].toULongLong() << list[list.count()-1].toULongLong() << std::endl;
            for(int i = 0;i<list.count();i++)
            {
               brainModel.m_voxelIndicesIJK.push_back(list[i].toULongLong());
            }
            //get end element
            xml.readNext();
            if(!xml.isEndElement()|| (xml.name().toString() != "VoxelIndicesIJK"))
            {
               xml.raiseError("End element for VoxelIndicesIJK not found.");
            }
         }         
         else std::cout << "unknown element: " << elementName.toAscii().data() << std::endl;             
      }
   }

   //get end element for BrainModel
   while(!(xml.isEndElement() && (xml.name().toString() == "BrainModel"))&& !xml.hasError()) xml.readNext();
   if(!xml.isEndElement() || (xml.name().toString() != "BrainModel"))
   {
      xml.raiseError("End element for brain Model not found.");
   }
}

void parseVolume(QXmlStreamReader &xml, CiftiVolume &volume)
{
   QXmlStreamAttributes attributes = xml.attributes();
   
   //get attribute values
   if(attributes.hasAttribute("VolumeDimensions"))
   {
      QStringList list = attributes.value("VolumeDimensions").toString().split(',');
      for(int i=0;i<3;i++)
      {
         volume.m_volumeDimensions[i]=list[i].toUInt();
      }   
   }
   else xml.raiseError("Volume does not contain required VolumeDimensions attribute\n");
   
   while (!(xml.isEndElement() && (xml.name().toString() == "Volume"))  && !xml.hasError()) {
      xml.readNext();
      if(xml.isStartElement())
      {
         QString elementName = xml.name().toString();
         if(elementName == "TransformationMatrixVoxelIndicesIJKtoXYZ")
         {
            volume.m_transformationMatrixVoxelIndicesIJKtoXYZ.push_back(TransformationMatrixVoxelIndicesIJKtoXYZ());
            parseTransformationMatrixVoxelIndicesIJKtoXYZ(xml,volume.m_transformationMatrixVoxelIndicesIJKtoXYZ.back());
         }         
         else std::cout << "unknown element: " << elementName.toAscii().data() << std::endl;             
      }
   }
   
   //check end element for Volume
   if(!xml.isEndElement())
   {
      xml.raiseError("End element for Volume not found.");
   }
}

void parseTransformationMatrixVoxelIndicesIJKtoXYZ(QXmlStreamReader &xml, TransformationMatrixVoxelIndicesIJKtoXYZ &transform)
{
   QXmlStreamAttributes attributes = xml.attributes();
   
   //get attribute values
   if(attributes.hasAttribute("DataSpace"))
   {
      QString dataSpace = attributes.value("DataSpace").toString();
      
      if(dataSpace == "NIFTI_XFORM_UNKNOWN") transform.m_dataSpace = NIFTI_XFORM_UNKNOWN;
      else if(dataSpace == "NIFTI_XFORM_SCANNER_ANAT") transform.m_dataSpace = NIFTI_XFORM_SCANNER_ANAT;
      else if(dataSpace == "NIFTI_XFORM_ALIGNED_ANAT") transform.m_dataSpace = NIFTI_XFORM_ALIGNED_ANAT;
      else if(dataSpace == "NIFTI_XFORM_TALAIRACH") transform.m_dataSpace = NIFTI_XFORM_TALAIRACH;
      else if(dataSpace == "NIFTI_XFORM_MNI_152") transform.m_dataSpace = NIFTI_XFORM_MNI_152;
      else xml.raiseError("Volume contains unknown or unsupported data space.");
   }
   else xml.raiseError("TransformationMatrixVoxelIndicesIJKtoXYZ does not contain dataSpace.");
   
   if(attributes.hasAttribute("TransformedSpace"))
   {
      QString transformedSpace = attributes.value("TransformedSpace").toString();
      
      if(transformedSpace == "NIFTI_XFORM_UNKNOWN") transform.m_transformedSpace = NIFTI_XFORM_UNKNOWN;
      else if(transformedSpace == "NIFTI_XFORM_SCANNER_ANAT") transform.m_transformedSpace = NIFTI_XFORM_SCANNER_ANAT;
      else if(transformedSpace == "NIFTI_XFORM_ALIGNED_ANAT") transform.m_transformedSpace = NIFTI_XFORM_ALIGNED_ANAT;
      else if(transformedSpace == "NIFTI_XFORM_TALAIRACH") transform.m_transformedSpace = NIFTI_XFORM_TALAIRACH;
      else if(transformedSpace == "NIFTI_XFORM_MNI_152") transform.m_transformedSpace = NIFTI_XFORM_MNI_152;
      else xml.raiseError("Volume contains unknown or unsupported transformed space.");
   }
   else xml.raiseError("TransformationMatrixVoxelIndicesIJKtoXYZ does not contain transformedSpace.");
   
   if(attributes.hasAttribute("UnitsXYZ"))
   {
      QString unitsXYZ = attributes.value("UnitsXYZ").toString();
      
      if(unitsXYZ == "NIFTI_UNITS_MM") transform.m_unitsXYZ = NIFTI_UNITS_MM;
      else if(unitsXYZ == "NIFTI_UNITS_MICRON") transform.m_unitsXYZ = NIFTI_UNITS_MICRON;
      else xml.raiseError("Volume contains unknown or unsupported spatial XYZ coordinates.");
   }
   else xml.raiseError("TransformationMatrixVoxelIndicesIJKtoXYZ does not contain UnitsXYZ.");
   
   xml.readNext();
   if(xml.tokenType() != QXmlStreamReader::Characters) {
      xml.raiseError("Error reading Transformation matrix.");
   }
   QString voxelIndicesString = xml.text().toString();
   QStringList voxelIndices = voxelIndicesString.split(QRegExp("\\s+"),QString::SkipEmptyParts);
   for(int i = 0;i<16;i++)
      transform.m_transform[i] = voxelIndices.at(i).toFloat();
   
   //get end element for TransformationMatrixVoxelIndicesIJKtoXYZ
   while(!xml.isEndElement() && !xml.hasError())  xml.readNext();
   if(!xml.isEndElement())
   {
      xml.raiseError("End element for TransformationMatrixVoxelIndicesIJKtoXYZ not found.");
   }
}