Ejemplo n.º 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()
Ejemplo n.º 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
Ejemplo n.º 3
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
Ejemplo n.º 4
0
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)
Ejemplo n.º 6
0
def get_stein_steady_states(fps):
    """This function imports  values from the dictionary that contains "stein's steady states" 
    and  also imports steady states with up to 2 unstable directions (almost_stable=2). This 
    code outputs matching steady states between stein's set and the calculated set"""
    # imports stein steady states A through E
    stein_dict = bb.get_all_ss()
    # takes each value from stein_dict (keys : A->E) and creates a list of lists which correspond to each value
    stein_ss = np.array([stein_dict[val] for val in stein_dict])
    # creates a matrix of size stein_ss filled with zeros
    final_list = np.array(
        [np.zeros(len(stein_ss[0])) for i in range(len(stein_ss))])
    # generates permuations of all elements of fps and stein_ss
    iterations_list = list(itertools.product(stein_ss, fps))
    # take each permutation
    for i in range(len(iterations_list)):
        compare_lists = iterations_list[i]
        # check to see whether the lists that are being combined are equal
        if np.linalg.norm(compare_lists[0] - compare_lists[1]) < .001:
            for j in range(len(stein_ss)):
                # verify that compare_lists[1] and [2] equal stein_ss
                if np.linalg.norm(compare_lists[1] - stein_ss[j]) < .001:
                    #if they are equal, then add the element compare_lists[1] to final_list
                    final_list[j] = compare_lists[1]
    return final_list
Ejemplo n.º 7
0
        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
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
Ejemplo n.º 8
0
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))
Ejemplo n.º 10
0
            counter += 1
            if verbose:
                print('{} is stable'.format(fp,stein_stable))
                print(counter)
        else:
            steady_state_2_list = [fp] + steady_state_2_list
            counter += 1
            if verbose:
                print('the stein {} is unstable in {} direction'.format(fp, stein_stable))
                print(counter)
        num_stable_fps += 1
        fp_list2.append(fp)
fp_list2 = np.array(fp_list2)
print('there were {} stein stable fps out of {} total cases'.format(num_stable_fps, len(fps)))

test_call = bb.get_all_ss()
stein_stable = get_stability(fp, mu, M, almost_stable=2, substability=False)

stein_values = list(test_call.values())

#xa = fp_list[0]; xb = fp_list[1]  
#sep_xa, sep_xb = get_separatrix_point(xa, xb,M,mu, 101)
#call = SSR(xa,xb,mu,M)
#print(sep_xa, sep_xb)
#This returns Stein's steady states
stein_steady_states = get_stein_steady_states(stein_values, steady_state_2_list)
itertools.permutations(stein_steady_states,2)

#returns all iterations of the possible combinations of Stein's Steady States
combos = list(itertools.combinations(range(5), 2))
for i,j in combos: