-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_data.py
executable file
·510 lines (459 loc) · 17 KB
/
collect_data.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#!/usr/bin/python3
from bs4 import BeautifulSoup
import requests
import csv
import io
import os
import re
from pathlib import Path
import copy
import time
from pprint import pprint
import json
import collections
from bidict import bidict
from numpy.polynomial.polynomial import Polynomial
import numpy as np
import statistics
state_name = {
'AL': 'Alabama',
'AK': 'Alaska',
'AZ': 'Arizona',
'AR': 'Arkansas',
'CA': 'California',
'CO': 'Colorado',
'CT': 'Connecticut',
'DE': 'Delaware',
'FL': 'Florida',
'GA': 'Georgia',
'HI': 'Hawaii',
'ID': 'Idaho',
'IL': 'Illinois',
'IN': 'Indiana',
'IA': 'Iowa',
'KS': 'Kansas',
'KY': 'Kentucky',
'LA': 'Louisiana',
'ME': 'Maine',
'MD': 'Maryland',
'MA': 'Massachusetts',
'MI': 'Michigan',
'MN': 'Minnesota',
'MS': 'Mississippi',
'MO': 'Missouri',
'MT': 'Montana',
'NE': 'Nebraska',
'NV': 'Nevada',
'NH': 'New Hampshire',
'NJ': 'New Jersey',
'NM': 'New Mexico',
'NY': 'New York',
'NC': 'North Carolina',
'ND': 'North Dakota',
'OH': 'Ohio',
'OK': 'Oklahoma',
'OR': 'Oregon',
'PA': 'Pennsylvania',
'RI': 'Rhode Island',
'SC': 'South Carolina',
'SD': 'South Dakota',
'TN': 'Tennessee',
'TX': 'Texas',
'UT': 'Utah',
'VT': 'Vermont',
'VA': 'Virginia',
'WA': 'Washington',
'WV': 'West Virginia',
'WI': 'Wisconsin',
'WY': 'Wyoming',
'AS': 'American Samoa',
'DC': 'Washington, D.C.',
'FM': 'Federated States of Micronesia',
'GU': 'Guam',
'MH': 'Marshall Islands',
'MP': 'Northern Mariana Islands',
'PW': 'Palau',
'PR': 'Puerto Rico',
'VI': 'United States Virgin Islands',
}
state_short = {short: lng for lng, short in state_name.items()}
part_code_dict = bidict()
def generate_acronym(word, length):
if len(word) <= length:
yield word.upper()
i = 0
while True:
yield(word + str(i)).upper()
for i in range(len(word) - length):
if length == 1:
yield word[i].upper()
else:
for tail in generate_acronym(word[i+1:], length - 1):
yield word[i].upper() + tail
def add_part_code(path, code):
code = code.upper()
if len(path) > 2 and path[0] == "World":
# Skip world and region
path = path[2:]
text = "".join(path).lower()
text = re.sub(r'\W', '', text)
if text in part_code_dict:
return part_code_dict[text]
if text not in part_code_dict and code not in part_code_dict.inverse:
part_code_dict[text] = code
def make_code(path):
if len(path) > 2 and path[0] == "World":
# Skip world and region
path = path[2:]
text = "".join(path).lower()
text = re.sub(r'\W', '', text)
if text in part_code_dict:
return part_code_dict[text]
for code in generate_acronym(text, 5):
if code not in part_code_dict.inverse:
part_code_dict[text] = code
return code
for suffix in (chr(ord('A')+i) for i in range(0, 26)):
for code in generate_acronym(text, 4):
code = code + suffix
if code not in part_code_dict.inverse:
part_code_dict[text] = code
return code
assert 0, "Couldn't generate code for %r." % text
def read_csv(f, has_header=True, skip=0):
reader = csv.reader(f)
for i in range(skip):
next(reader)
if has_header:
header = next(reader)
else:
header = None
data = []
for row in reader:
if not header:
header = range(len(row))
entry = {'_': row}
for i, field in enumerate(header):
assert (i < len(row)), row
entry[field] = row[i]
data.append(entry)
return data
def open_cached(url):
cache_path = Path(".cache")
os.makedirs(cache_path, exist_ok=True)
cache_file = cache_path.joinpath(re.sub(r'.*/', '', url))
if not cache_file.exists() or \
time.time() - cache_file.stat().st_mtime > 3540:
print("Fetching", url, "...")
r = requests.get(url)
assert(r.status_code == 200)
fd = open(cache_file, "w")
fd.write(r.text)
fd.close()
return open(cache_file, "r")
def shorten(country_name):
table = {
"United States of America": "United States",
"United Kingdom of Great Britain and Northern Ireland": "United Kingdom",
"Iran (Islamic Republic of)": "Iran",
"Taiwan, Province of China": "Taiwan",
"Moldova, Republic of": "Moldova",
"Bolivia (Plurinational State of)": "Bolivia",
"Korea, Republic of": "South Korea",
"Venezuela (Bolivarian Republic of)": "Venezuela",
"Tanzania, United Republic of": "Tanzania"
}
return table.get(country_name, country_name)
class Collector(object):
def __init__(self):
# Area Of Interest
self.aoi = {
# 'XKX': {
# 'name': 'Kosovo',
# 'population': 1831000,
# 'hospital_beds': 5269}
}
# country -> state -> county -> city
# At each level there may be _data -> t -> d -> number
self.area_tree = {}
self.enable_debug = False
self.alpha3 = bidict()
# Contents of all.csv, indexed by countryID, as used in the timeseries data
self.all = {}
def add_area(self, path):
node = self.area_tree
partial = []
for part in path:
# Create a new list
partial = partial + [part]
code = make_code(partial)
node = node.setdefault(part,
{ '_path': partial, '_code': code })
self.aoi.setdefault(code, {})
self.aoi[code]['path'] = partial
return node
def debug(self, *args):
if self.enable_debug:
print(*args)
def tally_data(self, node, t, d, depth=0):
data = node.get('_data', {})
if t in data and d in data[t]:
return data[t][d]
values = []
for child in node:
if child.startswith('_'):
continue
v = self.tally_data(node[child], t, d, depth + 1)
if not v is None:
values.append(v)
if len(values) > 0:
return sum(values)
else:
return None
def tally_population(self, node, depth=0):
population = node.get('_population')
if population:
return population
population = 0
for child in node:
if child.startswith('_'):
continue
population += self.tally_population(node[child], depth + 1)
return population
def get_node(self, path):
node = self.area_tree
for part in path:
node = node[part]
return node
def squash_unlikely_values(self, node, data_key):
# Convert unlikely data values to None
lookaround = 5
if not data_key in node:
return
for t in node[data_key]:
dates = sorted(node[data_key][t].keys())
values = [node[data_key][t][d] for d in dates]
tolerance = 1
for i, date in enumerate(dates):
value = values[i]
if value is None:
continue
previous = [v for v in values[max(0, i-lookaround):i]
if not v is None]
if len(previous) > 1:
previous_median = statistics.median(previous)
else:
previous_median = None
following = [v for v in values[i+1:i+2+lookaround]
if not v is None]
if len(following) > 1:
following_median = statistics.median(following)
else:
following_median = None
if (previous_median and value < previous_median - tolerance) or \
(previous_median and following_median and
previous_median < following_median and
value > following_median + tolerance):
# context = previous + following
# if len(context) > 3:
# average = sum(context) / len(context)
# stddev = statistics.stdev(context)
# if value > average + (stddev + 1) * 3 or \
# value < average - (stddev + 1) * 3:
path = node.get('path', node.get('_path'))
print("Squash %r %s in %s (%s) on %s (%r)" % (
value, t, path[-1], node.get('_locationID'), date,
previous + [value] + following))
node[data_key][t][date] = None
def timeseries(self):
world_name = "World"
allDates = set()
data = json.load(open_cached(
"https://liproduction-reportsbucket-bhk8fnhv1s76.s3-us-west-1.amazonaws.com/v1/latest/timeseries-byLocation.json"))
for entry in data:
if "(unassigned)" in entry['locationID']:
# Unassigned cases are ones that we know that happened in e.g.
# a state, but we don't know in which county they occurred.
# I'm ignoring them because they are rare, and included in the
# state count (in this case) regardless.
continue
path = [world_name,
self.all.get(entry['countryID'], {'region': 'Unknown'})['region']]
for part in ('countryName', 'stateName', 'countyName'):
if part in entry:
path.append(entry[part])
if "(unassigned)" in entry['locationID']:
path[-1] += " (unassigned)"
code = None
for part in entry['locationID'].split("#"):
if ":" in part:
encoding, c = part.split(":")
if encoding.startswith('iso'):
code = c
if code:
if "(unassigned)" in entry['locationID']:
code += "-u"
add_part_code(path, code)
node = self.add_area(path)
try:
node['_population'] = int(entry['population'])
except ValueError:
pass
node['_locationID'] = entry['locationID']
aoi = self.aoi[node['_code']]
aoi["level"] = entry["level"]
aoi.setdefault('data', {})
for date in entry['dates']:
allDates.add(date)
for t in ('cases', 'deaths', 'recovered', 'tested'):
if t in entry['dates'][date]:
try:
node.setdefault('_data', {}) \
.setdefault(t, {})[date] = int(entry['dates'][date][t])
except ValueError:
pass
# Mark this as one we need to fill in later.
aoi['data'].setdefault(t, {})[date] = None
self.squash_unlikely_values(node, '_data')
allDates = sorted(list(allDates))
# Now do a second pass, filling in missing data by summing up all the child nodes.
for aoi in self.aoi.values():
if len(aoi['path']) > 3:
aoi['fullName'] = ", ".join(aoi['path'][-3:])
else:
aoi['fullName'] = ", ".join(aoi['path'])
aoi['name'] = aoi['path'][-1]
node = self.get_node(aoi['path'])
population = self.tally_population(node)
if population:
aoi['population'] = population
for t in ('cases', 'deaths', 'recovered', 'tested'):
started = False
for d in allDates:
value = self.tally_data(node, t, d)
if value:
started = True
if started:
aoi.setdefault('data', {}).setdefault(t, {})[d] = value
self.squash_unlikely_values(aoi, 'data')
# Do another pass, translating date->value hashes into lists with a
# value for each date.
# TODO: we assume that allDates is contiguous
for aoi in self.aoi.values():
for t in list(aoi['data'].keys()):
sequence = []
started = False
startDate = None
endDate = max(aoi['data'][t].keys())
# Hide runs of None at the end
noneRun = 0
for d in allDates:
if aoi['data'][t].get(d):
started = True
if startDate is None:
startDate = d
if started and d <= endDate:
value = aoi['data'][t].get(d)
if value is None:
noneRun += 1
else:
sequence += [None] * noneRun
noneRun = 0
sequence.append(value)
if sequence:
aoi['data'][t] = sequence
aoi['data'][t + "-start"] = startDate
else:
del aoi['data'][t]
def save_data(self, path):
json.dump(self.aoi, open(path, "w"))
def all_csv(self):
for entry in read_csv(open("data/all.csv")):
self.alpha3[entry['alpha-3']] = shorten(entry['name'])
self.all['iso1:' + entry['alpha-2']] = entry
def population(self):
for entry in read_csv(open("data/API_SP.POP.TOTL_DS2_en_csv_v2_821007.csv"), skip=4):
code = entry['Country Code']
if code not in self.aoi:
continue
for year in range(2020, 2000, -1):
if entry.get(str(year)):
population = int(entry[str(year)])
break
else:
print("No population info for", entry['Country Name'])
continue
if not self.aoi[code].get('population'):
self.aoi[code]['population'] = population
def hospital_beds(self):
for entry in read_csv(open("data/API_SH.MED.BEDS.ZS_DS2_en_csv_v2_821439.csv"), skip=4):
code = entry['Country Code']
if code not in self.aoi or 'hospital_beds' in self.aoi[code]:
continue
for year in range(2020, 2000, -1):
if entry.get(str(year)):
bed_fraction = float(entry[str(year)])
break
else:
print("No hospital bed info for", entry['Country Name'])
continue
self.aoi[code]['hospital_beds'] = int(bed_fraction * self.aoi[code]['population'])
def state_hospital_beds(self):
soup = BeautifulSoup(open("data/state_statistics.html").read(), 'html.parser')
divs = soup.find_all("div")
div = [d for d in divs if d.get('class')==['report']][0]
for tr in div.find_all('tr'):
td = tr.find_all('td')
if not td:
continue
if td[0].a and re.match(r'[A-Z]{2} - ', td[0].a.text):
state = td[0].a.text[:2]
code = "US-" + state
if code not in self.aoi:
continue
hospital_beds = int(td[2].text.replace(",", ""))
self.aoi[code]['hospital_beds'] = hospital_beds
def measure(self):
"""Compute some metrics that give an idea of how well each area is doing."""
for aoi in self.aoi.values():
if 'population' not in aoi:
print("No population info for", aoi['name'])
continue
if 'cases' not in aoi['data']:
print("No cases for", aoi['name'])
continue
if len(aoi['data']['cases']) < 2:
print("Insufficient cases for", aoi['name'])
continue
distance = []
pop = aoi['population']
distance = [(v or 0)/pop for v in aoi['data']['cases']]
window = min(14, len(distance))
polynomial = Polynomial.fit(range(window), distance[-window:],
min(3, window))
coefficients = list(polynomial.convert().coef[1:])
# If the trailing coefficients are 0, they're not included in coef.
# Add them back.
while len(coefficients) < 3:
coefficients.append(0)
aoi['velocity'] = coefficients[0]
aoi['acceleration'] = coefficients[1]
aoi['jerk'] = coefficients[2]
def build(self):
self.all_csv()
self.timeseries()
self.population()
self.hospital_beds()
self.state_hospital_beds()
self.measure()
def main(args):
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("--output", "-o")
args = parser.parse_args(args)
c = Collector()
c.build()
c.save_data(args.output or "outbreak.json")
if __name__ == "__main__":
import sys
sys.exit(main(sys.argv[1:]))