Diff of /rpdrToJSONPal.py [000000] .. [8d2107]

Switch to unified view

a b/rpdrToJSONPal.py
1
import json
2
import os 
3
4
path = '/PHShome/ju601/crt/MGHpallcare/'
5
file_template = 'CL491_070915120556917455_MGH_'
6
out_path = path + '/json/'
7
8
def get_docs_empi_map(doc_type):
9
    doc_file = path + file_template + doc_type + '.txt'
10
    with open(doc_file, 'r') as f:
11
        doc_text = f.read()
12
        doc_array = doc_text.split('[report_end]')
13
    
14
    doc_map = {}
15
    for doc in doc_array:
16
        empi = doc.split('\n')[1].split('|')[0]
17
        if empi not in doc_map:
18
            doc_map[empi] = []
19
        doc_map[empi].append(doc)
20
21
    return doc_map
22
23
def get_struct_empi_map(doc_type):
24
    doc_file = path + file_template + doc_type + '.txt'
25
    with open(doc_file, 'r') as f:
26
        doc_text = f.read()
27
        doc_array = doc_text.split('\n')
28
    
29
    doc_map = {}
30
    keys = doc_array[0].split('|')
31
    for row in doc_array[1:]:
32
        cols = row.split('|')
33
        empi = cols[0]
34
        record = {}
35
        try:
36
            for i in range(3, len(keys)):
37
                key = keys[i]
38
                record[key] = cols[i]
39
        except:
40
            print 'Error for type:' + doc_type + ' column:' + str(i) + ' row:' + row
41
42
        if empi not in doc_map:
43
            doc_map[empi] = []
44
        doc_map[empi].append(record)
45
46
    return doc_map
47
48
def get_patient_dem_info():
49
    # Get demographic text
50
    dem_file = path + file_template + 'Dem.txt'
51
    with open(dem_file, 'r') as f:
52
        dem_text = f.read()
53
        dem_array = dem_text.split('\n')
54
55
    # Build dictionary
56
    patients = {}
57
    key_arr = dem_array[0].split('|')
58
    for row in dem_array[1:]:
59
        cols = row.strip().split('|')
60
        if len(cols) == len(key_arr):
61
            patient = {}
62
            for (i, col) in enumerate(cols):
63
                try:
64
                    if i == 2:
65
                        patient['MRNS'] = map(lambda x: x.strip(), col.strip().split(','))
66
                    else:
67
                        patient[key_arr[i]] = col
68
                except:
69
                    print('Error in contact dict build for row:')
70
                    print row
71
72
            patients[patient['EMPI']] = patient
73
74
    return patients
75
76
def get_mrn_to_empi(patients):
77
    mrn_empi = {}
78
    for empi in patients.keys():
79
        patient = patients[empi]
80
        for mrn in patient['MRNS']:
81
            mrn_empi[mrn] = empi
82
    return mrn_empi
83
84
85
def parse_consultation_date_file(patients):
86
    date_file = path + 'Date.txt'
87
88
    mrn_to_empi = get_mrn_to_empi(patients)
89
    patients_dates = {}
90
    with open(date_file, 'r') as f:
91
        date_arr = f.read().split('\n')
92
        for row in date_arr:
93
            row_arr = row.split(' ')
94
            if len(row_arr) == 2:
95
                (mrn, date) = row_arr
96
                try:
97
                    empi = mrn_to_empi[mrn]
98
                    print empi
99
                    patient = patients[empi]
100
                    patient['Consult_Date'] = date
101
                except KeyError:
102
                    print "Error parsing consult date for MRN:" + mrn
103
104
    write_null_if_empty(patients, 'Consult_Date')
105
106
107
def write_null_if_empty(patients, key):
108
    for empi in patients.keys():
109
        if key not in patients[empi]:
110
            patients[empi][key] = None
111
            
112
113
if __name__ == "__main__":
114
    # Do the conversion!
115
116
    patients = get_patient_dem_info()
117
    parse_consultation_date_file(patients)
118
119
    # All unstructured report types:
120
    # report_types = ['Car', 'Dis', 'End', 'Lno', 'Mic', 'Opn', 'Pat', 'Pul', 'Rad']
121
    # Only include the ones you anticipate using in the following array:
122
    report_types = []
123
    for report_type in report_types:
124
        report_map = get_docs_empi_map(report_type)
125
        for patient in patients.keys():
126
            try:
127
                patients[patient][report_type] = report_map[patient]
128
            except:
129
                patients[patient][report_type] = []
130
                print('Patient with empi:' + patient + ' doesnt have report for report_type:' + report_type)
131
132
133
    # All structured report types:
134
    # struct_reports = ['Dia', 'Enc', 'Lab', 'Lhm', 'Lme', 'Lpr', 'Lvs', 'Med', 'Phy', 'Prc', 'Prv', 'Rdt', 'Rnd', 'Trn']
135
    # Only include the ones you anticipate using in the following array:
136
    struct_reports = ['Dia', 'Enc']
137
    for report_type in struct_reports:
138
        report_map = get_struct_empi_map(report_type)
139
        for patient in patients.keys():
140
            try:
141
                patients[patient][report_type] = report_map[patient]
142
            except:
143
                patients[patient][report_type] = []
144
                print('Patient with empi:' + patient + ' doesnt have report for report_type:' + report_type)
145
146
    # Final write of info
147
    if not os.path.exists(out_path):
148
        os.makedirs(out_path)
149
150
    for key in patients.keys():
151
        with open(out_path + key + '.json','w') as f:
152
            json.dump(patients[key], f)