forked from vikramc1/ccda-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_Dict1.py
545 lines (480 loc) · 19.6 KB
/
test_Dict1.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import xmltodict
import os.path
from pprint import pprint
import sys
from nameparser import HumanName
from datetime import datetime
try:
from pyccda import CCDA
except ModuleNotFoundError:
# #
sys.path.append("..")
from pyccda import CCDA
def build_address(address_data, start_data=None):
if start_data:
out_addr = start_data
else:
out_addr = {}
if hasattr(address_data,"city"):
out_addr["City"] = address_data.city
if hasattr(address_data,"country"):
out_addr["Country"] = address_data.country
if hasattr(address_data,"state"):
out_addr["State"] = address_data.state
if hasattr(address_data,"street"):
out_addr["Street"] = address_data.street
if hasattr(address_data,"zip"):
out_addr["Zip"] = address_data.zip
return out_addr
def is_empty(data):
return data in ["", None, "None", []]
def build_name(name_data):
if type(name_data) is str:
return name_data
elif name_data in [None]:
return ""
name = ""
if hasattr(name_data, "prefix"):
if not is_empty(name_data.prefix):
name += "%s " % name_data.prefix
if not is_empty(name_data.given):
name += " ".join(name_data.given)
if not is_empty(name_data.family):
name += " %s" % name_data.family
if hasattr(name_data, "suffix"):
if not is_empty(name_data.suffix):
name += " %s" % name_data.suffix
if is_empty(name):
return None
return HumanName(name)
def build_phone(phone_data):
out_phone = []
if phone_data in ["", None, {}]:
return None
if type(phone_data) is str:
#print("Phone String")
return phone_data.strip()
elif type(phone_data) is list:
#print("Phone List")
for entry in phone_data:
out_phone.append(classify(entry, out_phone))
return out_phone
outphone = []
for ptype in phone_data:
out_phone.append ({ptype.title():phone_data[ptype]})
return out_phone
def build_author(author_data):
output = {}
if hasattr(author_data, "name"):
output["Name"] = build_name(author_data.name)
if hasattr(author_data, "phone"):
output["Phone"] = build_phone(author_data.phone)
if hasattr(author_data, "address"):
output["Address"] = build_address(author_data.address)
return output
def build_qty(qty_date):
return {"Unit":qty_date.unit,
"Value":qty_date.value}
def build_location(location_data):
output = {}
output["Address"] = build_address(location_data)
# output["Name"] = build_name(location_data.name)
if hasattr(location_data, "organization"):
output["Organization"] = location_data.organization
return output
def build_date_range(daterange):
output = {}
if hasattr(daterange, "start"):
output["Start"] = daterange.start
if hasattr(daterange, "end"):
output["End"] = daterange.end
if hasattr(daterange, "date"):
output["Date"] = daterange
return output
class ccda_document():
def __init__(self):
self.ccda_filename = None
self.ccda = None
def read_ccda(self,filename):
self.ccda_filename = filename
with open(self.ccda_filename) as f:
self.ccda = CCDA(f.read())
def return_allergies(self):
def return_allergy_element(datum):
return {"Date":build_date_range(datum.date_range),
#"Start":datum.date_range.start,
#"End":datum.date_range.end,
"Allergen":datum.allergen.name,
#"Name":datum.name,
"Reaction":datum.reaction.name,
"Reaction_Type":datum.reaction_type.name,
"Severity":datum.severity,
"Status":datum.status
}
output = []
data = self.ccda.data.allergies
for x in data:
output.append(return_allergy_element(x))
return output
def return_careplan(self):
def return_careplan_element(datum):
return {"Code":datum.code,
"Code_System":datum.code_system,
"Code_System_Name":datum.code_system_name,
"Name":datum.name,
"Text":datum.text
}
output = []
data = self.ccda.data.care_plan
for x in data:
output.append(return_careplan_element(x))
return output
def return_documents(self):
# def return_document_element(datum):
# return {"Code":datum.code,
# "Code_System":datum.code_system,
# "Code_System_Name":datum.code_system_name,
# "Name":datum.name,
# "Text":datum.text
# }
data = self.ccda.data.document
output = {"Author":build_author(data.author),
"Date":data.date,
"Documentation_Of":[],
"Title":data.title,
"Location":build_location(data.location)
}
for x in data.documentation_of:
output["Documentation_Of"].append({
"Address":build_address(x.address),
"Name":build_name(x.name),
"phone":build_phone(x.phone)
})
return output
def return_demographics(self):
def return_provider(datum):
return { "Start_Date":datum["start_date"],
"End_Date":datum["end_date"],
"Name":build_name(datum["name"]),
"Address":build_address(datum["address"]),
#"Organization":data.organization,
"Phone":build_phone(datum["phone"]),
"Email":datum["email"],
"Label":datum["label"],
#"Start_Date":
}
data = self.ccda.data.demographics
pcp = {"Name":build_name(data.pcp.name),
#"Suffix":data.pcp.suffix,
"Address":build_address(data.pcp.address),
"Phone":build_phone(data.pcp.phone),
"Label":data.pcp.label,
"Start":data.pcp.start_date,
"End":data.pcp.end_date,
"Email":data.pcp.email}
associated = {"Address":build_address(data.associated.address),
"Name":build_name(data.associated.name),
"Phone":build_phone(data.associated.phone),
"Email":data.associated.email,
"Relationship":data.associated.relationship,
"Relationship_Code":data.associated.relationship_code}
guardian = {"Address":build_address(data.guardian.address),
"Name":build_name(data.guardian.name),
"Phone":build_phone(data.guardian.phone),
"Email":data.guardian.email,
"Relationship":data.guardian.relationship,
"Relationship_Code":data.guardian.relationship_code}
lauth = {"Address":build_address(data.legal_authenticator["address"]),
"Name":build_name(data.legal_authenticator["name"]),
"Phone":build_phone(data.legal_authenticator["phone"]),
"Email":data.legal_authenticator["email"],
"Time":data.legal_authenticator["time"]}
providers = []
for prov in data.providers:
providers.append(return_provider(prov))
return {"Mrns":data.mrns,
"Address":build_address(data.address),
"Birthplace":build_address(data.birthplace),#data.birthplace,
"Dob":data.dob,
"Email":data.email,
"Ethnicity":data.ethnicity,
"Gender":data.gender,
"Associated":associated,
"Guardian":guardian,
"LanguageCode":data.languageCode,
"Language":data.language,
"Marital_Status":data.marital_status,
"Name":build_name(data.name),
"Phone":build_phone(data.phone),
"Providers":providers,
"Pcp":pcp,
"Race":data.race,
"Religion":data.religion,
"Legal_Authenticator":lauth,
}
def return_encounters(self):
def return_encounter_element(datum):
#findings = []
#for entry in findings:
# findings.append(entry)
return {"Code":datum.code,
"Code_System":datum.code_system,
"Code_System_Name":datum.code_system_name,
"Start Date":datum.start_date,
"End Date":datum.end_date,
"Findings":datum.findings,
"Location":build_location(datum.location),
"Name":datum.name,
"Performer":{"Name":build_name(datum.performer.name),
"Suffix":datum.performer.suffix,
"Organization":datum.performer.org,
"Address":build_address(datum.performer.address),
"Phone":datum.performer.phone,
},
"Translation":datum.translation.name,
"Translation Code":datum.translation.code
}
output = []
data = self.ccda.data.encounters
for x in data:
output.append(return_encounter_element(x))
return output
def return_functional_statuses(self):
"""
untested
"""
output = []
data = self.ccda.data.functional_statuses
for x in data:
output.append({"Date":build_date_range(x.date),
"Name":x.name})
return output
def return_immunizations(self):
"""
"""
def build_route(datum):
return {"Code":datum.code,
"Code_System":datum.code_system,
"Code_System_Name":datum.code_system_name,
"Name":build_name(datum.name)}
def build_translation(trans):
return {"Code":trans.code,
"Code_System":trans.code_system,
"Code_System_Name":trans.code_system_name,
"Name":build_name(trans.name)}
def build_product(datum):
return {"Code":datum.code,
"Code_System":datum.code_system,
"Code_System_Name":datum.code_system_name,
"Lot_Number":datum.lot_number,
"Manufacturer_Name":datum.manufacturer_name,
"Name":build_name(datum.name),
"Translation":build_translation(datum.translation)}
def build_education(datum):
return {"Code":datum.code,
"Code_System":datum.code_system,
"Name":build_name(datum.name)}
output = []
data = self.ccda.data.immunizations
for entry in data:
immunization = {"Date":build_date_range(entry.date),
"Dose_Quantity":build_qty(entry.dose_quantity),
"Education_Type":build_education(entry.education_type),
"Instructions":entry.instructions,
"product":build_product(entry.product),
"route":build_route(entry.route)}
output.append(immunization)
return output
def return_instructions(self):
"""
untested
"""
def build_instruction(datum):
return {"Code":datum.code,
"Code_System":datum.code_system,
"Name":build_name(datum.name),
"Text":datum.text}
output = []
data = self.ccda.data.instructions
for entry in data:
output.append(build_instruction(entry))
return output
def return_medications(self):
"""
untested
"""
def return_prescriber(datum):
return {"Organization":datum.organization,
"Person":datum.person,
"Provider":build_name(datum.provider),
#"Address":build_address(datum.address),
#"Phone":build_phone(datum.phone)
}
def return_product(datum):
return {"Name":datum.name,
"Text":datum.text,
"Translation":datum.translation.name}
def return_schedule(datum):
return {"Period_Unit":datum.period_unit,
"Period_Value":datum.period_value,
"Type":datum.type}
def return_medication_element(datum):
#print("QTY:",datum.dose_quantity.unit, datum.dose_quantity.value)
return {
"Administration":datum.administration.name,
"Status":datum.status,
"Date":build_date_range(datum.date_range),
"Dose_Quantity":build_qty(datum.dose_quantity),
"Precondition":datum.precondition.name,
"Prescriber":return_prescriber(datum.prescriber),
"Product":return_product(datum.product),
"Rate_Quantity":build_qty(datum.rate_quantity),
"Refills":datum.refills,
"Fill_Quantity":datum.fill_quantity,
"Fill_Unit":datum.fill_unit,
"Reason":datum.reason.name,
"Route":datum.route.name,
"Schedule":return_schedule(datum.schedule),
"Text":datum.text,
"Vehicle":build_name(datum.vehicle.name),
}
output = []
data = self.ccda.data.medications
for entry in data:
output.append(return_medication_element(entry))
return output
def return_problems(self):
output = []
data = self.ccda.data.problems
for entry in data:
output.append({"Age":entry.age,
"Code":entry.code,
"Code_System":entry.code_system,
"Code_System_Name":entry.code_system_name,
"Comment":entry.comment,
"Date":build_date_range(entry.date_range),
"Name":entry.name,
"Status":entry.status,
"Translation":entry.translation.name,
"Translation Code":entry.translation.code,
"Translation Code Name":entry.translation.code_system_name})
return output
def return_procedures(self):
"""
Please note speciman is *ALWAYS* None.
This in the library pyccda\parsers\_ccda\procedures.py
"""
def build_procedure(datum):
#print(datum.name, dir(datum.name))
performer = {"Name":build_name(datum.performer.name),
"Address":build_address(datum.performer),
"Organization":datum.performer.organization,
"Phone":build_phone(datum.performer.phone)}
return {"Code":datum.code,
"Start_Date":datum.start_date,
"End_Date":datum.end_date,#build_date_range(datum.date),
"Status":datum.status,
"Device":datum.device.name,
"Name":datum.name,
"Performer":performer,
"Specimen":datum.specimen.name}
output = []
data = self.ccda.data.procedures
for entry in data:
output.append(build_procedure(entry))
return output
def return_results(self):
def build_reference_range(ref_datum):
return {"High_Unit":ref_datum.high_unit,
"High_Value":ref_datum.high_value,
"Low_Unit":ref_datum.low_unit,
"Low_Value":ref_datum.low_value,
"Text":ref_datum.text,
}
def build_test(test_datum):
#
# Split name into the components?
#
#print(dir(test_datum.reference_range))
return {"Date":build_date_range(test_datum.date),
"Name":test_datum.name,
"Status":test_datum.status,
"Provider":build_name(test_datum.provider),
"Reference_Range":build_reference_range(test_datum.reference_range),
"Translation":test_datum.translation.name,
"Unit":test_datum.unit,
"Value":str(test_datum.value).strip()}
def build_results(datum):
datum_out = []
for x in datum.tests:
#print(dir(x))
datum_out.append(build_test(x))
return {"Name":datum.name,
"Tests":datum_out}
output = []
data = self.ccda.data.results
for entry in data:
output.append(build_results(entry))
return output
def return_smoking(self):
data = self.ccda.data.smoking_status
print(data)
return data.name
def return_vitals(self):
output = {}
data = self.ccda.data.vitals
for entry in data:
for result in entry.results:
if result.name == None and result.unit == None and result.value in [0, None, ""]:
continue
if result.date not in output:
output[result.date] = {}
if len(result.date) == 14:
date = datetime.strptime(result.date, "%Y%m%d%H%M%S")
else:
date = datetime.strptime(result.date, "%Y%m%d")
output[result.date][result.name] = {"Name:":result.name,
"Unit":result.unit,
"Value":result.value,
"Date":date
}
return output
def return_social_history(self):
shistory = self.ccda.data.social_history
return shistory
def convert_demographics(ddata):
#
# Need to flatten the data
#
pass
document = ccda_document()
test_path = None
filenames = None
for fname in filenames:
if len(sys.argv) > 1:
test_path = ""
filenames = sys.argv[1:]
for fname in filenames:
test_file = os.path.join(test_path, fname)
document.read_ccda(test_file)
demographics = document.return_demographics()
print()
print(demographics["Name"])
#pprint(document.return_demographics())
# pprint(document.return_social_history())
# pprint(document.return_documents()) # CCD Title
# pprint(document.return_encounters()[0:5])
# pprint(document.return_functional_statuses())
#pprint(document.return_immunizations())
#print(document.return_instructions())
# pprint(document.return_medications()[0:5])
# pprint(document.return_problems())
pprint(document.return_procedures()[0:3])
#pprint(document.return_allergies())
#print (document.return_careplan())
# pprint(document.return_results()[0:5]) #???
#pprint(document.return_smoking())
#pprint(document.return_vitals())
# pprint(document.return_social_history())
#pprint(output)
print("-"*30)