-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
597 lines (508 loc) · 21.7 KB
/
app.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#from importlib.metadata import version
#print('flask:', version('flask'))
#print('gunicorn:', version('gunicorn'))
#print('dash:', version('dash'))
#print('dash_bootstrap_components:', version('dash_bootstrap_components'))
#print('pandas:', version('pandas'))
#print('plotly:', version('plotly'))
#print('statsmodels:', version('statsmodels'))
#print('scipy:', version('scipy'))
#print('numpy:', version('numpy'))
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State
from dash import dash_table
from dash.exceptions import PreventUpdate
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.graph_objects as go
import warnings
import sys
import os
import re
import numpy as np
from scipy import stats
import random
import statsmodels.api as sm
FONT_AWESOME = "https://use.fontawesome.com/releases/v5.10.2/css/all.css"
#tf.random.set_seed(1)
np.random.seed(1)
random.seed(1)
#########################################################################################
################################# CONFIG APP ############################################
#########################################################################################
warnings.filterwarnings('ignore')
#pd.set_option('display.max_columns', None)
#external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
external_stylesheets=[dbc.themes.BOOTSTRAP, FONT_AWESOME]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.config.suppress_callback_exceptions = True
server = app.server
#########################################################################################
################################# LOAD DATA #############################################
#########################################################################################
mydir = (os.getcwd()).replace('\\','/')+'/'
sys.path.append(mydir)
#counties_df = pd.read_pickle(mydir + 'data/dat_for_app.pkl')
main_df = pd.read_pickle(mydir + 'data/dat_for_app.pkl')
#tdf = main_df.filter(items=['date', 'Confirmed'], axis=1)
#########################################################################################
######################## Define static variables ########################################
#########################################################################################
features = list(main_df)
x1_features = list(features)
x2_features = features[1:]
y_features = features[1:]
operators = ['/', '*', '+', '-']
#########################################################################################
########################### CUSTOM FUNCTIONS ############################################
#########################################################################################
def obs_pred_rsquare(obs, pred):
# Determines the prop of variability in a data set accounted for by a model
# In other words, this determines the proportion of variation explained by
# the 1:1 line in an observed-predicted plot.
return 1 - sum((obs - pred) ** 2) / sum((obs - np.mean(obs)) ** 2)
#########################################################################################
################# DASH APP CONTROL FUNCTIONS ############################################
#########################################################################################
def description_card1():
"""
:return: A Div containing dashboard title & descriptions.
"""
return html.Div(
id="description-card1",
children=[
html.H5("IL COVID Trends",
style={
'textAlign': 'left',
}),
html.P("Examine trends in COVID cases, hospitalization, hospital utilization, testing, and vaccination. Most data pertain to Illinois. That that aren't pertain to the US.",
style={
'textAlign': 'left',
}),
],
)
def control_card1():
return html.Div(
id="control-card1",
children=[
html.Div(id='control-card1-container',
children=[
html.H5("Design your x-variable", style={'display': 'inline-block', 'width': '55%'}),
html.I(className="fas fa-question-circle fa-lg", id="target1",
style={'display': 'inline-block', 'width': '20px', 'color':'#99ccff'},
),
dbc.Tooltip("If you want a simple variable, then choose None for the second feature. If you choose 'date' for your first feature, the app will ignore the second feature.",
target="target1", style = {'font-size': 12},
),
dcc.Dropdown(
id="x1",
options=[{"label": i, "value": i} for i in x1_features],
multi=False,
value='date',
style={
#'font-size': "100%",
'width': '420px',
'display': 'inline-block',
#'border-radius': '15px',
#'box-shadow': '1px 1px 1px grey',
#'background-color': '#f0f0f0',
#'padding': '10px',
#'margin-bottom': '10px',
'margin-right': '10px',
}
),
dcc.Dropdown(
id="operator1",
options=[{"label": i, "value": i} for i in operators],
multi=False,
value='/',
style={
'font-size': "100%",
'width': '30px',
'display': 'inline-block',
'margin-right': '10px',
}
),
dcc.Dropdown(
id="x2",
options=[{"label": i, "value": i} for i in ['None'] + x2_features],
multi=False,
value='None',
style={
'font-size': "100%",
'width': '380px',
'display': 'inline-block',
}
),
],
),
],
)
def control_card2():
return html.Div(
id="control-card2",
children=[
html.Div(id='control-card2-container',
children=[
html.H5("Design your y-variables", style={'display': 'inline-block', 'width': '57%'}),
html.I(className="fas fa-question-circle fa-lg", id="target2",
style={'display': 'inline-block', 'width': '10%', 'color':'#99ccff'},
),
dbc.Tooltip("If you want simple variables, then choose None for the second feature.",
target="target2", style = {'font-size': 12},
),
dcc.Dropdown(
id="y1",
options=[{"label": i, "value": i} for i in y_features],
multi=True,
value=None,
placeholder='Choose 1 to 10 features',
style={
#'font-size': "100%",
'width': '420px',
#'display': 'inline-block',
#'border-radius': '15px',
#'box-shadow': '1px 1px 1px grey',
#'background-color': '#f0f0f0',
#'padding': '10px',
'margin-bottom': '10px',
'margin-right': '10px',
}
),
dcc.Dropdown(
id="operator2",
options=[{"label": i, "value": i} for i in operators],
multi=False,
value='/',
style={
'font-size': "100%",
'width': '30px',
'display': 'inline-block',
'margin-right': '10px',
}
),
dcc.Dropdown(
id="y2",
options=[{"label": i, "value": i} for i in ['None'] + y_features],
multi=False,
value='None',
style={
'font-size': "100%",
'width': '380px',
'display': 'inline-block',
}
),
],
),
],
)
def control_card3():
return html.Div(
id="control-card3",
children=[
html.Div(id='control-card3-container',
children=[
html.H5("Dampen extreme outliers", style={'display': 'inline-block', 'width': '260px'}),
html.I(className="fas fa-question-circle fa-lg", id="target3",
style={'display': 'inline-block', 'color':'#99ccff'},
),
dbc.Tooltip("Extreme outlier points are likely the result of time-lags and reporting issues. Choose this option to replace outliers with interpolated data points.",
target="target3", style = {'font-size': 12},
),
dcc.RadioItems(
id="outliers",
options=[
{'label': ' Dampen outliers', 'value': 'dampen_outliers'},
{'label': ' Leave outliers as is', 'value': 'keep_outliers'},
],
value='dampen_outliers',
labelStyle={'display': 'inline-block', 'width': '160px'}
),
],
),
],
)
#########################################################################################
################### DASH APP PLOT FUNCTIONS #############################################
#########################################################################################
#########################################################################################
################################# DASH APP LAYOUT #######################################
#########################################################################################
app.layout = html.Div([
html.Div(
id='main_df',
style={'display': 'none'}
),
html.Div(
id='counties_df',
style={'display': 'none'}
),
html.Div(
style={'background-color': '#f9f9f9'},
id="banner1",
className="banner",
children=[html.Img(src=app.get_asset_url("RUSH_full_color.jpg"),
style={'textAlign': 'left'}),
html.Img(src=app.get_asset_url("plotly_logo.png"),
style={'textAlign': 'right'}),
],
),
html.Div(
id="description_card1",
className="ten columns",
children=[description_card1()],
style={'width': '95%', 'display': 'inline-block',
'border-radius': '15px',
'box-shadow': '1px 1px 1px grey',
'background-color': '#f0f0f0',
'padding': '10px',
'margin-bottom': '10px',
},
),
html.Div(
id="right-column1",
className="four columns",
children=[control_card1(),
html.Br(),
html.Hr(),
control_card2(),
html.Br(),
html.Hr(),
control_card3(),
],
style={#'width': '95%',
#'display': 'inline-block',
'border-radius': '15px',
'box-shadow': '1px 1px 1px grey',
'background-color': '#f0f0f0',
'padding': '10px',
'margin-bottom': '10px',
},
),
html.Div(
id="right-column2",
className="eight columns",
children=[
html.Div(
id="Figure1",
children=[dcc.Loading(
id="loading-2",
type="default",
fullscreen=False,
children=html.Div(id="figure1",
children=[dcc.Graph(id="figure_plot1"),
]))],
style={'width': '95%',
'height': '650px',
'display': 'inline-block',
'border-radius': '15px',
'box-shadow': '1px 1px 1px grey',
'background-color': '#f0f0f0',
'padding': '10px',
'margin-bottom': '10px',
},
),
]),
])
#########################################################################################
############################ Call backs #######################################
#########################################################################################
@app.callback(Output('figure_plot1', 'figure'),
[Input('x1', 'value'),
Input('x2', 'value'),
Input('y1', 'value'),
Input('y2', 'value'),
Input('operator1', 'value'),
Input('operator2', 'value'),
Input('outliers', 'value')
],
)
def update_results_figure(x1, x2, y1, y2, operator1, operator2, outliers):
if x1 in [None, 'None', ''] or y1 in [None, [None], ['None'], []]:
figure = go.Figure(data=[go.Table(
header=dict(values=[],
fill_color='#b3d1ff',
align='left'),
),
],
)
figure.update_layout(title_font=dict(size=14,
color="rgb(38, 38, 38)",
),
margin=dict(l=10, r=10, b=10, t=0),
paper_bgcolor="#f0f0f0",
plot_bgcolor="#f0f0f0",
height=400,
)
return figure
t_features = [x1] + y1
if x2 not in [None, 'None', '', []]:
t_features.append(x2)
if y2 not in [None, 'None', '', []]:
t_features.append(y2)
t_features = list(set(t_features))
tdf = main_df.filter(items=t_features, axis=1)
if x1 == 'date':
x2 = 'None'
if 'date' in y1:
y1 = ['date']
y2 = 'None'
xlab = str(x1)
if x2 != 'None':
xlab = x1 + ' ' + operator1 + ' ' + x2
numerator = tdf[x1]
denominator = tdf[x2]
if operator1 == '/':
tdf[xlab] = numerator / denominator
elif operator1 == '*':
tdf[xlab] = numerator * denominator
elif operator1 == '+':
tdf[xlab] = numerator + denominator
elif operator1 == '-':
tdf[xlab] = numerator - denominator
ylabs = y1
if y2 not in ['None', None, np.nan]:
ylabs = []
for yvar in y1:
ylab = yvar + ' ' + operator2 + ' ' + y2
ylabs.append(ylab)
#print(ylabs)
numerator = tdf[yvar]
denominator = tdf[y2]
q = 0
if operator2 == '/':
q = numerator / denominator
elif operator2 == '*':
q = numerator * denominator
elif operator2 == '+':
q = numerator + denominator
elif operator2 == '-':
q = numerator - denominator
tdf[ylab] = q
#print('xlab:', xlab)
#print('ylabs:', ylabs, '\n')
#print(list(tdf), '\n')
#print(tdf.head(10))
fig_data = []
clrs = ['#ff0000', '#0000ff', '#009900', '#993399', '#009999',
'#ff9966', '#00ff00', '#3366cc', '#cc6699', '#000066',
'#ff0000', '#0000ff', '#009900', '#993399', '#009999',
'#ff9966', '#00ff00', '#3366cc', '#cc6699', '#000066',
'#ff0000', '#0000ff', '#009900', '#993399', '#009999',
'#ff9966', '#00ff00', '#3366cc', '#cc6699', '#000066',
'#ff0000', '#0000ff', '#009900', '#993399', '#009999',
'#ff9966', '#00ff00', '#3366cc', '#cc6699', '#000066',
'#ff0000', '#0000ff', '#009900', '#993399', '#009999',
'#ff9966', '#00ff00', '#3366cc', '#cc6699', '#000066',
'#ff0000', '#0000ff', '#009900', '#993399', '#009999',
'#ff9966', '#00ff00', '#3366cc', '#cc6699', '#000066',
]
for i, ylab in enumerate(ylabs):
ttdf = tdf.filter(items=[xlab, ylab], axis=1)
if outliers == 'dampen_outliers':
ys = ttdf[ylab].tolist()
xs = ttdf[ylab].tolist()
for ii, y in enumerate(ys):
if ii > 0:
if y > 10 * ys[ii-1]:
ys[ii] = ys[ii-1]
ttdf[ylab] = ys
if xlab != 'date':
xs = ttdf[ylab].tolist()
for ii, x in enumerate(xs):
if ii > 0:
if x > 10 * xs[ii-1]:
xs[ii] = xs[ii-1]
ttdf[xlab] = xs
ttdf.dropna(how='any', inplace=True)
fig_data.append(
go.Scatter(x = ttdf[xlab], y = ttdf[ylab],
mode="markers",
marker_color = clrs[i],
name = ylab,
#text = tdf['TP'] + '<br>' + tdf['FP'] + '<br>' + tdf['TN'] + '<br>' + tdf['FN'] + '<br>' + tdf['threshold'] + '<br>' + tdf['N'],
opacity = 0.75,
line=dict(color=clrs[0], width=2),
))
X = []
if xlab == 'date':
X = list(range(len(ttdf[xlab].tolist())))
else:
X = ttdf[xlab].tolist()
Y = ttdf[ylab].tolist()
lowess = sm.nonparametric.lowess
ty = lowess(Y, X, frac=1/40)
ty = np.transpose(ty)
ty = ty[1]
r2 = obs_pred_rsquare(Y, ty)
r2 = np.round(100*r2, 1)
fig_data.append(
go.Scatter(x = ttdf[xlab], y = ty,
mode="lines",
marker_color = clrs[i],
name = 'Trend: ' + ylab,
#text = tdf['TP'] + '<br>' + tdf['FP'] + '<br>' + tdf['TN'] + '<br>' + tdf['FN'] + '<br>' + tdf['threshold'] + '<br>' + tdf['N'],
opacity = 0.75,
line=dict(color=clrs[i], width=2),
))
if len(y1) > 1:
ylab = None
figure = go.Figure(
data=fig_data,
layout=go.Layout(
xaxis=dict(
title=dict(
text=xlab,
font=dict(
family='"Open Sans", "HelveticaNeue", "Helvetica Neue",'
" Helvetica, Arial, sans-serif",
size=14,
),
),
rangemode="tozero",
zeroline=True,
showticklabels=True,
),
yaxis=dict(
title=dict(
text=ylab,
font=dict(
family='"Open Sans", "HelveticaNeue", "Helvetica Neue",'
" Helvetica, Arial, sans-serif",
size=14,
),
),
rangemode="tozero",
zeroline=True,
showticklabels=True,
),
margin=dict(l=60, r=30, b=10, t=40),
showlegend=True,
height=600,
paper_bgcolor="rgb(245, 247, 249)",
plot_bgcolor="rgb(245, 247, 249)",
),
)
ypos = -0.15
figure.update_layout(
legend=dict(
orientation = "h",
y = ypos,
yanchor = "top",
xanchor="left",
traceorder = "normal",
font = dict(
size = 12,
color = "rgb(38, 38, 38)"
),
)
)
del tdf
return figure
#########################################################################################
############################# Run the server ############################################
#########################################################################################
if __name__ == "__main__":
app.run_server(host='0.0.0.0', debug=True) # modified to run on linux server