示例#1
0
 def __init__(s):
     s.steady_states = bb.get_all_ss()
     _, s.mu, s.M, _ = bb.get_stein_params()
     s.N = len(s.mu)
     s.unstable_fps = {
         i: s.get_sane_steady_states(num_unstable=i)
         for i in range(5)
     }
     s.unstable_fps['stein'] = s.steady_states.values()
示例#2
0
def get_matrix():   ##############gives nu and L from two steady states
    # labels = list of 11 bacteria names; mu = bacterial growth rates (11D);
    # M = bacterial interactions (11D);
    # eps = susceptibility to antibiotics (we won't use this)
    labels, mu, M, eps = bb.get_stein_params()
    # stein_ss = dictionary of steady states A-E. We choose to focus on two steady
    # states (C and E) 
    stein_ss = bb.get_all_ss()
    ssa = stein_ss['E']; ssb = stein_ss['C']
    # we get the reduced 2D growth rates nu and 2D interactions L through steady
    # state reduction
    nu, L = bb.SSR(ssa, ssb, mu, M)
    return nu,L
def time_change_M(t):
    labels, mu, M, eps = bb.get_stein_params()
    deltaM_4_2 = 0.18520005075054308
    t_change = 2000
    coord = ([4, 2])
    K = np.zeros((11, 11))
    for i in range(11):
        for j in range(11):
            if coord == ([i, j]):
                K[i][j] = M[i][j] + deltaM_4_2
            else:
                K[i][j] = M[i][j]
    if (t < t_change):
        return K
    else:
        return M
示例#4
0
def time_change_11D_traj(delta_K, coord):
    t = np.linspace(0, 10000, 10001)
    stein_ss = bb.get_all_ss()
    ssa = stein_ss['E']; ssb = stein_ss['C']
    ic = 0.5*ssa+ssb*0.5
    labels, mu, M, eps = bb.get_stein_params()
    K = np.zeros((11,11))
    for i in range(11):
        for j in range(11):
            if coord == ([i,j]):
                K[i][j] = M[i][j]+delta_K
            else:
                K[i][j] = M[i][j]
    traj = integrate.odeint(time_change_integrand, ic, t, args=(mu, time_change_M))
    print(traj[-1])
    traj_2D = bb.project_to_2D(traj, ssa, ssb)
    return traj_2D
def get_11D_traj():  ########uses _11D_traj to create plots
    labels, mu, M, eps = bb.get_stein_params()
    # stein_ss = dictionary of steady states A-E. We choose to focus on two steady
    # states (C and E)
    stein_ss = bb.get_all_ss()
    ssa = stein_ss['C']
    ssb = stein_ss['B']

    Delta_M1 = np.array([[0, -0.7], [0, 0]])
    coeff_alpha = np.zeros((11, 11))
    for i in range(11):
        for j in range(11):
            coeff_alpha[i][j] = ssa[i] * ssb[j] / sum(ssa)

    Delta_M_4_2 = Delta_M1[0][1] / coeff_alpha[4][2]
    traj_2D = _11D_traj(Delta_M_4_2, ([4, 2]))
    plt.plot(traj_2D[:, 0], traj_2D[:, 1], 'b', label='Old Trajectory')
    traj_2D_old = _11D_traj(0, ([4, 2]))
    plt.plot(traj_2D_old[:, 0], traj_2D_old[:, 1], 'r', label='Old Trajectory')
def how_to_get_separatrix():
    # labels = list of 11 bacteria names; mu = bacterial growth rates (11D);
    # M = bacterial interactions (11D);
    # eps = susceptibility to antibiotics (we won't use this)
    labels, mu, M, eps = bb.get_stein_params()
    # stein_ss = dictionary of steady states A-E. We choose to focus on two steady
    # states (C and E)
    stein_ss = bb.get_all_ss()
    ssa = stein_ss['E']
    ssb = stein_ss['C']
    # we get the reduced 2D growth rates nu and 2D interactions L through steady
    # state reduction
    nu, L = bb.SSR(ssa, ssb, mu, M)
    # solve the gLV equations for ic=[.5, .5]
    ic = [.5, .5]
    t = np.linspace(0, 10, 1001)
    traj_2D = integrate.odeint(integrand, ic, t, args=(nu, L))

    # generate Taylor expansion of separatrix
    p = bb.Params((L, [0, 0], nu))
    # now p is a Class, and it contains elements p.M, and p.mu, as well as various
    # helper functions (e.g. the get_11_ss function, which returns the semistable
    # coexistent fixed point
    u, v = p.get_11_ss()
    # return Taylor coefficients to 5th order
    taylor_coeffs = p.get_taylor_coeffs(order=5)
    # create separatrix
    xs = np.linspace(0, 1, 1001)
    ys = np.array([
        sum([(taylor_coeffs[i] / math.factorial(i)) * (x - u)**i
             for i in range(len(taylor_coeffs))]) for x in xs
    ])

    plt.plot(traj_2D[:, 0], traj_2D[:, 1])
    plt.plot(xs, ys, color='grey', ls='--')
    plt.axis([0, 1, 0, 1])
    plt.tight_layout()
    filename = 'figs/example_trajectory.pdf'
    plt.savefig(filename)
