refactor
This commit is contained in:
69
coronavis.py
69
coronavis.py
@@ -6,23 +6,35 @@ 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
|
||||||
|
# countries of interest
|
||||||
|
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!!"""
|
||||||
|
|
||||||
dataurl = "https://covid.ourworldindata.org/data/full_data.csv"
|
dataurl = "https://covid.ourworldindata.org/data/full_data.csv"
|
||||||
date = datetime.date.today()
|
date = datetime.date.today()
|
||||||
|
|
||||||
datafile = f"{date}-full-data.csv"
|
datafile = f"{date}-full-data.csv"
|
||||||
|
|
||||||
if not os.path.isfile(datafile):
|
if not os.path.isfile(datafile):
|
||||||
r = requests.get(dataurl)
|
r = requests.get(dataurl)
|
||||||
with open(datafile, "wb") as f:
|
with open(datafile, "wb") as f:
|
||||||
f.write(r.content)
|
f.write(r.content)
|
||||||
else:
|
else:
|
||||||
print(f"file found: {datafile}")
|
print(f"file found: {datafile}")
|
||||||
|
|
||||||
# processing
|
# processing
|
||||||
data = {}
|
data = {}
|
||||||
with open(datafile, "r") as f:
|
with open(datafile, "r") as f:
|
||||||
reader = csv.reader(f)
|
reader = csv.reader(f)
|
||||||
for row in reader:
|
for row in reader:
|
||||||
date,location,new_cases,new_deaths,total_cases,total_deaths = row
|
date,location,new_cases,new_deaths,total_cases,total_deaths = row
|
||||||
@@ -38,12 +50,9 @@ with open(datafile, "r") as f:
|
|||||||
except:
|
except:
|
||||||
return np.nan
|
return np.nan
|
||||||
data[location].append([datetime.date(int(year), int(month), int(day)), new_cases, new_deaths, total_cases, total_deaths])
|
data[location].append([datetime.date(int(year), int(month), int(day)), new_cases, new_deaths, total_cases, total_deaths])
|
||||||
|
# reorganize data
|
||||||
|
data2 = {}
|
||||||
data2 = {}
|
for loc in data:
|
||||||
|
|
||||||
# reorganize data
|
|
||||||
for loc in data:
|
|
||||||
time = []
|
time = []
|
||||||
new_cases, new_deaths, total_cases, total_deaths = [], [], [], []
|
new_cases, new_deaths, total_cases, total_deaths = [], [], [], []
|
||||||
for entry in data[loc]:
|
for entry in data[loc]:
|
||||||
@@ -54,29 +63,13 @@ for loc in data:
|
|||||||
total_cases.append(toint(total_cases_))
|
total_cases.append(toint(total_cases_))
|
||||||
total_deaths.append(toint(total_deaths_))
|
total_deaths.append(toint(total_deaths_))
|
||||||
data2[loc] = [time, new_cases, new_deaths, total_cases, total_deaths]
|
data2[loc] = [time, new_cases, new_deaths, total_cases, total_deaths]
|
||||||
|
return data2
|
||||||
|
|
||||||
# countries of interest
|
data = get_data()
|
||||||
countries = ["Germany", "Italy", "Spain", "Denmark", "China", "Japan", "South Korea", "Iran", "Belgium", "World"]
|
|
||||||
|
|
||||||
# plotting
|
for plot in plots:
|
||||||
for loc in data2:
|
#try:
|
||||||
if loc not in countries:
|
i = importlib.import_module(plot)
|
||||||
continue
|
i.plot(data, countries)
|
||||||
time, new_cases, new_deaths, total_cases, total_deaths = data2[loc]
|
#except:
|
||||||
|
# print(f"plotting {plot} failed")
|
||||||
# total cases
|
|
||||||
pp.figure("total_cases")
|
|
||||||
pp.plot(time, total_cases, label=f"{loc}")
|
|
||||||
|
|
||||||
# death/case
|
|
||||||
pp.figure("death_per_case")
|
|
||||||
pp.plot(time, np.array(total_deaths)/np.array(total_cases), label=f"{loc}")
|
|
||||||
# figure postprocessing
|
|
||||||
for fig in ["total_cases", "death_per_case"]:
|
|
||||||
pp.figure(fig)
|
|
||||||
pp.yscale("log")
|
|
||||||
pp.xticks(rotation=90)
|
|
||||||
pp.legend(frameon=False)
|
|
||||||
pp.tight_layout()
|
|
||||||
|
|
||||||
pp.savefig(f"{fig}.png")
|
|
||||||
|
|||||||
23
death_per_case.py
Normal file
23
death_per_case.py
Normal 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")
|
||||||
24
normalized_to_first_death.py
Normal file
24
normalized_to_first_death.py
Normal 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
23
total_cases.py
Normal 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")
|
||||||
Reference in New Issue
Block a user