import csv import os import sys import matplotlib.pyplot as plt import requests import data _FNAME = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.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'] def write_fig(regions_label, regions, dates, region_data, start_date_idx, scale, normalize): title = 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(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 >= 10: 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; aborting' % label return if not os.path.isdir('figs'): os.mkdir('figs') if not os.path.isdir('figs/%s' % label): os.mkdir('figs/%s' % label) for scale in ['linear', 'log']: for normalize in [True, False]: write_fig(regions_label, regions, dates, region_data, start_date_idx, scale, normalize) def load(): raw = requests.get(_FNAME).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__": dates, counts = load() for label, regions in _REGIONS: region_data = [counts[region] for region in regions] run(label, regions, dates, region_data)