Ejemplo n.º 1
0
def implementation_checks(window: int = 7):
    df = get_dataframe()
    ts = get_time_series(df)

    I = ts.Hospitalized.values
    for kernel in kernels.keys():
        plt.figure()
        plt.bar(x = ts.index, height = I, label = "raw")
        smoother = convolution(kernel, window)
        plt.plot(ts.index, smoother(I)[:-window+1], label = "smooth")
        plt.title(f"kernel: {kernel} (window: {window})", loc = "left")
    plt.show()

if __name__ == "__main__":
    root = cwd()
    data = root / "data"
    figs = root / "figs"

    gamma = 0.2
    window = 2
    num_migrants = 600000
    release_rate = 0.5

    state_cases = etl.load_cases(data / "Bihar_Case_data_May20.csv")
    district_cases = etl.split_cases_by_district(state_cases)
    district_ts = {
        district: etl.get_time_series(cases)
        for (district, cases) in district_cases.items()
    }
    R_mandatory = {
        district: estimate(district, ts, use_last=True)
        for (district, ts) in district_ts.items()
    }
    districts, pops, migrations = etl.district_migration_matrix(
        data / "Migration Matrix - District.csv")
    for district in districts:
        if district not in R_mandatory.keys():
            R_mandatory[district] = 1.5

    R_voluntary = {district: 1.5 * R for (district, R) in R_mandatory.items()}

    migration_spike = etl.migratory_influx_matrix(
data = Path("./data")
figs = Path("./figs/comparison/original")

# define data versions for api files
paths = {
    "v3": ["raw_data1.csv", "raw_data2.csv"],
    "v4": ["raw_data3.csv", "raw_data4.csv", "raw_data5.csv", "raw_data6.csv", "raw_data7.csv"]
}

df = load_all_data(
    v3_paths = [data/filepath for filepath in paths['v3']], 
    v4_paths = [data/filepath for filepath in paths['v4']]
)

ts = get_time_series(df, "detected_state")

for state in states:
    from scipy.stats import gamma # not sure why this needs to be recalled after each state, but otherwite get a type exception
    
    fig, ax = plt.subplots()

    # g=open('covid19-in-india-2/covid_19_india.csv', 'r')
    # reader=csv.reader(g)

    # confirmed=[]
    # dead=[]
    # recovered=[]
    # dates=[]
    # day=[]
    
    }

    # define data versions for api files
    paths = {
        "v3": ["raw_data1.csv", "raw_data2.csv"],
        "v4": ["raw_data3.csv", "raw_data4.csv"]
    }

    # download data from india covid 19 api
    for f in paths['v3'] + paths['v4']:
        download_data(data, f)

    # run rolling regressions on historical national case data
    dfn = load_all_data(v3_paths=paths['v3'], v4_paths=paths['v4'])
    data_recency = str(dfn["date_announced"].max()).split()[0]
    tsn = get_time_series(dfn)
    grn = run_regressions(tsn, window=5, infectious_period=1 / gamma)

    # disaggregate down to states
    dfs = {state: dfn[dfn["detected_state"] == state] for state in states}
    tss = {state: get_time_series(cases) for (state, cases) in dfs.items()}
    for (_, ts) in tss.items():
        ts['Hospitalized'] *= prevalence
    grs = {
        state: run_regressions(timeseries,
                               window=5,
                               infectious_period=1 / gamma)
        for (state, timeseries) in tss.items() if len(timeseries) > 5
    }

    # voluntary and mandatory reproductive numbers
Ejemplo n.º 5
0
def project(p: pd.Series):
    t = (p.R - p.Intercept)/p.gradient
    return (max(0, p.R), max(0, p.Intercept + p.gradient*(t + 7)), max(0, p.Intercept + p.gradient*(t + 14)), np.sqrt(p.gradient_stderr))

if __name__ == "__main__":
    root = cwd()
    data = root/"data"
    figs = root/"figs"
    
    gamma  = 0.2
    window = 5

    state_cases    = etl.load_cases(data/"Bihar_Case_data_May20.csv")
    district_cases = etl.split_cases_by_district(state_cases)
    district_ts    = {district: etl.get_time_series(cases) for (district, cases) in district_cases.items()}
    R_mandatory    = {district: estimate(district, ts, use_last = True) for (district, ts) in district_ts.items()}
    districts, pops, migrations = etl.district_migration_matrix(data/"Migration Matrix - District.csv")
    for district in districts:
        if district not in R_mandatory.keys():
            R_mandatory[district] = 1.5
    
    R_voluntary    = {district: 1.5*R for (district, R) in R_mandatory.items()}

    si, sf = 0, 1000

    simulation_results = [ 
        run_policies(district_ts, pops, districts, migrations, gamma, R_mandatory, R_voluntary, seed = seed)
        for seed in tqdm(range(si, sf))
    ]
Ejemplo n.º 6
0
            np.sqrt(p.gradient_stderr))


