Skip to content

epymorph.tools.ipm_diagram

Produce graphical diagrams of compartmental disease models (IPMs).

Usage of these features requires separate installation of a latex converter and graphviz.

CompartmentName

Bases: Protocol

A simplified CompartmentName interface.

CompartmentDef

Bases: Protocol

A simplified CompartmentDef interface.

name abstractmethod property

The compartment name.

EdgeDef

Bases: Protocol

A simplified EdgeDef interface.

rate abstractmethod property

rate: Expr

The rate of flow along this edge.

tuple abstractmethod property

tuple: tuple[str, str]

The edge in tuple form: (from_name, to_name).

CompartmentModel

Bases: Protocol

A simplified CompartmentModel interface.

compartments abstractmethod property

compartments: Sequence[CompartmentDef]

The unique compartments in the model.

events abstractmethod property

events: Sequence[EdgeDef]

The unique transition events in the model.

check_dependencies

check_dependencies() -> None

Check if the external requirements for drawing diagrams are installed.

The results of this check are memorized so it can be called repeatedly without adding much overhead.

Raises:

construct_digraph

construct_digraph(
    ipm: CompartmentModel,
) -> Iterator[Digraph]

Construct a graphviz object (Digraph) for the compartment model's diagram.

The Digraph instance is only valid within the managed context because we rely on temporary files to render latex expressions.

Yields:

  • Digraph

    The Digraph instance, which is valid as soon as the context opens but only until the context is closed.

render_diagram_to_bytes

render_diagram_to_bytes(ipm: CompartmentModel) -> BytesIO

Render a diagram of the given compartment model and return it as the bytes of a png-formatted image.

Parameters:

Returns:

  • BytesIO

    The bytes of the image.

render_diagram

render_diagram(
    ipm: CompartmentModel,
    *,
    file: str | Path | None = None,
    figsize: tuple[float, float] | None = None,
) -> None

Render a diagram of the given compartment model and either show it with matplotlib (default) or save it to file as a png image.

Parameters:

  • ipm (CompartmentModel) –

    The compartment model to render.

  • file (str | Path | None, default: None ) –

    Provide a file path to save a png image of the diagram to this path. If file is None, we will instead use matplotlib to show the diagram.

  • figsize (tuple[float, float] | None, default: None ) –

    The matplotlib figure size to use when displaying the diagram. Only used if file is not provided.