コード例 #1
0
ファイル: diet.py プロジェクト: pzivich/publications-code
def diet_dgm(network, restricted=False):
    """
    Parameters
    ----------
    network:
        input network
    restricted:
        whether to use the restricted treatment assignment
    """
    graph = network.copy()
    data = network_to_df(graph)

    adj_matrix = nx.adjacency_matrix(graph, weight=None)
    data['G_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['G']),
                                  measure='mean')
    data['E_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['E']),
                                  measure='mean')
    data['E_sum'] = fast_exp_map(adj_matrix,
                                 np.array(data['E']),
                                 measure='sum')
    data['B_mean_dist'] = fast_exp_map(adj_matrix,
                                       np.array(data['B']),
                                       measure='mean_dist')
    data['B_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['B']),
                                  measure='mean')

    # Running Data Generating Mechanism for A
    pr_a = logistic.cdf(-0.5 + 0.05 * (data['B'] - 30) +
                        0.25 * data['G'] * data['E'] + 0.05 * data['E_mean'])
    diet = np.random.binomial(n=1, p=pr_a, size=nx.number_of_nodes(graph))
    data['diet'] = diet
    if restricted:  # if we are in the restricted scenarios
        attrs = exposure_restrictions(network=network.graph['label'],
                                      exposure='diet')
        data.update(
            pd.DataFrame(list(attrs.values()),
                         index=list(attrs.keys()),
                         columns=['diet']))

    data['diet_sum'] = fast_exp_map(adj_matrix,
                                    np.array(data['diet']),
                                    measure='sum')
    data['diet_t3'] = np.where(data['diet_sum'] > 3, 1, 0)

    # Running Data Generating Mechanism for Y
    bmi = (3 + data['B'] - 5 * data['diet'] - 5 * data['diet_t3'] +
           3 * data['G'] - 3 * data['E'] - 0.5 * data['E_sum'] +
           data['B_mean_dist'] +
           np.random.normal(0, scale=1, size=nx.number_of_nodes(graph)))
    data['bmi'] = bmi

    # Adding node information back to graph
    for n in graph.nodes():
        graph.nodes[n]['diet'] = int(data.loc[data.index == n, 'diet'].values)
        graph.nodes[n]['bmi'] = float(data.loc[data.index == n, 'bmi'].values)

    return graph
コード例 #2
0
ファイル: statin.py プロジェクト: pzivich/publications-code
def statin_dgm_truth(network, pr_a, shift=False, restricted=False):
    graph = network.copy()
    data = network_to_df(graph)

    # Running Data Generating Mechanism for A
    if shift:  # If a shift in the Odds distribution is instead specified
        prob = logistic.cdf(-5.3 + 0.2 * data['L'] + 0.15 * (data['A'] - 30) +
                            0.4 * np.where(data['R_1'] == 1, 1, 0) +
                            0.9 * np.where(data['R_2'] == 2, 1, 0) +
                            1.5 * np.where(data['R_3'] == 3, 1, 0))
        odds = probability_to_odds(prob)
        pr_a = odds_to_probability(np.exp(np.log(odds) + pr_a))

    statin = np.random.binomial(n=1, p=pr_a, size=nx.number_of_nodes(graph))
    data['statin'] = statin

    if restricted:  # removing other observations from the restricted set
        attrs = exposure_restrictions(network=network.graph['label'],
                                      exposure='statin')
        exclude = list(attrs.keys())
        data = data.loc[~data.index.isin(exclude)].copy()

    # Running Data Generating Mechanism for Y
    pr_y = logistic.cdf(-5.05 - 0.8 * data['statin'] + 0.37 *
                        (np.sqrt(data['A'] - 39.9)) + 0.75 * data['R'] +
                        0.75 * data['L'])
    cvd = np.random.binomial(n=1, p=pr_y, size=data.shape[0])
    return np.mean(cvd)