示例#7
0
    """ Return N-dimensional gLV equations """
    dxdt = (np.dot(np.diag(mu), x) + np.dot(np.diag(x), np.dot(M, x)))
    for i in range(len(x)):
        if abs(x[i]) < 1e-8:
            dxdt[i] = 0
    return dxdt


###############################################################################

## MAIN FUNCTION

# labels = list of 11 bacteria names; mu = bacterial growth rates (11D);
# M = bacterial interactions (11D);
# eps = susceptibility to antibiotics (we won't use this)
labels, mu, M, eps = bb.get_stein_params()
# stein_ss = dictionary of steady states A-E. We choose to focus on two steady
# states (C and E)
stein_ss = bb.get_all_ss()
ssa = stein_ss['E']
ssb = stein_ss['C']
# we get the reduced 2D growth rates nu and 2D interactions L through steady
# state reduction
nu, L = bb.SSR(ssa, ssb, mu, M)
# solve the gLV equations for ic=[.5, .5]
ic = [.5, .5]
ic2 = [1.1, .3]
t = np.linspace(0, 10, 1001)
traj_2D = integrate.odeint(integrand, ic, t, args=(nu, L))
traj_2D_2 = integrate.odeint(integrand, ic2, t, args=(nu, L))
# generate Taylor expansion of separatrix
def get_ss_fates(num_points, delta_K, coord):
    labels, mu, M, eps = bb.get_stein_params()
    stein_ss = bb.get_all_ss()
    ssa = stein_ss['C']
    ssb = stein_ss['B']
    K = np.zeros((11, 11))
    for i in range(11):
        for j in range(11):
            if coord == ([i, j]):
                K[i][j] = M[i][j] + delta_K
            else:
                K[i][j] = M[i][j]

    phase_dict = {}
    max_x = 1
    max_y = 1
    xs = np.linspace(0, max_x * 1.1, num_points)
    ys = np.linspace(0, max_y * 1.1, num_points)

    filename = 'gLV_phases_N_{}'.format(num_points)
    # if read_data = True, read the phase values from the disk
    # if read_data = False, rerun the simulation and save values to disk
    # (run it as False first, then switch to True)
    read_data = False
    if not read_data:
        for x in xs:
            print(x)
            for y in ys:
                ic = x * ssa + y * ssb
                t = np.linspace(0, 1000, 1001)
                z = integrate.odeint(integrand, ic, t, args=(mu, K))

                eps = .001
                if np.linalg.norm(z[-1] - ssa) < eps: color = 'purple'
                elif np.linalg.norm(z[-1] - ssb) < eps: color = 'g'

                else:
                    print('{}, {}: neither SS'.format(x, y))
                    color = 'orange'
                phase_dict[(x, y)] = (z[-1], color)
        with open('data/{}'.format(filename), 'wb') as f:
            pickle.dump(phase_dict, f)
            print('... SAVED data to {}'.format(filename))
    else:
        with open('data/{}'.format(filename), 'rb') as f:
            phase_dict = pickle.load(f)
            print('... LOADED data from {}'.format(filename))

    purples = []
    greens = []
    blacks = []
    blues = []
    reds = []

    for key in phase_dict:
        if phase_dict[key][1] == 'g':
            greens.append(list(key))
        elif phase_dict[key][1] == 'purple':
            purples.append(list(key))
        elif phase_dict[key][1] == 'k':
            blacks.append(list(key))
        elif phase_dict[key][1] == 'b':
            blues.append(list(key))
        elif phase_dict[key][1] == 'r':
            reds.append(list(key))
        else:
            print('{} didn\'t go to either steady state'.format(key))

    greens = np.array(greens)
    purples = np.array(purples)
    blacks = np.array(blacks)
    reds = np.array(reds)
    blues = np.array(blues)

    markertype = 's'
    green = 'green'
    purple = 'purple'
    black = 'k'
    blue = 'b'
    red = 'r'
    zorder = 0
    markersize = 5.3
    alpha = 1

    # fig, ax = plt.subplots(figsize=(6,6))

    # ax.plot(greens[:,0], greens[:,1], markertype, color=green,
    #        zorder=zorder, markersize=markersize, alpha=alpha)
    # ax.plot(purples[:,0], purples[:,1], markertype, color=purple, zorder=zorder,
    #      markersize=markersize, alpha=alpha)
    # if blacks != []:
    #   ax.plot(blacks[:,0], blacks[:,1], markertype, color=black, zorder=zorder,
    #        markersize=markersize, alpha=alpha)
    # if blues != []:
    #   ax.plot(blues[:,0], blues[:,1], markertype, color=blue, zorder=zorder,
    #        markersize=markersize, alpha=alpha)
    # if reds != []:
    #   ax.plot(reds[:,0], reds[:,1], markertype, color=red, zorder=zorder,
    #        markersize=markersize, alpha=alpha)

    # ax.set_xlim(0, max_x*1.1)
    # ax.set_ylim(0, max_y*1.1)

    savefig = False
    if savefig:
        plt.savefig('figs/{}_plot.pdf'.format(filename), bbox_inches='tight')
        print('... saved to {}_plot.pdf'.format(filename))

    return phase_dict
