forked from QIICR/dcm2tables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDICOMParser.py
316 lines (269 loc) · 12.1 KB
/
DICOMParser.py
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
import pydicom, os, sys, json
class DICOMParser(object):
def __init__(self,fileName,rulesDictionary,dcmqiPath=None,tempPath=None):
try:
self.dcm = pydicom.read_file(fileName)
except:
raise
self.fileName = fileName
self.rulesDictionary = rulesDictionary
self.tempPath = tempPath
self.dcmqiPath = dcmqiPath
self.tables = {}
def getTables(self):
return self.tables
def parse(self):
self.readTopLevelAttributes("CompositeContext")
self.readReferences()
modality = self.dcm.Modality
if modality in ["SR", "PT", "CT", "SEG", "RWV"]:
self.readTopLevelAttributes(self.dcm.Modality)
if modality == "SEG":
self.readSegments()
self.readSegmentFrames()
if modality == "SR":
tid = self.dcm.ContentTemplateSequence[0].TemplateIdentifier
if tid == "1500":
# convert to JSON
from subprocess import call
outputJSON = os.path.join(self.tempPath,"measurements.json")
tid1500reader = os.path.join(self.dcmqiPath,"tid1500reader")
print tid1500reader
call([tid1500reader,"--inputDICOM",self.fileName,"--outputMetadata",outputJSON])
with open(outputJSON) as jsonFile:
measurementsJSON = json.load(jsonFile)
self.readMeasurements(measurementsJSON)
def readTopLevelAttributes(self,modality):
self.tables[modality] = {}
unresolvedAttributes = []
for a in self.rulesDictionary[modality]:
try:
dataElement = self.dcm.data_element(a)
if dataElement.VM>1:
self.tables[modality][a] = '/'.join([str(i) for i in dataElement.value])
else:
self.tables[modality][a] = str(dataElement.value)
# else:
# self.tables[table]
# self.tables[tableName][a] = self.dcm.data_element(a).value
# print self.dcm.data_element(a).VM
except:
unresolvedAttributes.append(a)
self.tables[modality][a] = None
for a in unresolvedAttributes:
if hasattr(self,"read"+modality+a):
resolvedAttribute = str(getattr(self, "read%s%s" % (modality, a) )())
self.tables[modality][a] = resolvedAttribute
'''
if resolvedAttribute is not None:
print "Successfully resolved",a
else:
print "Failed to resolve",a
'''
#print self.tables[tableName][a]
# functions to read specific attributes that are not top-level or that are SR # tree elements
#def readReferencedImageRealWorldValueMappingSequence(self):
# de = self.dcm.data_element("ReferencedImageRealWorldValueMappingSequence")
# if de:
# de = de.data_element("RealWorldValueMappingSequence")
# if de:
# given the input data element, which must be a SQ, and must have the structure
# of items that follow the pattern ConceptNameCodeSequence/ConceptCodeSequence, find the sequence item that has
# ConceptNameCodeSequence > CodeMeaning, and return the data element corresponding
# to the ConceptCodeSequnce matching the requested ConceptNameCodeSequence meaning
def getConceptCodeByConceptNameMeaning(self,dataElement,conceptNameMeaning):
for item in dataElement:
if item.ConceptNameCodeSequence[0].CodeMeaning == conceptNameMeaning:
return item.ConceptCodeSequence[0]
def getMeasurementUnitsCodeSequence(self):
dataElement = self.dcm.data_element("ReferencedImageRealWorldValueMappingSequence").value[0]
dataElement = dataElement.data_element("RealWorldValueMappingSequence").value[0]
dataElement = dataElement.data_element("MeasurementUnitsCodeSequence").value
return dataElement
def getQuantityDefinitionSequence(self):
dataElement = self.dcm.data_element("ReferencedImageRealWorldValueMappingSequence").value[0]
dataElement = dataElement.data_element("RealWorldValueMappingSequence").value[0]
dataElement = dataElement.data_element("QuantityDefinitionSequence").value
return dataElement
def readRWVUnits_CodeValue(self):
dataElement = self.getMeasurementUnitsCodeSequence()[0]
return dataElement.CodeValue
def readRWVUnits_CodingSchemeDesignator(self):
dataElement = self.getMeasurementUnitsCodeSequence()[0]
return dataElement.CodingSchemeDesignator
def readRWVUnits_CodeMeaning(self):
dataElement = self.getMeasurementUnitsCodeSequence()[0]
return dataElement.CodeMeaning
def readRWVQuantity_CodeValue(self):
dataElement = self.getQuantityDefinitionSequence()
dataElement = self.getConceptCodeByConceptNameMeaning(dataElement, "Quantity")
return dataElement.CodeValue
def readRWVQuantity_CodingSchemeDesignator(self):
dataElement = self.getQuantityDefinitionSequence()
dataElement = self.getConceptCodeByConceptNameMeaning(dataElement, "Quantity")
return dataElement.CodingSchemeDesignator
def readRWVQuantity_CodeMeaning(self):
dataElement = self.getQuantityDefinitionSequence()
dataElement = self.getConceptCodeByConceptNameMeaning(dataElement, "Quantity")
return dataElement.CodeMeaning
def readRWVMeasurementMethod_CodeValue(self):
dataElement = self.getQuantityDefinitionSequence()
dataElement = self.getConceptCodeByConceptNameMeaning(dataElement, "Measurement Method")
return dataElement.CodeValue
def readRWVMeasurementMethod_CodingSchemeDesignator(self):
dataElement = self.getQuantityDefinitionSequence()
dataElement = self.getConceptCodeByConceptNameMeaning(dataElement, "Measurement Method")
return dataElement.CodingSchemeDesignator
def readRWVMeasurementMethod_CodeMeaning(self):
dataElement = self.getQuantityDefinitionSequence()
dataElement = self.getConceptCodeByConceptNameMeaning(dataElement, "Measurement Method")
return dataElement.CodeMeaning
def readRWVRealWorldValueIntercept(self):
dataElement = self.dcm.data_element("ReferencedImageRealWorldValueMappingSequence").value[0]
dataElement = dataElement.data_element("RealWorldValueMappingSequence").value[0]
return dataElement.RealWorldValueIntercept
def readRWVRealWorldValueSlope(self):
dataElement = self.dcm.data_element("ReferencedImageRealWorldValueMappingSequence").value[0]
dataElement = dataElement.data_element("RealWorldValueMappingSequence").value[0]
return dataElement.RealWorldValueSlope
# this part is not driven at all by the QDBD schema!
# (maybe it should be changed to generalize things better)
def readReferences(self):
self.tables["References"] = []
try:
refSeriesSeq = self.dcm.data_element("ReferencedSeriesSequence")
except:
refSeriesSeq = None
try:
evidenceSeq = self.dcm.data_element("CurrentRequestedProcedureEvidenceSequence")
except:
evidenceSeq = None
if refSeriesSeq:
self.readReferencedSeriesSequence(refSeriesSeq)
if evidenceSeq:
self.readEvidenceSequence(evidenceSeq)
def readReferencedSeriesSequence(self, seq):
for r in seq:
seriesUID = r.data_element("SeriesInstanceUID").value
refInstancesSeq = r.data_element("ReferencedInstanceSequence").value
for i in refInstancesSeq:
refClassUID = i.ReferencedSOPClassUID
refInstanceUID = i.ReferencedSOPInstanceUID
self.tables["References"].append({"SOPInstanceUID": self.dcm.SOPInstanceUID, "ReferencedSOPClassUID": refClassUID, "ReferencedSOPInstanceUID": refInstanceUID, "SeriesInstanceUID": seriesUID})
def readEvidenceSequence(self, seq):
for l1item in seq:
seriesSeq = l1item.data_element("ReferencedSeriesSequence").value
for l2item in seriesSeq:
sopSeq = l2item.data_element("ReferencedSOPSequence").value
seriesUID = l2item.SeriesInstanceUID
for l3item in sopSeq:
refClassUID = l3item.ReferencedSOPClassUID
refInstanceUID = l3item.ReferencedSOPInstanceUID
self.tables["References"].append({"SOPInstanceUID": self.dcm.SOPInstanceUID, "ReferencedSOPClassUID": refClassUID, "ReferencedSOPInstanceUID": refInstanceUID, "SeriesInstanceUID": seriesUID})
def readSegments(self):
seq = self.dcm.data_element("SegmentSequence")
self.tables["SEG_Segments"] = []
for segment in seq:
sAttr = {}
# Attrubute should be either in a sub-sequence, at the
# top level of the sequence, or at the top level of the dataset
# Try all those options
for attr in self.rulesDictionary["SEG_Segments"]:
if attr.find("_")>0:
# it is (supposed to be!) a code tuple in a sequence
seqName,attrName = attr.split("_")
try:
sAttr[attr] = segment.data_element(seqName)[0].data_element(attrName).value
except:
continue
else:
try:
sAttr[attr] = segment.data_element(attr).value
except:
try:
sAttr[attr] = self.dcm.data_element(attr).value
except:
sAttr[attr] = None
self.tables["SEG_Segments"].append(sAttr)
def readSegmentFrames(self):
pfFG = self.dcm.data_element("PerFrameFunctionalGroupsSequence")
sFG = self.dcm.data_element("SharedFunctionalGroupsSequence")
self.tables["SEG_SegmentFrames"] = []
# Attrubute should be either in a sub-sequence, in the shared FG,
# or at the top level of the dataset
# Try all those options
for frame in pfFG:
fAttr = {}
for attr in self.rulesDictionary["SEG_SegmentFrames"]:
# print "Looking for",attr
# recursively search in the per-frame FG item
value = self.recursiveFindInDataset(frame,attr)
if value is None:
# recursively search in the shared FG
value = self.recursiveFindInDataElement(sFG,attr)
# if those fail, look it up top-level
if value is None:
value = self.dcm.data_element(attr).value
fAttr[attr] = value
self.tables["SEG_SegmentFrames"].append(fAttr)
# idea from https://github.com/pieper/Chronicle/blob/master/bin/record.py#L58
def recursiveFindInDataElement(self,de,attr):
if de.keyword == attr:
return de.value
elif de.VR == "SQ":
for item in de:
return self.recursiveFindInDataset(item,attr)
return None
def recursiveFindInDataset(self,ds,attr):
for key in ds.keys():
de = ds[key]
value = self.recursiveFindInDataElement(de,attr)
if value is not None:
return value
return None
def readMeasurements(self,measurements):
self.tables["SR1500_MeasurementGroups"] = []
self.tables["SR1500_Measurements"] = []
for mg in measurements["Measurements"]:
mAttr = {}
for attr in self.rulesDictionary["SR1500_MeasurementGroups"]:
# first try to find it in the top-level of the measurements group json
if attr in mg.keys():
value = mg[attr]
elif attr.find("_")>0:
# this is a code sequence
concept = attr.split("_")[0]
item = attr.split("_")[1]
value = mg[concept][item]
else:
# if all other attempts fail, read it at the top level of the
# DICOM dataset (it must be a foreign key)
value = self.dcm.data_element(attr).value
mAttr[attr] = value
self.tables["SR1500_MeasurementGroups"].append(mAttr)
for mi in mg["measurementItems"]:
miAttr = {}
# OMG! Such a terrible code duplication
for iattr in self.rulesDictionary["SR1500_Measurements"]:
# first try to find it in the top-level of the measurements group json
if iattr in mi.keys():
value = mi[iattr]
elif iattr.find("_")>0:
# this is a code sequence
concept = iattr.split("_")[0]
item = iattr.split("_")[1]
try:
value = mi[concept][item]
except:
continue
# the attribute is one level above!
# our secondary foreign key is TrackingUniqueIdentifier ...
# So this is a tiny bit different from the code above!
elif iattr in mAttr.keys():
value = mAttr[iattr]
else:
# if all other attempts fail, read it at the top level of the
# DICOM dataset (it must be a foreign key)
value = self.dcm.data_element(iattr).value
miAttr[iattr] = value
self.tables["SR1500_Measurements"].append(miAttr)