| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import csv
- import os
- import sys
- import matplotlib.pyplot as plt
- import requests
- import data
- _URLS = {
- 'cases': 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv',
- 'deaths': 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv',
- }
- _DATES_START_AT = 4
- _REGIONS = [
- ('Sweden', ['Sweden']),
- ('Ireland', ['Ireland']),
- ('Iran', ['Iran']),
- ('Italy', ['Italy']),
- ('NY', ['US, New York']),
- ('Europe', ['Italy', 'Sweden', 'Ireland']),
- ]
- _MARKERS = ['o', 'x', 'D']
- _MIN_COUNT = 10
- def write_fig(url_label, regions_label, regions, dates, region_data, start_date_idx, scale, normalize):
- title = '%s-%s' % (url_label, scale)
- if normalize:
- title += '-normalized'
-
- plt.figure()
- fig, ax = plt.subplots()
- plt.yscale(scale)
- min_value = 10**10
- max_value = None
- for region, series, marker in zip(regions, region_data, _MARKERS):
- series = series[start_date_idx:]
- if normalize:
- if region not in data.POPULATIONS:
- print 'Cannot normalize data for %s: population data is not available. Aborting.' % region
- return
-
- series = [x / data.POPULATIONS[region] for x in series]
- ax.plot(series, marker=marker)
- max_value = max(max_value, max(series))
- min_value = min(min_value, min([x for x in series if x > 0]))
- xlabels = dates[start_date_idx:]
- if len(xlabels) > 7:
- for i, date in enumerate(xlabels):
- if i != len(xlabels) -1 and i % (len(xlabels) / 5) != 0:
- xlabels[i] = ''
- plt.xticks(range(len(xlabels)), xlabels)
- plt.title('%s-%s' % (regions_label, title))
- if scale == 'log' and normalize:
- yticks = [10**exp for exp in range(-10, 0) if min_value / 3 < 10 ** exp < max_value * 3]
-
- ylabels = ['%f' % (x*100) for x in yticks]
- ylabels = ['0' + x.strip('0') + '%' for x in ylabels]
- plt.yticks(yticks, ylabels)
- if len(regions) > 1:
- ax.legend(regions)
- ax.grid()
- fig.savefig('figs/%s/%s.png' % (regions_label, title))
- plt.close()
- def run(url_label, regions_label, regions, dates, region_data):
- start_date_idx = None
- for i, (series, region) in enumerate(zip(region_data, regions)):
- for j, count in enumerate(series):
- if count >= _MIN_COUNT:
- if start_date_idx is None:
- start_date_idx = j-1 if j else j
- else:
- start_date_idx = min(start_date_idx, j-1 if j else j)
-
- if start_date_idx is None:
- print 'No data for %s-%s; aborting' % (url_label, regions_label)
- return
- if not os.path.isdir('figs'):
- os.mkdir('figs')
- if not os.path.isdir('figs/%s' % regions_label):
- os.mkdir('figs/%s' % regions_label)
- for scale in ['linear', 'log']:
- for normalize in [True, False]:
- write_fig(url_label, regions_label, regions, dates, region_data, start_date_idx, scale, normalize)
- def load(url):
- raw = requests.get(url).content
- dates = None
- counts = {}
- reader = csv.reader(raw.splitlines(), delimiter=',', quotechar='"')
- for row in reader:
- if dates is None:
- dates = row[_DATES_START_AT:]
- continue
- region = '%s, %s' % (row[1], row[0]) if row[0] else row[1]
- counts[region] = [float(x) for x in row[_DATES_START_AT:]]
-
- dates = ['%s/%s' % tuple(d.split('/')[:2]) for d in dates]
- return dates, counts
- def print_regions(counts):
- for region in sorted(counts.keys()):
- print region
- if __name__ == "__main__":
- for url_label, url in _URLS.items():
- dates, counts = load(url)
- for region_label, regions in _REGIONS:
- regions_data = [counts[region] for region in regions]
- run(url_label, region_label, regions, dates, regions_data)
|