-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateGeo.py
135 lines (102 loc) · 3.93 KB
/
UpdateGeo.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
# -*- 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
# 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
#%%
# 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)
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]
# set any negative values to 0
def zeroneg(x):
if x > 0:
return x
else:
return 0
hosp_for_map = hosp_for_map.apply(zeroneg)
countiesWI['Hospitalizations'] = hosp_for_map
countiesWI['Hospitalizations per 100K'] = countiesWI['Hospitalizations'] / countiesWI['Population'] * 100000
#%% 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.07
# append 'County' for display names
display_names = [n + ' County' for n in countiesWI.index]
# set scales for sizes of bubbles
popscale = 300
cases_size_factor = 0.12
hosp_size_factor = 0.015 # now fixed, new hospitalizations I think are very lagged, have more to do with reporting than actual hospitalizations
# hosp_size_factor = cases_size_factor*.10 # so that bubbles are same size if hosp = 10% of cases
cases_color_range = [0, 30]
hosp_color_range=[0, 7]
#%% 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\\_includes\\plotly\\Map-Cases-WI.html',
fig_height=600,
showfig=False,
)
#%% 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\\_includes\\plotly\\Map-Hosp-WI.html',
fig_height=600,
showfig=False,
)