-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlogChristmas.py
300 lines (227 loc) · 8.48 KB
/
BlogChristmas.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
# -*- coding: utf-8 -*-
"""
Analyze Christmas spurt.
@author: Matt Bayer
"""
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
import datetime
import plotly.subplots
import plotly.express as px
import plotly.graph_objects as go
from plotly.offline import plot as pplot
plotpath = '.\\docs\\assets\\plotly'
import covid
#%% Load data
state = covid.read_covid_data_wi('state')
# rename
col_rename = {'Date': 'Date', 'POS_NEW': 'Cases', 'TEST_NEW': 'Tests', 'DTH_NEW': 'Deaths', 'HOSP_NEW': 'Hospitalizations'}
state = state.rename(columns=col_rename)
# tests by result date
tests = covid.read_bytest_wi("data\\By_Test_Data_data_2021-01-06.csv")
#%% Process tests by result date
tests['Tests 7-day'] = tests['Tests'].rolling(7).mean()
tests['Positives 7-day'] = tests['Positives'].rolling(7).mean()
tests['Positivity 7-day'] = tests['Positives 7-day'] / tests['Tests 7-day']
tests['Positivity'] = tests['Positives'] / tests['Tests']
tests['Positivity 7-day x20000'] = tests['Positivity 7-day'] * 20000
tests['Prevalence Index'] = np.sqrt(tests['Positives 7-day'] * tests['Positivity 7-day']) * 200
tests['Weekday'] = tests.Date.apply(lambda d: d.weekday())
tests.plot(x='Date', y=['Positives 7-day', 'Positivity 7-day x20000', 'Prevalence Index'])
# Bar plot of tests and positives, like DHS
# fig = px.bar(tests, x='Date', y=['Positives', 'Tests'], barmode='group')
# pplot(fig, include_plotlyjs='cdn', filename=plotpath+'\\temp1.html')
# Make separate lines for each day of the week
# fig = px.line(tests, x='Date', y='Positives', color='Weekday')
# pplot(fig, include_plotlyjs='cdn', filename=plotpath+'\\temp2.html')
#%% Plot tests by result date
# remember below shows 7-day average positivity as average of the positivity,
# not more correctly positivity of the averages.
covid.plotly_twolines(
tests,
'Positivity',
'Positives',
plotcolors=['violet', 'steelblue', 'thistle'],
secondary_scale=3e4,
savefile=plotpath+'\\Pos-Positivity-ByResult-WI.html',
)
# covid.plotly_casetest(sourcedata=tests,
# case_col='Positives',
# test_col='Tests',
# date_col='Date',
# savefile=plotpath + '\\Pos-Test-ByResult-WI.html',
# )
#%% Cases by test date for Wisconsin
filename = 'C:\dev\covid-wisconsin\data\Cases_with_prob_stacked_data_2021-01-08.csv'
wi = pd.read_csv(filename)
# filter out redundant data
wi = wi.loc[wi['Measure Names'] == 'Confirmed cases']
# rename columns
col_rename = {'Day of Epi Dt': 'Date', 'Stacked Confirm + Probable cases': 'Cases'}
wi = wi[col_rename.keys()]
wi = wi.rename(columns=col_rename)
wi['Date'] = pd.to_datetime(wi['Date'])
#%% cases bar plot
wi = wi.loc[wi['Date'] >= datetime.datetime(2020,12,7)]
wi = wi.loc[wi['Date'] <= datetime.datetime(2021,1,3)]
fig = px.bar(
wi,
x='Date',
y='Cases',
color_discrete_sequence=['steelblue'],
title='Cases by Symptom/Test Date - Wisconsin',
width=700,
height=340,
)
fig.update_layout(showlegend=False)
# add dividers
date = datetime.datetime(2020,12,13,12)
delta = datetime.timedelta(days=7)
dividers = list()
for d in range(0,3):
dividers.append(
dict(
type= 'line', line_color='gray', line_dash='dot',
yref= 'paper', y0= 0, y1= 1,
xref= 'x', x0=date, x1=date
)
)
date = date + delta
fig.update_layout(shapes=dividers)
fig.add_annotation(text='Christmas',
x=datetime.datetime(2020,12,25,0),
y=1400,
yanchor='bottom', xanchor='center', showarrow=False, textangle=270)
pplot(fig, include_plotlyjs='cdn', filename=plotpath+'\\Christmas-WI.html')
save_png = '.\\docs\\assets\\Christmas-WI.png'
fig.write_image(
save_png,
width=700,
height=340,
engine='kaleido',
)
os.startfile(save_png)
#%% Christmas surge for Milwaukee
# get milwaukee test numbers by copying html of the dashboard and scraping it
# get this originally by doing a browser html inspection of the graph on the Milwaukee county dashboard,
# then copying it to a text file. The tooltips of the graph are stored in "aria-label" labels, so
# I can regex on these labels below.
html_cases = 'C:\dev\covid-wisconsin\data\Dashboard-Milwaukee-Cases_2021-01-08.html'
html_tests = 'C:\dev\covid-wisconsin\data\Dashboard-Milwaukee-Tests_2021-01-08.html'
mke_case = covid.read_dashboard_mke(html_cases)
mke_test = covid.read_dashboard_mke(html_tests)
mke_test = mke_test.replace(to_replace='7 Day Average', value='Tests 7-day')
mke = mke_case.append(mke_test)
mke = mke.pivot(index='Date', columns='variable', values='value')
mke = mke.reset_index()
mke['Weekday'] = mke['Date'].apply(lambda d: d.weekday())
mke['Positivity'] = mke['Cases'] / mke['Tests']
# sum weekly Mon-Sun
# sum with a rolling weekly window, then keep the results from Sunday
weekly = mke[['Cases', 'Tests']].rolling(7).sum()
weekly['Date'] = mke['Date']
weekly = weekly.loc[mke['Weekday'] == 6]
# sum weekly Mon-Wed only
# sum with a rolling 3-day window, then keep the results from Wednesday
monwed = mke[['Cases', 'Tests']].rolling(3).sum()
monwed['Date'] = mke['Date']
monwed = monwed.loc[mke['Weekday'] == 2]
monwed['Positivity'] = monwed['Cases'] / monwed['Tests'] * 100 # in %
weekly['Positivity'] = weekly['Cases'] / weekly['Tests']
weekly.plot(x='Date', y='Positivity', marker='.')
#%% facet bar plot?
temp = mke.loc[mke['Date'] >= datetime.datetime(2020,12,7)]
temp = temp.loc[temp['Date'] <= datetime.datetime(2021,1,3)]
temp = temp[['Date', 'Cases', 'Tests']].melt(id_vars='Date')
fig = px.bar(
temp,
x='Date',
y='value',
facet_row='variable',
color='variable',
color_discrete_sequence=['steelblue', 'olivedrab'],
title='Cases and Tests by Test Date - Milwaukee',
width=700,
height=500,
)
fig.for_each_annotation(lambda a: a.update(text=''))
fig.update_layout(showlegend=False)
fig.update_yaxes(matches=None)
fig.update_yaxes(title='Cases')
fig.update_yaxes(title='Tests', row=1)
# add dividers
date = datetime.datetime(2020,12,13,12)
delta = datetime.timedelta(days=7)
dividers = list()
for d in range(0,3):
dividers.append(
dict(
type= 'line', line_color='gray', line_dash='dot',
yref= 'paper', y0= 0, y1= 1,
xref= 'x', x0=date, x1=date
)
)
date = date + delta
fig.update_layout(shapes=dividers)
fig.add_annotation(text='Christmas',
x=datetime.datetime(2020,12,24,23),
y=700,
yanchor='bottom', xanchor='center', showarrow=False, textangle=270)
fig.add_annotation(text='Christmas',
x=datetime.datetime(2020,12,24,23),
y=50, row=0, col=0,
yanchor='bottom', xanchor='center', showarrow=False, textangle=270)
pplot(fig, include_plotlyjs='cdn', filename=plotpath+'\\Christmas-Milwaukee.html')
save_png = '.\\docs\\assets\\Christmas-Milwaukee.png'
fig.write_image(
save_png,
width=700,
height=500,
engine='kaleido',
)
os.startfile(save_png)
#%% Mon-Wed line plot
# fig = px.bar(weekly, x='Date', y=['Cases', 'Tests'], barmode='group')
# pplot(fig, include_plotlyjs='cdn', filename=plotpath+'\\temp.html')
monwed['Week of'] = monwed['Date'].apply(lambda d: d.strftime('%b %d'))
plotdata = monwed.iloc[-5:].reset_index(drop=True)
plotdata.loc[3,'Week of'] = 'Christmas'
fig = plotly.subplots.make_subplots(
specs=[[{'secondary_y': True}]]
)
fig.add_trace(
go.Scatter(
x=plotdata['Week of'],
y=plotdata.Cases,
name='Cases',
line_color='steelblue',
),
secondary_y=False,
)
# add positivity on secondary axis
fig.add_trace(
go.Scatter(
x=plotdata['Week of'],
y=plotdata.Positivity,
name='Positivity',
line_color='violet',
),
secondary_y=True,
)
fig.update_layout(title='Milwaukee Mon-Wed Sums')
fig.update_traces(mode='lines+markers', marker_size=10, line_dash='dot')
fig.update_xaxes(title='Week of...')
fig.update_yaxes({'range': [0, 4500]}, title='Cases', secondary_y=False)
fig.update_yaxes({'range': [0, 20]}, title='Positivity (%)', secondary_y=True)
pplot(fig, include_plotlyjs='cdn', filename=plotpath+'\\Christmas-MonWed-Milwaukee.html')
save_png = '.\\docs\\assets\\Christmas-MonWed-Milwaukee.png'
fig.write_image(
save_png,
width=700,
height=400,
engine='kaleido',
)
os.startfile(save_png)