def setUp(self): Monomer('A', ['a']) Monomer('B', ['b']) Parameter('ksynthA', 100) Parameter('ksynthB', 100) Parameter('kbindAB', 100) Parameter('A_init', 0) Parameter('B_init', 0) Initial(A(a=None), A_init) Initial(B(b=None), B_init) Observable("A_free", A(a=None)) Observable("B_free", B(b=None)) Observable("AB_complex", A(a=1) % B(b=1)) Rule('A_synth', None >> A(a=None), ksynthA) Rule('B_synth', None >> B(b=None), ksynthB) Rule('AB_bind', A(a=None) + B(b=None) >> A(a=1) % B(b=1), kbindAB) self.model = model generate_equations(self.model) # Convenience shortcut for accessing model monomer objects self.mon = lambda m: self.model.monomers[m] # This timespan is chosen to be enough to trigger a Jacobian evaluation # on the various solvers. self.time = np.linspace(0, 1) self.sim = BngSimulator(self.model, tspan=self.time)
def test_nfsim_different_initials_params_lengths(self): A = self.model.monomers['A'] sim = BngSimulator(self.model, tspan=np.linspace(0, 1), initials={A(): [150, 250, 350]}, param_values=self.param_values_2sets)
def test_set_initials_by_params(): # This tests setting initials by changing their underlying parameter values # BNG Simulator uses a dictionary for initials, unlike e.g. # ScipyOdeSimulator, so a separate test is needed model = robertson.model t = np.linspace(0, 40, 51) ic_params = model.parameters_initial_conditions() param_values = np.array([p.value for p in model.parameters]) ic_mask = np.array([p in ic_params for p in model.parameters]) bng_sim = BngSimulator(model, tspan=t, verbose=0) # set all initial conditions to 1 param_values[ic_mask] = np.array([1, 1, 1]) traj = bng_sim.run(param_values=param_values) # set properly here assert np.allclose(traj.initials, [1, 1, 1]) # overwritten in bng file. lines 196-202. # Values from initials_dict are used, but it should take them from # self.initials, so I don't see how they are getting overwritten? print(traj.dataframe.loc[0]) assert np.allclose(traj.dataframe.loc[0][0:3], [1, 1, 1]) # Same here param_values[ic_mask] = np.array([0, 1, 1]) traj = bng_sim.run(param_values=param_values) assert np.allclose(traj.initials, [0, 1, 1]) assert np.allclose(traj.dataframe.loc[0][0:3], [0, 1, 1])
def test_nfsim_different_initials_lengths(self): A = self.model.monomers['A'] B = self.model.monomers['B'] sim = BngSimulator(self.model, tspan=np.linspace(0, 1), initials={B(): [150, 250, 350]}) sim.run(initials={A(): [275, 375]})
def test_bng_ode_with_expressions(): model = expression_observables.model model.reset_equations() sim = BngSimulator(model, tspan=np.linspace(0, 1)) x = sim.run(n_runs=1, method='ode') assert len(x.expressions) == 50 assert len(x.observables) == 50
def test_nfsim(): model = robertson.model # Reset equations from any previous network generation model.reset_equations() sim = BngSimulator(model, tspan=np.linspace(0, 1)) x = sim.run(n_runs=1, method='nf', seed=_BNG_SEED) observables = np.array(x.observables) assert len(observables) == 50 A = model.monomers['A'] x = sim.run(n_runs=2, method='nf', tspan=np.linspace(0, 1), initials={A(): 100}, seed=_BNG_SEED) assert np.allclose(x.dataframe.loc[0, 0.0], [100.0, 0.0, 0.0])
def test_stop_if(): Model() Monomer('A') Rule('A_synth', None >> A(), Parameter('k', 1)) Observable('Atot', A()) Expression('exp_const', k + 1) Expression('exp_dyn', Atot + 1) sim = BngSimulator(model, verbose=5) tspan = np.linspace(0, 100, 101) x = sim.run(tspan, stop_if='Atot>9', seed=_BNG_SEED) # All except the last Atot value should be <=9 assert all(x.observables['Atot'][:-1] <= 9) assert x.observables['Atot'][-1] > 9 # Starting with Atot > 9 should terminate simulation immediately y = sim.run(tspan, initials=x.species[-1], stop_if='Atot>9') assert len(y.observables) == 1
def test_hpp(): model = robertson.model # Reset equations from any previous network generation model.reset_equations() A = robertson.model.monomers['A'] klump = Parameter('klump', 10000, _export=False) model.add_component(klump) population_maps = [PopulationMap(A(), klump)] sim = BngSimulator(model, tspan=np.linspace(0, 1)) x = sim.run(n_runs=1, method='nf', population_maps=population_maps, seed=_BNG_SEED) observables = np.array(x.observables) assert len(observables) == 50
num_u = 100 u_line = np.linspace(u_min, u_max, num_u) s_line = np.zeros(num_u) for i in range(0, num_u): s_line[i] = (beta/d_s)*u_line[i] # ---------------------------------------------------------- # Define observed times. tmin = 0 tmax = 50 num_t = 100 t = np.linspace(tmin, tmax, num_t) # Simulate model. simulator = BngSimulator(model, tspan=t) simresult = simulator.run(method='ssa') # ssa: discrete stochastic simulations yout = simresult.all # Plot results. plt.plot(t, yout['o_g_0'], label="$g_0$") plt.plot(t, yout['o_g_1'], label="$g_1$") plt.plot(t, yout['o_u'], label='$u$') plt.plot(t, yout['o_s'], label="$s$") plt.plot(t, yout['o_p'], label="$p$") plt.xlabel('$t$') plt.ylabel('molecule number')
import matplotlib.pyplot as plt import numpy as np from pysb.simulator.bng import BngSimulator from kinase_cascade import model # We will integrate from t=0 to t=40 t = np.linspace(0, 40, 50) # Simulate the model print("Simulating...") sim = BngSimulator(model) x = sim.run(tspan=t, verbose=False, n_runs=5, method='ssa') tout = x.tout y = np.array(x.observables) plt.plot(tout.T, y['ppMEK'].T) plt.show()
def setUp(self): self.model = robertson.model self.model.reset_equations() self.sim = BngSimulator(self.model, tspan=np.linspace(0, 1)) self.param_values_2sets = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]