|
a |
|
b/anonymizer.py |
|
|
1 |
import csv |
|
|
2 |
import json |
|
|
3 |
import os |
|
|
4 |
import re |
|
|
5 |
|
|
|
6 |
path = '/home/josh/code/healthanon/CRT MGH 950 cohort/' |
|
|
7 |
file_template = 'cl491_092315161707361168_' |
|
|
8 |
|
|
|
9 |
def anonymize_doc(patient, doc): |
|
|
10 |
replacements = [] |
|
|
11 |
replacements.append((patient['First_Name'], 'First_Name')) |
|
|
12 |
replacements.append((patient['Last_Name'], 'Last_Name')) |
|
|
13 |
replacements.append((patient['Last_Name'], 'Last_Name')) |
|
|
14 |
replacements.append((patient['EMPI'], patient['NEW_EMPI'])) |
|
|
15 |
for mrn in patient['MRNS']: |
|
|
16 |
replacements.append((mrn, 'MRN')) |
|
|
17 |
|
|
|
18 |
for (regex, sub) in replacements: |
|
|
19 |
pattern = re.compile(regex, re.IGNORECASE) |
|
|
20 |
doc = re.sub(pattern, sub, doc) |
|
|
21 |
|
|
|
22 |
return doc |
|
|
23 |
|
|
|
24 |
|
|
|
25 |
def get_docs_empi_map(doc_type): |
|
|
26 |
doc_file = path + file_template + doc_type + '.txt' |
|
|
27 |
with open(doc_file, 'r') as f: |
|
|
28 |
doc_text = f.read() |
|
|
29 |
doc_array = doc_text.split('[report_end]') |
|
|
30 |
|
|
|
31 |
doc_map = {} |
|
|
32 |
for doc in doc_array: |
|
|
33 |
empi = doc.split('\n')[1].split('|')[0] |
|
|
34 |
if empi not in doc_map: |
|
|
35 |
doc_map[empi] = [] |
|
|
36 |
doc_map[empi].append(doc) |
|
|
37 |
|
|
|
38 |
return doc_map |
|
|
39 |
|
|
|
40 |
def get_struct_empi_map(doc_type): |
|
|
41 |
doc_file = path + file_template + doc_type + '.txt' |
|
|
42 |
with open(doc_file, 'r') as f: |
|
|
43 |
doc_text = f.read() |
|
|
44 |
doc_array = doc_text.split('\n') |
|
|
45 |
|
|
|
46 |
doc_map = {} |
|
|
47 |
keys = doc_array[0].split('|') |
|
|
48 |
for row in doc_array[1:]: |
|
|
49 |
cols = row.split('|') |
|
|
50 |
empi = cols[0] |
|
|
51 |
record = {} |
|
|
52 |
try: |
|
|
53 |
for i in range(3, len(keys)): |
|
|
54 |
key = keys[i] |
|
|
55 |
record[key] = cols[i] |
|
|
56 |
except: |
|
|
57 |
print 'Error for type:' + doc_type + ' column:' + str(i) + ' row:' + row |
|
|
58 |
|
|
|
59 |
if empi not in doc_map: |
|
|
60 |
doc_map[empi] = [] |
|
|
61 |
doc_map[empi].append(record) |
|
|
62 |
|
|
|
63 |
return doc_map |
|
|
64 |
|
|
|
65 |
def get_patient_dem_info(): |
|
|
66 |
# Get demographic text |
|
|
67 |
dem_file = path + file_template + 'Dem.txt' |
|
|
68 |
with open(dem_file, 'r') as f: |
|
|
69 |
dem_text = f.read() |
|
|
70 |
dem_array = dem_text.split('\n') |
|
|
71 |
|
|
|
72 |
# Get contact text |
|
|
73 |
contact_file = path + file_template + 'Con.txt' |
|
|
74 |
with open(contact_file, 'r') as f: |
|
|
75 |
contact_text = f.read() |
|
|
76 |
contact_array = contact_text.split('\n') |
|
|
77 |
|
|
|
78 |
# Build dictionary |
|
|
79 |
patients = {} |
|
|
80 |
for (i, row) in enumerate(contact_array[1:]): |
|
|
81 |
cols = row.strip().split('|') |
|
|
82 |
patient = {} |
|
|
83 |
try: |
|
|
84 |
patient['NEW_EMPI'] = 'FAKE_EMPI_' + str(i) |
|
|
85 |
patient['EMPI'] = cols[0].strip() |
|
|
86 |
patient['MRNS'] = map(lambda x: x.strip(), cols[2].strip().split(',')) |
|
|
87 |
patient['Last_Name'] = cols[3].strip() |
|
|
88 |
patient['First_Name'] = cols[4].strip() |
|
|
89 |
patient['Middle_Name'] = cols[5].strip() |
|
|
90 |
|
|
|
91 |
# store in dict |
|
|
92 |
patients[patient['EMPI']] = patient |
|
|
93 |
except: |
|
|
94 |
print('Error in contact dict build for row:') |
|
|
95 |
print row |
|
|
96 |
|
|
|
97 |
for row in dem_array[1:]: |
|
|
98 |
cols = row.strip().split('|') |
|
|
99 |
try: |
|
|
100 |
empi = cols[0].strip() |
|
|
101 |
patient = patients[empi] |
|
|
102 |
|
|
|
103 |
patient['Vital_status'] = cols[13].strip() |
|
|
104 |
patient['Date_Of_Death'] = cols[14].strip() |
|
|
105 |
except: |
|
|
106 |
print('Error in dem build for row:') |
|
|
107 |
print row |
|
|
108 |
|
|
|
109 |
return patients |
|
|
110 |
|
|
|
111 |
def get_mrn_to_empi(patients): |
|
|
112 |
mrn_empi = {} |
|
|
113 |
for empi in patients.keys(): |
|
|
114 |
patient = patients[empi] |
|
|
115 |
for mrn in patient['MRNS']: |
|
|
116 |
mrn_empi[mrn] = empi |
|
|
117 |
return mrn_empi |
|
|
118 |
|
|
|
119 |
|
|
|
120 |
def parse_procedure_date_file(patients): |
|
|
121 |
proc_file = path + 'additional data/PATIENT_LIST_with_appointment_dates.csv' |
|
|
122 |
|
|
|
123 |
mrn_to_empi = get_mrn_to_empi(patients) |
|
|
124 |
patients_procs = {} |
|
|
125 |
with open(proc_file, 'r') as f: |
|
|
126 |
reader = csv.DictReader(f) |
|
|
127 |
for row in reader: |
|
|
128 |
mrn = row['MR#'].strip().replace('-','') |
|
|
129 |
if mrn != '': |
|
|
130 |
try: |
|
|
131 |
empi = mrn_to_empi[mrn] |
|
|
132 |
patient = patients[empi] |
|
|
133 |
|
|
|
134 |
del row['Name'] |
|
|
135 |
del row['MR#'] |
|
|
136 |
patients[empi]['Procedure'] = row |
|
|
137 |
except KeyError: |
|
|
138 |
print "Error parsing procedure date for MRN:" + mrn |
|
|
139 |
|
|
|
140 |
write_null_if_empty(patients, 'Procedure') |
|
|
141 |
|
|
|
142 |
|
|
|
143 |
def write_null_if_empty(patients, key): |
|
|
144 |
for empi in patients.keys(): |
|
|
145 |
if key not in patients[empi]: |
|
|
146 |
patients[empi][key] = None |
|
|
147 |
|
|
|
148 |
|
|
|
149 |
def parse_response_file(patients): |
|
|
150 |
resp_file = path + 'additional data/9 24 14 CRT Database for Charlotta.csv' |
|
|
151 |
|
|
|
152 |
mrn_to_empi = get_mrn_to_empi(patients) |
|
|
153 |
patients_procs = {} |
|
|
154 |
with open(resp_file, 'r') as f: |
|
|
155 |
reader = csv.DictReader(f) |
|
|
156 |
for row in reader: |
|
|
157 |
mrn = row['Patient_ID'].strip() |
|
|
158 |
if mrn != '': |
|
|
159 |
try: |
|
|
160 |
empi = mrn_to_empi[mrn] |
|
|
161 |
patient = patients[empi] |
|
|
162 |
|
|
|
163 |
del row['First_Name'] |
|
|
164 |
del row['Last_Name'] |
|
|
165 |
del row['Patient_ID'] |
|
|
166 |
del row['DOB'] |
|
|
167 |
patients[empi]['Supplemental'] = row |
|
|
168 |
except KeyError: |
|
|
169 |
print "Error parsing supplemental data for MRN:" + mrn |
|
|
170 |
|
|
|
171 |
write_null_if_empty(patients, 'Supplemental') |
|
|
172 |
|
|
|
173 |
|
|
|
174 |
if __name__ == "__main__": |
|
|
175 |
# Do the anonymization! |
|
|
176 |
|
|
|
177 |
patients = get_patient_dem_info() |
|
|
178 |
parse_procedure_date_file(patients) |
|
|
179 |
parse_response_file(patients) |
|
|
180 |
|
|
|
181 |
report_types = ['Car', 'Dis', 'End', 'Lno', 'Mic', 'Opn', 'Pat', 'Pul', 'Rad'] |
|
|
182 |
for report_type in report_types: |
|
|
183 |
report_map = get_docs_empi_map(report_type) |
|
|
184 |
for key in patients.keys(): |
|
|
185 |
try: |
|
|
186 |
docs = report_map[key] |
|
|
187 |
for doc in docs: |
|
|
188 |
if report_type not in patients[key]: |
|
|
189 |
patients[key][report_type] = [] |
|
|
190 |
patients[key][report_type].append(anonymize_doc(patients[key], doc)) |
|
|
191 |
except: |
|
|
192 |
patients[key][report_type] = [] |
|
|
193 |
print('Patient with empi:' + key + ' doesnt have report for report_type:' + report_type) |
|
|
194 |
|
|
|
195 |
struct_reports = ['Dia', 'Enc', 'Lab', 'Lhm', 'Lme', 'Lpr', 'Lvs', 'Med', 'Phy', 'Prc', 'Prv', 'Rdt', 'Rnd', 'Trn'] |
|
|
196 |
for report_type in struct_reports: |
|
|
197 |
report_map = get_struct_empi_map(report_type) |
|
|
198 |
for key in patients.keys(): |
|
|
199 |
try: |
|
|
200 |
patients[key][report_type] = report_map[key] |
|
|
201 |
except: |
|
|
202 |
patients[key][report_type] = [] |
|
|
203 |
print('Patient with empi:' + key + ' doesnt have report for report_type:' + report_type) |
|
|
204 |
|
|
|
205 |
# Final delete of info |
|
|
206 |
anon_patients = {} |
|
|
207 |
old_mapping = {} |
|
|
208 |
for key in patients.keys(): |
|
|
209 |
patient = patients[key] |
|
|
210 |
old_mapping[patient['EMPI']] = patient['NEW_EMPI'] |
|
|
211 |
del patient['EMPI'] |
|
|
212 |
del patient['MRNS'] |
|
|
213 |
del patient['Last_Name'] |
|
|
214 |
del patient['First_Name'] |
|
|
215 |
del patient['Middle_Name'] |
|
|
216 |
anon_patients[patient['NEW_EMPI']] = patient |
|
|
217 |
|
|
|
218 |
out_path = '/home/josh/code/healthanon/data/' |
|
|
219 |
if not os.path.exists(out_path): |
|
|
220 |
os.makedirs(out_path) |
|
|
221 |
|
|
|
222 |
with open(out_path + 'mapping.csv', 'w') as f: |
|
|
223 |
for key in old_mapping.keys(): |
|
|
224 |
f.write(key + ',' + old_mapping[key] + '\n') |
|
|
225 |
|
|
|
226 |
for key in anon_patients.keys(): |
|
|
227 |
with open(out_path + key + '.json','w') as f: |
|
|
228 |
json.dump(anon_patients[key], f) |