Source code for modelly.viz.mpl.figure
"""Draw a model's space into a Matplotlib figure, without a GUI or a notebook.
``space_figure`` is what Modelly Studio shows, what its figure export saves and what
``modelly.viz.animation`` turns into frames: one ``SpaceRenderer`` on the Matplotlib backend,
drawn on a plain ``Figure`` (no pyplot), so it works headless on every OS.
"""
from __future__ import annotations
from collections.abc import Callable
from typing import TYPE_CHECKING, Any
import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
from modelly.viz.render.portrayal import ClassColours
from modelly.viz.render.renderer import SpaceRenderer
if TYPE_CHECKING:
from modelly.core.model import Model
DEFAULT_FIGSIZE = (5.0, 5.0)
DEFAULT_DPI = 100
RGB_CHANNELS = 3
[docs]
def model_space(model: Model) -> Any:
"""The space a model draws: ``model.grid``, else ``model.space``, else None."""
space = getattr(model, "grid", None)
return space if space is not None else getattr(model, "space", None)
[docs]
def can_draw(model: Model) -> bool:
"""Whether ``model`` has a space ``SpaceRenderer`` can draw."""
space = model_space(model)
if space is None:
return False
try:
SpaceRenderer(model, backend="matplotlib")
except (ValueError, TypeError, AttributeError):
return False
return True
__all__ = ["can_draw", "figure_pixels", "model_space", "space_figure"]