Exemple #1
0
def epidemic_p_inocs(
        wt_pct,
        f_mut = None,
        bottleneck = None,
        v = None,
        R0_population = None,
        escape = None,
        fermi_steepness = None,
        distribution = [],
        susceptibility_model = "linear",
        inoculum_model = "poisson",
        cross_imm_sigma = None,
        z_wt = None,
        exact = False,
        N = 1):
    
    sus_func = pick_sus_func(susceptibility_model,
                             escape,
                             fermi_steepness,
                             homotypic_sus = 1 - z_wt)

    z_wts = get_z_wts(distribution, sus_func)
    wt_susses = 1.0 - z_wts # tell numpy this is a float!
    freqs = get_freqs(wt_pct, distribution)

    tot_inoc, class_inoc = multi_class_inoculations(
        wt_susses,
        freqs,
        R0_population)

    s_finals, r_finals = multi_class_final_size(wt_susses,
                                                freqs,
                                                R0_population)
    imm_final = np.sum(r_finals)
    s_finals = list(s_finals) + [imm_final]
        
    log_per_cap_none = [
        (class_inoc *
         np.log(1 - analytic_p_inocs(bottleneck,
                                     f_mut,
                                     p_loss = 0,
                                     wt_neut_prob = neutralize_prob_from_z(
                                         z_wt,
                                         v,
                                         inoculum_model,
                                         conditional_on_inoculated = True),
                                     mut_neut_prob = neutralize_prob_from_z(
                                         z_wt,
                                         v,
                                         inoculum_model) * cross_imm_sigma,
                                     inoculum_size = v,
                                     exact=exact)))
        for z_wt, class_inoc in zip(z_wts, class_inoc)]

    return 1 - np.exp(np.sum(log_per_cap_none * N))
def plot_sus_funcs(axis=None,
                   escape=0.25,
                   fermi_steepness=3,
                   fermi_style=fermi_style,
                   multiplicative_style=multiplicative_style,
                   linear_style=linear_style,
                   titer_style=titer_style,
                   cmaps=[None] * 2,
                   max_dark=0.7,
                   line_alpha=line_alpha,
                   z_homotypic=None,
                   **kwargs):
    if axis is None:
        fig, axis = plt.subplots()

    xs = np.linspace(0, 8, 100)

    for display_name, model_name, model_style, cmap in zip(
        ['multiplicative', 'sigmoid'], ['multiplicative', 'titer'],
        [multiplicative_style, titer_style], cmaps):
        sus_func = pick_sus_func(model_name,
                                 escape,
                                 fermi_steepness=fermi_steepness,
                                 homotypic_sus=1 - z_homotypic)
        ys = [sus_func(x) for x in xs]

        if cmap is not None:
            color = cmap(max_dark)
        else:
            color = None

        axis.plot(xs,
                  ys,
                  linestyle=model_style,
                  label=display_name,
                  alpha=line_alpha,
                  color=color,
                  **kwargs)

    axis.set_xlabel("cluster distance")
    axis.set_ylabel("susceptibility")
    axis.legend()
