Reference: Compartmental Model Simulations

class pydemic.Reaction(lhs, rhs, evaluator)[source]
lhs

A string specifying the compartment sourcing the reaction. Only valid python identifiers are allowed, as their state is accessed as an attribute.

rhs

A string specifying the compartment being sourced by the reaction. Only valid python identifiers are allowed, as their state is accessed as an attribute.

evaluator

A callable with signature (t, y) of the time and SimulationState returning the rate associated with the reaction. See the documentation of SimulationState for guidance on accessing compartment state information from y.

get_reactions()[source]
Returns

A tuple of the fundamental Reaction’s comprising the full process.

class pydemic.ErlangProcess(lhs, rhs, shape, scale)[source]
lhs

A string specifying the compartment sourcing the reaction. Only valid python identifiers are allowed, as their state is accessed as an attribute.

rhs

A string specifying the compartment being sourced by the reaction. Only valid python identifiers are allowed, as their state is accessed as an attribute.

shape
scale
get_reactions()[source]
Returns

A tuple of the fundamental Reaction’s comprising the full process.

class pydemic.GammaProcess(lhs, rhs, shape, scale)[source]

Currently, an alias to ErlangProcess.

class pydemic.PassiveReaction(lhs, rhs, evaluator)[source]

A reaction used for bookkeeping purposes.

get_reactions()[source]
Returns

A tuple of the fundamental Reaction’s comprising the full process.

class pydemic.Simulation(reactions)[source]

Main driver for compartmental model simulations.

Parameters

reactions – A list of Reaction’s (or subclasses thereof) used to specify the dynamics of the compartmental model.

__call__(tspan, y0, dt, stochastic_method=None, samples=1, seed=None, logger=None)[source]
Parameters
  • tspan – A tuple specifying the initiala and final times.

  • y0 – A dict with the initial values (as numpy.ndarray’s) for each of compartments.

  • dt – The (initial) timestep to use.

  • stochastic_method – A string specifying whether to use direct Gillespie stepping (‘direct’) or \(\tau\)-leaing (‘tau_leap’). Defaults to None, i.e., a deterministic evolution.

  • samples – The number of stochastic samples to simulate simultaneously. Defaults to 1.

  • seed – The value with which to seed numpy’s random number. Defaults to None, in which case no seed is passed.

Returns

A StateLogger.

compartments

The compartment names comprising the simulation state, inferred as the set of all Reaction.lhs’s and Reaction.rhs’s from the input list of Reaction’s.

class pydemic.SimulationState(t, compartments, hidden_compartments, passive_compartments)[source]

Manages the state for Simulation’s. User-specified compartments are accessed as attributes, e.g.:

>>> state = SimulationState(0., {'a': np.array([1., 0.])}, {})
>>> state.a
array([1., 0.])
>>> state.t
0.

Note that Simulation initializes state with an extra axis relative to the input data, corresponding to the number of requested stochastic samples (see Simulation.__call__()). Any user-implemented axes occupy all but the first axis of the state arrays.

Parameters
  • t – The current time.

  • compartments – A dict of current values (as numpy.ndarray’s) of all canonical compartments (the keys).

  • hidden_compartments – A dict of current values (as numpy.ndarray’s) of all compartments (the keys) not present in compartments (i.e., those used to implement and ErlangProcess.)

  • passive_compartments – A tuple of compartment keys which are computed for PassiveReaction’s (i.e., those which do not count toward the total population).

sum()[source]
Returns

The total population across all summed compartments.

class pydemic.StateLogger(chunk_length=1000)[source]

Used to log simulation results returned by Simulation.__call__().

t

A numpy.ndarray of output times.

y

A dict whose values are numpy.ndarray’s of the timeseries for each key (each of Simulation.compartments). The time axis is the first axis of the numpy.ndarray’s.