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
CompartmentDef
EdgeDef
CompartmentModel
Bases: Protocol
A simplified CompartmentModel
interface.
compartments
abstractmethod
property
compartments: Sequence[CompartmentDef]
The unique compartments in the model.
check_dependencies
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:
-
ExternalDependencyError
–If some dependencies are missing.
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:
-
ipm
(CompartmentModel
) –The compartment model to render.
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.