-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkGeo.py
305 lines (241 loc) · 10.7 KB
/
WorkGeo.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
# -*- coding: utf-8 -*-
"""
Create interactive maps for cases and hospitalizations using Plotly.
"""
import geopandas as gpd
import covid
#%% Get the coviddata
# 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')
#%% Geography work
# WI DNR shapefile - doesn't have lake winnebago either, so never mind
# shapefile = 'data\\geo\\WI_County_Boundaries_24K.shp'
# countiesWI = gpd.read_file(shapefile)
# countiesWI['NAME'] = countiesWI.COUNTY_NAM
# shapefile from US census - doesn't have lake winnebago which is annoying
shapefile = 'data\\geo\\cb_2019_us_county_500k.shp'
# read data set of all USA counties
countiesUSA = gpd.read_file(shapefile)
# filter on wisconsin
countiesWI = countiesUSA[countiesUSA.STATEFP == '55']
# reindex on county name
countiesWI = countiesWI.set_index('NAME')
# sort by name
countiesWI = countiesWI.sort_index()
# add Population column
countiesWI['Population'] = popdata
# # chloropleth by population from geopandas
# base = countiesWI.plot(column='Population', edgecolor='w', linewidth=0.5)
#%%
# create new hospitalizations column; need to sort by date first
widata = widata.sort_values('Date')
widata = widata.assign(HOSP_NEW = widata.groupby('NAME').HOSP_YES.diff(periods=1))
# reduce and rename columns
col_rename = {'Date': 'Date', 'NAME': 'County', 'POS_NEW': 'Cases', 'TEST_NEW': 'Tests', 'DTH_NEW': 'Deaths', 'HOSP_NEW': 'Hospitalizations'}
reduced = widata[col_rename.keys()]
reduced = reduced.rename(columns=col_rename)
# 7-day rolling average
# reset_index() at end to take result back to original format, instead of counties as a MultiIndex
# reduced_avg = reduced.groupby('County').rolling(window=7, on='Date', center=False).mean().reset_index(level=0)
# last_avg = reduced_avg[reduced_avg.Date == reduced_avg.Date.max()]
# not working...something wrong here.
avg_window = 7
# isolate cases
cases = reduced.pivot(index='Date', columns='County', values='Cases')
cases_avg = cases.rolling(window=avg_window, center=False).mean()
cases_for_map = cases_avg.iloc[-1]
countiesWI['Cases'] = cases_for_map
countiesWI['Cases per 100K'] = countiesWI['Cases'] / countiesWI['Population'] * 100000
# hospitalizations
hosp = reduced.pivot(index='Date', columns='County', values='Hospitalizations')
hosp_avg = hosp.rolling(window=avg_window, center=False).mean()
hosp_for_map = hosp_avg.iloc[-1]
countiesWI['Hospitalizations'] = hosp_for_map
countiesWI['Hospitalizations per 100K'] = countiesWI['Hospitalizations'] / countiesWI['Population'] * 100000
#%% Plotly
import json
import plotly.express as px
from plotly.offline import plot as pplot
import plotly.graph_objects as go
do_choropleth = False
plotcol = 'Cases per 100K'
plotcol2 = 'Hospitalizations per 100K'
hosp_scale = [0, 4]
# filter counties shapefile to WI, convert to JSON format string, then decode
# to dictionary with json.loads()
countiesJS = json.loads(countiesWI.to_json())
#%% Choropleth maps
if do_choropleth:
fig = px.choropleth(countiesWI,
geojson=countiesJS,
locations=countiesWI.index,
color=plotcol,
color_continuous_scale=px.colors.sequential.Blues,
title='Cases by County',
projection='mercator')
fig.update_geos(fitbounds='locations', visible=False)
pplot(fig, filename='.\\plots\\plotly\\temp.html' )
fig2 = px.choropleth(countiesWI,
geojson=countiesJS,
locations=countiesWI.index,
color=plotcol2,
color_continuous_scale=px.colors.sequential.Oranges,
range_color=hosp_scale,
title='Hospitalizations by County',
projection='mercator')
fig2.update_geos(fitbounds='locations', visible=False)
pplot(fig2, filename='.\\plots\\plotly\\temp2.html' )
#%% Bubble map - size is numbers, color is per-population
# get latitude and longitude of centroids of counties for plotting
# this will give warning but I don't care
countiesWI['plotlon'] = countiesWI.geometry.centroid.x
countiesWI['plotlat'] = countiesWI.geometry.centroid.y
# move Milwaukee's plot center to the right a bit to make more room for its bubble
countiesWI.loc['Milwaukee', 'plotlon'] = countiesWI.loc['Milwaukee', 'plotlon'] + 0.15
# append 'County' for display names
display_names = [n + ' County' for n in countiesWI.index]
# set scales for sizes of bubbles
popscale = 300
casescale = 0.3
cases_size_factor = casescale
hospscale = casescale*.05 # so that bubbles are same size if hosp = 5% of cases
hosp_scale = [0, 7]
case_scale = [0, 140]
cases_color_range = case_scale
hosp_size_factor=hospscale
hosp_color_range=hosp_scale
#%% Cases figure
covid.plotly_colorbubble(
countiesWI,
sizecol='Cases',
colorcol='Cases per 100K',
size_factor=cases_size_factor,
color_range=cases_color_range,
colorscale='Blues',
location_names=display_names,
plotlabels=dict(title='Cases by County<br>(Daily, 7-day avg)'),
savefile='.\\docs\\assets\\plotly\\Map-Cases-WI.html',
fig_height=600,
)
#%% Hospitalizations figure
covid.plotly_colorbubble(
countiesWI,
sizecol='Hospitalizations',
colorcol='Hospitalizations per 100K',
size_factor=hosp_size_factor,
color_range=hosp_color_range,
colorscale='Oranges',
location_names=display_names,
plotlabels=dict(
title='Hospitalizations by County<br>(Daily, 7-day avg)',
colorlabel='Hosp per 100K',
),
savefile='.\\docs\\assets\\plotly\\Map-Hosp-WI.html',
fig_height=600,
)
#%% Total people tested figure
# total tests
temp = widata.loc[widata.Date == widata.Date.max()]
temp['People tested'] = temp.POSITIVE + temp.NEGATIVE
temp = temp.rename(columns={'NAME':'County'})
tested_for_map = temp.set_index('County')['People tested']
countiesWI['Tested'] = tested_for_map
countiesWI['Tested %'] = countiesWI['Tested'] / countiesWI['Population'] * 100
covid.plotly_colorbubble(
countiesWI,
sizecol='Tested',
colorcol='Tested %',
size_factor=100,
color_range=[0, 60],
colorscale='Greens',
location_names=display_names,
plotlabels=dict(
title='People Tested by County<br>(Total)',
colorlabel='Tested %',
),
savefile='.\\docs\\assets\\plotly\\Map-TotalTested-WI.html',
fig_height=600,
)
#%% Create background to figures
# line_colors = {'land':'darkgrey', 'border':'lightgray', 'marker':'dimgray'}
# line_colors = {'land':'darkgrey', 'border':'dimgray', 'marker':'lightgray'}
# line_colors = {'land':'lightgray', 'border':'white', 'marker':'dimgray'}
line_colors = {'land':'lightgray', 'border':'darkgray', 'marker':'dimgray'}
# background map of counties
fig_bkgd = px.choropleth(countiesWI,
geojson=countiesJS,
locations=countiesWI.index,
color_discrete_sequence=[line_colors['land']],
width=600,
height=600,
projection='mercator')
# turn off hover tooltips for this layer - have to set both of these because
# hovertemplate is set automatically and it supersedes hoverinfo.
# Also take out legend because it's not very useful right now; I could add
# a fancier custom legend later.
fig_bkgd.update_traces(hovertemplate=None,
hoverinfo='skip',
marker_line_color=line_colors['border'],
showlegend=False)
#%% Complete the figures - cases
plotcol = 'Cases per 100K'
# copy background
fig_cases = go.Figure(fig_bkgd)
fig_cases.update_layout(title='Cases by County<br>(7-day avg)')
fig_cases.add_trace(go.Scattergeo(lon=countiesWI.plotlon,
lat=countiesWI.plotlat,
text=display_names,
customdata=countiesWI.Population,
marker=dict(size=countiesWI.Cases, #size=countiesWI.Population / popscale,
sizeref=casescale,
color=countiesWI[plotcol],
cmin=case_scale[0],
cmax=case_scale[1],
sizemode='area',
colorscale='Blues'),
line=dict(color=line_colors['marker']),
hovertemplate=
'<b>%{text}</b><br>' +
'Population: %{customdata:.0f}<br>' +
'Cases: %{marker.size:.1f}<br>' +
'Cases per 100K : %{marker.color:.1f}'+
'<extra></extra>'
))
# only display wisconsin counties, not whole world
fig_cases.update_geos(fitbounds='locations', visible=False)
pplot(fig_cases,
filename='.\\docs\\assets\\plotly\\Map-Cases-WI-1.html',
include_plotlyjs='cdn')
#%% Hospitalizations
plotcol = 'Hospitalizations per 100K'
# copy background
fig_hosp = go.Figure(fig_bkgd)
fig_hosp.update_layout(title='Hospitalizations by County<br>(7-day avg)')
fig_hosp.add_trace(go.Scattergeo(lon=countiesWI.plotlon,
lat=countiesWI.plotlat,
text=display_names,
customdata=countiesWI.Population,
marker=dict(size=abs(countiesWI.Hospitalizations),
sizeref=hospscale,
color=countiesWI[plotcol],
cmin=hosp_scale[0],
cmax=hosp_scale[1],
sizemode='area',
colorscale='Oranges'),
line=dict(color=line_colors['marker']),
hovertemplate=
'<b>%{text}</b><br>' +
'Population: %{customdata:.0f}<br>' +
'Hospitalizations: %{marker.size:.1f}<br>' +
'Hospitalizations per 100K: %{marker.color:.1f}' +
'<extra></extra>'
))
fig_hosp.update_geos(fitbounds='locations', visible=False)
pplot(fig_hosp,
filename='.\\docs\\assets\\plotly\\Map-Hosp-WI-1.html',
include_plotlyjs='cdn')