def get_ss_fates():
    labels, mu, M, eps = bb.get_stein_params()
    stein_ss = bb.get_all_ss()
    ssa = stein_ss['E']
    ssb = stein_ss['C']

    phase_dict = {}
    max_x = 1
    max_y = 1
    num_points = 10  # 11 is saved
    xs = np.linspace(0, max_x * 1.1, num_points)
    ys = np.linspace(0, max_y * 1.1, num_points)

    filename = 'gLV_phases_N_{}'.format(num_points)
    # if read_data = True, read the phase values from the disk
    # if read_data = False, rerun the simulation and save values to disk
    # (run it as False first, then switch to True)
    read_data = False
    if not read_data:
        for x in xs:
            print(x)
            for y in ys:
                ic = x * ssa + y * ssb
                t = np.linspace(0, 1000, 501)
                z = integrate.odeint(integrand, ic, t, args=(mu, M))

                eps = .001
                if np.linalg.norm(z[-1] - ssa) < eps: color = 'purple'
                elif np.linalg.norm(z[-1] - ssb) < eps: color = 'g'
                else:
                    print('{}, {}: neither SS'.format(x, y))
                    color = 'orange'

                phase_dict[(x, y)] = (z[-1], color)
        with open('data/{}'.format(filename), 'wb') as f:
            pickle.dump(phase_dict, f)
            print('... SAVED data to {}'.format(filename))
    else:
        with open('data/{}'.format(filename), 'rb') as f:
            phase_dict = pickle.load(f)
            print('... LOADED data from {}'.format(filename))

    purples = []
    greens = []
    for key in phase_dict:
        if phase_dict[key][1] == 'g':
            greens.append(list(key))
        elif phase_dict[key][1] == 'purple':
            purples.append(list(key))
        else:
            print('{} didn\'t go to either steady state'.format(key))

    greens = np.array(greens)
    purples = np.array(purples)

    markertype = 's'
    green = 'green'
    purple = 'purple'
    zorder = 0
    markersize = 5.3
    alpha = 1

    fig, ax = plt.subplots(figsize=(6, 6))

    ax.plot(greens[:, 0],
            greens[:, 1],
            markertype,
            color=green,
            zorder=zorder,
            markersize=markersize,
            alpha=alpha)
    ax.plot(purples[:, 0],
            purples[:, 1],
            markertype,
            color=purple,
            zorder=zorder,
            markersize=markersize,
            alpha=alpha)

    ax.set_xlim(0, max_x * 1.1)
    ax.set_ylim(0, max_y * 1.1)

    savefig = True
    if savefig:
        plt.savefig('figs/{}_plot.pdf'.format(filename), bbox_inches='tight')
        print('... saved to {}_plot.pdf'.format(filename))