This commit is contained in:
fordprefect
2020-03-19 17:09:47 +01:00
parent ae83263ccb
commit 4d9108f35b
4 changed files with 131 additions and 68 deletions

View File

@@ -6,77 +6,70 @@ import datetime
import os import os
import matplotlib.pyplot as pp import matplotlib.pyplot as pp
import numpy as np import numpy as np
import sys
import importlib
sys.path.append(".")
#### config
dataurl = "https://covid.ourworldindata.org/data/full_data.csv"
date = datetime.date.today()
datafile = f"{date}-full-data.csv"
if not os.path.isfile(datafile):
r = requests.get(dataurl)
with open(datafile, "wb") as f:
f.write(r.content)
else:
print(f"file found: {datafile}")
# processing
data = {}
with open(datafile, "r") as f:
reader = csv.reader(f)
for row in reader:
date,location,new_cases,new_deaths,total_cases,total_deaths = row
if location=="location":
# table header
continue
if location not in data:
data[location] = []
year, month, day = date.split("-")
def toint(a):
try:
return int(a)
except:
return np.nan
data[location].append([datetime.date(int(year), int(month), int(day)), new_cases, new_deaths, total_cases, total_deaths])
data2 = {}
# reorganize data
for loc in data:
time = []
new_cases, new_deaths, total_cases, total_deaths = [], [], [], []
for entry in data[loc]:
t_, new_cases_, new_deaths_, total_cases_, total_deaths_ = entry
time.append(t_)
new_cases.append(toint(new_cases_))
new_deaths.append(toint(new_deaths_))
total_cases.append(toint(total_cases_))
total_deaths.append(toint(total_deaths_))
data2[loc] = [time, new_cases, new_deaths, total_cases, total_deaths]
# countries of interest # countries of interest
countries = ["Germany", "Italy", "Spain", "Denmark", "China", "Japan", "South Korea", "Iran", "Belgium", "World"] countries = ["Germany", "Italy", "Spain", "Denmark", "China", "Japan", "South Korea", "Iran", "Belgium", "World"]
# enabled plots
plots = ["total_cases", "death_per_case", "normalized_to_first_death"]
###
def get_data():
"""fetch data from remote, cache locally and reorganize internal data
not beautiful (at all), but effective!!"""
# plotting dataurl = "https://covid.ourworldindata.org/data/full_data.csv"
for loc in data2: date = datetime.date.today()
if loc not in countries:
continue
time, new_cases, new_deaths, total_cases, total_deaths = data2[loc]
# total cases datafile = f"{date}-full-data.csv"
pp.figure("total_cases")
pp.plot(time, total_cases, label=f"{loc}")
# death/case if not os.path.isfile(datafile):
pp.figure("death_per_case") r = requests.get(dataurl)
pp.plot(time, np.array(total_deaths)/np.array(total_cases), label=f"{loc}") with open(datafile, "wb") as f:
# figure postprocessing f.write(r.content)
for fig in ["total_cases", "death_per_case"]: else:
pp.figure(fig) print(f"file found: {datafile}")
pp.yscale("log")
pp.xticks(rotation=90)
pp.legend(frameon=False)
pp.tight_layout()
pp.savefig(f"{fig}.png") # processing
data = {}
with open(datafile, "r") as f:
reader = csv.reader(f)
for row in reader:
date,location,new_cases,new_deaths,total_cases,total_deaths = row
if location=="location":
# table header
continue
if location not in data:
data[location] = []
year, month, day = date.split("-")
def toint(a):
try:
return int(a)
except:
return np.nan
data[location].append([datetime.date(int(year), int(month), int(day)), new_cases, new_deaths, total_cases, total_deaths])
# reorganize data
data2 = {}
for loc in data:
time = []
new_cases, new_deaths, total_cases, total_deaths = [], [], [], []
for entry in data[loc]:
t_, new_cases_, new_deaths_, total_cases_, total_deaths_ = entry
time.append(t_)
new_cases.append(toint(new_cases_))
new_deaths.append(toint(new_deaths_))
total_cases.append(toint(total_cases_))
total_deaths.append(toint(total_deaths_))
data2[loc] = [time, new_cases, new_deaths, total_cases, total_deaths]
return data2
data = get_data()
for plot in plots:
#try:
i = importlib.import_module(plot)
i.plot(data, countries)
#except:
# print(f"plotting {plot} failed")

23
death_per_case.py Normal file
View File

@@ -0,0 +1,23 @@
"""
Plot total deaths per total cases of countries over time on log scale
"""
import matplotlib.pyplot as pp
import numpy as np
name="death_per_case"
def plot(data, countries):
for loc in data:
if loc not in countries:
continue
time, new_cases, new_deaths, total_cases, total_deaths = data[loc]
# death/case
pp.figure(name)
pp.plot(time, np.array(total_deaths)/np.array(total_cases), label=f"{loc}")
pp.yscale("log")
pp.xticks(rotation=90)
pp.legend(frameon=False)
pp.tight_layout()
pp.savefig(name+".png")

View File

@@ -0,0 +1,24 @@
"""
Plot total cases timeshiftet to first death
"""
import matplotlib.pyplot as pp
import numpy as np
name="normalized_to_first_death"
def plot(data, countries):
for loc in data:
if loc not in countries:
continue
time, new_cases, new_deaths, total_cases, total_deaths = data[loc]
pp.figure(name)
day_of_first_death = np.argwhere(np.array(total_deaths) > 0)[0][0]
new_time_axis = np.arange(len(time)) - day_of_first_death
pp.plot(new_time_axis, np.array(total_cases), label=f"{loc}")
pp.yscale("log")
pp.xticks(rotation=90)
pp.legend(frameon=False)
pp.tight_layout()
pp.savefig(name+".png")

23
total_cases.py Normal file
View File

@@ -0,0 +1,23 @@
"""
Plot total cases of countries over time on log scale
"""
import matplotlib.pyplot as pp
name="total_cases"
def plot(data, countries):
for loc in data:
if loc not in countries:
continue
time, new_cases, new_deaths, total_cases, total_deaths = data[loc]
# total cases
pp.figure(name)
pp.plot(time, total_cases, label=f"{loc}")
pp.yscale("log")
pp.xticks(rotation=90)
pp.legend(frameon=False)
pp.tight_layout()
pp.savefig(name+".png")