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

Switch to unified view

a b/palCareEval.py
1
from collections import Counter
2
from datetime import datetime, timedelta
3
4
from loader import get_data
5
6
class Range:
7
    def __init__(self, low, high):
8
        self.low = self.icd9_to_float(low)
9
        self.high = self.icd9_to_float(high)
10
    def icd9_to_float(self, icd9):
11
        try:
12
            return float(icd9)
13
        except ValueError:
14
            extension = float(icd9[1:])
15
            letter = icd9[0]
16
            return ord(letter)*1000 + extension
17
    def __eq__(self, other):
18
        other = self.icd9_to_float(other)
19
        return other >= self.low and other <= self.high
20
    def __ne__(self, other):
21
        return not self.__eq__(other)
22
23
num_patients = 1579
24
#num_patients = 100
25
26
dead_patients = []
27
gender = []
28
age = []
29
is_cancer = []
30
consult_time = []
31
cancer_time = []
32
cancer_mgh_time = []
33
cardio_mgh_time = []
34
noncancer_time = []
35
utilization = []
36
utilization_cancer = []
37
utilization_mgh_cancer = []
38
utilization_noncancer = []
39
utilization_mgh_cardio = []
40
for i in range(num_patients):
41
    p = get_data([i])[0]
42
    # Filter to only dead patients
43
    if p['Vital_status'] == 'Date of Death reported from SS Death Master File':
44
        if p['Consult_Date'] not in [None, '']:
45
            dead_patients.append(p['EMPI'])
46
            gender.append(p['Gender'])
47
48
            # Dates
49
            dob = datetime.strptime(p['Date_of_Birth'], "%m/%d/%Y")
50
            dod = datetime.strptime(p['Date_Of_Death\r'], "%m/%d/%Y")
51
            doc = datetime.strptime(p['Consult_Date'], "%m/%d/%Y")
52
            age.append((dod - dob).days/365.0)
53
            timing = dod - doc
54
            consult_time.append(timing.days)
55
56
            # Diagnoses
57
            cancer_icds = [Range(140.00, 209.99), Range(230.00, 239.99)]
58
            is_cancer_patient = False
59
            for d in p['Dia']:
60
                if d['Code_Type'] == 'ICD9':
61
                    try:
62
                        if d['Code'] in cancer_icds:
63
                            is_cancer_patient = True
64
                    except:
65
                        pass
66
            is_cancer.append(is_cancer_patient)
67
68
69
            # Stratify
70
            if is_cancer_patient:
71
                cancer_time.append(timing.days)
72
            else:
73
                noncancer_time.append(timing.days)
74
75
            # Utilization
76
            mgh_onc_enc_count = 0
77
            mgh_cardio_enc_count = 0
78
            utilization_cutoff = timedelta(days=30*3)
79
            num_eol_enc = 0
80
            len_eol_enc = 0
81
            died_in_hospital = False
82
            for enc in p['Enc']:
83
                admitted = datetime.strptime(enc['Admit_Date'], "%m/%d/%Y")
84
85
                # MGH?
86
                if (dod - admitted) < timedelta(days=365):
87
                    if enc['Clinic_Name'] == 'Medical Oncology Group (609)':
88
                        mgh_onc_enc_count += 1
89
                    elif enc['Clinic_Name'] == 'Cardiology (12)':
90
                        mgh_cardio_enc_count += 1
91
92
                if (dod - admitted) < utilization_cutoff:
93
                    num_eol_enc += 1
94
                    try:
95
                        discharged = datetime.strptime(enc['Discharge_Date'], "%m/%d/%Y")
96
                        if enc['Inpatient_Outpatient'] == 'Inpatient':
97
                            len_eol_enc += (discharged-admitted).days
98
                            if dod == discharged:
99
                                died_in_hospital = True
100
                    except:
101
                        pass
102
            utilization.append((timing.days, num_eol_enc, len_eol_enc, died_in_hospital))
103
            if is_cancer_patient:
104
                utilization_cancer.append((timing.days, num_eol_enc, len_eol_enc, died_in_hospital))
105
            else:
106
                utilization_noncancer.append((timing.days, num_eol_enc, len_eol_enc, died_in_hospital))
107
108
            # Is an MGH Oncology patient
109
            if mgh_onc_enc_count >= 2:
110
                utilization_mgh_cancer.append((timing.days, num_eol_enc, len_eol_enc, died_in_hospital))
111
                cancer_mgh_time.append(timing.days)
112
113
            if not is_cancer_patient and mgh_cardio_enc_count >= 2:
114
                utilization_mgh_cardio.append((timing.days, num_eol_enc, len_eol_enc, died_in_hospital))
115
                cardio_mgh_time.append(timing.days)
116
    else:
117
        if p['Vital_status'] != 'Not reported as deceased':
118
            print "***********************"
119
            print p['Vital_status'] 
120
            print "***********************"
121
# Step 1:
122
print "Percent of patients dead: " + str(float(len(dead_patients))/num_patients)
123
124
# Step 2:
125
# Age
126
print "Age distribution:"
127
print age
128
# Gender
129
print "Gender Distribution: " + str(Counter(gender))
130
# Cancer?
131
print "Is Cancer Patient: " + str(Counter(is_cancer))
132
133
# Step 3:
134
print "Consult timing distribution:"
135
print consult_time
136
137
# Step 4:
138
print "Cancer patient consult times:"
139
print cancer_time
140
print "MGH Cancer patient consult times:"
141
print cancer_mgh_time
142
print "Non-Cancer patient consult times:"
143
print noncancer_time 
144
print "MGH Cardio patient consult times:"
145
print cardio_mgh_time
146
147
# Step 5:
148
print "Utilization"
149
print utilization
150
print "Utilization: Cancer"
151
print utilization_cancer
152
print "Utilization: MGH Cancer"
153
print utilization_mgh_cancer
154
print "Utilization: Non Cancer"
155
print utilization_noncancer
156
print "Utilization: MGH Cardio"
157
print utilization_mgh_cardio