def plot_optimal_selector_cluster(f_mut=None,
                                  bottleneck=None,
                                  R0_wh=None,
                                  escape=None,
                                  axis=None,
                                  legend=True,
                                  fermi_steepness=None,
                                  homotypic_titer=None,
                                  inoculum_size=25,
                                  exact=False,
                                  plot_drift=True,
                                  inoculum_model=None,
                                  effective_num_independent=None,
                                  cross_imm_sigma=None,
                                  cmaps=[None] * 2,
                                  darkness=0.99,
                                  line_alpha=line_alpha,
                                  drift_style=drift_style,
                                  z_homotypic=None,
                                  verbose=False,
                                  **kwargs):
    if axis is None:
        fig, axis = plt.subplots()

    dists = np.linspace(0, 10, 1000)

    if plot_drift:
        drift_prob = analytic_p_inocs(bottleneck,
                                      f_mut,
                                      p_loss=0,
                                      mut_neut_prob=0,
                                      wt_neut_prob=0,
                                      inoculum_model=inoculum_model,
                                      bottleneck_model="binomial",
                                      inoculum_size=inoculum_size)
        print("drift emergence prob: {}".format(drift_prob))
        axis.axhline(drift_prob, linestyle=drift_style, color="k", alpha=1)

    models = [
        #"linear",
        "multiplicative",
        #"fermi",
        "titer"
    ]

    styles = [
        # linear_style,
        multiplicative_style,
        # fermi_style,
        titer_style
    ]

    for model_name, linestyle, cmap, in zip(models, styles, cmaps):
        print("calculating wild-type and mutant protection "
              "for {} with fermi_steepness {} and "
              "homotypic_titer {}".format(model_name, fermi_steepness,
                                          homotypic_titer))
        sus_func = pick_sus_func(model_name,
                                 escape,
                                 fermi_steepness=fermi_steepness,
                                 homotypic_sus=1 - z_homotypic)

        z_list = [(1 - sus_func(dist), 1 - sus_func(dist + 1))
                  for dist in dists]

        if effective_num_independent is None:
            effective_num_independent = inoculum_size

        wt_neut_probs = [
            neutralize_prob_from_z(z_wt, inoculum_size, inoculum_model)
            for z_wt, _ in z_list
        ]
        if cross_imm_sigma is not None:
            mut_neut_probs = [wt_n * cross_imm_sigma for wt_n in wt_neut_probs]
        else:
            mut_neut_probs = [
                neutralize_prob_from_z(z_mut, effective_num_independent,
                                       inoculum_model) for _, z_mut in z_list
            ]

        print("calculating escape probs for {}...".format(model_name))
        probs = [
            analytic_p_inocs(bottleneck,
                             f_mut,
                             p_loss=0,
                             mut_neut_prob=mnp,
                             wt_neut_prob=wnp,
                             inoculum_size=inoculum_size,
                             exact=exact,
                             verbose=False)
            for mnp, wnp in zip(mut_neut_probs, wt_neut_probs)
        ]
        if cmap is not None:
            color = cmap(darkness)
        else:
            color = None
        axis.plot(dists,
                  probs,
                  label=model_name,
                  color=color,
                  linestyle=linestyle,
                  alpha=line_alpha,
                  **kwargs)

    if legend:
        axis.legend()
    axis.set_xlabel("cluster distance")
    axis.set_ylabel("emergence probability")
Exemple #4
0
def get_inocs_from_immunity(
        f_mut = None,
        final_bottleneck = None,
        mucus_bottleneck = None,
        escape = None,
        fermi_steepness = None,
        distribution = [],
        x_min = 0,
        x_max = 1,
        susceptibility_model = None,
        cross_imm_sigma = None,
        homotypic_sus = None,
        fineness = 1000,
        inoculum_model = "poisson"):

    sus_func = pick_sus_func(susceptibility_model,
                             escape,
                             fermi_steepness = fermi_steepness,
                             homotypic_sus = homotypic_sus)
    
    z_wts = get_z_wts(distribution, sus_func)

    wt_neut_probs = np.array([
        neutralize_prob_from_z(
            z_wt,
            mucus_bottleneck,
            inoculum_model)
        for z_wt in z_wts])
    
    z_muts = get_z_muts(distribution, sus_func)

    if cross_imm_sigma is not None:
        mut_neut_probs = cross_imm_sigma * wt_neut_probs
    else:
        mut_neut_probs = np.array([
            neutralize_prob_from_z(
                z_mut,
                mucus_bottleneck,
                inoculum_model)
            for z_mut in z_muts])

    print(wt_neut_probs)
    print(weighted_minimal_p_inocs(
        final_bottleneck,
        f_mut,
        wt_neut_probs=[wt_neut_probs[0]],
        mut_neut_probs=[mut_neut_probs[0]],
        inoculum_size=mucus_bottleneck))
    wt_pcts = np.linspace(x_min, x_max, fineness)
    if x_max > 1:
        raise ValueError("Cannot have more than 100% immune")
    per_inoc_inocs_prob = [
        weighted_minimal_p_inocs(final_bottleneck,
                                 f_mut,
                                 host_frequencies=get_freqs(wt_pct,
                                                            distribution),
                                 wt_neut_probs=wt_neut_probs,
                                 mut_neut_probs=mut_neut_probs,
                                 inoculum_size=mucus_bottleneck)
        for wt_pct in wt_pcts]

    return (wt_pcts, per_inoc_inocs_prob)
