-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCovidMaps.py
324 lines (195 loc) · 7.88 KB
/
CovidMaps.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python
# coding: utf-8
# # Covid-19 Week to Week % Change in New Cases by Parish
#
# This notebook will created maps for comparing weekly new cases by parish for Louisiana, as well as a timelaps video
#
# [Watch Latest Video](https://youtu.be/KHUjEgC_evk)
# In[1]:
#change to true to regenerate video
create_new_video = False
# In[2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.colors import Normalize
from matplotlib.dates import DateFormatter
import geoplot as gplt
import geopandas as gpd
import geoplot.crs as gcrs
import datetime
# In[3]:
df = gpd.read_file('./uscensus/cb_2018_us_county_500k.shp')
df = df[df['STATEFP']=='22']
df['FIPS'] = (df['STATEFP'].astype(int) * 1000) + df['COUNTYFP'].astype(int)
df.index = df['NAME']
df['GEOID'] = df['GEOID'].astype(int)
# In[4]:
df.head(2)
# In[5]:
covid_df = pd.read_excel("./LaDeptHealth/LA_COVID_TESTBYDAY_PARISH_PUBLICUSE.xlsx")
covid_df['Parish'] = covid_df['Parish'].str.replace('DeSoto', 'De Soto') #De Soto fix
# In[6]:
covid_df.index = covid_df['Lab Collection Date'].dt.strftime('%Y-%m-%d_') + covid_df['Parish']
covid_df.tail()
# In[7]:
curdate = startdate = covid_df['Lab Collection Date'].min()
stopdate = covid_df['Lab Collection Date'].max()
daydelta = datetime.timedelta(days=1)
weeks = []
curweek = []
while curdate <= stopdate:
curweek.append(curdate)
if len(curweek) == 7:
weeks.append(curweek)
curweek = []
curdate += daydelta
print(stopdate)
print(weeks[-1])
# In[8]:
colnames = []
la_df = pd.DataFrame(['Louisiana'], columns=['Name'],index=['Louisiana'])
for week in weeks:
parishes = []
totals = []
for parish in df['NAME']:
total = 0
for day in week:
try:
total += covid_df.loc[day.strftime('%Y-%m-%d_') + parish]['Daily Case Count']
except KeyError:
pass
totals.append(total)
parishes.append(parish)
la_total = np.sum(totals)
weekname = week[0].strftime('%Y-%m-%d')
newdataframe = pd.DataFrame(totals,index=parishes,columns=[weekname])
newladataframe = pd.DataFrame(la_total,index=['Louisiana'],columns=[weekname])
df = df.join(newdataframe,how='left')
la_df = la_df.join(newladataframe,how='left')
colnames.append(weekname)
# In[9]:
df.head(2)
# In[10]:
la_df.head()
# In[11]:
#sanity check
fig = plt.figure(figsize=(12,6))
plt.plot(df.loc['Orleans'][colnames], label='Orleans')
plt.plot(df.loc['St. Tammany'][colnames], label = 'Tammany')
plt.plot(df.loc['East Baton Rouge'][colnames], label = 'EBR')
_=plt.xticks(rotation=90)
plt.legend()
# In[12]:
#sanity check
fig = plt.figure(figsize=(12,6))
plt.bar(colnames,la_df.loc['Louisiana'][colnames], label='Louisiana')
_=plt.xticks(rotation=90)
# In[13]:
pop_df = pd.read_csv('./uscensus/co-est2019-alldata.csv', encoding='latin1')
pop_df = pop_df[pop_df['STATE']==22]
pop_df.index = (pop_df['STATE'] * 1000) + pop_df['COUNTY']
pop_df = pop_df[['STNAME', 'CTYNAME','POPESTIMATE2019']]
pop_df.head()
# In[14]:
la_df['FIPS'] = [22000]
df = df.join(pop_df,how='left', on='FIPS')
la_df = la_df.join(pop_df,how='left', on='FIPS')
# In[15]:
#Sanity
df.loc['Orleans']
# In[16]:
df.describe()
# In[17]:
df.loc['Orleans']
# In[18]:
#calculate percent changes
for lastweek, thisweek in zip(weeks[:-1],weeks[1:]):
lastweek_col = lastweek[0].strftime('%Y-%m-%d')
thisweek_col = thisweek[0].strftime('%Y-%m-%d')
change_col = thisweek_col + '_chg'
df[change_col] = ((df[thisweek_col] - df[lastweek_col]) / df[lastweek_col] * 100)
la_df[change_col] = ((la_df[thisweek_col] - la_df[lastweek_col]) / la_df[lastweek_col] * 100)
#print("Values for %s:\t%d" % (change_col, np.count_nonzero( df[change_col])))
df.describe()
# In[19]:
df.loc['Orleans']
# In[20]:
la_series = la_df.loc['Louisiana'][colnames]
la_series.index = pd.to_datetime(la_series.index)
# In[21]:
poplabelcutoff=50000
label_places = df[df['POPESTIMATE2019'] > poplabelcutoff]
extent=(-94.1,29,-88.6,33.05)
#generates labels
def labelgen(value):
if np.isnan(value) or np.isinf(value):
return 'NaN'
elif value > 0.0:
return '⇑' + str(int(value))
else:
return '⇓' + str(int(-value))
def getfacecolor(value):
return '#ffffff'
#if np.isnan(value) or np.isinf(value):
# return '#00BFFF'
#elif value > 0:
# return '#FF0000'
#else:
# return '#00FF00'
def plotme(week = weeks[-1][0], startweek=weeks[-1][0], webmap = False, setnorm=False, stddev=1, savename = 'RecentMap.jpg', closefig=False, figsize=(25,14)):
projectioncode = 'EPSG:3395'
fig = plt.figure(figsize=figsize)
spec = gridspec.GridSpec(ncols=2, nrows=1,width_ratios=[1,10])
colname = week.strftime('%Y-%m-%d') + '_chg'
validmask = np.logical_not(np.logical_or( np.isnan(df[colname]), np.isinf(df[colname]) ))
normmin,normmax = (df[validmask][colname].min(), df[validmask][colname].max())
print("DataMin=%d\tDataMax=%d"%(normmin,normmax))
if webmap:
ax1 = fig.add_subplot(spec[1],projection=gcrs.WebMercator())
gplt.webmap(df,ax=ax1)
projectioncode = 'EPSG:3785'
else:
ax1 = fig.add_subplot(spec[1],projection=gcrs.Mercator())
if setnorm:
norm = Normalize(vmin=setnorm[0],vmax=setnorm[1],clip=True)
else:
#maxmin = np.max(np.abs([df[colname].min(),df[colname].max()]))
#norm = Normalize(vmin=-maxmin,vmax=maxmin)
norm = Normalize(vmin=normmin, vmax=normmax.max())
gplt.choropleth(df[validmask],zorder=10,hue=colname, norm=norm, legend=True, ax=ax1,extent=extent, edgecolor='black', cmap='RdYlGn_r')
#color NaNs and inf
gplt.polyplot( df[np.logical_not(validmask)], facecolor='#0000FF', zorder=20, ax=ax1, extent=extent)
fig.suptitle("Covid-19 Week to Week %% Change in New Cases by Parish for %s" % week.strftime('%b %d, %Y').ljust(14))
ax2 = fig.add_subplot(spec[0])
ax2.set_title('Weekly New Cases for Louisiana')
ax2.barh(la_series.index,la_series, color='y', height=6.5, label='Weekly New Cases')
ax2.axhline(week,color='red',label='Current Week')
ax2.yaxis.set_major_formatter(DateFormatter('%b %d'))
for tick in ax2.get_yticklabels():
tick.set_rotation(90)
ax2.legend()
fig.text(0.78,0.93,week.strftime('%b %d, %Y').rjust(12), fontsize=18, fontfamily='monospace', bbox=dict(boxstyle='round',facecolor='#ffffa8',alpha=0.7))
#get outliers
mean = df[validmask][colname].mean()
outdev = df[validmask][colname].std() * stddev
print("outdev=%d" %outdev)
label_outliers = df[np.abs(df[colname] - mean) > outdev]
labels = label_outliers.append(label_places).drop_duplicates()
centroids = labels['geometry'].to_crs(projectioncode).centroid
for x,y, name in zip(centroids.x,centroids.y, labels['NAME']):
ax1.annotate(name.replace(' ', "\n"),xy=(x,y),xytext=(0, -6), textcoords="offset points", zorder=50, ha='center', va='top')
centroids = df['geometry'].to_crs(projectioncode).centroid
for x,y, value in zip(centroids.x,centroids.y, df[colname]):
ax1.annotate(labelgen(value),xy=(x,y),xytext=(0, 6), textcoords="offset points", zorder=50, ha='center', va='center',fontsize=15, bbox=dict(boxstyle='round',facecolor=getfacecolor(value),alpha=0.4))
fig.tight_layout()
if savename != None:
fig.savefig(savename)
if closefig:
plt.close()
return norm
#plot most recent map
#norm = plotme(webmap=False, week=weeks[-1][0])
norm = plotme(webmap=False, week=weeks[-1][0])
print("vmin=%d\tvmax=%d" % (norm.vmin,norm.vmax))