-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
165 lines (125 loc) · 5.97 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
# -*- coding: utf-8 -*-
# Run this app with `python app.py` and
# visit http://127.0.0.1:8050/ in your web browser.
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import seaborn as sns
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
data = pd.read_csv('./COVID_NET/valid_df.csv')
cdr_df = pd.read_csv('./COVID_NET/merged_data.csv')
cdr_df = cdr_df[['catchment', 'date', 'Confirmed', 'Deaths', 'Recovered']]
population_df = pd.read_csv('./US_Census/nst-est2020.csv')
# filter for all the valid rows for states (exclude 'Entire Network')
valid_df = data[pd.notnull(data['weekly_rate'])]
valid_df = valid_df.loc[valid_df['catchment'] != 'Entire Network']
# filter for all overall rows for states exclude entire network)
overall_df = valid_df.loc[(valid_df['age_category'] == 'Overall') &
(valid_df['sex'] == 'Overall') &
(valid_df['race'] == 'Overall')]
# filter based on sex for states
sex_df = valid_df.loc[(valid_df['age_category'] == 'Overall') &
(valid_df['sex'] != 'Overall') &
(valid_df['race'] == 'Overall')]
# filter based on race for states
race_df = valid_df.loc[(valid_df['age_category'] == 'Overall') &
(valid_df['sex'] == 'Overall') &
(valid_df['race'] != 'Overall')]
# filter based on age
age_df = valid_df.loc[(valid_df['age_category'] != 'Overall') &
(valid_df['sex'] == 'Overall') &
(valid_df['race'] == 'Overall')]
# generalize to five age group as it's shown on the website
age_df = age_df.loc[(age_df['age_category'] == '0-4 yr') |
(age_df['age_category'] == '5-17 yr') |
(age_df['age_category'] == '18-49 yr') |
(age_df['age_category'] == '50-64 yr') |
(age_df['age_category'] == '65+ yr')]
overallWeekly = px.line(overall_df, x="date", y="weekly_rate", color="catchment")
overallCumulative = px.line(overall_df, x="date", y="cumulative_rate", color="catchment")
weeklyBySex = px.box(sex_df, x="date", y="weekly_rate", color="sex")
cumulativeBySex = px.box(sex_df, x="date", y="cumulative_rate", color="sex")
weeklyByRace = px.box(race_df, x="date", y="weekly_rate", color="race")
cumulativeByRace = px.box(race_df, x="date", y="cumulative_rate", color="race")
weeklyByAge = px.box(age_df, x="date", y="weekly_rate", color="age_category")
cumulativeByAge = px.box(age_df, x="date", y="cumulative_rate", color="age_category")
population_df = population_df[['state', 'name', 'popestimate2020']]
hosp_states = ["Tennessee","Utah","California","Colorado","Connecticut","Georgia","Iowa","Maryland","Michigan","Minnesota","New Mexico","New York","Ohio","Oregon"]
state_pop_df = population_df.loc[population_df['name'].isin(hosp_states)]
state_pop_df.head()
# Left join (using merger)
overall_df = overall_df.merge(state_pop_df, left_on='catchment', right_on='name')
# Then drop useless columns
overall_df = overall_df.drop(labels=['network', 'name', 'state'], axis=1)
overall_df = overall_df.assign(cumuhos_count = np.ceil(overall_df['popestimate2020']/1e6 * overall_df['cumulative_rate']).astype(int))
overall_df = overall_df.assign(weeklyhos_count = np.ceil(overall_df['popestimate2020']/1e6 * overall_df['weekly_rate']).astype(int))
overall_df = pd.merge(overall_df, cdr_df, on=['catchment', 'date'], how='left')
overall_df = joined.drop_duplicates()
overall_df = overall_df.assign(hoscount_per_confirmed = overall_df['cumuhos_count']/overall_df['Confirmed'])
overall_df = overall_df.assign(recovery_per_confirmed = overall_df['Recovered']/overall_df['Confirmed'])
overall_df = overall_df.assign(infecting_ratio = overall_df['Confirmed']/overall_df['popestimate2020'])
app.layout = html.Div(children=[
html.Div([
html.H1(children='COVID-19 Hospitalization'),
html.Div(children='''
By Yixin Tian, Harris Zheng, Ron Hu
'''),
]),
# All elements from the top of the page
html.Div([
html.Div([
html.H1(children='Overall weekly rate trends for all states'),
dcc.Graph(
id='graph1',
figure=overallWeekly
),
], className='six columns'),
html.Div([
html.H1(children='Overall cumulative rate trends for all states'),
dcc.Graph(
id='graph2',
figure=overallCumulative
),
], className='six columns'),
], className='row'),
html.Div([
html.Div([
html.H1(children='Overall weekly rate (based on gender) trends for all states'),
dcc.Graph(
id='graph3',
figure=weeklyBySex
),
], className='six columns'),
html.Div([
html.H1(children='Overall cumulative rate (based on gender) trends for all states'),
dcc.Graph(
id='graph4',
figure=cumulativeBySex
),
], className='six columns'),
], className='row'),
html.Div([
html.Div([
html.H1(children='Overall weekly rate (based on race) trends for all states'),
dcc.Graph(
id='graph5',
figure=weeklyByRace
),
], className='six columns'),
html.Div([
html.H1(children='Overall cumulative rate (based on race) trends for all states'),
dcc.Graph(
id='graph6',
figure=cumulativeByRace
),
], className='six columns'),
], className='row'),
])
if __name__ == '__main__':
app.run_server(debug=True)