-
Notifications
You must be signed in to change notification settings - Fork 3
/
SRCDParser.py
336 lines (302 loc) · 16.4 KB
/
SRCDParser.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from DICOMParser import DICOMParser
import pydicom
import copy
class SRCDParser(DICOMParser):
def parse(self):
modality = self.dcm.Modality
if modality == "SR":
if hasattr(self.dcm, "ContentTemplateSequence"):
if self.dcm.ContentTemplateSequence[0].TemplateIdentifier == "QIICR_2000":
self.readTopLevelAttributes("CompositeContext")
self.readTopLevelAttributes("CD")
self.ClinicalDataParser("CD")
else:
super(SRCDParser,self).parse()
else:
super(SRCDParser, self).parse()
def ClinicalDataParser(self, modality):
completedContainers = []
self.tables['CD']=[self.tables['CD']]
for container in ('ProblemList','SocialHistory','TumorStaging','MedicalHistory','Biopsy','Surgery','Radiotherapy',
'Chemotherapy','OriginalPathology','CervicalLymphNodeGroupExcision','DiseaseOutcome'):
str(getattr(self, "readCD%s" % container)(container))
completedContainers.append(container)
def readCDProblemList(self, container):
dataElement = getattr(self, "getCD%sContainer" % container)()
if not dataElement is None:
dataElement=dataElement[0].ContentSequence
self.readCDValue(container, 'Code', 'Problem', 'Problem', dataElement[0])
srcTables=self.tables['CD']
self.tables['CD']=[]
for i in range(1,len(dataElement)):
destTables=copy.deepcopy(srcTables)
self.readCDValueMulti(container, 'Code', 'Therapy', 'Therapy', dataElement[i], destTables)
self.tables['CD'] += destTables
def readCDSocialHistory(self, container):
dataElement = getattr(self, "getCD%sContainer" % container)()
if not dataElement is None:
self.readCDValue(container, 'Code', 'TobaccoSmokingBehavior', 'Tobacco Smoking Behavior', dataElement)
self.readCDValue(container, 'Code', 'AlcoholConsumption', 'Alcohol consumption', dataElement)
self.readCDValue(container, 'Code', 'DetailsOfTobaccoChewing', 'Details of tobacco chewing', dataElement)
def readCDTumorStaging(self, container):
dataElement = getattr(self, "getCD%sContainer" % container)()
if not dataElement is None:
self.readCDValue(container, 'Code', "PrimaryTumorSite", "Primary tumor site", dataElement)
self.readCDValue(container, "Code", "TumorStageFinding", "Tumor stage finding", dataElement)
dataElement = self.getContainerByConceptNameMeaning(dataElement, "TNM Category")
if not dataElement is None:
self.readCDValue(container, "Code", "TStage", "T Stage", dataElement)
self.readCDValue(container, "Code", "NStage", "N Stage", dataElement)
self.readCDValue(container, "Code", "MStage", "M Stage", dataElement)
def readCDMedicalHistory(self, container):
dataElement = getattr(self, "getCD%sContainer" % container)()
if not dataElement is None:
self.readCDValue(container, "Code", "RadiationTherapy", "History of radiation therapy", dataElement)
self.readCDValue(container, "Code", "MalignantNeoplasm", "History of malignant neoplasm", dataElement)
def readCDBiopsy(self, container):
dataElements = getattr(self, "getCD%sContainer" % container)()
if not dataElements is None and dataElements.VM>0:
srcTables=self.tables['CD']
self.tables['CD']=[]
for dataElement in dataElements:
destTables=copy.deepcopy(srcTables)
dataElement = dataElement.ContentSequence
self.readCDValueMulti(container, "Date", "DateOfProcedure", "Date of procedure", dataElement,
destTables)
self.readCDValueMulti(container, "TextValue", "Site", "Biopsy Site", dataElement,
destTables)
self.tables['CD'] += destTables
def readCDSurgery(self, container):
dataElements = getattr(self, "getCD%sContainer" % container)()
if not dataElements is None and len(dataElements)>0:
srcTables=self.tables['CD']
self.tables['CD']=[]
for dataElement in dataElements:
destTables=copy.deepcopy(srcTables)
self.readCDValueMulti(container, "Date", "DateOfProcedure", "Date of procedure", dataElement,
destTables)
self.readCDValueMulti(container, "TextValue", "ProcedureDescription", "Procedure Description",
dataElement, destTables)
self.readCDValueMulti(container, "Code", "ResectionOfPrimaryTumor", "Resection of primary tumor",
dataElement, destTables)
self.readCDValueMulti(container, "Code", "BlockDissectionOfCervicalLymphNodes",
"Block dissection of cervical lymph nodes", dataElement, destTables)
self.tables['CD'] += destTables
def readCDRadiotherapy(self, container):
dataElements = getattr(self, "getCD%sContainer" % container)()
if not dataElements is None and len(dataElements)>0:
srcTables=self.tables['CD']
self.tables['CD']=[]
for dataElement in dataElements:
destTables=copy.deepcopy(srcTables)
self.readCDValueMulti(container, "Date", "DateTreatmentStarted", "Date treatment started", dataElement,
destTables)
self.readCDValueMulti(container, "Date", "DateTreatmentStopped", "Date treatment stopped", dataElement,
destTables)
self.readCDValueMulti(container, "NumericValue", "TotalRadiationDoseDelivered",
"Total radiation dose delivered", dataElement, destTables)
self.readCDValueMulti(container, "NumericValue", "RadiationDosePerFraction", "Radiation dose per fraction",
dataElement, destTables)
self.readCDValueMulti(container, "TextValue", "ProcedureDescription", "Procedure Description", dataElement,
destTables)
self.tables['CD'] += destTables
def readCDChemotherapy(self, container):
dataElements = getattr(self, "getCD%sContainer" % container)()
if not dataElements is None and len(dataElements)>0:
srcTables = self.tables['CD']
self.tables['CD'] = []
for dataElement in dataElements:
destTables=copy.deepcopy(srcTables)
self.readCDValueMulti(container, "Date", "DateTreatmentStarted", "Date treatment started", dataElement,
destTables)
self.readCDValueMulti(container, "Date", "DateTreatmentStopped", "Date treatment stopped", dataElement,
destTables)
self.readCDValueMulti(container, "Code", "AntineoplasticAgent", "Antineoplastic agent",
dataElement, destTables) # Need to support up to 3
self.tables['CD'] += destTables
def readCDOriginalPathology(self, container):
dataElement = getattr(self, "getCD%sContainer" % container)()
if not dataElement is None:
self.readCDValue(container, "Code", "Pathology", "Pathology", dataElement)
dataElement = dataElement[0].data_element('ContentSequence')
if not dataElement is None:
self.readCDValue(container, "Code", "HistologicalGradeFinding", "Histological grade finding",
dataElement)
self.readCDValue(container, "Code", "MalignancyType", "Malignancy Type", dataElement)
self.readCDValue(container, "Code", "TumorMarginStatus", "Tumor margin status", dataElement)
self.readCDValue(container, "Code", "PerineuralInvasionFinding", "Perineural invasion finding",
dataElement)
self.readCDValue(container, "Code", "StatusOfVascularInvasionByTumor",
"Status of vascular invasion by tumor", dataElement)
def readCDCervicalLymphNodeGroupExcision(self, container):
dataElements = getattr(self, "getCD%sContainer" % container)()
if not dataElements is None:
lymphNodeGroup = [dataElement for dataElement in dataElements if
self.readCDValue(container, "Code", "StatusOfExtraCapsularExtensionOfNodalTumor",
"Status of extra-capsular extension of nodal tumor", dataElement) +
self.readCDValue(container, "TextValue", "Comment", "Comment", dataElement) == 0]
if len(lymphNodeGroup)>0:
srcTables=self.tables['CD']
self.tables['CD']=[]
for dataElement in lymphNodeGroup:
destTables=copy.deepcopy(srcTables)
self.readCDValueMulti(container,"Code","CervicalLymphNodeGroup","Cervical lymph node group",dataElement,
destTables)
try:
dataElement = dataElement.data_element('ContentSequence')
self.readCDValueMulti(container, "Code", "Sidedness", "Sidedness", dataElement, destTables)
self.readCDValueMulti(container, "NumericValue", "NumberOfNodesRemoved", "Number of nodes removed",
dataElement, destTables)
self.readCDValueMulti(container, "NumericValue", "NumberOfNodesPositive", "Number of nodes positive",
dataElement, destTables)
except:
pass
self.tables['CD'] += destTables
def readCDDiseaseOutcome(self, container):
dataElement = getattr(self, "getCD%sContainer" % container)()
if not dataElement is None:
self.readCDValue(container, "Date", "FollowupVisitDate", "Follow-up visit date", dataElement)
self.readCDValue(container, "Code", "FollowupStatus", "Followup status", dataElement)
self.readCDValue(container, "Date", "DateOfDeath", "Date of death", dataElement)
self.readCDValue(container, "Code", "CauseOfDeath", "Cause of death", dataElement)
self.readCDValue(container, "Code", "PostRadiotherapyTreatment", "Post-radiotherapy treatment", dataElement)
self.readCDValue(container, "Date", "DateOfCancerRecurrence", "Date of cancer recurrence", dataElement)
self.readCDValue(container, "Code", "LocationOfFirstRecurrence", "Location of first recurrence",
dataElement)
dataElement = self.getContainerByConceptNameMeaning(dataElement, "Pathology of recurrent tumor")
if not dataElement is None:
dataElement = dataElement[0].data_element('ContentSequence')
self.readCDValue(container, "Code", "Pathology", "Pathology", dataElement)
dataElement = dataElement[0].data_element('ContentSequence')
self.readCDValue(container, "Code", "HistologicalGradeFinding", "Histological grade finding",
dataElement)
self.readCDValue(container, "Code", "MalignancyType", "Malignancy Type", dataElement)
self.readCDValue(container, "Code", "TumorMarginStatus", "Tumor margin status", dataElement)
self.readCDValue(container, "Code", "PerineuralInvasionFinding", "Perineural invasion finding",
dataElement)
self.readCDValue(container, "Code", "StatusOfVascularInvasionByTumor",
"Status of vascular invasion by tumor", dataElement)
def readCDValueMulti(self, container, valueType, field, codeMeaning, dataElement,destTables):
value = None
if type(dataElement) == pydicom.dataset.Dataset:
if dataElement.ConceptNameCodeSequence[0].CodeMeaning == codeMeaning:
if valueType == "Date" or valueType == "TextValue":
value = dataElement.data_element(valueType).value
elif valueType == "NumericValue":
value = dataElement.MeasuredValueSequence[0].NumericValue
elif valueType == "Code":
value = dataElement.ConceptCodeSequence[0]
else:
for item in dataElement:
if item.ConceptNameCodeSequence[0].CodeMeaning == codeMeaning:
if valueType == "Date" or valueType == "TextValue":
value = item.data_element(valueType).value
elif valueType == "NumericValue":
value = item.MeasuredValueSequence[0].NumericValue
elif valueType == "Code":
value = item.ConceptCodeSequence[0]
break
if not value is None:
for table in destTables:
if valueType == "Code":
table["%s_%s_CodeValue" % (container, field)] = value.CodeValue
table["%s_%s_CodingSchemeDesignator" % (container, field)] = value.CodingSchemeDesignator
table["%s_%s_CodeMeaning" % (container, field)] = value.CodeMeaning
else:
table["%s_%s" % (container, field)] = value
return not value is None
def readCDValue(self, container, valueType, field, codeMeaning, dataElement):
value = None
if type(dataElement) == pydicom.dataset.Dataset:
if dataElement.ConceptNameCodeSequence[0].CodeMeaning == codeMeaning:
if valueType == "Date" or valueType == "TextValue":
value = dataElement.data_element(valueType).value
elif valueType == "NumericValue":
value = dataElement.MeasuredValueSequence[0].NumericValue
elif valueType == "Code":
value = dataElement.ConceptCodeSequence[0]
else:
for item in dataElement:
if item.ConceptNameCodeSequence[0].CodeMeaning == codeMeaning:
if valueType == "Date" or valueType == "TextValue":
value = item.data_element(valueType).value
elif valueType == "NumericValue":
value = item.MeasuredValueSequence[0].NumericValue
elif valueType == "Code":
value = item.ConceptCodeSequence[0]
break
if not value is None:
for table in self.tables['CD']:
if valueType == "Code":
table["%s_%s_CodeValue" % (container, field)] = value.CodeValue
table["%s_%s_CodingSchemeDesignator" % (container, field)] = value.CodingSchemeDesignator
table["%s_%s_CodeMeaning" % (container, field)] = value.CodeMeaning
else:
table["%s_%s" % (container, field)] = value
return not value is None
def getCDProblemListContainer(self):
container = self.getCDSummaryClinicalDocumentContainer()[2]
try:
return container.data_element('ContentSequence')
except:
return None
def getCDSocialHistoryContainer(self):
return self.getCDSummaryClinicalDocumentContainer()[3].data_element('ContentSequence')
def getCDTumorStagingContainer(self):
return self.getCDSummaryClinicalDocumentContainer()[4].data_element('ContentSequence')
def getCDTNMCategoryContainer(self):
dataElement = self.getCDTumorStagingContainer()
return self.getContainerByConceptNameMeaning(dataElement, "TNM Category")
def getCDMedicalHistoryContainer(self):
return self.getCDSummaryClinicalDocumentContainer()[5].data_element('ContentSequence')
def getCDDiagnosticProcedureContainer(self):
try:
return self.getCDSummaryClinicalDocumentContainer()[6].data_element('ContentSequence')
except:
return None
def getCDBiopsyContainer(self):
return self.getCDDiagnosticProcedureContainer()
def getCDTherapeuticProcedureContainer(self):
return self.getCDSummaryClinicalDocumentContainer()[7].data_element('ContentSequence')
def getCDSurgeryContainer(self):
containers = self.getCDTherapeuticProcedureContainer()
surgeryContainers = []
for container in containers:
if container.ConceptNameCodeSequence[0].CodeMeaning == "Surgical Procedure":
surgeryContainers.append(container.ContentSequence)
return surgeryContainers
def getCDRadiotherapyContainer(self):
containers = self.getCDTherapeuticProcedureContainer()
surgeryContainers = []
for container in containers:
if container.ConceptNameCodeSequence[0].CodeMeaning == "Radiotherapy Procedure":
surgeryContainers.append(container.ContentSequence)
return surgeryContainers
def getCDChemotherapyContainer(self):
containers = self.getCDTherapeuticProcedureContainer()
surgeryContainers = []
for container in containers:
if container.ConceptNameCodeSequence[0].CodeMeaning == "Chemotherapy":
surgeryContainers.append(container.ContentSequence)
return surgeryContainers
def getCDPathologyOfOriginalTumorContainer(self):
return self.getCDSummaryClinicalDocumentContainer()[8].data_element('ContentSequence')
def getCDOriginalPathologyContainer(self):
dataElement = self.getCDPathologyOfOriginalTumorContainer()
return self.getContainerByConceptNameMeaning(dataElement, "Pathology Results")
def getCDCervicalLymphNodeGroupExcisionContainer(self):
dataElement = self.getCDPathologyOfOriginalTumorContainer()
return self.getContainerByConceptNameMeaning(dataElement, "Excision of cervical lymph nodes group")
def getCDDiseaseOutcomeContainer(self):
return self.getCDSummaryClinicalDocumentContainer()[9].data_element('ContentSequence')
def getCDSummaryClinicalDocumentContainer(self):
return self.dcm.data_element("ContentSequence")
def getContainerByConceptNameMeaning(self, dataElement, conceptNameMeaning):
if not dataElement is None:
for item in dataElement:
if item.ConceptNameCodeSequence[0].CodeMeaning == conceptNameMeaning:
try:
return item.data_element('ContentSequence')
except:
return None
return None