def plot_kernel_shift_repl(k=None,
                           mu=None,
                           d_v=None,
                           R0=None,
                           bottleneck=None,
                           t_M=None,
                           t_transmit=None,
                           vb_ratio=None,
                           p_loss=0,
                           phenotypes=np.linspace(-1, 1, 250),
                           sus_func=None,
                           susceptibility_model='linear',
                           escape=1,
                           wt_phenotype=0,
                           generator_phenotype=None,
                           sd_mut=None,
                           axis=None,
                           z_homotypic=None,
                           recipient_phenotype=-99,
                           kernel_color=None,
                           kernel_linestyle=None,
                           kernel_alpha=None,
                           dist_color=None,
                           dist_alpha=None,
                           kernel_lw=None,
                           mark_original_phenotype=False,
                           wt_phen_lw=4,
                           wt_phen_color='black',
                           wt_phen_linestyle='dashed',
                           **kwargs):

    if axis is None:
        fig, axis = plt.subplots()

    if generator_phenotype is None:
        generator_phenotype = wt_phenotype

    if sus_func is None:
        sus_func = pick_sus_func(susceptibility_model,
                                 escape=escape,
                                 homotypic_sus=1 - z_homotypic)

    gen_wt_sus = sus_func(abs(wt_phenotype - generator_phenotype))
    recip_wt_sus = sus_func(abs(wt_phenotype - recipient_phenotype))
    c_w = (1 - gen_wt_sus)
    print("generator susceptibility to wild-type: ", gen_wt_sus)
    print("generator wild-type neut rate within host: ", k * c_w)
    recip_wt_neut_prob = neutralize_prob_from_z_poisson(
        (1 - recip_wt_sus) * z_homotypic, vb_ratio)

    prob_generate = norm.pdf(phenotypes, wt_phenotype, sd_mut)

    results = []
    for phenotype in phenotypes:
        gen_mut_sus = sus_func(abs(phenotype - generator_phenotype))
        recip_mut_sus = sus_func(abs(phenotype - recipient_phenotype))
        c_m = (1 - gen_mut_sus)
        recip_mut_neut_prob = neutralize_prob_from_z_poisson(
            (1 - recip_mut_sus) * z_homotypic, vb_ratio)

        results.append(
            p_transmit(c_w=c_w,
                       c_m=c_m,
                       k=k,
                       mu=mu,
                       d_v=d_v,
                       R0=R0,
                       bottleneck=bottleneck,
                       t_M=t_M,
                       t_transmit=t_transmit,
                       t_peak=t_transmit,
                       wt_neut_prob=recip_wt_neut_prob,
                       mut_neut_prob=recip_mut_neut_prob,
                       vb_ratio=vb_ratio,
                       p_loss=p_loss))
    prob_survive = np.array(results)
    unnormed_probs = prob_generate * prob_survive
    plot_probs = unnormed_probs / np.sum(unnormed_probs)

    kernel_normed = prob_generate / np.sum(prob_generate)

    ## first plot shifted result, then overlay kernel
    axis.plot(phenotypes,
              plot_probs,
              color=dist_color,
              alpha=dist_alpha,
              **kwargs)

    axis.plot(phenotypes,
              kernel_normed,
              linestyle=kernel_linestyle,
              color=kernel_color,
              alpha=kernel_alpha,
              lw=kernel_lw,
              **kwargs)

    if mark_original_phenotype:
        axis.axvline(wt_phenotype,
                     lw=wt_phen_lw,
                     color=wt_phen_color,
                     linestyle=wt_phen_linestyle)