[8d2107]: / rpdrToJSON.py

Download this file

190 lines (160 with data), 6.9 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
import csv
import json
import os
path1 = '/PHShome/ju601/crt/data/patients_10_2015/'
path2 = '/PHShome/ju601/crt/data/added_patients_4_2016/'
file_template1 = 'cl491_092315161707361168_'
file_template2 = 'cl491_04071618244390383_'
out_path = '/PHShome/ju601/crt/data/json/'
procedure_file = '/PHShome/ju601/crt/data/procedure_dates_updated.csv'
def get_docs_empi_map(doc_type, path, file_template):
doc_file = path + file_template + doc_type + '.txt'
with open(doc_file, 'r') as f:
doc_text = f.read()
doc_array = doc_text.split('[report_end]')
# Get structured data fields from each report
doc0_split = doc_array[0].split('\r\n')
keys = doc0_split[0].split('|')
doc_array[0] = '\r\n'.join(doc0_split[1:])
doc_map = {}
for doc_text in doc_array:
doc = {}
structured = doc_text.split('\r\n')[1].split('|')
for (i, val) in enumerate(structured):
doc[keys[i]] = val
doc['free_text'] = doc_text
empi = structured[0]
if empi not in doc_map:
doc_map[empi] = []
doc_map[empi].append(doc)
return doc_map
def get_struct_empi_map(doc_type, path, file_template):
doc_file = path + file_template + doc_type + '.txt'
with open(doc_file, 'r') as f:
doc_text = f.read()
doc_array = doc_text.split('\r\n')
doc_map = {}
keys = doc_array[0].split('|')
for row in doc_array[1:]:
cols = row.split('|')
empi = cols[0]
record = {}
try:
for i in range(3, len(keys)):
key = keys[i]
record[key] = cols[i]
except:
print 'Error for type:' + doc_type + ' column:' + str(i) + ' row:' + row
if empi not in doc_map:
doc_map[empi] = []
doc_map[empi].append(record)
return doc_map
def get_patient_dem_info(dem_file, patients = {}):
# Get demographic text
with open(dem_file, 'r') as f:
dem_text = f.read()
dem_array = dem_text.split('\r\n')
# Build dictionary
key_arr = dem_array[0].split('|')
for row in dem_array[1:]:
cols = row.strip().split('|')
if len(cols) == len(key_arr):
patient = {}
for (i, col) in enumerate(cols):
try:
if i == 2:
patient['MRNS'] = map(lambda x: x.strip(), col.strip().split(','))
else:
patient[key_arr[i]] = col
except:
print('Error in contact dict build for row:')
print row
if patient['EMPI'] in patients:
print "DEMFILE- Duplicate patient: " + patient['EMPI']
else:
patients[patient['EMPI']] = patient
return patients
def get_mrn_to_empi(patients):
mrn_empi = {}
for empi in patients.keys():
patient = patients[empi]
for mrn in patient['MRNS']:
mrn_empi[mrn] = empi
return mrn_empi
def parse_procedure_date_file(proc_file, patients):
mrn_to_empi = get_mrn_to_empi(patients)
patients_procs = {}
with open(proc_file, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
mrn = row['MRN']
if mrn != '':
try:
empi = mrn_to_empi[mrn]
patient = patients[empi]
proc = {}
proc['Implant Status'] = row['ImplantDate']
proc['1 Mo. Appt'] = ''
proc['3 Mo. Appt'] = ''
patient['Procedure'] = proc
except KeyError:
print "Error parsing procedure date for MRN:" + mrn
write_null_if_empty(patients, 'Procedure')
def add_reports(patients, report_types, is_structured, paths):
if is_structured:
report_id_key = 'Encounter_number'
else:
report_id_key = 'Report_Number'
for report_type in report_types:
patients_no_report = set(patients.keys())
for (path, file_template) in paths:
if is_structured:
report_map = get_struct_empi_map(report_type, path, file_template)
else:
report_map = get_docs_empi_map(report_type, path, file_template)
for patient in patients.keys():
# Patient does not have any of these reports yet
if report_type not in patients[patient]:
if patient in report_map and report_map[patient] != []:
patients[patient][report_type] = report_map[patient]
patients_no_report.remove(patient)
# Patient already has some of these reports:
else:
if patient in report_map:
for report in report_map[patient]:
# Only add unique reports
if report not in patients[patient][report_type]:
patients[patient][report_type].append(report)
for patient in patients_no_report:
patients[patient][report_type] = []
print "Patients without report: " + report_type
print patients_no_report
def write_null_if_empty(patients, key):
for empi in patients.keys():
if key not in patients[empi]:
patients[empi][key] = None
print "No " + key + " for patient: " + str(empi)
if __name__ == "__main__":
paths = [(path1, file_template1), (path2, file_template2)]
# Do the conversion!
patients = get_patient_dem_info(path1 + file_template1 + 'Dem.txt')
patients = get_patient_dem_info(path2 + file_template2 + 'Dem.txt', patients)
parse_procedure_date_file(procedure_file, patients)
# All unstructured report types:
# report_types = ['Car', 'Dis', 'End', 'Lno', 'Mic', 'Opn', 'Pat', 'Pul', 'Rad']
# Only include the ones you anticipate using in the following array:
report_types = ['Car', 'Dis', 'End', 'Lno', 'Mic', 'Opn', 'Pat', 'Pul', 'Rad']
report_types = ['Car', 'Dis', 'Lno', 'Rad']
add_reports(patients, report_types, False, paths)
# All structured report types:
# struct_reports = ['Dia', 'Enc', 'Lab', 'Lhm', 'Lme', 'Lpr', 'Lvs', 'Med', 'Mrn', 'Phy', 'Prc', 'Prv', 'Rdt', 'Rnd', 'Trn']
# Only include the ones you anticipate using in the following array:
struct_reports = ['Dia', 'Enc', 'Lab', 'Lhm', 'Lme', 'Lpr', 'Lvs', 'Med', 'Mrn', 'Phy', 'Prc', 'Prv', 'Rdt', 'Trn']
struct_reports = ['Dia', 'Enc', 'Lab', 'Med']
add_reports(patients, struct_reports, True, paths)
# Final write of info
if not os.path.exists(out_path):
os.makedirs(out_path)
for key in patients.keys():
with open(out_path + key + '.json','w') as f:
json.dump(patients[key], f)