-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlogVaccinesRegion.py
260 lines (199 loc) · 7.98 KB
/
BlogVaccinesRegion.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
# -*- coding: utf-8 -*-
"""
Update plots for regional vaccine allocation
"""
import pandas as pd
import datetime
from plotly.offline import plot as pplot
import plotly.express as px
import os
import covid
#%% Get the data
# Updated by UpdateData.py, just load from csv here
datapath = '.\\data'
csv_file_pop = datapath + '\\Population-Data-WI.csv'
# population data
popdata = covid.read_pop_data_wi(csv_file_pop)
# covid data
widata = covid.read_covid_data_wi('county')
#%% Sum up population by region
# get mapping between county and region
region_file = 'data\\Regions-WI.csv'
region_map = pd.read_csv(region_file)
region_map = region_map[['County', 'Modified HERC Short']]
region_map = region_map.set_index('County')
region_map = region_map.squeeze()
# get a list of region names
region_list = region_map.unique()
# add WI overall to the map
region_map['WI'] = 'WI'
# apply this map to the county name column to create a new Region column
widata['Region'] = widata.NAME.apply(lambda n: region_map[n])
# group data by regions
regiondata = widata.groupby(['Date', 'Region']).sum()
regiondata = regiondata.reset_index()
# find population of these regions
popdata_region = popdata.to_frame(name='Population')
popdata_region['Region'] = region_map
pop_region = popdata_region.groupby('Region').sum().squeeze()
#%% Trim data
col_rename = {'Date': 'Date',
'Region': 'Region',
'POS_NEW': 'Cases',
'TEST_NEW': 'Tests',
'DTH_NEW': 'Deaths',
'HOSP_NEW': 'Hospitalizations',
}
regiondata = regiondata[col_rename.keys()]
regiondata = regiondata.rename(columns=col_rename)
#%% convert per-capita (per 100K)
regiondata['RegionPop'] = regiondata.Region.apply(lambda n: pop_region[n])
capita = regiondata.copy()
datacols = ['Cases', 'Tests', 'Deaths', 'Hospitalizations']
capita[datacols] = regiondata[datacols].div(regiondata['RegionPop'], axis=0) * 100000
#%% Region names and colors
plotpath = '.\\docs\\_includes\\plotly'
regiondata['NAME'] = regiondata['Region']
region_ordered = ['Northwest', 'North Central', 'Northeast',
'Western', 'Fox Valley', 'Southeast',
'South Central', 'Madison', 'Milwaukee']
color_dict = {'Northwest': 'thistle',
'North Central': 'khaki',
'Northeast': 'green',
'Western': 'sandybrown',
'Fox Valley': 'yellowgreen',
'Southeast': 'lightsteelblue',
'Outer Southeast': 'lightsteelblue',
'South Central': 'pink',
'Outer South Central': 'pink',
'Madison': 'red',
'Milwaukee': 'navy'}
colors = [color_dict[r] for r in region_ordered]
#%% Vaccine numbers
# # 25-Jan
# vaccine_region = {'Northwest': 28443,
# 'North Central': 25285,
# 'Northeast': 30578,
# 'Western': 17984,
# 'Fox Valley':23719,
# 'Southeast':116672,
# 'South Central':89329,
# 'Madison': 49644,
# 'Milwaukee': 46190,
# 'WI': 339858
# }
# # 6-Feb
# vaccine_region = {'Northwest': 62911,
# 'North Central': 55091,
# 'Northeast': 64064,
# 'Western': 39936,
# 'Fox Valley':58512,
# 'Southeast':247299,
# 'South Central':184981,
# 'Madison': 91797,
# 'Milwaukee': 91308,
# 'WI': 728724,
# 'Waukesha': 54696,
# 'Washington': 16126,
# 'Ozaukee': 13441
# }
# 7-Feb
vaccine_region = {'Northwest': 64127,
'North Central': 57117,
'Northeast': 65698,
'Western': 40317,
'Fox Valley':61100,
'Southeast':254563,
'South Central':189785,
'Madison': 94405,
'Milwaukee': 93985,
'WI': 749223,
'Waukesha': 55759,
'Washington': 16622,
'Ozaukee': 13815
}
vaccine_region = pd.Series(vaccine_region)
wow_counties = vaccine_region[['Waukesha', 'Washington', 'Ozaukee']] / popdata[['Waukesha', 'Washington', 'Ozaukee']]
wow_sum = vaccine_region[['Waukesha', 'Washington', 'Ozaukee']].sum() / popdata[['Waukesha', 'Washington', 'Ozaukee']].sum()
metro_sum = vaccine_region[['Milwaukee', 'Waukesha', 'Washington', 'Ozaukee']].sum() / popdata[['Milwaukee', 'Waukesha', 'Washington', 'Ozaukee']].sum()
# harmonize region naming
pop_region['Outer Southeast'] = pop_region['Southeast']
pop_region['Total Southeast'] = pop_region['Southeast'] + pop_region['Milwaukee']
vaccine_region['Outer Southeast'] = vaccine_region['Southeast'] - vaccine_region['Milwaukee']
vaccine_region['Total Southeast'] = vaccine_region['Southeast']
pop_region['Outer South Central'] = pop_region['South Central']
pop_region['Total South Central'] = pop_region['South Central'] + pop_region['Madison']
vaccine_region['Outer South Central'] = vaccine_region['South Central'] - vaccine_region['Madison']
vaccine_region['Total South Central'] = vaccine_region['South Central']
# drop these labels as now inconsistent
vaccine_region = vaccine_region.drop(['Southeast', 'South Central'])
pop_region = pop_region.drop(['Southeast', 'South Central'])
# per-capita vaccinations
vaccine_capita = vaccine_region / pop_region
# Data frame with all relevant data
vaccine_region = pd.DataFrame({'Vaccines': vaccine_region, 'Population': pop_region}, index=pop_region.index)
vaccine_region['Vaccinations per 100 people'] = vaccine_region['Vaccines'] / vaccine_region['Population'] * 100
#%% Shares of vaccine/population.
share_region = vaccine_region / vaccine_region.loc['WI']
share_region.Vaccines = pd.to_numeric(share_region.Vaccines)
share_region.Population = pd.to_numeric(share_region.Population)
share_region = share_region.drop(['WI', 'Total Southeast', 'Total South Central'], axis=0)
share_region = share_region.reset_index()
#%% Plots
plot_region = vaccine_region.loc[['Madison', 'Outer South Central', 'Milwaukee', 'Outer Southeast',
'Fox Valley', 'Northeast', 'North Central', 'Northwest', 'Western']]
plot_region = plot_region.reset_index()
def perc_to_text(p):
return '{:0.1f}'.format(p)
textlabels = plot_region['Vaccinations per 100 people'].apply(perc_to_text)
fig = px.bar(
plot_region,
x='Region',
y='Vaccinations per 100 people',
text=textlabels,
color='Region',
title='Vaccinated by region',
color_discrete_map=color_dict,
width=700,
height=550,
)
fig.update_traces(textposition='outside')
# # vaccine share vs. population share
# fig = px.bar(
# share_region,
# x='Region',
# y=['Vaccines', 'Population'],
# # text=textlabels,
# # facet_col='variable',
# # facet_col_wrap=2,
# # color='variable',
# # color_discrete_sequence=col_colors,
# # labels={'variable': '', 'Percentage':'Percent of total'},
# barmode='group',
# title='WI Vaccine Share by Region',
# width=700,
# height=700,
# )
# # take out 'variable=' part of the axis titles
# fig.for_each_annotation(
# lambda a: a.update(
# text=a.text.split("=")[-1],
# font=dict(size=15),
# )
# # )
# fig.update_traces(textposition='outside')
fig.update_traces(marker_line_color='gray')
# other layout
# fig.update_layout(showlegend=False)
pplot(fig,
filename='.\\docs\\assets\\plotly\\Vaccination-Region.html',
include_plotlyjs='cdn',
)
save_png = '.\\docs\\assets\\Vaccination-Region.png'
fig.write_image(
save_png,
width=700,
height=550,
engine='kaleido',
)
os.startfile(save_png)