Skip to content

Instantly share code, notes, and snippets.

@foobuzz
Created April 4, 2020 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foobuzz/c5a18dd8c983b128ee788fe7747a031b to your computer and use it in GitHub Desktop.
Save foobuzz/c5a18dd8c983b128ee788fe7747a031b to your computer and use it in GitHub Desktop.
Some COVID-19 graphs
"""
Usage:
wget https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv
python growth.py
"""
import csv
from datetime import date
import matplotlib.pyplot as plt
LOCKDOWN = {
('Hubei', 'China'): date(2020, 1, 23),
('', 'Italy'): date(2020, 3, 11),
('', 'France'): date(2020, 3, 17),
}
COLORS = {
('Hubei', 'China'): 's-r',
('', 'Italy'): '^-b',
('', 'France'): 'o-c',
}
def get_start_index(my_date):
start = date(2020, 1, 22)
return (my_date - start).days + 4
growth = []
with open('time_series_covid19_confirmed_global.csv') as _:
reader = csv.reader(_)
next(reader) # Header
for row in reader:
key = (row[0], row[1])
lockdown = LOCKDOWN.get(key)
if lockdown is None:
continue
start_index = get_start_index(lockdown) - 1
series = [int(cases) for cases in row[start_index:] if cases != '']
# Removing duplicated counts
for i, cases in enumerate(series[1:]):
if cases == series[i] and i < 30:
series[i+1] = (series[i] + series[i+2]) // 2
growth.append((
key,
[
(series[n] - series[n-1])/series[n-1]*100
for n in range(1, len(series))
]
))
graph = plt.figure().subplots()
graph.set_title("Growth of Covid-19 cases starting from lockdown")
graph.set_xlabel("Days since lockdown")
graph.set_ylabel("New cases (as percentage of cumulative cases)")
graph.axis([1, 40, 0, 60])
graph.grid(True, color=(0.9, 0.9, 0.9, 1))
for key, series in growth:
graph.plot(
[i+1 for i in range(len(series))],
series,
COLORS[key],
)
graph.legend([', '.join([stuff for stuff in key if stuff]) for key, _ in growth])
plt.savefig('growth.svg')
plt.show()
"""
Usage:
wget https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv
wget https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv
python recovery.py
"""
import csv
import matplotlib.pyplot as plt
start_col = 37 # Febrary 24
data = {}
with open('time_series_covid19_confirmed_global.csv') as confirmed, \
open('time_series_covid19_recovered_global.csv') as recovered:
for label, fileobj in zip(['confirmed', 'recovered'], [confirmed, recovered]):
reader = csv.reader(fileobj)
for row in reader:
if row[:2] == ['Hubei', 'China']:
data[label] = [int(c) for c in row[start_col:] if c != '']
break
graph = plt.figure().subplots()
graph.set_title("Unrecovered cases vs all confirmed cases in Hubei after "
"30 days of lockdown")
graph.set_xlabel("Days since second month of lockdown")
graph.set_ylabel("Number of unrecovered and confirmed cases")
confirmed = data['confirmed']
patients = [c - r for c, r in zip(data['confirmed'], data['recovered'])]
graph.bar(range(len(confirmed)), confirmed)
graph.bar(range(len(patients)), patients)
graph.legend(['Confirmed', 'Unrecovered'])
plt.savefig('recovery.svg')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment