Esempio n. 1
0
def plot_confirmed(region,
                   dates=(datetime(2020, 3, 1), datetime(2021, 3, 15)),
                   event=None,
                   save=False,
                   name='img/discussion/restrictions.png'):
    """Create plot of confirmed with restrictions.
    
    Args:
        region (str): Region to make the plot of.
        dates (tuple): Date range (date start, date end).
        event (datetime.datetime): Single event to show.
        save (bool, optional): Whether to save the figure, defaultly not.
        name (str, optional): Path to save the plot to.
    """
    # load calendar
    cal = load_calendar()
    cal = cal[cal.Country == region]
    if event is not None:
        cal = cal[cal.Date == event]
        cal['Restriction'] = cal['Title']
    else:
        cal = cal[(cal.Date >= dates[0]) & (cal.Date <= dates[1])]
    cal['Y'] = -.05
    # load data
    x = posterior._posterior_data(region, dates, weekly=False)
    POP = demographic.population.get_population(region)
    x['Confirmed cases per 1000 people'] = x.confirmed / POP * 1e3
    x = x[(x.date >= dates[0]) & (x.date <= dates[1])]
    # plot
    plt.rcParams["figure.figsize"] = (12, 6)
    fig, ax = plt.subplots()
    sns.lineplot(x='date', y='Confirmed cases per 1000 people', data=x, ax=ax)
    ax.axhline(0, alpha=.4, color='grey')
    sns.scatterplot(x='Date', y='Y', hue='Restriction', data=cal, ax=ax)
    if save: fig.savefig(name)
Esempio n. 2
0
def plot_weekly(dates, region, now=None, weekly=True, save=False):
    """Generates the plots of the simulation.
    
    Args:
        dates (tuple (2) of datetime): Time range for simulation (start, end).
        region (str): Region to load result for.
        now (datetime, optional): Time of production of the simulation. By default today.
        weekly (bool, optional): Weekly data time slots if True, otherwise daily.
        save (bool, optional): Whether to save the figure, defaultly not.
    """
    # path
    path = _get_path(dates, region, now=now, create_if_not_exists=False)
    # load
    x = posterior._posterior_data(region, dates, weekly=weekly)
    print(x)
    (sim_mean, sim_obs_mean), dt, region, params = load(dates, region, now)
    print(x.shape)
    print(sim_mean.shape)
    # plot
    posterior._plot_confirmed((sim_mean, sim_obs_mean), None, x)
    if save: _savefig(f'{path}/confirmed.png')
    posterior._plot_recovered((sim_mean, sim_obs_mean), None, x)
    if save: _savefig(f'{path}/recovered.png')
    posterior._plot_deaths((sim_mean, sim_obs_mean), None, x)
    if save: _savefig(f'{path}/deaths.png')
    plot_characteristics(dates, region, now, par='R0')
    if save: _savefig(f'{path}/R0.png')
    plot_characteristics(dates, region, now, par='Incubation period')
    if save: _savefig(f'{path}/incubation.png')
    plot_characteristics(dates, region, now, par='IFR')
    if save: _savefig(f'{path}/ifr.png')
    plot_characteristics(dates, region, now, par='Symptom duration')
    if save: _savefig(f'{path}/symptom.png')
Esempio n. 3
0
def covid_deaths(save=False, name='img/data/deaths_per100K.png'):
    """Constructs the trace plot of Covid-19 deaths.
    
    Args:
        save (bool, optional): Whether to save the figure, defaultly not.
        name (str, optional): Path to save the plot to.
    """
    # get data
    countries = ['CZ', 'PL', 'IT', 'SE']
    xx = pd.concat([
        posterior._posterior_data(country,
                                  (datetime(2020, 3, 1), datetime(2021, 5, 1)))
        for country in countries
    ])
    # population
    POP = {
        country: population.get_population(country)
        for country in countries
    }
    xx['POP'] = xx.region.apply(POP.get)
    # normalize
    xx['deaths100K'] = xx.deaths / xx.POP * 1e5
    # to weekly
    xx['year'] = xx.date.apply(lambda d: int(datetime.strftime(d, '%Y')))
    xx['week'] = xx.date.apply(lambda d: int(datetime.strftime(d, '%W')))

    def q025(x):
        return x.quantile(0.)

    def q975(x):
        return x.quantile(1.)
    xx = xx\
        .groupby(['year','week','region'])\
        .aggregate({'deaths100K': 'sum'})\
        .reset_index(drop=False)
    xx['date'] = xx.apply(
        lambda r: datetime.strptime('%04d-%02d-1' %
                                    (r.year, r.week), '%Y-%W-%w'),
        axis=1)
    # plot
    fig, ax = plt.subplots(figsize=(8, 6))
    for label, df in xx.groupby('region'):
        ax.plot(df.date, df.deaths100K, label=label)
    ax.set_xlabel('Date')
    ax.set_ylabel('Deaths per 100K')
    ax.legend()
    if save: fig.savefig(name)
Esempio n. 4
0
def parameters(regions, delta=None, weekly=False):
    """
    
    Args:
        regions ():
        delta ():
        weekly ():
    """
    # initialize
    pars = {
        'Region': [],
        'Year': np.array([]),
        'Month': np.array([]),
        'a': np.array([]),
        'c': np.array([]),
        'b': np.array([]),
        'd': np.array([])
    }
    # regions
    for region in regions:
        # load and crop
        _, dt, params = _load_result(region=region)
        if delta is None:
            dates = [dt.min(), dt.max()]
        else:
            dates = [dt.min(), dt.min() + delta]
        params = params[:, (dates[0] <= dt) & (dates[1] >= dt)]
        dt = dt[(dates[0] <= dt) & (dates[1] >= dt)]
        x = posterior._posterior_data(
            region, (max(dt.min(), dates[0]), min(dt.max(), dates[1])),
            weekly=weekly)
        # dates
        months = dt.apply(lambda d: int(datetime.strftime(d, '%m')))
        years = dt.apply(lambda d: int(datetime.strftime(d, '%Y')))
        for _ in range(dt.shape[0]):
            pars['Region'].append(region)
        pars['Year'] = np.concatenate([pars['Year'], years.to_numpy()])
        pars['Month'] = np.concatenate([pars['Month'], months.to_numpy()])
        pars['a'] = np.concatenate([pars['a'], params[0, :]])
        pars['c'] = np.concatenate([pars['c'], params[1, :]])
        pars['b'] = np.concatenate([pars['b'], params[2, :]])
        pars['d'] = np.concatenate([pars['d'], params[3, :]])
    pars = pd.DataFrame(pars)
    pars['Date'] = pars.apply(lambda r: '%04d-%02d' % (r.Year, r.Month),
                              axis=1)
    return pars
Esempio n. 5
0
def plotSusceptible_PL_Weekly(save=False, name='TODO'):
    """Generates plot of weekly susceptible for Poland.
    
    Args:
        save (bool, optional): Whether to save the figure, defaultly not.
        name (str, optional): Path to save the plot to.
    """
    # load result
    (sim_mean, sim_obs_mean), dates, region, params = load(
        (datetime(2020, 3, 3), datetime(2021, 4, 16)), 'PL',
        datetime(2021, 4, 18))
    x = posterior._posterior_data(
        'PL', (datetime(2020, 3, 3), datetime(2021, 4, 16)), weekly=False)
    # plot
    fig, ax = plt.subplots()
    ax.plot(dates, sim_mean[0, :], label='S')
    ax.legend()
    if save: fig.savefig(name)
Esempio n. 6
0
def _load_data(dates):
    """Load Covid-19 statistics in the appropriate format.
    
    Args:
        dates (tuple (2)): Date range of the data.
    """
    global _cache
    if _cache is not None:
        return _cache
    # regions
    with open('model/regions.json') as fp:
        regions = [k for k in json.load(fp) if len(k) > 2]
    # fetch data
    data_c, data_d, data_r = None, None, None
    dateaxis = None
    regions_r = []
    for reg in regions:
        x = posterior._posterior_data(reg, dates, weekly=True)
        if dateaxis is None:
            dateaxis = x.date
        POP = population.get_population(reg)
        # normalize by popylation
        x['I1K'] = x.confirmed / POP * 1e3
        x['D1K'] = x.deaths / POP * 1e3
        # confirmed
        c = x.I1K.to_numpy().reshape((1, -1))
        data_c = np.concatenate([data_c, c],
                                axis=0) if data_c is not None else c
        # deaths
        d = x.D1K.to_numpy().reshape((1, -1))
        data_d = np.concatenate([data_d, d],
                                axis=0) if data_d is not None else d
        # recovered
        if 'recovered' in x:
            x['R1K'] = x.recovered / POP * 1e3
            r = x.recovered.to_numpy().reshape((1, -1))
            data_r = np.concatenate([data_r, r],
                                    axis=0) if data_r is not None else r
            regions_r.append(reg)
    _cache = (data_c, data_d, data_r), dateaxis, (regions, regions, regions_r)
    return _cache
Esempio n. 7
0
def covid_recovered(save=False, name='img/data/recovered_per100K.png'):
    """Constructs the trace plot of Covid-19 recovered.
    
    Args:
        save (bool, optional): Whether to save the figure, defaultly not.
        name (str, optional): Path to save the plot to.
    """
    # get data
    countries = ['CZ', 'PL', 'IT']
    xx = pd.concat([
        posterior._posterior_data(country,
                                  (datetime(2020, 3, 1), datetime(2021, 5, 1)))
        for country in countries
    ])
    # population
    POP = {
        country: population.get_population(country)
        for country in countries
    }
    xx['POP'] = xx.region.apply(POP.get)
    # normalize
    xx['deaths100K'] = xx.deaths / xx.POP * 1e5
    # to weekly
    xx['year'] = xx.date.apply(lambda d: int(datetime.strftime(d, '%Y')))
    xx['week'] = xx.date.apply(lambda d: int(datetime.strftime(d, '%W')))

    def q025(x):
        return x.quantile(0.)

    def q975(x):
        return x.quantile(1.)

    # plot
    fig, ax = plt.subplots(figsize=(8, 6))
    for label, df in xx.groupby('region'):
        alpha = 1 if label != 'PL' else .5
        ax.plot(df.date, df.deaths100K, label=label, alpha=alpha)
    ax.set_xlabel('Date')
    ax.set_ylabel('Deaths per 100K')
    ax.legend()
    if save: fig.savefig(name)
