~humaid/areweherdimmuneyet

areweherdimmuneyet/main.py -rw-r--r-- 3.1 KiB
458b90b3 — Humaid AlQassimi Show first dose and last entry in template 16 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import sys
import math
from datetime import date, timedelta, datetime
import requests
from quik import FileLoader

# Doses goal for calculations
UAE_POP = 9890620
R0 = 6.5
GOAL = (1 - (1/R0))*100
print("goal", GOAL)

owid = pd.read_csv('data.csv')

dat = [ ] 

for index, row in owid.iterrows():
    fully = row['people_fully_vaccinated']
    if not math.isnan(fully):
        dat.append([row['date'], (fully/UAE_POP)*100])

last_entry = dat[len(dat)-1][0]

first_dose = (owid.tail(1)['people_vaccinated'].item()/UAE_POP)*100
first_dose = round(first_dose, 2)
print(first_dose)
dataset = pd.DataFrame(dat, columns=['Date', 'Percentage'])
dataset['Date'] = pd.to_datetime(dataset['Date'])

x = mdates.date2num(pd.Index(dataset.Date).to_pydatetime())
y = dataset['Percentage']


def get_herd_immunity():
    model = np.poly1d(np.polyfit(x, y, 2))
    x0 = (model - GOAL).roots
    #print(mdates.num2date(x0[0]), mdates.num2date(x[1]))
    return (model, mdates.num2date(x0[1]))


def gen_html(estimate):
    print("Rendering HTML")
    loader = FileLoader('.')
    template = loader.load_template('template.html')
    estimate_txt = estimate.strftime("%d %B")
    res = template.render(
        {'estimate': estimate_txt,
         'goalMs': round(estimate.timestamp()),
         'today': date.today().strftime("%d %B"),
         'ver': round(datetime.now().timestamp()),
         'goal': int(GOAL),
         'lastEntry': datetime.strptime(last_entry, "%Y-%m-%d").strftime("%d %B"),
         'vaccinated': first_dose},
        loader=loader).encode('utf-8')
    f = open("index.html", "w")
    f.write(res.decode("utf-8"))
    f.close()


def gen_img():
    print("Generating image")
    model, estimate = get_herd_immunity()
    estimate_txt = estimate.strftime("%d %b")

    # show graph up to:
    until = estimate + timedelta(days=3)
    myline = np.linspace(mdates.date2num(dataset.Date[0]),
                         mdates.date2num(until))

    fig, ax = plt.subplots()

    # set color to match page
    fig.set_facecolor("#fafafa")

    dayloc = mdates.DayLocator()
    fdloc = mdates.DayLocator(interval=15)
    maj_fmt = mdates.DateFormatter('%-d %b')
    ax.xaxis.set_major_formatter(maj_fmt)
    ax.xaxis.set_major_locator(fdloc)
    ax.xaxis.set_minor_locator(dayloc)
    fig.autofmt_xdate()
    ax.grid(True)
    ax.grid(b=True, which="minor")

    plt.plot(dataset.Date, y, label="People fully vaccinated",
             color="tab:green")
    plt.plot(myline, model(myline), label="Estimate (polynomial regression)",
             linestyle=":", color="tab:orange")
    plt.plot(mdates.date2num(estimate), GOAL, 'g*',
            label="Estimated Goal ("+str(int(GOAL))+"%)")
    plt.text(mdates.date2num(estimate)-15, GOAL-2, "("+estimate_txt+")")
    plt.ylabel('Percentage')
    plt.xlabel('Date')
    plt.title('People Fully Vaccinated')
    plt.legend()

    ax.tick_params(axis='x', which='both', labelsize=5)
    plt.savefig('chart.svg')

    print("We will reach herd immunity at: " + estimate_txt)
    gen_html(estimate)

if __name__ == "__main__":
    gen_img()