def regular_exp(t,
                n,
                k,
                beta,
                sigma,
                gamma,
                symptomatic_rate,
                sir_0,
                seed,
                npi=None,
                t_apply_npi=None,
                t_open_up=None,
                **kwargs):
    random.seed(seed)
    np.random.seed(seed)
    G = nx.random_regular_graph(k, n)
    graph = construct_network(G, n, sir_0)

    transmissibility = beta / k

    seir = np.zeros((t, len(SEIR_COMPARTMENTS)))

    if npi is None:
        seir = network_exp_with_seed(transmissibility, sigma, symptomatic_rate,
                                     gamma, n, t, graph)
    elif t_open_up is None:
        seir = network_exp_with_npi(transmissibility, sigma, symptomatic_rate,
                                    gamma, n, t, graph, npi, t_apply_npi,
                                    **kwargs)
    else:
        seir = network_exp_with_open_up(transmissibility, sigma,
                                        symptomatic_rate, gamma, n, t, graph,
                                        npi, t_apply_npi, t_open_up, **kwargs)
    # print(seir.shape)
    return seir
def ba_exp(t,
           n,
           m,
           p,
           transmissibility,
           sigma,
           gamma,
           symptomatic_rate,
           sir_0,
           seed,
           npi=None,
           t_apply_npi=None,
           t_open_up=None,
           **kwargs):
    random.seed(seed)
    np.random.seed(seed)
    G = nx.dual_barabasi_albert_graph(n, m1=m, m2=1, p=p)
    # print("ba number of edges", G.number_of_edges())
    graph = construct_network(G, n, sir_0)

    seir = np.zeros((t, len(SEIR_COMPARTMENTS)))

    if npi is None:
        seir = network_exp_with_seed(transmissibility, sigma, symptomatic_rate,
                                     gamma, n, t, graph)
    elif t_open_up is None:
        seir = network_exp_with_npi(transmissibility, sigma, symptomatic_rate,
                                    gamma, n, t, graph, npi, t_apply_npi,
                                    **kwargs)
    else:
        seir = network_exp_with_open_up(transmissibility, sigma,
                                        symptomatic_rate, gamma, n, t, graph,
                                        npi, t_apply_npi, t_open_up, **kwargs)
    return seir
def real_exp(t,
             n,
             G,
             transmissibility,
             sigma,
             gamma,
             symptomatic_rate,
             sir_0,
             seed,
             scale=1,
             npi=None,
             t_apply_npi=None,
             t_open_up=None,
             **kwargs):
    random.seed(seed)
    np.random.seed(seed)

    graph = G.copy()
    graph = construct_network(graph, n, sir_0)

    seir = np.zeros((t, len(SEIR_COMPARTMENTS)))

    if npi is None:
        seir = network_exp_with_seed(transmissibility, sigma, symptomatic_rate,
                                     gamma, n, t, graph, scale)
    elif t_open_up is None:
        seir = network_exp_with_npi(transmissibility, sigma, symptomatic_rate,
                                    gamma, n, t, graph, npi, t_apply_npi,
                                    scale, **kwargs)
    else:
        seir = network_exp_with_open_up(transmissibility, sigma,
                                        symptomatic_rate, gamma, n, t, graph,
                                        npi, t_apply_npi, t_open_up, scale,
                                        **kwargs)
    return seir
