CompartmentModel

compartment_model.CompartmentModel()

A compartment model definition and its corresponding metadata. Effectively, a collection of compartments, transitions between compartments, and the data parameters which are required to compute the transitions.

CompartmentModel is an abstract class. To create a custom IPM, you will write an implementation of this class.

Examples

from epymorph.kit import *
from sympy import Max


class MyIpm(CompartmentModel):
    compartments = (
        compartment("S"),
        compartment("I"),
        compartment("R"),
    )

    requirements = [
        AttributeDef("beta", type=float, shape=Shapes.TxN, comment="infection rate"),
        AttributeDef("gamma", type=float, shape=Shapes.TxN, comment="recovery rate"),
    ]

    def edges(self, symbols):
        [S, I, R] = symbols.all_compartments
        [beta, gamma] = symbols.all_requirements
        N = Max(1, S + I + R)
        return [
            edge(S, I, rate=beta * S * I / N),
            edge(I, R, rate=gamma * I),
        ]

Attributes

compartments: Sequence[CompartmentDef]

The compartments of the model.

requirements: Sequence[AttributeDef]

The attributes required by the model.

quantities: Iterator[CompartmentDef | EdgeDef]

All quantities from the model, compartments then edges, in definition order.

symbols: ModelSymbols

The symbols which represent parts of this model.

transitions: Sequence[TransitionDef]

The transitions in the model.

events: Sequence[EdgeDef]

Iterate over all events in order.

requirements_dict: OrderedDict[AbsoluteName, AttributeDef]

The attributes required by this model.

strata: Sequence[str]

The names of the strata involved in this compartment model.

is_multistrata: bool

True if this compartment model is multistrata (False for single-strata).

select: QuantitySelector

Make a quantity selection from this IPM.

Methods

Name Description
diagram Render a diagram of this IPM, either by showing it with matplotlib (default) or by saving it to file as a png image.
edges When implementing a CompartmentModel, override this method to define the transition edges between compartments.