76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
#!/usr/bin/python
|
|
|
|
import requests
|
|
import csv
|
|
import datetime
|
|
import os
|
|
import matplotlib.pyplot as pp
|
|
import numpy as np
|
|
import sys
|
|
import importlib
|
|
sys.path.append(".")
|
|
|
|
#### config
|
|
# countries of interest
|
|
countries = ["Germany", "Italy", "Spain", "China", "Japan", "South Korea", "Iran", "United States", "World"]
|
|
# enabled plots
|
|
plots = ["basics", "death_per_case", "normalized_to_first_death", "delay_from_china"]
|
|
###
|
|
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/ecdc/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])
|
|
# 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")
|