various new features: new vaccination overview on country specific side, slightly changed text on index head, moved data correction to parser, made population an int, added the used vaccines to the vaccination table, and some bugfixing here and there

This commit is contained in:
fordprefect
2021-01-21 18:42:48 +01:00
parent 3f0ce1b621
commit 71c4ef8065
4 changed files with 166 additions and 22 deletions

View File

@@ -9,6 +9,9 @@ import pickle
import logging
logging.getLogger().setLevel(logging.CRITICAL)
import os
import datetime
import locale
locale.setlocale(locale.LC_ALL, "de_DE.utf8")
def plot(data, countries, pop, metadata, **kwargs):
figsize = (10,5)
@@ -24,7 +27,31 @@ def plot(data, countries, pop, metadata, **kwargs):
if not os.path.isdir(path):
os.mkdir(path)
if not os.path.isfile(path+"/index.html") or True: # TODO enable html file generation
# is this country vaccinating?
is_vaccinating = True if data[loc]['total_vaccinations'][-1] > 0 else False
if is_vaccinating:
first_vac_report = np.argwhere(np.isnan(data[loc]['total_vaccinations']))[-1][0] + 1
vac_text = f"<tr><td>Impfstart</td><td>{data[loc]['time'][first_vac_report]}</td></tr>"
try:
vac_rate = data[loc]['total_vaccinations'][-1]/metadata[loc]['population']*100
vac_text+= f"<tr><td>Impfrate</td><td>{vac_rate:1.3f} %</td></tr>"
except:
pass
try:
immune_rate = (data[loc]['total_vaccinations'][-1] + data[loc]['total_cases'][-1])/metadata[loc]['population']*100
vac_text += f"<tr><td>Immunrate</td><td>{immune_rate:1.3f} %</td></tr>"
except:
pass
try:
vaccines = metadata[loc]['vaccines'] if 'vaccines' in metadata[loc] else "unknown"
vac_text += f"<tr><td>Impfstoffe</td><td>{vaccines}</td></tr>"
except:
pass
total_cases = data[loc]['total_cases'][-1]
total_deaths = data[loc]['total_deaths'][-1]
today = datetime.datetime.now().strftime("%d.%m.%Y")
if True: #not os.path.isfile(path+"/index.html") or False: # TODO enable html file generation
with open(path+"/index.html", "w") as f:
f.write(f"""
<html>
@@ -42,6 +69,7 @@ def plot(data, countries, pop, metadata, **kwargs):
<a href=https://dukun.de/corona>Zurück</a><br><br>
<details open><summary><h4>Landesspezifische Kennzahlen</h4></summary>
(Stand: {today})
<table>
<tr><td>ISO Code</td><td>{metadata[loc]["iso_code"]}</td></tr>
<tr><td>Continent</td><td>{metadata[loc]["continent"]}</td></tr>
@@ -61,12 +89,18 @@ def plot(data, countries, pop, metadata, **kwargs):
<tr><td>hospital beds per thousand inhabitants</td><td>{metadata[loc]["hospital_beds_per_thousand"]}</td></tr>
<tr><td>life expectancy</td><td>{metadata[loc]["life_expectancy"]}</td></tr>
<tr><td>human development index</td><td>{metadata[loc]["human_development_index"]}</td></tr>
<tr><td>Absolute bestätigte Infektionsfälle</td><td>{total_cases}</td></tr>
<tr><td>Absolute bestätigte Todesfälle</td><td>{total_deaths}</td></tr>
{vac_text if is_vaccinating else ''}
</table>
</details>
<details open><summary>Übersicht</summary><img src=https://dukun.de/corona/img/ac_all_{loc.replace(" ", "%20")}.png /></details>
<details open><summary>Krankenhaussituation</summary><img src=https://dukun.de/corona/{path}/hospitals.png /></details>
<details open><summary>Testsituation</summary><img src=https://dukun.de/corona/{path}/tests.png /></details>
<details open><summary>Testsituation</summary><img src=https://dukun.de/corona/{path}/tests.png />
<br>
Testing dataset: Hasell, J., Mathieu, E., Beltekian, D. et al. A cross-country database of COVID-19 testing. Sci Data 7, 345 (2020). <a href=https://doi.org/10.1038/s41597-020-00688-8>DOI:10.1038/s41597-020-00688-8</a></details>
<details open><summary>Impfsituation</summary><img src=https://dukun.de/corona/{path}/vaccinations.png /></details>
<br><br>
<a href=https://dukun.de/corona>Zurück</a><br>
@@ -81,6 +115,7 @@ Ein Infoservice von <a href=dukun.de>dukun.de</a>; Anregungen gern <a href="mail
time = data[loc]['time']
new_cases = data[loc]['new_cases']
new_deaths = data[loc]['new_deaths']
new_vaccinations = data[loc]['new_vaccinations']
total_cases = data[loc]['total_cases']
total_deaths = data[loc]['total_deaths']
total_vaccinations = data[loc]['total_vaccinations']
@@ -151,15 +186,19 @@ Ein Infoservice von <a href=dukun.de>dukun.de</a>; Anregungen gern <a href="mail
ttest_map = ~np.isnan(total_tests)
total_tests = np.array(total_tests)
new_tests = (total_tests[ttest_map][1:] - total_tests[ttest_map][:-1])/7.
ax1.plot(np.array(time)[ttest_map][1:], new_tests, color="blue", linestyle="-", linewidth=2, label="new tests")
try: ax1.plot(np.array(time)[ttest_map][1:], new_tests, color="blue", linestyle="-", linewidth=2, label="new tests")
except: pass
elif not np.isnan(new_tests).all() :
ntest_map = ~np.isnan(new_tests)
ax1.plot(np.array(time)[ntest_map], np.array(new_tests)[ntest_map]/7, color="grey", linestyle="--", label="new tests")
ax1.plot(np.array(time)[ntest_map][3:-3], np.convolve(np.array(new_tests)[ntest_map], np.ones((7,))/7, mode="valid")/7, color="blue", linewidth=2, label="new tests 7day mean")
try: ax1.plot(np.array(time)[ntest_map], np.array(new_tests)[ntest_map]/7, color="grey", linestyle="--", label="new tests")
except: pass
try: ax1.plot(np.array(time)[ntest_map][3:-3], np.convolve(np.array(new_tests)[ntest_map], np.ones((7,))/7, mode="valid")/7, color="blue", linewidth=2, label="new tests 7day mean")
except: pass
if not np.isnan(positive_rate).all():
prate_map = ~np.isnan(positive_rate)
ax2.plot(np.array(time)[prate_map], np.array(positive_rate)[prate_map]*100, color="black", linestyle="-", linewidth=2, label="positive rate (%)")
try: ax2.plot(np.array(time)[prate_map], np.array(positive_rate)[prate_map]*100, color="black", linestyle="-", linewidth=2, label="positive rate (%)")
except: pass
ax1.set_ylabel(f"tests (unit: {testunit})")
ax2.set_ylabel("positive rate")
@@ -170,8 +209,45 @@ Ein Infoservice von <a href=dukun.de>dukun.de</a>; Anregungen gern <a href="mail
pp.savefig(path+"/tests.png")
pp.close(fig)
########### vaccine situation
if True:
fig, ax1 = pp.subplots(num=loc+"_vac", figsize=figsize)
ax2 = ax1.twinx()
if not np.isnan(new_vaccinations).all():
ax1.plot(np.array(time), new_vaccinations, color="grey", linestyle="--", linewidth=1, label="new vaccinations")
if not np.isnan(total_vaccinations).all():
ax2.plot(np.array(time), total_vaccinations, color="blue", linestyle="-", linewidth=1, label="total vaccinations")
immune_mask = ~np.isnan(total_vaccinations) & ~np.isnan(total_cases)
assert len(total_vaccinations) == len(total_cases)
total_immune = np.array(total_vaccinations) + np.array(total_cases)
ax2.plot(np.array(time)[immune_mask], total_immune[immune_mask], color="green", linestyle="-", linewidth=1, label="total immune")
ax1.set_ylabel(f"new vaccinations")
ax2.set_ylabel("total vaccinations")
fig.legend(frameon=False, loc="upper left", bbox_to_anchor=(0,1), bbox_transform=ax1.transAxes)
title = f"Vaccination situation in {loc}"
try:
title += f", Vaccines: {metadata[loc]['vaccines']}"
except: pass
try:
title += f", Immune rate: {immune_rate:1.2f}%"
except:
if loc == "Germany":
print(f", Immune rate: {immune_rate:1.2f}%")
exit()
pass
ax1.set_title(title)
pp.text(0.002,0.005, f"plot generated {time_module.strftime('%Y-%m-%d %H:%M')}, CC-by-sa-nc, origin: dukun.de/corona, datasource: ourworldindata.org/coronavirus-source-data", color="dimgrey", fontsize=8, transform=fig.transFigure)
pp.savefig(path+"/vaccinations.png")
pp.close(fig)
if __name__ == "__main__":
import pickle
with open("20201221-data-metadata.dmp", "rb") as f:
with open("data.dump", "rb") as f:
data, metadata = pickle.load(f)
plot(data, [], {}, metadata=metadata)