util.py 1.94 KB
Newer Older
Pierre Augier's avatar
Pierre Augier committed
1
2
from datetime import datetime, timedelta

Pierre Augier's avatar
Pierre Augier committed
3
4
import numpy as np

Pierre Augier's avatar
Pierre Augier committed
5
from numba import jit
6

7
from .load_data import population
Pierre Augier's avatar
Pierre Augier committed
8
from .load_data import load_dataframe_dep
Pierre Augier's avatar
Pierre Augier committed
9

10
default_first_day_in_plot = "2021-05-03"
Pierre Augier's avatar
Pierre Augier committed
11
12
13
14

fmt_date = "%Y-%m-%d"


Pierre Augier's avatar
Pierre Augier committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def compute_min_incidence_default():
    df = load_dataframe_dep()
    df = df[df.cl_age90 == 0]
    incidences = []
    for dep in df.dep.unique():
        if len(dep) > 2:
            # skip DOM-TOM (only "métropole")
            continue
        tmp = df[df.dep == dep].copy()
        complete_df_1loc_1age(tmp, dep)
        last = tmp[tmp.index == tmp.index.max()]
        incidences.append(last["incidence"].values[0])
    incidences.sort()
    return round(incidences[-13])


Pierre Augier's avatar
Pierre Augier committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def create_date_object(date: str):
    return datetime.strptime(date, fmt_date)


def format_date_for_human(date: str):
    date_obj = create_date_object(date)
    return date_obj.strftime("%d/%m/%Y")


def format_date(date_obj):
    return date_obj.strftime(fmt_date)


def shift_date_str(date: str, nb_days: int):
    date_new = create_date_object(date) + timedelta(nb_days)
    return format_date(date_new)

Pierre Augier's avatar
Pierre Augier committed
48

Pierre Augier's avatar
Pierre Augier committed
49
@jit
Pierre Augier's avatar
Pierre Augier committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def cumul7(data: "int[]"):
    n = 7
    ret = np.empty_like(data)

    tmp = 0
    for i in range(n):
        ret[i] = tmp = tmp + data[i]

    for i in range(n, len(data)):
        ret[i] = tmp = tmp + data[i] - data[i - n]

    return ret


Pierre Augier's avatar
Pierre Augier committed
64
def complete_df_1loc_1age(tmp, dep=None):
Pierre Augier's avatar
Pierre Augier committed
65
66
67
68
69
70

    tmp["Tc"] = cumul7(tmp["T"].values)
    tmp["Pc"] = cumul7(tmp["P"].values)
    tmp["ratio_c"] = 100 * tmp["Pc"] / tmp["Tc"]

    if dep is not None:
Pierre Augier's avatar
Pierre Augier committed
71
        tmp["incidence"] = 100000 / population[dep] * tmp["Pc"]
Pierre Augier's avatar
Pierre Augier committed
72
73
74
75
76
77
78
79


def estimate_Reff(serie, date_R_begin, date_R_end):
    serie = serie[(serie.index >= date_R_begin) & (serie.index <= date_R_end)]
    log2_I = np.log2(serie.values)
    sigma12, _ = np.polyfit(np.arange(len(log2_I)), log2_I, 1)
    R_eff = 2 ** (7 * sigma12)
    return R_eff, 1 / sigma12
Pierre Augier's avatar
Pierre Augier committed
80
81
82


min_incidence_default = compute_min_incidence_default()