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 andSimulationState
returning the rate associated with the reaction. See the documentation ofSimulationState
for guidance on accessing compartment state information fromy
.
-
-
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
¶
-
-
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.
-
class
pydemic.
Simulation
(reactions)[source]¶ Main driver for compartmental model simulations.
- Parameters
reactions – A
list
ofReaction
’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 (asnumpy.ndarray
’s) for each ofcompartments
.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 andReaction.rhs
’s from the input list ofReaction
’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 (seeSimulation.__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 (asnumpy.ndarray
’s) of all canonical compartments (the keys).hidden_compartments – A
dict
of current values (asnumpy.ndarray
’s) of all compartments (the keys) not present incompartments
(i.e., those used to implement andErlangProcess
.)passive_compartments – A
tuple
of compartment keys which are computed forPassiveReaction
’s (i.e., those which do not count toward the total population).
-
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 arenumpy.ndarray
’s of the timeseries for each key (each ofSimulation.compartments
). The time axis is the first axis of thenumpy.ndarray
’s.
-