|
a |
|
b/rpdrToJSON.py |
|
|
1 |
import csv |
|
|
2 |
import json |
|
|
3 |
import os |
|
|
4 |
|
|
|
5 |
path1 = '/PHShome/ju601/crt/data/patients_10_2015/' |
|
|
6 |
path2 = '/PHShome/ju601/crt/data/added_patients_4_2016/' |
|
|
7 |
file_template1 = 'cl491_092315161707361168_' |
|
|
8 |
file_template2 = 'cl491_04071618244390383_' |
|
|
9 |
out_path = '/PHShome/ju601/crt/data/json/' |
|
|
10 |
procedure_file = '/PHShome/ju601/crt/data/procedure_dates_updated.csv' |
|
|
11 |
|
|
|
12 |
def get_docs_empi_map(doc_type, path, file_template): |
|
|
13 |
doc_file = path + file_template + doc_type + '.txt' |
|
|
14 |
with open(doc_file, 'r') as f: |
|
|
15 |
doc_text = f.read() |
|
|
16 |
doc_array = doc_text.split('[report_end]') |
|
|
17 |
|
|
|
18 |
# Get structured data fields from each report |
|
|
19 |
doc0_split = doc_array[0].split('\r\n') |
|
|
20 |
keys = doc0_split[0].split('|') |
|
|
21 |
doc_array[0] = '\r\n'.join(doc0_split[1:]) |
|
|
22 |
|
|
|
23 |
doc_map = {} |
|
|
24 |
for doc_text in doc_array: |
|
|
25 |
doc = {} |
|
|
26 |
structured = doc_text.split('\r\n')[1].split('|') |
|
|
27 |
for (i, val) in enumerate(structured): |
|
|
28 |
doc[keys[i]] = val |
|
|
29 |
doc['free_text'] = doc_text |
|
|
30 |
empi = structured[0] |
|
|
31 |
if empi not in doc_map: |
|
|
32 |
doc_map[empi] = [] |
|
|
33 |
doc_map[empi].append(doc) |
|
|
34 |
|
|
|
35 |
return doc_map |
|
|
36 |
|
|
|
37 |
def get_struct_empi_map(doc_type, path, file_template): |
|
|
38 |
doc_file = path + file_template + doc_type + '.txt' |
|
|
39 |
with open(doc_file, 'r') as f: |
|
|
40 |
doc_text = f.read() |
|
|
41 |
doc_array = doc_text.split('\r\n') |
|
|
42 |
|
|
|
43 |
doc_map = {} |
|
|
44 |
keys = doc_array[0].split('|') |
|
|
45 |
for row in doc_array[1:]: |
|
|
46 |
cols = row.split('|') |
|
|
47 |
empi = cols[0] |
|
|
48 |
record = {} |
|
|
49 |
try: |
|
|
50 |
for i in range(3, len(keys)): |
|
|
51 |
key = keys[i] |
|
|
52 |
record[key] = cols[i] |
|
|
53 |
except: |
|
|
54 |
print 'Error for type:' + doc_type + ' column:' + str(i) + ' row:' + row |
|
|
55 |
|
|
|
56 |
if empi not in doc_map: |
|
|
57 |
doc_map[empi] = [] |
|
|
58 |
doc_map[empi].append(record) |
|
|
59 |
|
|
|
60 |
return doc_map |
|
|
61 |
|
|
|
62 |
def get_patient_dem_info(dem_file, patients = {}): |
|
|
63 |
# Get demographic text |
|
|
64 |
with open(dem_file, 'r') as f: |
|
|
65 |
dem_text = f.read() |
|
|
66 |
dem_array = dem_text.split('\r\n') |
|
|
67 |
|
|
|
68 |
# Build dictionary |
|
|
69 |
key_arr = dem_array[0].split('|') |
|
|
70 |
for row in dem_array[1:]: |
|
|
71 |
cols = row.strip().split('|') |
|
|
72 |
if len(cols) == len(key_arr): |
|
|
73 |
patient = {} |
|
|
74 |
for (i, col) in enumerate(cols): |
|
|
75 |
try: |
|
|
76 |
if i == 2: |
|
|
77 |
patient['MRNS'] = map(lambda x: x.strip(), col.strip().split(',')) |
|
|
78 |
else: |
|
|
79 |
patient[key_arr[i]] = col |
|
|
80 |
except: |
|
|
81 |
print('Error in contact dict build for row:') |
|
|
82 |
print row |
|
|
83 |
|
|
|
84 |
if patient['EMPI'] in patients: |
|
|
85 |
print "DEMFILE- Duplicate patient: " + patient['EMPI'] |
|
|
86 |
else: |
|
|
87 |
patients[patient['EMPI']] = patient |
|
|
88 |
|
|
|
89 |
return patients |
|
|
90 |
|
|
|
91 |
def get_mrn_to_empi(patients): |
|
|
92 |
mrn_empi = {} |
|
|
93 |
for empi in patients.keys(): |
|
|
94 |
patient = patients[empi] |
|
|
95 |
for mrn in patient['MRNS']: |
|
|
96 |
mrn_empi[mrn] = empi |
|
|
97 |
return mrn_empi |
|
|
98 |
|
|
|
99 |
def parse_procedure_date_file(proc_file, patients): |
|
|
100 |
mrn_to_empi = get_mrn_to_empi(patients) |
|
|
101 |
patients_procs = {} |
|
|
102 |
with open(proc_file, 'r') as f: |
|
|
103 |
reader = csv.DictReader(f) |
|
|
104 |
for row in reader: |
|
|
105 |
mrn = row['MRN'] |
|
|
106 |
if mrn != '': |
|
|
107 |
try: |
|
|
108 |
empi = mrn_to_empi[mrn] |
|
|
109 |
patient = patients[empi] |
|
|
110 |
proc = {} |
|
|
111 |
proc['Implant Status'] = row['ImplantDate'] |
|
|
112 |
proc['1 Mo. Appt'] = '' |
|
|
113 |
proc['3 Mo. Appt'] = '' |
|
|
114 |
patient['Procedure'] = proc |
|
|
115 |
except KeyError: |
|
|
116 |
print "Error parsing procedure date for MRN:" + mrn |
|
|
117 |
|
|
|
118 |
write_null_if_empty(patients, 'Procedure') |
|
|
119 |
|
|
|
120 |
def add_reports(patients, report_types, is_structured, paths): |
|
|
121 |
if is_structured: |
|
|
122 |
report_id_key = 'Encounter_number' |
|
|
123 |
else: |
|
|
124 |
report_id_key = 'Report_Number' |
|
|
125 |
|
|
|
126 |
for report_type in report_types: |
|
|
127 |
patients_no_report = set(patients.keys()) |
|
|
128 |
for (path, file_template) in paths: |
|
|
129 |
if is_structured: |
|
|
130 |
report_map = get_struct_empi_map(report_type, path, file_template) |
|
|
131 |
else: |
|
|
132 |
report_map = get_docs_empi_map(report_type, path, file_template) |
|
|
133 |
for patient in patients.keys(): |
|
|
134 |
# Patient does not have any of these reports yet |
|
|
135 |
if report_type not in patients[patient]: |
|
|
136 |
if patient in report_map and report_map[patient] != []: |
|
|
137 |
patients[patient][report_type] = report_map[patient] |
|
|
138 |
patients_no_report.remove(patient) |
|
|
139 |
|
|
|
140 |
# Patient already has some of these reports: |
|
|
141 |
else: |
|
|
142 |
if patient in report_map: |
|
|
143 |
for report in report_map[patient]: |
|
|
144 |
# Only add unique reports |
|
|
145 |
if report not in patients[patient][report_type]: |
|
|
146 |
patients[patient][report_type].append(report) |
|
|
147 |
|
|
|
148 |
for patient in patients_no_report: |
|
|
149 |
patients[patient][report_type] = [] |
|
|
150 |
print "Patients without report: " + report_type |
|
|
151 |
print patients_no_report |
|
|
152 |
|
|
|
153 |
|
|
|
154 |
def write_null_if_empty(patients, key): |
|
|
155 |
for empi in patients.keys(): |
|
|
156 |
if key not in patients[empi]: |
|
|
157 |
patients[empi][key] = None |
|
|
158 |
print "No " + key + " for patient: " + str(empi) |
|
|
159 |
|
|
|
160 |
|
|
|
161 |
if __name__ == "__main__": |
|
|
162 |
paths = [(path1, file_template1), (path2, file_template2)] |
|
|
163 |
|
|
|
164 |
# Do the conversion! |
|
|
165 |
patients = get_patient_dem_info(path1 + file_template1 + 'Dem.txt') |
|
|
166 |
patients = get_patient_dem_info(path2 + file_template2 + 'Dem.txt', patients) |
|
|
167 |
parse_procedure_date_file(procedure_file, patients) |
|
|
168 |
|
|
|
169 |
# All unstructured report types: |
|
|
170 |
# report_types = ['Car', 'Dis', 'End', 'Lno', 'Mic', 'Opn', 'Pat', 'Pul', 'Rad'] |
|
|
171 |
# Only include the ones you anticipate using in the following array: |
|
|
172 |
report_types = ['Car', 'Dis', 'End', 'Lno', 'Mic', 'Opn', 'Pat', 'Pul', 'Rad'] |
|
|
173 |
report_types = ['Car', 'Dis', 'Lno', 'Rad'] |
|
|
174 |
add_reports(patients, report_types, False, paths) |
|
|
175 |
|
|
|
176 |
# All structured report types: |
|
|
177 |
# struct_reports = ['Dia', 'Enc', 'Lab', 'Lhm', 'Lme', 'Lpr', 'Lvs', 'Med', 'Mrn', 'Phy', 'Prc', 'Prv', 'Rdt', 'Rnd', 'Trn'] |
|
|
178 |
# Only include the ones you anticipate using in the following array: |
|
|
179 |
struct_reports = ['Dia', 'Enc', 'Lab', 'Lhm', 'Lme', 'Lpr', 'Lvs', 'Med', 'Mrn', 'Phy', 'Prc', 'Prv', 'Rdt', 'Trn'] |
|
|
180 |
struct_reports = ['Dia', 'Enc', 'Lab', 'Med'] |
|
|
181 |
add_reports(patients, struct_reports, True, paths) |
|
|
182 |
|
|
|
183 |
# Final write of info |
|
|
184 |
if not os.path.exists(out_path): |
|
|
185 |
os.makedirs(out_path) |
|
|
186 |
|
|
|
187 |
for key in patients.keys(): |
|
|
188 |
with open(out_path + key + '.json','w') as f: |
|
|
189 |
json.dump(patients[key], f) |