main.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import csv
  2. import os
  3. import sys
  4. import matplotlib.pyplot as plt
  5. import requests
  6. import data
  7. _FNAME = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv'
  8. _DATES_START_AT = 4
  9. _REGIONS = [
  10. ('Sweden', ['Sweden']),
  11. ('Ireland', ['Ireland']),
  12. ('Iran', ['Iran']),
  13. ('Italy', ['Italy']),
  14. ('NY', ['US, New York']),
  15. ('Europe', ['Italy', 'Sweden', 'Ireland']),
  16. ]
  17. _MARKERS = ['o', 'x', 'D']
  18. def write_fig(regions_label, regions, dates, region_data, start_date_idx, scale, normalize):
  19. title = scale
  20. if normalize:
  21. title += '-normalized'
  22. plt.figure()
  23. fig, ax = plt.subplots()
  24. plt.yscale(scale)
  25. min_value = 10**10
  26. max_value = None
  27. for region, series, marker in zip(regions, region_data, _MARKERS):
  28. series = series[start_date_idx:]
  29. if normalize:
  30. if region not in data.POPULATIONS:
  31. print 'Cannot normalize data for %s: population data is not available. Aborting.' % region
  32. return
  33. series = [x / data.POPULATIONS[region] for x in series]
  34. ax.plot(series, marker=marker)
  35. max_value = max(max_value, max(series))
  36. min_value = min(min_value, min([x for x in series if x > 0]))
  37. xlabels = dates[start_date_idx:]
  38. if len(xlabels) > 7:
  39. for i, date in enumerate(xlabels):
  40. if i != len(xlabels) -1 and i % (len(xlabels) / 5) != 0:
  41. xlabels[i] = ''
  42. plt.xticks(range(len(xlabels)), xlabels)
  43. plt.title('%s-%s' % (regions_label, title))
  44. if scale == 'log' and normalize:
  45. yticks = [10**exp for exp in range(-10, 0) if min_value / 3 < 10 ** exp < max_value * 3]
  46. ylabels = ['%f' % (x*100) for x in yticks]
  47. ylabels = ['0' + x.strip('0') + '%' for x in ylabels]
  48. plt.yticks(yticks, ylabels)
  49. if len(regions) > 1:
  50. ax.legend(regions)
  51. ax.grid()
  52. fig.savefig('figs/%s/%s.png' % (regions_label, title))
  53. plt.close()
  54. def run(regions_label, regions, dates, region_data):
  55. start_date_idx = None
  56. for i, (series, region) in enumerate(zip(region_data, regions)):
  57. for j, count in enumerate(series):
  58. if count >= 10:
  59. if start_date_idx is None:
  60. start_date_idx = j-1 if j else j
  61. else:
  62. start_date_idx = min(start_date_idx, j-1 if j else j)
  63. if start_date_idx is None:
  64. print 'No data for %s; aborting' % label
  65. return
  66. if not os.path.isdir('figs'):
  67. os.mkdir('figs')
  68. if not os.path.isdir('figs/%s' % label):
  69. os.mkdir('figs/%s' % label)
  70. for scale in ['linear', 'log']:
  71. for normalize in [True, False]:
  72. write_fig(regions_label, regions, dates, region_data, start_date_idx, scale, normalize)
  73. def load():
  74. raw = requests.get(_FNAME).content
  75. dates = None
  76. counts = {}
  77. reader = csv.reader(raw.splitlines(), delimiter=',', quotechar='"')
  78. for row in reader:
  79. if dates is None:
  80. dates = row[_DATES_START_AT:]
  81. continue
  82. region = '%s, %s' % (row[1], row[0]) if row[0] else row[1]
  83. counts[region] = [float(x) for x in row[_DATES_START_AT:]]
  84. dates = ['%s/%s' % tuple(d.split('/')[:2]) for d in dates]
  85. return dates, counts
  86. def print_regions(counts):
  87. for region in sorted(counts.keys()):
  88. print region
  89. if __name__ == "__main__":
  90. dates, counts = load()
  91. for label, regions in _REGIONS:
  92. region_data = [counts[region] for region in regions]
  93. run(label, regions, dates, region_data)