Simulation tools (pysb.simulator)

class pysb.simulator.ScipyOdeSimulator(model, tspan=None, initials=None, param_values=None, verbose=False, **kwargs)[source]

Simulate a model using SciPy ODE integration

Uses scipy.integrate.odeint() for the lsoda integrator, scipy.integrate.ode() for all other integrators.

Warning

The interface for this class is considered experimental and may change without warning as PySB is updated.

Parameters:

model : pysb.Model

Model to simulate.

tspan : vector-like, optional

Time values over which to simulate. The first and last values define the time range. Returned trajectories are sampled at every value unless the simulation is interrupted for some reason, e.g., due to satisfaction of a logical stopping criterion (see ‘tout’ below).

initials : vector-like or dict, optional

Values to use for the initial condition of all species. Ordering is determined by the order of model.species. If not specified, initial conditions will be taken from model.initial_conditions (with initial condition parameter values taken from param_values if specified).

param_values : vector-like or dict, optional

Values to use for every parameter in the model. Ordering is determined by the order of model.parameters. If passed as a dictionary, keys must be parameter names. If not specified, parameter values will be taken directly from model.parameters.

verbose : bool, optional (default: False)

Verbose output.

**kwargs : dict

Extra keyword arguments, including:

  • integrator: Choice of integrator, including vode (default), zvode, lsoda, dopri5 and dop853. See scipy.integrate.ode() for further information.
  • integrator_options: A dictionary of keyword arguments to supply to the integrator. See scipy.integrate.ode().
  • cleanup: Boolean, cleanup argument used for pysb.bng.generate_equations() call

Notes

If tspan is not defined, it may be defined in the call to the run method.

Examples

Simulate a model and display the results for an observable:

>>> from pysb.examples.robertson import model
>>> import numpy as np
>>> np.set_printoptions(precision=4)
>>> sim = ScipyOdeSimulator(model, tspan=np.linspace(0, 40, 10))
>>> simulation_result = sim.run()
>>> print(simulation_result.observables['A_total'])         
[ 1.      0.899   0.8506  0.8179  0.793   0.7728  0.7557  0.7408  0.7277
0.7158]

For further information on retrieving trajectories (species, observables, expressions over time) from the simulation_result object returned by run(), see the examples under the SimulationResult class.

run(tspan=None, param_values=None, initials=None)[source]

Run a simulation and returns the result (trajectories)

Note

tspan, param_values and initials values supplied to this method will persist to future run() calls.

Parameters:

tspan

param_values

initials

See parameter definitions in ScipyOdeSimulator.

Returns:

A SimulationResult object

class pysb.simulator.SimulationResult(simulator, trajectories)[source]

Results of a simulation with properties and methods to access them.

Warning

Please note that the interface for this class is considered experimental and may change without warning as PySB is updated.

Parameters:

simulator : Simulator

The simulator object that generated the trajectories

trajectories : list or numpy.ndarray

A set of species trajectories from a simulation. Should either be a list of 2D numpy arrays or a single 3D numpy array.

Notes

In the attribute descriptions, a “trajectory set” is a 2D numpy array, species on first axis and time on second axis, with each element containing the concentration or count of the species at the specified time.

A list of trajectory sets contains a trajectory set for each simulation.

Examples

The following examples use a simple model with three observables and one expression, with a single simulation.

>>> from pysb.examples.expression_observables import model
>>> from pysb.simulator import ScipyOdeSimulator
>>> import numpy as np
>>> np.set_printoptions(precision=4)
>>> sim = ScipyOdeSimulator(model, tspan=np.linspace(0, 40, 10))
>>> simulation_result = sim.run()

simulation_result is a SimulationResult object. An observable can be accessed like so:

>>> print(simulation_result.observables['Bax_c0'])         
[  1.0000e+00   1.1744e-02   1.3791e-04   1.6196e-06   1.9021e-08
   2.2344e-10   2.6394e-12   4.8067e-14  -6.2097e-14  -7.5308e-15]

It is also possible to retrieve the value of all observables at a particular time point, e.g. the final concentrations:

>>> print(simulation_result.observables[-1]) 
(-7.5308...e-15, -1.6809...e-13, 1.0000...)

Expressions are read in the same way as observables:

>>> print(simulation_result.expressions['NBD_signal'])         
[ 0.   4.7847  4.9956  4.9999  5.   5.   5.   5.   5.   5. ]

The species trajectories can be accessed as a numpy ndarray:

>>> print(simulation_result.species)
[[  1.0000e+00   0.0000e+00   0.0000e+00]
 [  1.1744e-02   5.2194e-02   9.3606e-01]
 [  1.3791e-04   1.2259e-03   9.9864e-01]
 [  1.6196e-06   2.1595e-05   9.9998e-01]
 [  1.9021e-08   3.3814e-07   1.0000e+00]
 [  2.2344e-10   4.9648e-09   1.0000e+00]
 [  2.6394e-12   7.0287e-11   1.0000e+00]
 [  4.8067e-14   1.3515e-12   1.0000e+00]
 [ -6.2097e-14  -1.3652e-12   1.0000e+00]
 [ -7.5308e-15  -1.6809e-13   1.0000e+00]]

Species, observables and expressions can be combined into a single numpy ndarray and accessed similarly. Here, the initial concentrations of all these entities are examined:

>>> print(simulation_result.all[0])
(1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0)

The all array can be accessed as a pandas DataFrame object, which allows for more convenient indexing and access to pandas advanced functionality, such as indexing and slicing. Here, the concentrations of the observable Bax_c0 and the expression NBD_signal are read at time points between 5 and 15 seconds:

>>> df = simulation_result.dataframe
>>> print(df.loc[5:15, ['Bax_c0', 'NBD_signal']])         
             Bax_c0  NBD_signal
time
8.888889   0.000138    4.995633
13.333333  0.000002    4.999927
all

Aggregate species, observables, and expressions trajectories into a numpy.ndarray with record-style data-type for return to the user.

dataframe

A conversion of the trajectory sets (species, observables and expressions for all simulations) into a single pandas.DataFrame.

expressions

List of trajectory sets. The first dimension contains expressions.

observables

List of trajectory sets. The first dimension contains observables.

species

List of trajectory sets. The first dimension contains species.