def estimate_ER(n, p_range, seeds, transmissibility, sigma, symptomatic_rate, gamma, t, base_seir, sir_0):
    best_p, min_error = 0, sys.maxsize
    for p_idx, p in enumerate(p_range):
        error_lst = np.zeros(len(seeds))

        for seed_idx in range(len(seeds)):
            random.seed(seeds[seed_idx])
            np.random.seed(seeds[seed_idx])

            G = nx.fast_gnp_random_graph(n, p)
            graph = construct_network(G, n, sir_0)

            seir = network_exp_with_seed(transmissibility, sigma, symptomatic_rate, gamma, n, t, graph)
            error = compute_error(base_seir[:, 2], seir[:, 2], t)

            error_lst[seed_idx] = error

        avg_error_p = np.average(error_lst)
        print("p", p,
              " error avg:", avg_error_p,
              " std:", np.std(error_lst),
              " max:", np.max(error_lst),
              " min:", np.min(error_lst),
              " best_seed:", seeds[np.argmin(error_lst)])

        if avg_error_p < min_error:
            min_error = avg_error_p
            best_p = p

    return best_p
def estimate_regular(n, k_range, seeds, beta, sigma, symptomatic_rate, gamma, t, base_seir, sir_0):
    best_k, min_error = 0, sys.maxsize
    for k_idx, k in enumerate(k_range):
        transmissibility = beta / k
        error_lst = np.zeros(len(seeds))

        for seed_idx in range(len(seeds)):
            random.seed(seeds[seed_idx])
            np.random.seed(seeds[seed_idx])

            G = nx.random_regular_graph(k, n)
            graph = construct_network(G, n, sir_0)

            seir = network_exp_with_seed(transmissibility, sigma, symptomatic_rate, gamma, n, t, graph)
            error = compute_error(base_seir[:, 2], seir[:, 2], t)

            error_lst[seed_idx] = error

        avg_error_k = np.average(error_lst)
        print("k:", k,
              " error avg:", avg_error_k,
              " std:", np.std(error_lst),
              " max:", np.max(error_lst),
              " min:", np.min(error_lst),
              " best_seed:", seeds[np.argmin(error_lst)])

        if avg_error_k < min_error:
            min_error = avg_error_k
            best_k = k

    return best_k
def estimate_BA(n, m_range, p_range, seeds, transmissibility, sigma, symptomatic_rate, gamma, t, base_SEIR, sir_0):
    best_m, best_p, min_error = 0, 0, sys.maxsize

    for idx, m in enumerate(m_range):
        for idx, p in enumerate(p_range):
            error_lst = np.zeros(len(seeds))
            for seed_idx in range(len(seeds)):
                random.seed(seeds[seed_idx])
                np.random.seed(seeds[seed_idx])

                G = nx.dual_barabasi_albert_graph(n, m1=m, m2=2, p=p, seed=seeds[seed_idx])
                graph = construct_network(G, n, sir_0)

                seir = network_exp_with_seed(transmissibility, sigma, symptomatic_rate, gamma, n, t, graph)
                error = compute_error(base_SEIR[:, 2], seir[:, 2], t)

                error_lst[seed_idx] = error

            avg_error_p = np.average(error_lst)
            print("m", m, " p", p,
                  " error avg:", avg_error_p,
                  " std:", np.std(error_lst),
                  " max:", np.max(error_lst),
                  " min:", np.min(error_lst),
                  " best_seed:", seeds[np.argmin(error_lst)])

            if avg_error_p < min_error:
                min_error = avg_error_p
                best_p = p
                best_m = m

    return best_m, best_p
def er_exp(t,
           n,
           p,
           transmissibility,
           sigma,
           gamma,
           symptomatic_rate,
           sir_0,
           seed,
           npi=None,
           t_apply_npi=None,
           **kwargs):
    random.seed(seed)
    np.random.seed(seed)
    G = nx.fast_gnp_random_graph(n, p)
    # print("er number of edges", G.number_of_edges())
    graph = construct_network(G, n, sir_0)

    seir = np.zeros((t, len(SEIR_COMPARTMENTS)))

    if npi is None:
        seir = network_exp_with_seed(transmissibility, sigma, symptomatic_rate,
                                     gamma, n, t, graph)
    else:
        seir = network_exp_with_npi(transmissibility, sigma, symptomatic_rate,
                                    gamma, n, t, graph, npi, t_apply_npi,
                                    **kwargs)
    return seir