/
covidNeighborhoodEngland.py
414 lines (322 loc) · 16.6 KB
/
covidNeighborhoodEngland.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# ---
# jupyter:
# jupytext:
# cell_metadata_filter: title,-all
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.6.0
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %% [markdown]
# ## Comparing Covid-19 cases with University population
# This code loads data from various places regarding covid-19 cases and University information. These data are defined
# at the Middle Layer Super Output Are (MSOA) level
# %%
import pandas
import numpy as np
import requests
import os.path
import time
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
#from uk_covid19 import Cov19API
# %% Define functions.
# Calculate distances between lat/lon in miles.
def haversine(lon1, lat1, lon2, lat2):
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
newlon = lon2 - lon1
newlat = lat2 - lat1
haver_formula = np.sin(newlat / 2.0) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(newlon / 2.0) ** 2
dist = 2 * np.arcsin(np.sqrt(haver_formula))
dist = 3958 * dist # 6367 for distance in KM for miles use 3958
return dist
# simple wrapper to download datafiles from source.
def get_file(url,filename):
#local_filename = url.split('/')[-1]
local_filename = filename
r = requests.get(url)
open(local_filename, 'wb').write(r.content)
return
def commonPlotDecoration(ax):
plt.xlabel('')
ax.set_xlim(right=ax.get_xlim()[1] + 1)
# ax.xaxis.set_major_formatter( ConciseDateFormatter( '%b' ) )
plt.annotate('Created by Justin Ales, code available: https://github.com/j-ales/covid19-neighborhood',
(0, 0), (20, -50), xycoords='axes fraction',
textcoords='offset points', va='top',
fontsize=8)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# %% Load/fetch data as needed.
#This file contains the population weighted centroid location of each MSOA
if not os.path.isfile('./MSOA_2011_EW_PWC_COORD_V2.CSV'):
url = "https://github.com/ITSLeeds/pct/releases/download/0.2.5/MSOA_2011_EW_PWC_COORD_V2.CSV"
filename = url.split('/')[-1]
get_file(url,filename)
# The sit: http://learning-provider.data.ac.uk contains lots of information for univeristy
# This includes the main locaiton of the university. But doesn't include all campuses.
if not os.path.isfile('./learning-providers-plus.csv'):
url = "http://learning-provider.data.ac.uk/data/learning-providers-plus.csv"
filename = url.split('/')[-1]
get_file(url, filename)
# # Get the latest case data from gov.uk
filename = './MSOAs_latest.csv'
# # if the file doesn't exist or it is more than 24 hours old
if not os.path.isfile(filename) or \
(time.time() - os.path.getmtime(filename)) / (60 * 60) > 24:
print('Getting updated case data')
#url = "https://coronavirus.data.gov.uk/downloads/msoa_data/MSOAs_latest.csv"
url = "https://coronavirus.data.gov.uk/api/v2/data?areaType=msoa&metric=newCasesBySpecimenDateRollingSum&metric=newCasesBySpecimenDateRollingRate&metric=newCasesBySpecimenDateChange&metric=newCasesBySpecimenDateDirection&format=csv"
get_file(url,filename)
# %% Load the data.
MSOA_centroids = pandas.read_csv('./MSOA_2011_EW_PWC_COORD_V2.CSV')
#The MSO population data comes from this table from the ONS:
#Table SAPE22DT4: Mid-2019 Population Estimates for Middle Layer Super Output Areas
#in England and Wales by Single Year of Age and Sex - Supporting Information
MSOA_pop = pandas.read_csv('./MSOA_pop.csv')
HE_centroids = pandas.read_csv('./learning-providers-plus.csv')
covidCases = pandas.read_csv('./MSOAs_latest.csv')
covidFileTimeStamp = os.path.getmtime('./MSOAs_latest.csv')
#Pop Density comes from Table QS102EW
#https://www.nomisweb.co.uk/census/2011/qs102uk
MSOA_density=pandas.read_csv('./MSOA_density.csv')
MSOA_density['msoacd']=MSOA_density['msoacd'].str.replace(' : (.*)','',regex=True)
#The student population numbers come from Table ID: DC6108EW, from the 2011 census
#https://www.nomisweb.co.uk/census/2011/dc6108ew
MSOA_student_pop = pandas.read_csv('./MSOA_student_pop.csv')
del MSOA_student_pop['date']
#Number of employees in bars/restuarants comes from the employee survey:
#https://www.nomisweb.co.uk/datasets/newbres6pub
MSOA_pubs = pandas.read_csv('./MSOA_pubs_bars.csv')
MSOA_pubs['msoacd']=MSOA_pubs['msoacd'].str.replace(' : (.*)','',regex=True)
#Definitions for several groupings of univeristies.
russell_group_rows = (HE_centroids['GROUPS'].str.contains('Russell_Group') == True)
million_plus_rows = (HE_centroids['GROUPS'].str.contains('Million') == True)
UA_rows = (HE_centroids['GROUPS'].str.contains('University_Alliance') == True)
# You can uncomment and/or change theses lines to pick a selection of unis
unis_2_keep = np.ones((len(HE_centroids.index))) > 0 # All Unis
# unis_2_keep = russell_group_rows | million_plus_rows
# unis_2_keep = russell_group_rows
HE_centroids = HE_centroids.loc[unis_2_keep, :]
HE_lon = HE_centroids['LONGITUDE']
HE_lat = HE_centroids['LATITUDE']
# Calculate distances. NOTE: Using a for loop like this is a very slow way to do it
# but is generally more clear to read.
allDist = np.zeros((len(MSOA_centroids.index), len(HE_centroids.index)))
for index, row in MSOA_centroids.iterrows():
this_MSOA_lon = row['LONGITUDE']
this_MSOA_lat = row['LATITUDE']
theseDist = haversine(this_MSOA_lon, this_MSOA_lat, HE_lon, HE_lat)
allDist[index,] = theseDist
#Calculate the distance to nearest university.
uni_distance = np.nanmin(allDist, axis=1)
rg_distance = np.nanmin(allDist[:, russell_group_rows], axis=1)
mp_distance = np.nanmin(allDist[:, million_plus_rows], axis=1)
closestUniIdx = np.nanargmin(allDist, axis=1)
tmp = allDist
#Stupid way to remove non russell group from disance calc. Just set the distance to highvalue
tmp[:, (-1*russell_group_rows.values+1)==1]=10000
closestRgIdx = np.nanargmin(tmp, axis=1)
uni_name = HE_centroids['PROVIDER_NAME'].iloc[closestUniIdx].values
rg_name = HE_centroids['PROVIDER_NAME'].iloc[closestRgIdx].values
groups = HE_centroids['GROUPS'].iloc[closestUniIdx].values
MSOA_centroids.insert(1, "uni_distance", uni_distance)
MSOA_centroids.insert(1, "rg_distance", rg_distance)
MSOA_centroids.insert(1, "mp_distance", mp_distance)
MSOA_centroids.insert(1, "uni_name", uni_name)
MSOA_centroids.insert(1, "rg_name", rg_name)
MSOA_centroids.insert(1, "GROUPS", groups)
# %% Make a plot of cases per week that classifies MSOAs by distance to universities.
# Put together the various dataframes above into one merged dataframe.
merged = pandas.merge(left=MSOA_centroids, right=covidCases, left_on='MSOA11CD', right_on='areaCode')
merged = pandas.merge(left=merged, right=MSOA_pop, left_on='MSOA11CD', right_on='MSOA Code')
merged = pandas.merge(left=merged, right=MSOA_student_pop, left_on='MSOA11CD', right_on='MSOA Code')
merged = pandas.merge(left=merged, right=MSOA_density, left_on='MSOA11CD', right_on='msoacd')
merged = pandas.merge(left=merged, right=MSOA_pubs, left_on='MSOA11CD', right_on='msoacd')
merged['date'] = pandas.to_datetime(merged['date'],format='%Y-%m-%d')
merged = merged.replace(-99, 0)
merged = merged.sort_values('uni_distance')
#Normalize by MSOA population
#merged['latest_7_days_per_100k'] = 1e5 * merged.filter(like='latest_7_days').div(merged['Total Pop'], axis=0)
merged['Student Percentage'] = merged['Student Population'] / merged['Total Pop']
# %%
# Separate English MSOA by student concentation.
###
popThresh = .3
###
highStudent = merged.loc[merged['Student Percentage'] >= popThresh]
lowStudent = merged.loc[merged['Student Percentage'] < popThresh]
highData= highStudent.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
lowData= lowStudent.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
numberHigh=len(highStudent['areaCode'].unique())
numberLow=len(lowStudent['areaCode'].unique())
fig, ax = plt.subplots(1, 1)
highData.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
lowData.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
plt.legend(['{} areas with students {:.0%}+ of pop.'.format(numberHigh,popThresh),
'{} areas with students less then {:.0%} of pop.'.format(numberLow,popThresh)],
frameon=False,fontsize=12,loc="lower left",bbox_to_anchor=(0,1.1))
plt.ylabel('Weekly Cases per 100k')
plt.title('England MSOA Case Rates ')
ax = plt.gca()
commonPlotDecoration(ax)
plt.ylabel('Cases/100k Pop 7-day average')
plt.show()
# %% Separate data by student population concentration & distance to russel group universities
# Look at all Russel Group Universities in England as a group
###
popThresh = .22
distThresh = 1
####
#Which attributes to select data by.
#classifyBy = 'Density (number of persons per hectare)'
classifyBy = 'Student Percentage'
#Choose russell group set, or all universities.
#uniGroup = 'uni_distance'
uniGroup = 'rg_distance'
# close = merged.loc[merged['uni_distance'] < distThresh, :]
# far = merged.loc[merged['uni_distance'] >= distThresh, :]
#Select specific Uni
selectTheseUni = np.full(merged.shape[0], True)
close = merged.loc[( merged[classifyBy] >= popThresh) & (merged[uniGroup] < distThresh) & selectTheseUni , :]
far = merged.loc[ (merged[classifyBy] < popThresh) & (merged[uniGroup] < distThresh) & selectTheseUni, :]
rest = merged.loc[~((merged[uniGroup] < distThresh) & selectTheseUni), :]
numberClose=len(close['areaCode'].unique())
numberFar=len(far['areaCode'].unique())
numberRest=len(rest['areaCode'].unique())
close= close.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
far= far.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
rest= rest.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
fig, ax = plt.subplots(1, 1)
close.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
far.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
rest.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
plt.legend(['{} areas within {} miles of Russell Group university\n and students over {:.0%} of population'.format(numberClose,distThresh, popThresh),
'{} areas within {} miles of Russell Group university\n and students below {:.0%} of population'.format(numberFar,distThresh, popThresh),
'{} areas outside {} miles of a Russell Group university'.format(numberRest,distThresh)],
fontsize=12,loc="lower left",bbox_to_anchor=(0,1))
commonPlotDecoration(ax)
plt.ylabel('Cases/100k Pop 7-day average')
plt.show()
# %% Separate data by student population concentration & distance to Sheffield
# Pull out just U Sheffield
###
popThresh = .10
distThresh = 3
####
#Which attributes to select data by.
#classifyBy = 'Density (number of persons per hectare)'
classifyBy = 'Student Percentage'
#Choose russell group set, or all universities.
uniGroup = 'uni_distance'
#uniGroup = 'rg_distance'
# close = merged.loc[merged['uni_distance'] < distThresh, :]
# far = merged.loc[merged['uni_distance'] >= distThresh, :]
#Select specific Uni
selectTheseUni = np.full(merged.shape[0], True)
selectTheseUni = (merged['uni_name']=='UNIVERSITY OF SHEFFIELD')
close = merged.loc[( merged[classifyBy] >= popThresh) & (merged[uniGroup] < distThresh) & selectTheseUni , :]
far = merged.loc[ (merged[classifyBy] < popThresh) & (merged[uniGroup] < distThresh) & selectTheseUni, :]
rest = merged.loc[~((merged[uniGroup] < distThresh) & selectTheseUni), :]
numberClose=len(close['areaCode'].unique())
numberFar=len(far['areaCode'].unique())
numberRest=len(rest['areaCode'].unique())
close= close.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
far= far.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
rest= rest.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
fig, ax = plt.subplots(1, 1)
close.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
far.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
rest.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
plt.legend(['{} areas within {} miles of U of Sheffield\n and students over {:.0%} of population'.format(numberClose,distThresh, popThresh),
'{} areas within {} miles of U of Sheffield\n and students below {:.0%} of population'.format(numberFar,distThresh, popThresh),
'{} areas outside {} miles of U of Sheffield'.format(numberRest,distThresh)],
fontsize=12,loc="lower left",bbox_to_anchor=(0,1))
commonPlotDecoration(ax)
plt.ylabel('Cases/100k Pop 7-day average')
plt.show()
# %% Separate data by student population concentration & distance to Manchester
# Pull out just U Manchester
###
popThresh = .15
distThresh = 2
####
#Which attributes to select data by.
#classifyBy = 'Density (number of persons per hectare)'
classifyBy = 'Student Percentage'
#Choose russell group set, or all universities.
uniGroup = 'uni_distance'
#uniGroup = 'rg_distance'
# close = merged.loc[merged['uni_distance'] < distThresh, :]
# far = merged.loc[merged['uni_distance'] >= distThresh, :]
#Select specific Uni
selectTheseUni = np.full(merged.shape[0], True)
uniNameToChoose = 'THE UNIVERSITY OF MANCHESTER'
selectTheseUni = (merged['uni_name']==uniNameToChoose)
close = merged.loc[( merged[classifyBy] >= popThresh) & (merged[uniGroup] < distThresh) & selectTheseUni , :]
far = merged.loc[ (merged[classifyBy] < popThresh) & (merged[uniGroup] < distThresh) & selectTheseUni, :]
rest = merged.loc[ ~((merged[uniGroup] < distThresh) & selectTheseUni), :]
numberClose=len(close['areaCode'].unique())
numberFar=len(far['areaCode'].unique())
numberRest=len(rest['areaCode'].unique())
close= close.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
far= far.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
rest= rest.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
fig, ax = plt.subplots(1, 1)
close.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
far.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
rest.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
plt.legend(['{} areas within {} miles of U of Manchester\n and students over {:.0%} of population'.format(numberClose,distThresh, popThresh),
'{} areas within {} miles of U of Manchester\n and students below {:.0%} of population'.format(numberFar,distThresh, popThresh),
'{} areas outside {} miles of U of Manchester'.format(numberRest,distThresh)],
fontsize=12,loc="lower left",bbox_to_anchor=(0,1))
commonPlotDecoration(ax)
plt.ylabel('Cases/100k Pop 7-day average')
plt.show()
# %% Separate data by student population concentration & distance to Cambridge
# Pull out just U Cambridge
###
popThresh = .15
distThresh = 3
####
#Which attributes to select data by.
#classifyBy = 'Density (number of persons per hectare)'
classifyBy = 'Student Percentage'
#Choose russell group set, or all universities.
uniGroup = 'uni_distance'
#uniGroup = 'rg_distance'
# close = merged.loc[merged['uni_distance'] < distThresh, :]
# far = merged.loc[merged['uni_distance'] >= distThresh, :]
#Select specific Uni
selectTheseUni = np.full(merged.shape[0], True)
#selectTheseUni = (merged['uni_name']=='UNIVERSITY OF OXFORD') | (merged['uni_name']=='UNIVERSITY OF CAMBRIDGE')
#selectTheseUni = (merged['uni_name']=='UNIVERSITY OF OXFORD')
selectTheseUni = (merged['uni_name']=='UNIVERSITY OF CAMBRIDGE')
close = merged.loc[( merged[classifyBy] >= popThresh) & (merged[uniGroup] < distThresh) & selectTheseUni , :]
far = merged.loc[ (merged[classifyBy] < popThresh) & (merged[uniGroup] < distThresh) & selectTheseUni, :]
rest = merged.loc[~((merged[uniGroup] < distThresh) & selectTheseUni), :]
numberClose=len(close['areaCode'].unique())
numberFar=len(far['areaCode'].unique())
numberRest=len(rest['areaCode'].unique())
close= close.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
far= far.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
rest= rest.groupby(['date'])[['newCasesBySpecimenDateRollingRate']].mean()
fig, ax = plt.subplots(1, 1)
close.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
far.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
rest.plot(y='newCasesBySpecimenDateRollingRate',ax=ax,linewidth=3)
plt.legend(['{} areas within {} miles of Cambridge\n and students over {:.0%} of population'.format(numberClose,distThresh, popThresh),
'{} areas within {} miles of Cambridge\n and students below {:.0%} of population'.format(numberFar,distThresh, popThresh),
'{} areas outside {} miles of Cambridge'.format(numberRest,distThresh)],
fontsize=12,loc="lower left",bbox_to_anchor=(0,1))
commonPlotDecoration(ax)
plt.ylabel('Cases/100k Pop 7-day average')
plt.show()
# %%