コード例 #3
0
def vaccine_dgm(network,
                restricted=False,
                n_init_infect=7,
                time_limit=10,
                inf_duration=5):
    """
    Parameters
    ----------
    network:
        input network
    restricted:
        whether to use the restricted treatment assignment
    n_init_infect:
        number of initial infections to start with
    time_limit:
        maximum time to let the outbreak go through
    inf_duration:
        duration of infection status in time-steps
    """
    graph = network.copy()
    data = network_to_df(graph)

    adj_matrix = nx.adjacency_matrix(graph, weight=None)
    data['A_sum'] = fast_exp_map(adj_matrix,
                                 np.array(data['A']),
                                 measure='sum')
    data['A_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['A']),
                                  measure='mean')
    data['H_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['H']),
                                  measure='mean')

    # Running Data Generating Mechanism for A
    pr_a = logistic.cdf(-1.9 + 1.75 * data['A'] + 0.95 * data['H'] +
                        1.2 * data['H_mean'])
    vaccine = np.random.binomial(n=1, p=pr_a, size=nx.number_of_nodes(graph))
    data['vaccine'] = vaccine
    if restricted:  # if we are in the restricted scenarios
        attrs = exposure_restrictions(network=network.graph['label'],
                                      exposure='vaccine')
        data.update(
            pd.DataFrame(list(attrs.values()),
                         index=list(attrs.keys()),
                         columns=['vaccine']))

    # print("Pr(V):", np.mean(vaccine))
    for n in graph.nodes():
        graph.nodes[n]['vaccine'] = int(data.loc[data.index == n,
                                                 'vaccine'].values)

    # Running outbreak simulation
    graph = _outbreak_(graph,
                       n_init_infect,
                       duration=inf_duration,
                       limit=time_limit)
    return graph
コード例 #4
0
ファイル: naloxone.py プロジェクト: pzivich/publications-code
def naloxone_dgm_truth(network, pr_a, shift=False, restricted=False):
    graph = network.copy()
    data = network_to_df(graph)
    adj_matrix = nx.adjacency_matrix(graph, weight=None)
    data['O_sum'] = fast_exp_map(adj_matrix,
                                 np.array(data['O']),
                                 measure='sum')
    data['O_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['O']),
                                  measure='mean')
    data['G_sum'] = fast_exp_map(adj_matrix,
                                 np.array(data['G']),
                                 measure='sum')
    data['G_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['G']),
                                  measure='mean')

    # Running Data Generating Mechanism for A
    if shift:  # If a shift in the Odds distribution is instead specified
        prob = logistic.cdf(-1.3 - 1.5 * data['P'] +
                            1.5 * data['P'] * data['G'] +
                            0.95 * data['O_mean'] + 0.95 * data['G_mean'])
        odds = probability_to_odds(prob)
        pr_a = odds_to_probability(np.exp(np.log(odds) + pr_a))

    naloxone = np.random.binomial(n=1, p=pr_a, size=nx.number_of_nodes(graph))
    data['naloxone'] = naloxone
    if restricted:  # if we are in the restricted scenarios
        attrs = exposure_restrictions(network=network.graph['label'],
                                      exposure='naloxone')
        data.update(
            pd.DataFrame(list(attrs.values()),
                         index=list(attrs.keys()),
                         columns=['naloxone']))
        exclude = list(attrs.keys())

    # Creating network summary variables
    data['naloxone_sum'] = fast_exp_map(adj_matrix,
                                        np.array(data['naloxone']),
                                        measure='sum')

    # Running Data Generating Mechanism for Y
    pr_y = logistic.cdf(-1.1 - 0.2 * data['naloxone_sum'] + 1.7 * data['P'] -
                        0.9 * data['G'] + 0.75 * data['O_mean'] -
                        0.75 * data['G_mean'])
    overdose = np.random.binomial(n=1, p=pr_y, size=nx.number_of_nodes(graph))
    if restricted:
        data['overdose'] = overdose
        data = data.loc[~data.index.isin(exclude)].copy()
        overdose = np.array(data['overdose'])

    return np.mean(overdose)
コード例 #5
0
ファイル: diet.py プロジェクト: pzivich/publications-code
def diet_dgm_truth(network, pr_a, restricted=False, shift=False):
    graph = network.copy()
    data = network_to_df(graph)
    adj_matrix = nx.adjacency_matrix(graph, weight=None)
    data['B_mean_dist'] = fast_exp_map(adj_matrix,
                                       np.array(data['B']),
                                       measure='mean_dist')
    data['E_sum'] = fast_exp_map(adj_matrix,
                                 np.array(data['E']),
                                 measure='sum')
    data['E_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['E']),
                                  measure='mean')

    if shift:  # If a shift in the Odds distribution is instead specified
        prob = logistic.cdf(-0.5 + 0.05 * (data['B'] - 30) +
                            0.25 * data['G'] * data['E'] +
                            0.05 * data['E_mean'])
        odds = probability_to_odds(prob)
        pr_a = odds_to_probability(np.exp(np.log(odds) + pr_a))

    diet = np.random.binomial(n=1, p=pr_a, size=nx.number_of_nodes(graph))
    data['diet'] = diet
    if restricted:  # if we are in the restricted scenarios
        attrs = exposure_restrictions(network=network.graph['label'],
                                      exposure='diet')
        data.update(
            pd.DataFrame(list(attrs.values()),
                         index=list(attrs.keys()),
                         columns=['diet']))
        exclude = list(attrs.keys())

    # Running Data Generating Mechanism for Y
    data['diet_sum'] = fast_exp_map(adj_matrix,
                                    np.asarray(data['diet']),
                                    measure='sum')
    data['diet_t3'] = np.where(data['diet_sum'] > 3, 1, 0)
    bmi = (3 + data['B'] - 5 * data['diet'] - 5 * data['diet_t3'] +
           3 * data['G'] - 3 * data['E'] - 0.5 * data['E_sum'] +
           data['B_mean_dist'] +
           np.random.normal(0, scale=1, size=nx.number_of_nodes(graph)))

    if restricted:
        data['bmi'] = bmi
        data = data.loc[~data.index.isin(exclude)].copy()
        bmi = np.array(data['bmi'])

    return np.mean(bmi)
