forked from russss/covidtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
300 lines (258 loc) · 7.71 KB
/
main.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
import json
import logging
import pandas as pd
from datetime import date
import coviddata.uk
import coviddata.uk.scotland
import coviddata.uk.wales
import coviddata.world
import sys
import os
from graphs import (
uk_cases_graph,
regional_cases,
case_ratio_heatmap,
case_ratio,
hospital_admissions_graph,
uk_test_positivity,
uk_test_capacity,
)
from graphs.genomics import (
fetch_cog_metadata,
genomes_by_nation,
mutation_prevalence,
lineage_prevalence,
)
from graphs.vaccine import vax_rate_graph, vax_cumulative_graph
from graphs.app import risky_venues, app_keys
from graphs.tadpole import la_tadpole
from graphs.unlocking import unlocking_graph
from template import render_template
from map import map_data
from score import calculate_score
from corrections import correct_scottish_data, cases_by_nhs_region
from nhs_app import NHSAppData
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("urllib3").setLevel(logging.INFO)
log = logging.getLogger(__name__)
log.info("Generating pages...")
la_region = pd.read_csv(
"https://raw.githubusercontent.com/russss/local_authority_nhs_region"
"/master/local_authority_nhs_region.csv",
index_col=["la_gss"],
)
def online_triage_by_nhs_region():
triage_online = coviddata.uk.triage_nhs_online()
triage = (
triage_online.sum(["age_band", "sex"])
.assign_coords(
{
"ccg": [
ccg_lookup["NHSER20NM"].get(i.item()) for i in triage_online["ccg"]
]
}
)
.rename(ccg="region")
.groupby("region")
.sum()
)
triage["count_rolling_7"] = (
triage["count"].fillna(0).rolling(date=7, center=True).mean().dropna("date")
)
return triage
def pathways_triage_by_nhs_region():
triage_pathways = coviddata.uk.triage_nhs_pathways()
triage_pathways = triage_pathways.where(
triage_pathways.ccg.str.startswith("E"), drop=True
)
triage = (
triage_pathways.sum(["age_band", "sex", "site_type"])
.assign_coords(
{
"ccg": [
ccg_lookup["NHSER20NM"].get(i.item())
for i in triage_pathways["ccg"]
]
}
)
.groupby("ccg")
.sum()
.rename(ccg="region")
)
triage["count_rolling_7"] = (
triage["count"].fillna(0).rolling(date=7, center=True).mean().dropna("date")
)
return triage
ccg_lookup = (
pd.read_csv("./data/ccg_region.csv").drop_duplicates("CCG20CD").set_index("CCG20CD")
)
populations = pd.read_csv("./data/region_populations.csv", thousands=",")
populations = populations[populations["Code"].str.len() == 9]
populations = (
populations.rename(columns={"Code": "gss_code"})
.set_index("gss_code")["All ages"]
.to_xarray()
)
scot_populations = pd.read_csv("./data/scot_populations.csv", thousands=",").set_index(
"gss code"
)
provisional_days = 5
uk_cases = coviddata.uk.cases_phe("countries")
uk_cases["cases_rolling"] = (
uk_cases["cases"].diff("date").rolling(date=7, center=True).mean().dropna("date")
)
eng_by_gss = coviddata.uk.cases_phe("ltlas", key="gss_code")
eng_by_gss["cases_rolling_14"] = (
eng_by_gss["cases"].diff("date").rolling(date=14, center=True).mean()
)
eng_by_gss["cases_norm"] = eng_by_gss["cases"] / populations
nhs_region_cases = cases_by_nhs_region(eng_by_gss, la_region)
nhs_region_cases["cases_rolling"] = (
nhs_region_cases["cases"]
.diff("date")
.rolling(date=7, center=True)
.mean()
.dropna("date")
)
hospital_admissions = coviddata.uk.hospitalisations_phe()
hospital_admissions["admissions_rolling"] = (
hospital_admissions["admissions"]
.diff("date")
.rolling(date=7, center=True)
.mean()
.dropna("date")
)
excess_deaths = pd.read_csv(
"./data/excess_deaths.csv", index_col="date", parse_dates=["date"], dayfirst=True
)
triage_online = None
triage_pathways = None
by_age = coviddata.uk.cases_by_age()
by_report_date = coviddata.uk.cases_phe(basis="report")
render_template(
"index.html",
graphs={
"confirmed_cases": uk_cases_graph(uk_cases),
"regional_cases": regional_cases(nhs_region_cases),
"case_ratio_heatmap": case_ratio_heatmap(by_age),
"hospital_admissions": hospital_admissions_graph(hospital_admissions),
"case_ratio_england": case_ratio(by_report_date),
"case_ratio_scotland": case_ratio(by_report_date, "Scotland"),
},
scores=calculate_score(
nhs_region_cases,
triage_online,
triage_pathways,
hospital_admissions,
),
sources=[
(
"UKHSA",
"Coronavirus (COVID-19) in the UK",
"https://coronavirus.data.gov.uk",
uk_cases.attrs["date"],
),
],
)
testing = coviddata.uk.tests_phe()
cases_by_publish_date = coviddata.uk.cases_phe(by="overview", basis="report")
uk_cases_pub_date = cases_by_publish_date.sel(location="United Kingdom").diff("date")[
"cases"
]
uk_cases_pub_date = uk_cases_pub_date.where(uk_cases_pub_date > 0)
positivity = uk_cases_pub_date / (
testing["newPillarOneTestsByPublishDate"]
+ testing["newPillarTwoTestsByPublishDate"]
)
render_template(
"testing.html",
graphs={
"positivity": uk_test_positivity(positivity),
"test_capacity": uk_test_capacity(testing),
},
sources=[
(
testing.attrs["source"],
"Coronavirus (COVID-19) in the UK",
testing.attrs["source_url"],
testing.attrs["date"],
)
],
)
positivity = coviddata.uk.test_positivity()
vaccine_uptake = coviddata.uk.vaccination_uptake_by_area()
render_template(
"map.html",
data=json.dumps(map_data(eng_by_gss, positivity, provisional_days, vaccine_uptake)),
provisional_days=provisional_days,
sources=[
(
"UKHSA",
"Coronavirus (COVID-19) in the UK",
"https://coronavirus.data.gov.uk",
uk_cases.attrs["date"],
),
],
)
vax_data = coviddata.uk.vaccinations()
vax_uptake = coviddata.uk.vaccination_uptake_by_area_date()
render_template(
"vaccination.html",
graphs={
"vax_rate": vax_rate_graph(vax_data),
"vax_cumulative": vax_cumulative_graph(vax_data),
},
sources=[
(
"UKHSA",
"Coronavirus (COVID-19) in the UK",
"https://coronavirus.data.gov.uk",
vax_data.attrs["date"],
)
],
)
if os.environ.get("SKIP_SLOW"):
print("SKIPPING SLOW STUFF")
sys.exit(1)
app_data = NHSAppData()
exposures = app_data.exposures()
render_template(
"app.html",
graphs={
"risky_venues": risky_venues(app_data.risky_venues()),
"app_keys": app_keys(exposures),
"app_keys_risk": app_keys(exposures, by="interval"),
},
sources=[
(
"Russ Garrett",
"NHS COVID-19 App Data",
"https://github.com/russss/nhs-covid19-app-data",
date.today(),
)
],
risky_venues_count=app_data.risky_venues().count()["id"],
risky_venues_unique=len(pd.unique(app_data.risky_venues()["id"])),
)
cog_metadata = fetch_cog_metadata()
try:
lin_prev = lineage_prevalence(cog_metadata)
except Exception:
print("Error generating lineage prevalence")
lin_prev = None
render_template(
"genomics.html",
graphs={
"genomes_by_nation": genomes_by_nation(cog_metadata),
"mutation_prevalence": mutation_prevalence(cog_metadata),
"lineage_prevalence": lin_prev,
},
sources=[
(
"COVID-19 Genomics UK (COG-UK) Consortium",
"Latest sequence metadata",
"https://www.cogconsortium.uk/",
date.today(),
)
],
)