Esempio n. 8
0
def prediction_data_correlation(regions, components, delta=None, weekly=False):
    """
    
    Args:
        regions ():
        components ():
        delta ():
        weekly ():
    """
    # initialize
    corrs = {'region': regions}
    for c in components:
        corrs[c] = []
    # regions
    for region in regions:
        # load and crop
        lat, dt, _ = _load_result(region=region)
        if delta is None:
            dates = [dt.min(), dt.max()]
        else:
            dates = [dt.min(), dt.min() + delta]
        lat = lat[:, (dates[0] <= dt) & (dates[1] >= dt)]
        dt = dt[(dates[0] <= dt) & (dates[1] >= dt)]
        x = posterior._posterior_data(
            region, (max(dt.min(), dates[0]), min(dt.max(), dates[1])),
            weekly=weekly)
        # compute correlation
        if 'I' in components:
            corrs['I'].append(
                np.corrcoef(lat[2, :], x.confirmed.to_numpy())[1, 0])
        if 'R' in components:
            corrs['R'].append(
                np.corrcoef(lat[3, :], x.recovered.to_numpy())[1, 0])
        if 'D' in components:
            corrs['D'].append(
                np.corrcoef(lat[4, :], x.deaths.to_numpy())[1, 0])
    corrs = pd.DataFrame(corrs)
    return corrs
Esempio n. 9
0
def save(sim, dates, region, params):
    """Save results to file.
    
    Args:
        sim (tuple (2) of matrices): Simulation results (latent [5xD], observed [5xD]).
        dates (tuple (2) of datetime): Time range for simulation (start, end).
        region (str): Region to load result for.
        now (datetime, optional): Time of production of the simulation. By default today.
    """
    # path
    path = _get_path(dates, region)
    # parse simulations
    sim_lat, sim_obs = sim
    lat = sim_lat.mean(axis=1)
    obs = sim_obs.mean(axis=1)
    x = posterior._posterior_data(region, dates)
    # save
    df = pd.DataFrame({
        'date': x.date,
        'region': region,
        'param_a': None, 'param_c': None, 'param_b': None, 'param_d': None,
        'latent_S': lat[0,:],
        'latent_E': lat[1,:],
        'latent_I': lat[2,:],
        'latent_R': lat[3,:],
        'latent_D': lat[4,:],
        'observed_I': obs[2,:], 'observed_R': obs[3,:], 'observed_D': obs[4,:],
        'data_I': x.confirmed,
        'data_R': x.recovered if 'recovered' in x.columns else np.nan,
        'data_D': x.deaths})\
        .reset_index(drop=True)
    # set parameters a,c,b,d
    for df_i in df.index:
        df_date = df.loc[df_i, 'date']
        params_i = (params.start <= df_date) & (params.end >= df_date)
        params_i = params[params_i].start.idxmax()
        for col in ['a', 'c', 'b', 'd']:
            df.loc[df_i, 'param_' + col] = float(params.loc[params_i, col])
    # save
    df.to_csv(f'{path}/data.csv', index=False)
    # aggregate
    sim_mean, sim_obs_mean = posterior.compute_sim_mean(sim)
    sim_ci, sim_obs_ci = posterior.compute_sim_ci(sim)
    # plot
    posterior._plot_confirmed((sim_mean, sim_obs_mean), (sim_ci, sim_obs_ci),
                              x)
    plt.savefig(f'{path}/confirmed.png')
    posterior._plot_recovered((sim_mean, sim_obs_mean), (sim_ci, sim_obs_ci),
                              x)
    plt.savefig(f'{path}/recovered.png')
    posterior._plot_deaths((sim_mean, sim_obs_mean), (sim_ci, sim_obs_ci), x)
    plt.savefig(f'{path}/deaths.png')
    plot_characteristics(dates, region, datetime.now(), par='R0')
    plt.savefig(f'{path}/R0.png')
    plot_characteristics(dates,
                         region,
                         datetime.now(),
                         par='Incubation period')
    plt.savefig(f'{path}/incubation.png')
    plot_characteristics(dates, region, datetime.now(), par='IFR')
    plt.savefig(f'{path}/ifr.png')
    plot_characteristics(dates, region, datetime.now(), par='Symptom duration')
    plt.savefig(f'{path}/symptom.png')