コード例 #6
0
def vaccine_dgm_truth(network,
                      pr_a,
                      shift=False,
                      restricted=False,
                      n_init_infect=7,
                      time_limit=10,
                      inf_duration=5):
    graph = network.copy()
    data = network_to_df(graph)

    # Running Data Generating Mechanism for A
    if shift:  # If a shift in the Odds distribution is instead specified
        adj_matrix = nx.adjacency_matrix(graph, weight=None)
        data['A_sum'] = fast_exp_map(adj_matrix,
                                     np.array(data['A']),
                                     measure='sum')
        data['H_mean'] = fast_exp_map(adj_matrix,
                                      np.array(data['H']),
                                      measure='mean')
        prob = logistic.cdf(-1.9 + 1.75 * data['A'] + 0.95 * data['H'] +
                            1.2 * data['H_mean'])
        odds = probability_to_odds(prob)
        pr_a = odds_to_probability(np.exp(np.log(odds) + pr_a))

    vaccine = np.random.binomial(n=1, p=pr_a, size=nx.number_of_nodes(graph))
    data['vaccine'] = vaccine
    if restricted:  # if we are in the restricted scenarios
        attrs = exposure_restrictions(network=network.graph['label'],
                                      exposure='vaccine')
        data.update(
            pd.DataFrame(list(attrs.values()),
                         index=list(attrs.keys()),
                         columns=['vaccine']))

    for n in graph.nodes():
        graph.nodes[n]['vaccine'] = int(data.loc[data.index == n,
                                                 'vaccine'].values)

    # Running Data Generating Mechanism for Y
    graph = _outbreak_(graph,
                       n_init_infect,
                       duration=inf_duration,
                       limit=time_limit)
    dis = []
    for nod, d in graph.nodes(data=True):
        dis.append(d['D'])
    return np.mean(dis)
コード例 #7
0
ファイル: statin.py プロジェクト: pzivich/publications-code
def statin_dgm(network, restricted=False):
    """
    Parameters
    ----------
    network:
        input network
    restricted:
        whether to use the restricted treatment assignment
    """
    graph = network.copy()
    data = network_to_df(graph)

    # Running Data Generating Mechanism for A
    pr_a = logistic.cdf(-5.3 + 0.2 * data['L'] + 0.15 * (data['A'] - 30) +
                        0.4 * np.where(data['R_1'] == 1, 1, 0) +
                        0.9 * np.where(data['R_2'] == 2, 1, 0) +
                        1.5 * np.where(data['R_3'] == 3, 1, 0))
    statin = np.random.binomial(n=1, p=pr_a, size=nx.number_of_nodes(graph))
    data['statin'] = statin

    if restricted:  # if we are in the restricted scenarios
        attrs = exposure_restrictions(network=network.graph['label'],
                                      exposure='statin')
        data.update(
            pd.DataFrame(list(attrs.values()),
                         index=list(attrs.keys()),
                         columns=['statin']))

    # Running Data Generating Mechanism for Y
    pr_y = logistic.cdf(-5.05 - 0.8 * data['statin'] + 0.37 *
                        (np.sqrt(data['A'] - 39.9)) + 0.75 * data['R'] +
                        0.75 * data['L'])
    cvd = np.random.binomial(n=1, p=pr_y, size=nx.number_of_nodes(graph))
    data['cvd'] = cvd

    # Adding node information back to graph
    for n in graph.nodes():
        graph.nodes[n]['statin'] = int(data.loc[data.index == n,
                                                'statin'].values)
        graph.nodes[n]['cvd'] = float(data.loc[data.index == n, 'cvd'].values)

    return graph
コード例 #8
0
        degree_restrict = (0, 18)
        restrict = True
    else:
        raise ValueError("Invalid set-up specification for " + network +
                         " network")

else:
    raise ValueError("Invalid network name in .sh script")

