[8d2107]: / rpdrToJSONPal.py

Download this file

153 lines (125 with data), 4.8 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
import json
import os
path = '/PHShome/ju601/crt/MGHpallcare/'
file_template = 'CL491_070915120556917455_MGH_'
out_path = path + '/json/'
def get_docs_empi_map(doc_type):
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]')
doc_map = {}
for doc in doc_array:
empi = doc.split('\n')[1].split('|')[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):
doc_file = path + file_template + doc_type + '.txt'
with open(doc_file, 'r') as f:
doc_text = f.read()
doc_array = doc_text.split('\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():
# Get demographic text
dem_file = path + file_template + 'Dem.txt'
with open(dem_file, 'r') as f:
dem_text = f.read()
dem_array = dem_text.split('\n')
# Build dictionary
patients = {}
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
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_consultation_date_file(patients):
date_file = path + 'Date.txt'
mrn_to_empi = get_mrn_to_empi(patients)
patients_dates = {}
with open(date_file, 'r') as f:
date_arr = f.read().split('\n')
for row in date_arr:
row_arr = row.split(' ')
if len(row_arr) == 2:
(mrn, date) = row_arr
try:
empi = mrn_to_empi[mrn]
print empi
patient = patients[empi]
patient['Consult_Date'] = date
except KeyError:
print "Error parsing consult date for MRN:" + mrn
write_null_if_empty(patients, 'Consult_Date')
def write_null_if_empty(patients, key):
for empi in patients.keys():
if key not in patients[empi]:
patients[empi][key] = None
if __name__ == "__main__":
# Do the conversion!
patients = get_patient_dem_info()
parse_consultation_date_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 = []
for report_type in report_types:
report_map = get_docs_empi_map(report_type)
for patient in patients.keys():
try:
patients[patient][report_type] = report_map[patient]
except:
patients[patient][report_type] = []
print('Patient with empi:' + patient + ' doesnt have report for report_type:' + report_type)
# All structured report types:
# struct_reports = ['Dia', 'Enc', 'Lab', 'Lhm', 'Lme', 'Lpr', 'Lvs', 'Med', 'Phy', 'Prc', 'Prv', 'Rdt', 'Rnd', 'Trn']
# Only include the ones you anticipate using in the following array:
struct_reports = ['Dia', 'Enc']
for report_type in struct_reports:
report_map = get_struct_empi_map(report_type)
for patient in patients.keys():
try:
patients[patient][report_type] = report_map[patient]
except:
patients[patient][report_type] = []
print('Patient with empi:' + patient + ' doesnt have report for report_type:' + report_type)
# 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)