-
Notifications
You must be signed in to change notification settings - Fork 2
/
brondata.py
executable file
·477 lines (381 loc) · 16.8 KB
/
brondata.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/env python3
#
# Haalt COVID-19 testresultaten op bij RIVM en NICE
#
#
# TODO:
# https://coronadashboard.government.nl/landelijk/varianten -> SVG format, need to find the source.
# https://www.zelftestgedaan.nl/self_tests.json -> unreliable source, too little data.
#
import urllib.request
from urllib.error import URLError, HTTPError
import os.path
import datetime
import time
import json
import csv
import re
from scipy.signal import savgol_filter
from statistics import mean
from math import log
import sys
# Import python files in modules directory
sys.path.insert(0, 'modules')
from defaults import timezone
from datecache import dateCache
from utilities \
import readjson, writejson, \
downloadIfStale, downloadBinaryIfStale, \
decimalstring, isnewer, switchdecimals, \
logError
from metenisweten import metenisweten, initrecord, writeMetenIsWeten
from download_Rt import process as process_Rt
from download_Zkh import process as process_Zkh
from download_casus_landelijk import process as process_casus_landelijk
from download_varianten import process as process_varianten
from download_rioolwaterdata import process as process_rioolwaterdata
from download_lcps_ziekenhuisopnames import process as process_lcps_ziekenhuisopnames
from calculate_geschat_ziek import calculate as calculate_geschat_ziek
def downloadMostRecentAppleMobilityReport(filename):
if os.path.isfile(filename) and os.stat(filename).st_mtime > (time.time() - 3600):
# print(filename+" exists.")
return False
else:
print("Downloading fresh data to "+filename, end="...")
url = 'https://covid19-static.cdn-apple.com/covid19-mobility-data/2210HotfixDev16/v3/en-us/applemobilitytrends-2022-03-26.csv'
try:
urllib.request.urlretrieve(url, filename)
print("done")
return True
except (HTTPError, URLError) as err:
print("Error downloding %s: %s" % (url, str(err)))
raise Exception(
"Sorry, no Apple mobility data found. Check https://covid19.apple.com/mobility")
def downloadECDCMap():
logError("ECDC No longer generates a COVID indicator map")
return
url = "https://www.ecdc.europa.eu/en/covid-19/situation-updates/weekly-maps-coordinated-restriction-free-movement"
with urllib.request.urlopen(url) as response:
meta = response.headers
charset = meta.get_content_charset() or 'utf-8'
responsebody = response.read().decode(charset)
imglink = re.findall(
'https://www\.ecdc\.europa\.eu/.+/public/images/.*_CouncilMap\.png', responsebody)[0]
print("Download ECDC map from: "+imglink)
return downloadBinaryIfStale(
'../docs/extern/ECDC_Subnational_Combined_traffic.png',
imglink
)
def download():
freshdata = False
markerfile = '../docs/extern/ECDC_Subnational_Combined_traffic.png'
if os.path.isfile(markerfile):
lastdownload = datetime.datetime.fromtimestamp(
os.stat(markerfile).st_mtime, tz=timezone)
else:
lastdownload = datetime.datetime.fromtimestamp(0, tz=timezone)
if lastdownload > (datetime.datetime.now(tz=timezone) - datetime.timedelta(minutes=20)):
print("Downloaded ECDC map less than 20 minutes ago (%s), no new download." % lastdownload)
return True
freshdata = downloadECDCMap() or freshdata
freshdata = downloadIfStale(
'../cache/COVID-19_prevalentie.json',
'https://data.rivm.nl/covid-19/COVID-19_prevalentie.json'
) or freshdata
# Uitgevoerde tests van VOOR 2021, per week
freshdata = downloadIfStale(
'../cache/J535D165-RIVM_NL_test_latest.csv',
'https://raw.githubusercontent.com/J535D165/CoronaWatchNL/master/data-misc/data-test/RIVM_NL_test_latest.csv'
) or freshdata
# Uitgevoerde tests in 2021, per dag van RIVM
freshdata = downloadIfStale(
'../cache/COVID-19_uitgevoerde_testen.json',
'https://data.rivm.nl/covid-19/COVID-19_uitgevoerde_testen.json'
) or freshdata
freshdata = downloadIfStale(
'../cache/rijksoverheid-coronadashboard-NL.json',
'https://coronadashboard.rijksoverheid.nl/json/NL.json'
) or freshdata
freshdata = downloadIfStale(
'../cache/lcps-covid-19.csv',
'https://lcps.nu/wp-content/uploads/covid-19.csv'
) or freshdata
freshdata = downloadIfStale(
'../cache/COVID-19_Infectieradar_symptomen_per_dag.json',
'https://data.rivm.nl/covid-19/COVID-19_Infectieradar_symptomen_per_dag.json'
) or freshdata
freshdata = downloadIfStale(
'../cache/COVID-19_vaccinatiegraad_per_gemeente_per_week_leeftijd.json',
'https://data.rivm.nl/covid-19/COVID-19_vaccinatiegraad_per_gemeente_per_week_leeftijd.json'
) or freshdata
return freshdata
def isvaliddate(datestring, filename):
dateCache.isvaliddate(datestring, filename)
# https://en.wikipedia.org/wiki/Savitzky%E2%80%93Golay_filter
def smooth(inputArray):
return double_savgol(inputArray, 2, 13, 1)
# https://en.wikipedia.org/wiki/Savitzky%E2%80%93Golay_filter
def double_savgol(inputArray, iterations, window, order):
outputArray = inputArray
while iterations > 0:
outputArray = savgol_filter(outputArray, window, order)
iterations = iterations - 1
return outputArray
def intOrNone(input):
try:
return int(input)
except TypeError:
return None
except ValueError:
return None
def intOrZero(input):
try:
return int(input)
except TypeError:
return 0
except ValueError:
return 0
def builddaily():
print('Transform per-case data to daily totals')
filename = '../cache/COVID-19_casus_landelijk.json'
# print("Add hospitalization data")
# filename = '../cache/NICE-intake-count.json'
# with open(filename, 'r') as json_file:
# data = json.load(json_file)
# print("Load veiligheidsregios for addign to sewage data")
# filename = '../data/veiligheidsregios.json'
# with open(filename, 'r') as json_file:
# veiligheidsregios = json.load(json_file)
print("Add estimated ill based on RIVM (prevalentie)")
filename = '../cache/COVID-19_prevalentie.json'
with open(filename, 'r') as json_file:
data = json.load(json_file)
cachedDate = None
cachedDateValid = False
for record in data:
stringdate = record['Date']
if cachedDate != stringdate:
cachedDate = stringdate
if not dateCache.isvaliddate(stringdate, filename):
cachedDateValid = False
else:
cachedDateValid = True
if not cachedDateValid:
continue
try:
metenisweten[stringdate]['rivm_schatting_besmettelijk']['min'] = int(
record['prev_low'])
metenisweten[stringdate]['rivm_schatting_besmettelijk']['max'] = int(
record['prev_up'])
metenisweten[stringdate]['rivm_schatting_besmettelijk']['value'] = int(
record['prev_avg'])
except ValueError as e:
print('Ignored: ', e)
pass
except KeyError as e:
print('Ignored: ', e)
pass
print("Add infectieradar percentage met COVID-19 klachten")
filename = '../cache/COVID-19_Infectieradar_symptomen_per_dag.json'
with open(filename, 'r') as json_file:
data = json.load(json_file)
cachedDate = None
cachedDateValid = False
for record in data:
stringdate = record['Date_of_statistics']
if cachedDate != stringdate:
cachedDate = stringdate
if not dateCache.isvaliddate(stringdate, filename):
cachedDateValid = False
else:
cachedDateValid = True
if not cachedDateValid:
continue
try:
percentage = float(record['Perc_covid_symptoms'])
metenisweten[stringdate]['rivm_infectieradar_perc'] = percentage
except TypeError as e:
print('Ignored: ', e)
pass
except ValueError as e:
print('Ignored: ', e)
pass
except KeyError as e:
print('Ignored: ', e)
pass
print("Add total tests (until 2020, weekbasis)")
filename = '../cache/J535D165-RIVM_NL_test_latest.csv'
with open(filename, 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count > 0:
# Jaar,Week,BeginDatum,EindDatum,Bron,AantalLaboratoria,Type,Aantal
# Jaar,Week,BeginDatum,EindDatum,AantalLaboratoria,Type,Aantal
year = row[0]
week = row[1]
startdatum = row[2]
einddatum = row[3]
bron = row[4]
aantal_labs = row[5]
valtype = row[6]
aantal = int(row[7])
if not dateCache.isvaliddate(startdatum, filename):
continue
if not dateCache.isvaliddate(einddatum, filename):
continue
if valtype == 'Totaal':
for n in range(int((dateCache.parse(einddatum) - dateCache.parse(startdatum)).days)+1):
weekdatum = dateCache.parse(
startdatum) + datetime.timedelta(n)
weekdatumstr = weekdatum.strftime("%Y-%m-%d")
m = initrecord(weekdatumstr)
m['rivm_totaal_personen_getest'] = aantal/7
m['rivm_aantal_testlabs'] = aantal_labs
# print(weekdatumstr+' '+str(aantal)+' /7= '+str(metenisweten[weekdatumstr]['rivm_totaal_personen_getest'])+' totaal personen getest per dag.')
if valtype == 'Positief':
for n in range(int((dateCache.parse(einddatum) - dateCache.parse(startdatum)).days)+1):
weekdatum = dateCache.parse(
startdatum) + datetime.timedelta(n)
weekdatumstr = weekdatum.strftime("%Y-%m-%d")
m = initrecord(weekdatumstr)
m['rivm_totaal_personen_positief'] = aantal/7
line_count += 1
print("Add total tests (from start 2021, daily basis)")
filename = '../cache/COVID-19_uitgevoerde_testen.json'
with open(filename, 'r') as json_file:
data = json.load(json_file)
temp_totaltests = {}
temp_positive = {}
for record in data:
# print(record)
if record['Date_of_statistics'] not in temp_totaltests:
temp_totaltests[record['Date_of_statistics']] = 0
temp_totaltests[record['Date_of_statistics']] = temp_totaltests[record['Date_of_statistics']
] + intOrZero(record['Tested_with_result'])
if record['Date_of_statistics'] not in temp_positive:
temp_positive[record['Date_of_statistics']] = 0
temp_positive[record['Date_of_statistics']
] = temp_positive[record['Date_of_statistics']] + intOrZero(record['Tested_positive'])
# Overwrite total number of tests
for key in temp_totaltests:
initrecord(key)['rivm_totaal_tests'] = temp_totaltests[key]
# Overwrite total number of positivetests
for key in temp_positive:
initrecord(key)['rivm_totaal_tests_positief'] = temp_positive[key]
print("Load LCPS data...")
filename = '../cache/lcps-covid-19.csv'
with open(filename, 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count > 0:
# Datum,IC_Bedden_COVID,IC_Bedden_Non_COVID,Kliniek_Bedden,IC_Nieuwe_Opnames_COVID,Kliniek_Nieuwe_Opnames_COVID
datum = datetime.datetime.strptime(
row[0], "%d-%m-%Y").strftime("%Y-%m-%d")
ic_bedden_covid = row[1]
ic_bedden_non_covid = row[2]
kliniek_bedden = row[3]
ic_nieuwe_opnames = row[4]
kliniek_nieuwe_opnames = row[5]
# print("LCPS bedden " + datum + " " + ic_bedden_covid + " " + kliniek_bedden)
m = initrecord(datum)
m['nu_op_ic_lcps'] = intOrNone(ic_bedden_covid)
m['nu_op_ic_noncovid_lcps'] = intOrNone(ic_bedden_non_covid)
m['nu_opgenomen_lcps'] = intOrNone(kliniek_bedden)
line_count = line_count + 1
print("Calculate average number of ill people based on Rna measurements")
dates = []
rna = []
rivmschattingratio = []
# Record the newest date with more than 30 RNA measurements, which
# is the last date we will use for estimates.
for date in metenisweten:
# Record last valid RNA date
# print('RNA metingen '+date+' '+str(metenisweten[date]['RNA']['totaal_RNA_metingen']))
if metenisweten[date]['RNA']['totaal_RNA_metingen'] > 30:
dates.append(date)
rna.append(metenisweten[date]['RNA']['RNA_per_100k_avg'])
# Smooth
rna_avg = double_savgol(rna, 2, 13, 1)
# Compare to RIVM estimates and correct scale
for idx, date in enumerate(dates):
rivmschatting = metenisweten[date]['rivm_schatting_besmettelijk']['value']
if rivmschatting and rivmschatting > 5000 and rna_avg[idx] > 1000:
rivmschattingratio.append(rivmschatting/rna_avg[idx])
averageratio = mean(rivmschattingratio)
print("Average ratio between RIVM estimates and RNA data: " +
str(round(averageratio, 5)))
rna_avg = [x * averageratio for x in rna_avg]
for i in range(len(dates)):
date = dates[i]
metenisweten[date]['RNA']['besmettelijk'] = max(
1, rna_avg[i]) # Don't allow negative or 0 numbers
print("Calculate average age of positive tested people")
for datum in metenisweten:
positief = 0
som = 0
for age in metenisweten[datum]['besmettingleeftijd']:
som += metenisweten[datum]['besmettingleeftijd'][age] * int(age)
positief += metenisweten[datum]['besmettingleeftijd'][age]
if positief > 0:
gemiddeld = som/positief
metenisweten[datum]['besmettingleeftijd_gemiddeld'] = gemiddeld
print("Calculate totals")
totaal_positief = 0
totaal_opgenomen = 0
totaal_overleden = 0
for datum in metenisweten:
totaal_positief += metenisweten[datum]['positief']
totaal_opgenomen += metenisweten[datum]['nu_opgenomen']
totaal_overleden += metenisweten[datum]['overleden']
metenisweten[datum]['totaal_positief'] = totaal_positief
metenisweten[datum]['totaal_opgenomen'] = totaal_opgenomen
metenisweten[datum]['totaal_overleden'] = totaal_overleden
# Write sorted data
writeMetenIsWeten()
def freshdata():
# New way of doing things. Download and add data:
newData = [process_casus_landelijk(),
process_Rt(),
process_Zkh(),
process_lcps_ziekenhuisopnames(),
process_varianten(),
process_rioolwaterdata]
if any(newData) or download() or not os.path.isfile('../data/daily-stats.json') or isnewer(__file__, '../data/daily-stats.json'):
builddaily()
# New way of calculating:
calculate_geschat_ziek()
# Write processed and sorted data
writeMetenIsWeten()
return True
elif os.stat('../data/daily-stats.json').st_mtime > (time.time() - 1200):
# downloaded data is "fresh" for 20 minutes
# This is a workaround so that all graphs get
# created if scripts are called within 20 minutes after
# building a new dataset.
return True
else:
return False
def getDateRange(metenisweten):
for datum in metenisweten:
try:
mindatum
except NameError:
mindatum = dateCache.parse(datum)
try:
maxdatum
except NameError:
maxdatum = dateCache.parse(datum)
mindatum = min(mindatum, dateCache.parse(datum))
maxdatum = max(maxdatum, dateCache.parse(datum))
# Woraround to make graphs look the same when rendering "full width"
mindatum = max(mindatum, dateCache.parse("2020-03-01"))
date_range = [mindatum + datetime.timedelta(days=x)
for x in range(0, (maxdatum-mindatum).days+7)]
print(maxdatum)
return date_range
def printDict(d):
print(json.dumps(d, indent=4))
if __name__ == "__main__":
freshdata()