pyssa package

Submodules

pyssa.direct module

pyssa.results module

Module that defines the Results class

class pyssa.results.Results(t_list: List[numpy.ndarray], x_list: List[numpy.ndarray], status_list: List[int], algorithm: str, seed: List[int], **kwargs)[source]

Bases: collections.abc.Collection

A class that stores simulation results and provides methods to access them

Parameters
t_listList[float]
x_listList[np.ndarray]
status_listList[int]
algorithmstr
seed: List[int]
Attributes
final

Returns the final times and states of the system in the simulations

Methods

get_state(t)

Returns the states of the system at time point t.

final

Returns the final times and states of the system in the simulations

Returns
Tuple[np.ndarray, np.ndarray]

The final times and states of the sytem

get_state(t: float) → List[numpy.ndarray][source]

Returns the states of the system at time point t.

Parameters
tfloat

Time point at which states are wanted.

Returns
List[np.ndarray]

The states of the system at t for all repetitions.

pyssa.simulation module

The main class for running stochastic simulation

class pyssa.simulation.Simulation(react_stoic: numpy.ndarray, prod_stoic: numpy.ndarray, init_state: numpy.ndarray, k_det: numpy.ndarray, chem_flag: bool = False, volume: float = 1.0)[source]

Bases: object

A main class for running simulations.

Parameters
react_stoic(ns, nr) ndarray

A 2D array of the stoichiometric coefficients of the reactants. Reactions are columns and species are rows.

prod_stoic(ns, nr) ndarray

A 2D array of the stoichiometric coefficients of the products. Reactions are columns and species are rows.

init_state(ns,) ndarray

A 1D array representing the initial state of the system.

k_det(nr,) ndarray

A 1D array representing the deterministic rate constants of the system.

volumefloat, optional

The volume of the reactor vessel which is important for second and higher order reactions. Defaults to 1 arbitrary units.

chem_flagbool, optional

If True, divide by Na while calculating stochastic rate constants. Defaults to False.

Raises
ValueError

If supplied with order > 3.

Examples

>>> V_r = np.array([[1,0],[0,1],[0,0]])
>>> V_p = np.array([[0,0],[1,0],[0,1]])
>>> X0 = np.array([10,0,0])
>>> k = np.array([1,1])
>>> sim = Simulation(V_r, V_p, X0, k)
>>> sim.simulate(max_t=10, max_iter=100, n_rep=n_runs)
Attributes
resultsResults

The Results instance of the simulation

Methods

plot(plot_indices, disp, names)

Plot the simulation

simulate(max_t, max_iter, volume, seed, …)

Run the simulation

plot(plot_indices: list = None, disp: bool = True, names: list = None)[source]

Plot the simulation

Parameters
plot_indiceslist, optional

The indices of the species to be plotted. The default is [i for i in range(self._ns)] plots all species.

dispbool, optional

If True, the plot is displayed. The default shows the plot.

nameslist, optional

The names of the species to be plotted. The default is xi for species i.

Returns
figclass ‘matplotlib.figure.Figure’

Figure object of the generated plot.

axclass ‘matplotlib.axes._subplots.AxesSubplot

Axis objected of the generated plot.

results

The Results instance of the simulation

Returns
Optional[Results]
simulate(max_t: float = 10.0, max_iter: int = 1000, volume: float = 1.0, seed: Optional[List[int]] = None, n_rep: int = 1, n_procs: int = 1, algorithm: str = 'direct', debug: bool = False, **kwargs)[source]

Run the simulation

Parameters
max_tfloat, optional

The end time of the simulation The default is 10.0

max_iterint, optional

The maximum number of iterations of the simulation loop The default is 1000 iterations

volumefloat, optional

The volume of the system The default value is 1.0

seedList[int], optional

The list of seeds for the simulations The length of this list should be equal to n_rep The default value is None

n_repint, optional

The number of repetitions of the simulation required The default value is 1

n_procsint, optional

The number of cpu cores to use for the simulation The default value is 1

algorithmstr, optional

The algorithm to be used to run the simulation The default value is “direct”

Returns
tfloat

End time of the simulation.

Xtndarray

System state at time t and initial.

statusint

Indicates the status of the simulation at exit. 1 : Succesful completion, terminated when max_iter iterations reached. 2 : Succesful completion, terminated when max_t crossed. 3 : Succesful completion, terminated when all species went extinct. -1 : Failure, order greater than 3 detected. -2 : Failure, propensity zero without extinction.

pyssa.simulation.wrapper(x, func)[source]

pyssa.tau_adaptive module

pyssa.tau_leaping module

pyssa.utils module

Module containing some utility functions

pyssa.utils.get_kstoc[source]

Compute k_stoc from k_det.

Return a vector of the stochastic rate constants (k_stoc) determined from the deterministic rate constants (k_det).

Parameters
react_stoic(ns, nr) ndarray

A 2D array of the stoichiometric coefficients of the reactants. Reactions are columns and species are rows.

k_det(nr,) ndarray

A 1D array representing the deterministic rate constants of the system.

volumefloat

The volume of the reactor vessel which is important for second and higher order reactions

chem_flagbool

If True, divide by Na while calculating stochastic rate constants.

Returns
k_stoc(nr,) ndarray

A 1D array representing the stochastic rate constants of the system.

References

1. Gillespie, D.T., 1976. A general method for numerically simulating the stochastic time evolution of coupled chemical reactions. J. Comput. Phys. 22, 403–434. doi:10.1016/0021-9991(76)90041-3.

pyssa.utils.roulette_selection[source]

Perform roulette selection on the list of propensities.

Return the index of the selected reaction (choice) by performing Roulette selection on the given list of reaction propensities.

Parameters
proparray_like

A 1D array of the propensities of the reactions.

Returns
choiceint

Index of the chosen reaction.

statusint

Status of the simulation as described in direct.

Module contents

Top-level package for Stochastic Simulation Algorithms in Python.