def run_n_cpt(self, cleanup=False): """Run a set of simulations using the n_cpt implementation. Builds the model using the :py:meth:`Job.build` method, then runs the number of simulations specified in ``self.num_sims`` using `pysb.bng.run_ssa` and returns the results. Parameters ---------- cleanup : boolean If True, specifies that the files created by BNG during simulation should be deleted after completion. Defaults is False. Returns ------- list of numpy.recarrays List of record arrays, each one containing the results of a single stochastic simulation. The entries in the record array correspond to observables in the model. """ b = self.n_cpt_builder() xrecs = [] for i in range(self.num_sims): print "Running BNG simulation %d of %d..." % (i+1, self.num_sims) xrecs.append(bng.run_ssa(b.model, t_end=self.tmax, n_steps=self.n_steps, cleanup=cleanup, output_dir='.', output_file_basename='%s_proc%d_sim%d' % (b.model.name, os.getpid(), i))) return xrecs
def plot_bng_and_kappa_sims(model, t_end, n_steps, title): # BNG x = bng.run_ssa(model, t_end=t_end, n_steps=n_steps) plt.figure() for name in x.dtype.names: if not name == 'time': plt.plot(x['time'], x[name], label="B:"+name) # Kappa x = kappa.run_simulation(model, time=t_end, points=n_steps) for name in x.dtype.names: if not name == 'time': plt.plot(x['time'], x[name], label="K:"+name) plt.title(title) plt.ylabel("Number") plt.xlabel("Time") plt.legend(loc='upper right')
def run_model(self, tmax=12000, num_sims=1): xrecs = [] dr_all = [] for i in range(0, num_sims): print "Running SSA simulation %d of %d..." % (i+1, num_sims) ssa_result = bng.run_ssa(self.model, t_end=tmax, n_steps=100, cleanup=True) xrecs.append(ssa_result) dr_all.append(get_dye_release(self.model, 'pores', ssa_result)) xall = np.array([x.tolist() for x in xrecs]) x_std = np.recarray(xrecs[0].shape, dtype=xrecs[0].dtype, buf=np.std(xall, 0)) x_avg = np.recarray(xrecs[0].shape, dtype=xrecs[0].dtype, buf=np.mean(xall, 0)) plot_simulation(x_avg, x_std, dr_all, self.model, num_sims)
Rule('State_change_02', Cell_0() <> Cell_2(), kf_02, kr_20) Observable("Cell_total", Cell_0()+Cell_1()+Cell_2()) Observable("Cell_0_obs", Cell_0()) Observable("Cell_1_obs", Cell_1()) Observable("Cell_2_obs", Cell_2()) for rule in model.rules: print rule # quit() time = plt.linspace(0, 6000, 6001) x = odesolve(model, time, verbose=True) plt.plot(time, (np.log2(x['Cell_total'])-np.log2(30000)), label="Cell_Total", lw=3) for i in range(n_cell_types): plt.plot(time, (np.log2(x['Cell_%d_obs' % i])-np.log2(model.parameters['Cell_%d_0'%i].value)), label="Cell_%d" % i, lw=3) # n_sims = 2 for n in range(n_sims): print n y = run_ssa(model, time, verbose=False) plt.plot(time, (np.log2(y['Cell_total'])-np.log2(30000)), color="0.5") for i in range(n_cell_types): plt.plot(time, (np.log2(y['Cell_%d_obs' % i])-np.log2(model.parameters['Cell_%d_0'%i].value)), lw=3) plt.xlabel('Time') plt.ylabel('log2(Cells)') plt.xlim(0,6000) plt.ylim(-20,20) plt.legend(loc=0) plt.show()
def test_run_ssa(): """Test run_ssa.""" run_ssa(robertson.model, t_end=20000, n_steps=100, verbose=False)
print rule time = plt.linspace(0, 1000, 1001) x1 = odesolve(model, time, verbose=True) plt.plot(time, (np.log2(x1['Cell_total'])-np.log2(10000)), label="Total", lw=3) #for i in range(n_cell_types): # plt.plot(time, (np.log2(x1['Cell_%d_obs' % i])-np.log2(model.parameters['Cell_%d_0'%i].value)), label="Cell_%d" % i, lw=3) #print x1['Cell_total'] #numpy.savetxt("20150713 Subclone7_cell_1_10000tp.csv", x1['Cell_total'], delimiter=",") #quit() n_sims = 20 for n in range(n_sims): print n y = run_ssa(model, time, verbose=True) plt.plot(time, (np.log2(y['Cell_total'])-np.log2(10000)), color="0.5") # for i in range(n_cell_types): # plt.plot(time, (np.log2(y['Cell_%d_obs' % i])-np.log2(model.parameters['Cell_%d_0'%i].value)), color='0.5') plt.xlabel('') plt.ylabel('') plt.ylim(-2,2) plt.xlim(0,1000) plt.legend(loc=0) plt.show()
import matplotlib.pyplot as plt from pysb.bng import run_ssa from pysb.integrate import odesolve import numpy as np import re run_ode = True # set_volume(10.*model.parameters['VOL'].value) # set_initial_conditions(myc=100.*model.parameters['Myc_0'].value) t = linspace(0,5,101) # ref = np.array([p.value for p in model.parameters]) # Run simulations x = run_ssa(model,t,seed=100,verbose=True) if run_ode: y = odesolve(model,t,verbose=True) print for ic in model.initial_conditions: print "%s: %g" % (ic[1].name, ic[1].value) print for p in model.parameters_rules(): print "%s: %g" % (p.name, p.value) print for e in model.expressions: subs = [(p.name, p.value) for p in model.parameters] print "%s: %s " % (e.name, e.expand_expr(model).subs(subs)) # Plot observables (each in a different figure)
def run_model(self, tmax=12000, num_sims=1, use_kappa=True, figure_ids=[0, 1]): xrecs = [] # The array to store the simulation data dr_all = [] # TODO: Delete this # Run multiple simulations and collect data for i in range(0, num_sims): # Run simulation using Kappa: if use_kappa: ssa_result = kappa.run_simulation(self.model, time=tmax, points=100, output_dir='simdata') xrecs.append(ssa_result) # Run simulation using BNG SSA implementation: else: ssa_result = bng.run_ssa(self.model, t_end=tmax, n_steps=100, cleanup=True) xrecs.append(ssa_result) #dr_all.append(get_dye_release(model, 'pores', ssa_result)) # Convert the multiple simulations in an array... xall = array([x.tolist() for x in xrecs]) # ...and calculate the Mean and SD across the simulations x_std = recarray(xrecs[0].shape, dtype=xrecs[0].dtype, buf=std(xall, 0)) x_avg = recarray(xrecs[0].shape, dtype=xrecs[0].dtype, buf=mean(xall, 0)) # Plotting parameters, aliases ci = color_iter() marker = 'x' linestyle = '-' tBid_0 = self['tBid_0'] Bax_0 = self['Bax_0'] # Translocation: plot cyto/mito tBid, and cyto/mito Bax plt.ion() plt.figure(figure_ids[0]) plt.errorbar(x_avg['time'], x_avg['ctBid']/tBid_0.value, yerr=x_std['ctBid']/tBid_0.value, color=ci.next(), marker=marker, linestyle=linestyle) plt.errorbar(x_avg['time'], x_avg['mtBid']/tBid_0.value, yerr=x_std['mtBid']/tBid_0.value, color=ci.next(), marker=marker, linestyle=linestyle) plt.errorbar(x_avg['time'], x_avg['cBax']/Bax_0.value, yerr=x_std['cBax']/Bax_0.value, color=ci.next(), marker=marker, linestyle=linestyle) plt.errorbar(x_avg['time'], x_avg['mBax']/Bax_0.value, yerr=x_std['mBax']/Bax_0.value, color=ci.next(), marker=marker, linestyle=linestyle) # Activation: plot iBax and tBidBax plt.errorbar(x_avg['time'], x_avg['iBax']/Bax_0.value, yerr=x_std['iBax']/Bax_0.value, label='iBax', color=ci.next(), marker=marker, linestyle=linestyle) plt.errorbar(x_avg['time'], x_avg['tBidBax']/tBid_0.value, yerr=x_std['tBidBax']/tBid_0.value, color=ci.next(), marker=marker, linestyle=linestyle) # Dye release calculated exactly ---------- #dr_avg = mean(dr_all, 0) #dr_std = std(dr_all, 0) #errorbar(x_avg['time'], dr_avg, # yerr=dr_std, label='dye_release', # color=ci.next(), marker=marker, linestyle=linestyle) # Pore Formation #plot(x['time'], x['pBax']/Bax_0.value, label='pBax') #leg = legend() #ltext = leg.get_texts() #setp(ltext, fontsize='small') #xlabel("Time (seconds)") #ylabel("Normalized Concentration") #ci = color_iter() # Plot pores/vesicle in a new figure ------ #figure(2) #errorbar(x_avg['time'], x_avg['pores'] / float(NUM_COMPARTMENTS), # yerr=x_std['pores']/float(NUM_COMPARTMENTS), label='pores', # color=ci.next(), marker=marker, linestyle=linestyle) #F_t = 1 - dr_avg #pores_poisson = -log(F_t) #plot(x_avg['time'], pores_poisson, color=ci.next(), label='-ln F(t), # stoch', # marker=marker, linestyle=linestyle) #xlabel("Time (seconds)") #ylabel("Pores/vesicle") #title("Pores/vesicle") #legend() #xlabel("Time (seconds)") #ylabel("Dye Release") #title("Dye release calculated via compartmental model") return xrecs[0]
import matplotlib.gridspec as gridspec from time import time na.model.monomers na.model.parameters na.model.observables na.model.initial_conditions na.model.rules t_end=200 n_steps=2000 t0=time() yout=run_ssa(na.model,t_end=t_end, n_steps=n_steps) t1=time() print('run_ssa: %.2g sec' % (t1-t0)) # Use a nice grid to plot 5 figures gs = gridspec.GridSpec(5,1,width_ratios=[1],height_ratios=[1,1,1,1,1]) ax1 = plt.subplot(gs[0]) plt.plot(yout['time'], yout['Glu_brain'], label='glu_brain', color='r') plt.legend('',frameon=False) # remove legend plt.xlim([0,t_end]) # set x lim #plt.xlabel('') # remove x ticks #plt.yticks([]) # remove y lim plt.xticks([]) plt.ylabel("mL") plt.text(t_end+1,0.5,'glu_brain')
def test_run_ssa(): """ Rudimentary test, mainly to ensure API compatibility """ run_ssa(robertson.model, t_end=20000, n_steps=100, verbose=False)