forked from klocey/cms-stars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent_fxns.py
502 lines (398 loc) · 18.2 KB
/
component_fxns.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
from dash import dash_table
from scipy import stats
from scipy.stats import percentileofscore
import pandas as pd
import numpy as np
import re
def whatif_table(hospital, whatif_df):
whatif_df = pd.read_json(whatif_df)
name = hospital[:-9]
measures = []
domains = []
m1 = ['H_COMP_1_STAR_RATING', 'H_COMP_2_STAR_RATING',
'H_COMP_3_STAR_RATING', 'H_COMP_5_STAR_RATING',
'H_COMP_6_STAR_RATING', 'H_COMP_7_STAR_RATING',
'H_GLOB_STAR_RATING', 'H_INDI_STAR_RATING']
domains.extend(['Patient Experience']*len(m1))
measures.extend(m1)
m2 = ['EDAC_30_AMI', 'EDAC_30_HF', 'EDAC_30_PN', 'OP_32',
'READM_30_CABG', 'READM_30_COPD', 'READM_30_HIP_KNEE',
'READM_30_HOSP_WIDE', 'OP_35_ADM', 'OP_35_ED', 'OP_36']
domains.extend(['Readmission']*len(m2))
measures.extend(m2)
m3 = ['MORT_30_AMI', 'MORT_30_CABG', 'MORT_30_COPD', 'MORT_30_HF',
'MORT_30_PN', 'MORT_30_STK', 'PSI_4_SURG_COMP']
domains.extend(['Mortality']*len(m3))
measures.extend(m3)
m4 = ['COMP_HIP_KNEE', 'HAI_1', 'HAI_2', 'HAI_3', 'HAI_4',
'HAI_5', 'HAI_6', 'PSI_90_SAFETY']
domains.extend(['Safety of Care']*len(m4))
measures.extend(m4)
m5 = ['HCP_COVID_19', 'IMM_3', 'OP_10', 'OP_13', 'OP_18B',
#'OP_2',
'OP_22', 'OP_23', 'OP_29', 'OP_3B',
'OP_8', 'PC_01', 'SEP_1']
domains.extend(['Timely & Effective Care']*len(m5))
measures.extend(m5)
rev_measures = ['MORT_30_AMI', 'MORT_30_CABG', 'MORT_30_COPD', 'MORT_30_HF',
'MORT_30_PN', 'MORT_30_STK', 'PSI_4_SURG_COMP', 'COMP_HIP_KNEE',
'HAI_1', 'HAI_2', 'HAI_3', 'HAI_4', 'HAI_5', 'HAI_6',
'PSI_90_SAFETY', 'EDAC_30_AMI', 'EDAC_30_HF', 'EDAC_30_PN',
'OP_32', 'READM_30_CABG', 'READM_30_COPD', 'READM_30_HIP_KNEE',
'READM_30_HOSP_WIDE', 'OP_35_ADM', 'OP_35_ED', 'OP_36', 'OP_22',
'PC_01', 'OP_3B', 'OP_18B', 'OP_8', 'OP_10','OP_13',
]
higher_better = []
for m in measures:
if m in rev_measures:
higher_better.append('No')
else:
higher_better.append('Yes')
tdf = whatif_df.filter(items=measures)
tdf = tdf.round(3)
mins = tdf.min().tolist()
maxs = tdf.max().tolist()
pnum = re.search(r'\d{6}', hospital)
pnum = pnum.group()
try:
tdf = whatif_df[whatif_df['PROVIDER_ID'] == pnum]
tdf = tdf.filter(items=measures)
tdf = tdf.round(3)
except:
return {}, {}, {}, hospital + ' has no data in the CMS Care Compare release that is used to make predictions.'
cols = ['Domain', 'Measure', 'Higher is better', 'Actual value', 'Min value', 'Max value',
'What-if value']
df_table = pd.DataFrame(columns=cols)
df_table['Domain'] = domains
df_table['Measure'] = list(tdf)
df_table['Higher is better'] = higher_better
df_table['Actual value'] = tdf.iloc[0].tolist()
df_table['Min value'] = mins
df_table['Max value'] = maxs
df_table['What-if value'] = tdf.iloc[0].tolist()
data=df_table.to_dict('records')
columns=[
{'id': c, 'name': c, 'editable': (c == 'What-if value')} # Make only "What-if value" column editable
for c in df_table.columns
]
name = hospital[:-9]
name1 = str(name)
if name == 'RUSH UNIVERSITY MEDICAL CENTER':
name1 = 'RUMC'
elif name == 'RUSH OAK PARK HOSPITAL':
name1 = 'ROPH'
elif name == 'RUSH COPLEY':
name1 = 'RUSH COPLEY'
else:
name1 = "hospital " + hospital[-7:-1]
return data, columns, df_table.to_json(), name1
def measure_table(main_df, jd1, feature_dict, hospital, option_hospitals, selected_hosps_btn, stars_peers_btn, domain, score_type, yr, selected_hospitals):
main_df = pd.read_json(main_df)
cols = ['Measure', 'Value', 'Delta value',
'Percentile', 'Delta percentile']
#'Weight', 'Delta weight']
df_table = pd.DataFrame(columns=cols)
for i in list(df_table):
df_table[i] = [np.nan]*4
dashT = dash_table.DataTable(
data=df_table.to_dict('records'),
columns=[{'id': c, 'name': c} for c in df_table.columns],
export_format="csv",
page_action='none',
sort_action="native",
sort_mode="multi",
filter_action="native",
style_table={#'height': '500px',
'overflowY': 'auto',
},
style_cell={'padding':'5px',
'minwidth':'140px',
'width':'160px',
'maxwidth':'160px',
'whiteSpace':'normal',
'textAlign': 'center',
},
)
# No hospital selected
if hospital is None:
txt = "Please select a focal hospital."
return dashT, txt
tdf_main = main_df.copy(deep=True)
name = hospital[:-9]
hosp_df = tdf_main[tdf_main['Name and Num'] == hospital]
# Selected hospital has no data among release years
if hosp_df.shape[0] == 0:
txt = hospital + " had no data among the CMS Stars release years. Try selecting another hospital."
return dashT, txt
yrs = sorted(hosp_df['Release year'].unique().tolist())
# There is only one release year for the selected hospital (can't compute deltas)
if len(yrs) <= 1:
txt = hospital + ' had only one release year. Cannot compute delta values. Try selecting another hospital.'
return dashT, txt
yrs = sorted(hosp_df['Release year'].unique().tolist())
yr1 = int(yr)
yr2 = int()
if yr1 == 2025:
yr2 = 2024
if yr2 in yrs:
pass
else:
yr2 == 2023
if yr1 == 2024:
yr2 = 2023
if yr2 in yrs:
pass
else:
yr2 == 2022
if yr1 == 2023:
yr2 = 2022
if yr2 in yrs:
pass
else:
yr2 == 2021
elif yr1 == 2022:
yr2 = 2021
set_select = 'Measures group'
if 'selected_hosps_btn3' in jd1:
set_select = 'Selected hospitals'
elif 'stars_peers_btn3' in jd1:
set_select = 'Measures group'
if set_select == 'Measures group':
tdf_main_LY = tdf_main[tdf_main['Release year'] == yr1]
grp_LY = tdf_main_LY[tdf_main_LY['Name and Num'] == hospital]['cnt_grp'].iloc[0]
tdf_main_LY = tdf_main_LY[tdf_main_LY['cnt_grp'].isin([grp_LY])]
if yr1 == 2021:
grp_PY = np.nan
tdf_main_PY = None
else:
tdf_main_PY = tdf_main[tdf_main['Release year'] == yr2]
grp_PY = tdf_main_PY[tdf_main_PY['Name and Num'] == hospital]['cnt_grp'].iloc[0]
tdf_main_PY = tdf_main_PY[tdf_main_PY['cnt_grp'].isin([grp_PY])]
elif set_select == 'Selected hospitals':
if option_hospitals is None or option_hospitals == []:
txt = ''
if option_hospitals is None or option_hospitals == []:
txt += "You either haven't selected any hospitals or the filters you chose left you with no hospitals to analyze."
return dashT, txt
if hospital in option_hospitals:
# Remove focal hospital from options, for use below
option_hospitals.remove(hospital)
if selected_hospitals is None or selected_hospitals == [] or selected_hospitals == [hospital]:
#There are options but either none were selected specifically by name or only our focal hospital was selected by name
selected_hospitals = list(option_hospitals)
if hospital in selected_hospitals:
#Remove focal hospital from options, for use below
selected_hospitals.remove(hospital)
tdf_main = main_df[main_df['Name and Num'].isin(selected_hospitals + [hospital])]
tdf_main_LY = tdf_main[tdf_main['Release year'] == yr1]
if yr1 == 2021:
tdf_main_PY = None
else:
tdf_main_PY = tdf_main[tdf_main['Release year'] == yr2]
######## GET RESULTS FOR LATEST YEAR ##############
# Get hospitals
hosp_ls_LY = tdf_main_LY['Name and Num'].tolist()
i = 0
try:
i = hosp_ls_LY.index(hospital)
except:
txt = hospital + " had no data among the CMS Stars release years. Try selecting another hospital."
return dashT, txt
# Get measures
measure_ls = []
if score_type == 'Standardized scores':
measure_ls = feature_dict[domain + ' (std)']
elif score_type == 'Raw scores':
measure_ls = feature_dict[domain]
hosp_scors_LY = []
hosp_percs_LY = []
hosp_wts_LY = []
# Get values for latest year
labels_ls = []
for ii, m in enumerate(measure_ls):
try:
ls = tdf_main_LY[m].tolist()
hosp_scors_LY.append(ls[i])
perc = round(stats.percentileofscore(ls, ls[i]), 1)
hosp_percs_LY.append(perc)
ls2 = feature_dict[domain + ' labels']
labels_ls.append(ls2[ii])
if domain == 'Patient Experience':
pref = 'patient_exp_'
elif domain == 'Readmission':
pref = 'readmission_'
elif domain == 'Mortality':
pref = 'mortality_'
elif domain == 'Safety of Care':
pref = 'safety_'
elif domain == 'Timely and Effective Care':
pref = 'process_'
ls = tdf_main_LY[pref + 'measure_wt'].tolist()
hosp_wts_LY.append(ls[i])
except:
pass
if yr1 == 2021:
hosp_scors_PY = [np.nan]*len(hosp_scors_LY)
hosp_percs_PY = [np.nan]*len(hosp_percs_LY)
hosp_wts_PY = [np.nan] * len(hosp_wts_LY)
else:
######## GET RESULTS FOR NEXT LATEST YEAR ##############
# Get hospitals
hosp_ls_PY = tdf_main_PY['Name and Num'].tolist()
i = 0
try:
i = hosp_ls_PY.index(hospital)
except:
txt = hospital + " had no data among the CMS Stars release years. Try selecting another hospital"
return dashT, txt
# Get measures
measure_ls = []
if score_type == 'Standardized scores':
measure_ls = feature_dict[domain + ' (std)']
elif score_type == 'Raw scores':
measure_ls = feature_dict[domain]
hosp_scors_PY = []
hosp_percs_PY = []
hosp_wts_PY = []
# Get values for next latest year
labels_ls = []
for ii, m in enumerate(measure_ls):
try:
ls = tdf_main_PY[m].tolist()
hosp_scors_PY.append(ls[i])
perc = round(stats.percentileofscore(ls, ls[i]), 1)
hosp_percs_PY.append(perc)
ls2 = feature_dict[domain + ' labels']
labels_ls.append(ls2[ii])
# get individual measure weight ... somehow
if domain == 'Patient Experience':
pref = 'patient_exp_'
elif domain == 'Readmission':
pref = 'readmission_'
elif domain == 'Mortality':
pref = 'mortality_'
elif domain == 'Safety of Care':
pref = 'safety_'
elif domain == 'Timely and Effective Care':
pref = 'process_'
ls = tdf_main_PY[pref + 'measure_wt'].tolist()
hosp_wts_PY.append(ls[i])
except:
pass
#########
# Compute values for columns
delta_value = np.round(np.array(hosp_scors_LY) - np.array(hosp_scors_PY), 3)
delta_perc = np.round(np.array(hosp_percs_LY) - np.array(hosp_percs_PY), 3)
#delta_wght = np.round(np.array(hosp_wts_LY) - np.array(hosp_wts_PY), 3)
cols = ['Measure', 'Value', 'Delta value',
'Percentile', 'Delta percentile']
#'Weight', 'Delta weight']
df_table = pd.DataFrame(columns=cols)
df_table['Measure'] = labels_ls
df_table['Value'] = np.round(hosp_scors_LY, 3)
df_table['Delta value'] = delta_value.tolist()
df_table['Percentile'] = hosp_percs_LY
df_table['Delta percentile'] = delta_perc
#df_table['Weight'] = np.round(hosp_wts_LY, 4)
#df_table['Delta weight'] = delta_wght
df_table.dropna(how='all', axis=0, subset=['Value', 'Delta value',
'Percentile', 'Delta percentile',
], inplace=True)
if score_type == 'Standardized scores':
dashT = dash_table.DataTable(
data=df_table.to_dict('records'), columns=[{'id': c, 'name': c} for c in df_table.columns],
export_format="csv", page_action='none', sort_action="native", sort_mode="multi", #filter_action="native",
style_table={'overflowY': 'auto'},
style_cell={'padding':'5px', 'minwidth':'140px',
'width':'160px', 'maxwidth':'160px',
'whiteSpace':'normal', 'textAlign': 'center'},
style_cell_conditional=[
{'if': {'column_id': 'Measure'},
'width': '30%'},
#{'if': {'column_id': 'Region'},
# 'width': '30%'},
],
style_data_conditional=[
{
'if': {
'column_id': 'Delta value',
'filter_query': '{Delta value} > 0'
},
'backgroundColor': 'green',
'color': 'white'
},
{
'if': {
'column_id': 'Delta value',
'filter_query': '{Delta value} < 0'
},
'backgroundColor': 'red',
'color': 'white'
},
{
'if': {
'column_id': 'Delta percentile',
'filter_query': '{Delta percentile} > 0'
},
'backgroundColor': 'green',
'color': 'white'
},
{
'if': {
'column_id': 'Delta percentile',
'filter_query': '{Delta percentile} < 0'
},
'backgroundColor': 'red',
'color': 'white'
}]
)
else:
dashT = dash_table.DataTable(
data=df_table.to_dict('records'), columns=[{'id': c, 'name': c} for c in df_table.columns],
export_format="csv", page_action='none', sort_action="native", sort_mode="multi", #filter_action="native",
style_table={'overflowY': 'auto'},
style_cell={'padding':'5px', 'minwidth':'140px',
'width':'160px', 'maxwidth':'160px',
'whiteSpace':'normal', 'textAlign': 'center'},
style_cell_conditional=[
{'if': {'column_id': 'Measure'},
'width': '30%'},
#{'if': {'column_id': 'Region'},
# 'width': '30%'},
]
)
name1 = str(name)
if name == 'RUSH UNIVERSITY MEDICAL CENTER':
name1 = 'RUMC'
elif name == 'RUSH OAK PARK HOSPITAL':
name1 = 'ROPH'
elif name == 'RUSH COPLEY':
name1 = 'RUSH COPLEY'
else:
name1 = "hospital " + hospital[-7:-1]
if yr1 == 2021:
txt = "This application does not provide delta values for 2021 because it does not include overall star results from 2020."
else:
txt = "The latest year of data used was " + str(yr1) + ". "
txt += "Delta values were computed using the prior year " + str(yr2) + ". "
if set_select == 'Measures group':
if np.isnan(grp_LY) == True and np.isnan(grp_PY) == True:
txt += "In both years, " + name1 + " was not assigned to a peer group and did not receive a star rating."
elif np.isnan(grp_LY) == True:
txt = "In " + str(yr1) + ', ' + name1 + " was not assigned to a peer group and did not receive a star rating."
elif np.isnan(grp_PY) == True and yr1 != 2021:
txt = "In " + str(yr2) + ', ' + name1 + " was not assigned to a peer group and did not receive a star rating."
elif grp_LY == grp_PY:
numD_LY = ' (hospitals w/ scores in ' + str(int(grp_LY + 2)) + ' domains)'
txt += "In both years, " + name1 + " was in group " + str(int(grp_LY)) + numD_LY + '. '
else:
numD_LY = ' (hospitals w/ scores in ' + str(int(grp_LY + 2)) + ' domains)'
numD_PY = ' (hospitals w/ scores in ' + str(int(grp_PY + 2)) + ' domains)'
txt += name1 + " was in group " + str(int(grp_PY)) + numD_PY + " in " + str(yr2)
txt += " and in group " + str(int(grp_LY)) + numD_LY + " in " + str(yr1) + ". "
if score_type == 'Raw scores':
txt += " Delta's are not color-coded because, unlike standardized scores, greater deltas for raw scores "
txt += "do not necessarily imply improvement."
else:
txt += "Delta's are color-coded (green = improved; red = worsened)."
return dashT, txt