-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcmdmp.py
More file actions
240 lines (220 loc) · 7.1 KB
/
dcmdmp.py
File metadata and controls
240 lines (220 loc) · 7.1 KB
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
# Copyright (c) 2020 Rick Stroobosscher
# import a commonly used command line argument parser
import argparse
# parse the command line
parser = argparse.ArgumentParser(description='Extracts measurements from DICOM SR as XML and outputs as JSON.')
parser.add_argument("-f", "--file", required=True, type=str, help="The name of the file to parse")
args = parser.parse_args()
# pick up the name of the dicom file from the command line
file = args.file
# import dicom library
# requires "pip3.8 install pydicom"
import pydicom
# read the dicom object
dataset = pydicom.dcmread('/Users/Rick/data/dicom/GENECG.dcm')
#print(dataset)
# this is an example of how to extract a single element
acn = dataset[0x0008,0x0050]
#print(acn)
# import an XML parser
# requires "pip3.8 install untangle"
import untangle
# define a resource using XML, just to figure out how to extract values
xml = '''
<measurement>
<valueQuantity value="107" unit="mmHg" system="http://unitsofmeasure.org" code="mm[Hg]"/>
</measurement>
'''
print(xml)
# extract some values, just to show how its done. its very easy using untangle
doc = untangle.parse(xml)
print(doc.measurement.valueQuantity['value'])
print(doc.measurement.valueQuantity['unit'])
print(doc.measurement.valueQuantity['system'])
print(doc.measurement.valueQuantity['code'])
# create a observation template.
# this is my second attempt at defining a fhir resource
# in my first attempt, i used a bunch fhir resources that allowed
# me to define fhir resources using python, but that turned out to be
# rather complex and error prone. perhaps this is no less error prone,
# but it seems much easier to read.
# we include a bunch of variables (e.g. $acn) that will be replaced
# with their actual values (e.g. accession number).
obs = '''
{
"resourceType" : "Observation", /* formal name for Observation fHIR resource */
"identifier" : [
{
"id" : {
"system" : "urn:ietf:rfc:3986",
"value" : "urn:uuid:$uuid" /* $uuid replaced by uniquely generated uuid4 */
}
}
],
"partOf" : [ /* references imaging study, using SIUID and ACN */
{
"identifier" : {
"system" : "urn:dicom:uid",
"value" : "urn:oid:$siuid" /* $siuid replaced by study instance uid */
},
"type" : "ImagingStudy"
},
{
"identifier" : {
"system" : "http:grh.org/accession",
"type" : {
"coding": [
{
"code" : "ACSN",
"system" : "http://terminology.hl7.org/CodeSystem/v2-0203"
}
]
},
"value" : "$acn" /* $acn replaced by accession number */
}
}
],
"status" : "final", /* copied from teri's sample */
"category" : { /* copied from teri's sample */
"system" : "http://hl7.org/fhir/ValueSet/observation-category",
"version" : "4.0.1",
"code" : "imaging",
"display" : "Imaging"
},
"code" : { /* need actual LOINC codes or codes from other system */
"coding" : [
{
"system" : "http://loinc.org",
"code" : "TBD", /* need LOINC code */
"display" : "TBD" /* need LOINC display */
}
]
},
"subject" : {
"reference" : "Patient/$pid", /* will replace $pid with patient id */
"display": "$givenname $familyname" /* will replace $givenname and $familyname with given and family name */
},
"encounter" : {}, /* anything to do here? */
"effectiveDateTime" : {}, /* need to put study date and time here */
"performer" : {}, /* what do we want to reference here? */
"valueCodeableConcept" : [ /* SNOMED codes for COVID-19 */
{
"system": "",
"code": "",
"display": ""
}
],
"interpretation" : [ /* abnormal or thickened */
{
"system": "",
"code": "",
"display": ""
}
],
"bodySite" : [ /* need code for lungs */
{
"system": "",
"code": "",
"display": ""
}
],
"method" : [ /* US */
{
"system": "",
"code": "",
"display": ""
}
],
"device" : {}, /* what do we reference here, if anything? */
"derivedFrom" : [ /* references imaging study, using SIUID and ACN */
{
"identifier" : {
"system" : "urn:dicom:uid",
"value" : "urn:oid:$siuid" /* $siuid replaced by study instance uid */
},
"type" : "ImagingStudy"
},
{
"identifier" : {
"system" : "http:grh.org/accession",
"type" : {
"coding": [
{
"code" : "ACSN",
"system" : "http://terminology.hl7.org/CodeSystem/v2-0203"
}
]
},
"value" : "$acn" /* $acn replaced by accession number */
}
}
],
"component" : [
{ /* right upper lung */
"code" : [
{
"system": "",
"code": "", /* need code */
"display": ""
}
],
"valueQuantity" : {
"value" : 0, /* need value */
"comparator" : "", /* do we need this? */
"unit" : "", /* need unit */
"system" : "<uri>", /* need uri? */
"code" : "" /* need code */
},
"interpretation" : [ /* do we want to use this? */
{
"system": "",
"code": "",
"display": ""
}
]
},
{ /* right mid lung 2 */
"code" : [
{
"system": "",
"code": "", /* need code */
"display": ""
}
],
"valueQuantity" : {
"value" : 0, /* need value */
"comparator" : "", /* do we need this? */
"unit" : "", /* need unit */
"system" : "<uri>", /* need uri? */
"code" : "" /* need code */
},
"interpretation" : [ /* do we want to use this? */
{
"system": "",
"code": "",
"display": ""
}
]
}
]
}
'''
# remove the comments
import re
obs = re.sub('/\*.*\*/', '', obs)
obs = re.sub('//.*$', '', obs)
# replace the variables
import uuid
uuid = uuid.uuid4()
obs = obs.replace('$uuid', str(uuid))
siuid = dataset[0x0020,0x000D]
obs = obs.replace('$siuid', siuid.value)
acn = dataset[0x0008,0x0050]
obs = obs.replace('$acn', acn.value)
pid = dataset[0x0010,0x0020]
obs = obs.replace('$pid', pid.value)
pname = dataset[0x0010,0x0010]
obs = obs.replace('$familyname', pname.value.family_name)
obs = obs.replace('$givenname', pname.value.given_name)
# print the observation
print(obs)