if __name__ == "__main__":
    root = cwd()
    data = root / "data"
    figs = root / "figs"

    gamma = 0.2
    window = 5

    state_cases = etl.load_cases(data / "Bihar_Case_data_May11.csv")
    state_cases14 = etl.load_cases(data / "Bihar_Case_data_May14.csv")
    district_cases = etl.split_cases_by_district(state_cases)
    district_ts = {
        district: etl.get_time_series(cases)
        for (district, cases) in district_cases.items()
    }
    R_mandatory = {
        district: estimate(district, ts, window=window, use_last=True)
        for (district, ts) in district_ts.items()
    }
    districts, pops, migrations = etl.district_migration_matrix(
        data / "Migration Matrix - District.csv")
    for district in districts:
        if district not in R_mandatory.keys():
            R_mandatory[district] = 1.5

    R_voluntary = {district: 1.5 * R for (district, R) in R_mandatory.items()}

    si, sf = 0, 1000
Ejemplo n.º 7
0
def state_checks():
    root = cwd()
    data = root/"data"

    # model details
    gamma      = 0.2
    prevalence = 1

    dfn = get_dataframe()

    states = ["Maharashtra", "Andhra Pradesh", "Tamil Nadu", "Madhya Pradesh", "Punjab", "Gujarat", "Kerala", "Bihar"]

    # first, check reimplementation against known figures
    CI = 0.99
    for state in states: 
        ts = get_time_series(dfn[
            (dfn.date_announced <= "2020-05-08") & 
            (dfn.detected_state == state)
        ])
        (
            dates,
            RR_pred, RR_CI_upper, RR_CI_lower,
            T_pred, T_CI_upper, T_CI_lower,
            total_cases, new_cases_ts,
            anomalies, anomaly_date
        ) = analytical_MPVS(ts.Hospitalized, CI = CI)
        plot_RR_est(dates, RR_pred, RR_CI_upper, RR_CI_lower, CI)
        plt.title(f"{state}: Estimated $R_t$", loc="left", fontsize=20)
        plt.figure()
        plot_T_anomalies(dates, T_pred, T_CI_upper, T_CI_lower, new_cases_ts, anomaly_dates, anomalies, CI)
        plt.title(f"{state}: Predicted Cases", loc="left", fontsize=20)

        plt.show()

    # generate smoothing checks for national time series
    ts = get_time_series(dfn)

    CI = 0.95
    # 1: box filter
    RRfig, RRax = plt.subplots(3, 1, sharex=True)
    T_fig, T_ax = plt.subplots(3, 1, sharex=True)
    for (i, n) in enumerate((5, 10, 15)): 
        (
            dates, 
            RR_pred, RR_CI_upper, RR_CI_lower, 
            T_pred, T_CI_upper, T_CI_lower, 
            total_cases, new_cases_ts, 
            anomalies, anomaly_dates
        ) = analytical_MPVS(ts.Hospitalized, CI = CI, smoothing = lambda ts: box_filter(ts, n, None))
        
        plt.sca(RRax[i])
        plot_RR_est(dates, RR_pred, RR_CI_upper, RR_CI_lower, CI)
        plt.title(f"smoothing window: {n}, no local smoothing", loc = "left", fontsize = 16)
        
        plt.sca(T_ax[i])
        plot_T_anomalies(dates, T_pred, T_CI_upper, T_CI_lower, new_cases_ts, anomaly_dates, anomalies, CI)
        plt.title(f"smoothing window: {n}, no local smoothing", loc = "left", fontsize = 16)
    
    plt.figure(RRfig.number)
    RRfig.tight_layout()
    plt.suptitle("Estimated Rt (box filter convolution)", fontsize = 20)
    plt.figure(T_fig.number)
    T_fig.tight_layout()
    plt.suptitle("Estimated Daily Cases (box filter convolution)", fontsize = 20)
    plt.show()


    # 2: box filter, local smooothing
    RRfig, RRax = plt.subplots(3, 1, sharex=True)
    T_fig, T_ax = plt.subplots(3, 1, sharex=True)
    for (i, n) in enumerate((5, 10, 15)): 
        s = n//2 + 1
        (
            dates, 
            RR_pred, RR_CI_upper, RR_CI_lower, 
            T_pred, T_CI_upper, T_CI_lower, 
            total_cases, new_cases_ts, 
            anomalies, anomaly_dates
        ) = analytical_MPVS(ts.Hospitalized, CI = CI, smoothing = lambda ts: box_filter(ts, n, s))
        
        plt.sca(RRax[i])
        plot_RR_est(dates, RR_pred, RR_CI_upper, RR_CI_lower, CI)
        plt.title(f"smoothing window: {n}, local smoothing: last {s} points", loc = "left", fontsize = 16)
        
        plt.sca(T_ax[i])
        plot_T_anomalies(dates, T_pred, T_CI_upper, T_CI_lower, new_cases_ts, anomaly_dates, anomalies, CI)
        plt.title(f"smoothing window: {n}, local smoothing: last {s} points", loc = "left", fontsize = 16)
    
    plt.figure(RRfig.number)
    RRfig.tight_layout()
    plt.suptitle("Estimated Rt (box filter convolution, local smoothing)", fontsize = 20)
    plt.subplots_adjust(right = 0.9)

    plt.figure(T_fig.number)
    T_fig.tight_layout()
    plt.suptitle("Estimated Daily Cases (box filter convolution, local smoothing)", fontsize = 20)
    plt.subplots_adjust(right = 0.9)
    
    plt.show()