# Determining if shift or absolute
shift = bool(int(shift))
if shift:
    prop_treated = [-2.5, -2.0, -1.5, -1.0, -0.5, 0.5, 1.0, 1.5, 2.0, 2.5]

    # Generating probabilities (true) to assign
    data = network_to_df(G)
    adj_matrix = nx.adjacency_matrix(G, weight=None)
    data['O_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['O']),
                                  measure='mean')
    data['G_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['G']),
                                  measure='mean')
    prob = logistic.cdf(-1.3 - 1.5 * data['P'] + 1.5 * data['P'] * data['G'] +
                        0.95 * data['O_mean'] + 0.95 * data['G_mean'])
    log_odds = np.log(probability_to_odds(prob))

else:
    prop_treated = [
        0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65,
        0.7, 0.75, 0.8, 0.85, 0.9, 0.95
コード例 #9
0
ファイル: naloxone.py プロジェクト: pzivich/publications-code
def naloxone_dgm(network, restricted=False):
    """
    Parameters
    ----------
    network:
        input network
    restricted:
        whether to use the restricted treatment assignment
    """
    graph = network.copy()
    data = network_to_df(graph)

    adj_matrix = nx.adjacency_matrix(graph, weight=None)
    data['O_sum'] = fast_exp_map(adj_matrix,
                                 np.array(data['O']),
                                 measure='sum')
    data['O_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['O']),
                                  measure='mean')
    data['G_sum'] = fast_exp_map(adj_matrix,
                                 np.array(data['G']),
                                 measure='sum')
    data['G_mean'] = fast_exp_map(adj_matrix,
                                  np.array(data['G']),
                                  measure='mean')

    # Running Data Generating Mechanism for A
    pr_a = logistic.cdf(-1.3 - 1.5 * data['P'] + 1.5 * data['P'] * data['G'] +
                        0.95 * data['O_mean'] + 0.95 * data['G_mean'])
    naloxone = np.random.binomial(n=1, p=pr_a, size=nx.number_of_nodes(graph))
    data[
        'naloxone'] = naloxone  # https://www.sciencedirect.com/science/article/pii/S074054721730301X (30%)
    if restricted:  # if we are in the restricted scenarios
        attrs = exposure_restrictions(network=network.graph['label'],
                                      exposure='naloxone')
        data.update(
            pd.DataFrame(list(attrs.values()),
                         index=list(attrs.keys()),
                         columns=['naloxone']))

    data['naloxone_sum'] = fast_exp_map(adj_matrix,
                                        np.array(data['naloxone']),
                                        measure='sum')

    # Running Data Generating Mechanism for Y
    pr_y = logistic.cdf(-1.1 - 0.2 * data['naloxone_sum'] + 1.7 * data['P'] -
                        0.9 * data['G'] + 0.75 * data['O_mean'] -
                        0.75 * data['G_mean'])
    overdose = np.random.binomial(n=1, p=pr_y, size=nx.number_of_nodes(graph))
    data[
        'overdose'] = overdose  # https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5832501/?report=classic (20%)
    # print(data[['naloxone', 'overdose']].describe())

    # Adding node information back to graph
    for n in graph.nodes():
        graph.nodes[n]['naloxone'] = int(data.loc[data.index == n,
                                                  'naloxone'].values)
        graph.nodes[n]['overdose'] = int(data.loc[data.index == n,
                                                  'overdose'].values)

    return graph
コード例 #10
0
########################################
results = pd.DataFrame(index=range(n_mc), columns=['inc_'+exposure, 'inc_'+outcome] +
                                                  ['bias_' + str(p) for p in prop_treated] +
                                                  ['lcl_' + str(p) for p in prop_treated] +
                                                  ['ucl_' + str(p) for p in prop_treated] +
                                                  ['var_' + str(p) for p in prop_treated]
                       )


########################################
# Running simulation
########################################
for i in range(n_mc):
    # Generating Data
    H = obs_net_gen(G)
    df = network_to_df(H)
    results.loc[i, 'inc_'+exposure] = np.mean(df[exposure])
    results.loc[i, 'inc_'+outcome] = np.mean(df[outcome])

    if estimator == 'tmle':
        # Network TMLE
        ntmle = NetworkTMLE(H, exposure=exposure, outcome=outcome)
        ntmle.exposure_model(gi_model)
        ntmle.exposure_map_model(gs_model)
        ntmle.outcome_model(qi_model)
        for p in prop_treated:  # loops through all treatment plans
            try:
                ntmle.fit(p=p, bound=0.01)
                results.loc[i, 'bias_'+str(p)] = ntmle.marginal_outcome - truth[p]
                results.loc[i, 'var_'+str(p)] = ntmle.conditional_variance
                results.loc[i, 'lcl_'+str(p)] = ntmle.conditional_ci[0]