|
| 1 | +"""custom solara components.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Callable |
| 6 | + |
| 7 | +from .altair_components import SpaceAltair, make_altair_space |
| 8 | +from .matplotlib_components import ( |
| 9 | + SpaceMatplotlib, |
| 10 | + make_mpl_plot_component, |
| 11 | + make_mpl_space_component, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def make_space_component( |
| 16 | + agent_portrayal: Callable | None = None, |
| 17 | + propertylayer_portrayal: dict | None = None, |
| 18 | + post_process: Callable | None = None, |
| 19 | + backend: str = "matplotlib", |
| 20 | + **space_drawing_kwargs, |
| 21 | +) -> SpaceMatplotlib | SpaceAltair: |
| 22 | + """Create a Matplotlib-based space visualization component. |
| 23 | +
|
| 24 | + Args: |
| 25 | + agent_portrayal: Function to portray agents. |
| 26 | + propertylayer_portrayal: Dictionary of PropertyLayer portrayal specifications |
| 27 | + post_process : a callable that will be called with the Axes instance. Allows for fine-tuning plots (e.g., control ticks) |
| 28 | + backend: the backend to use {"matplotlib", "altair"} |
| 29 | + space_drawing_kwargs : additional keyword arguments to be passed on to the underlying backend specific space drawer function. See |
| 30 | + the functions for drawing the various spaces for the appropriate backend further details. |
| 31 | +
|
| 32 | +
|
| 33 | + Returns: |
| 34 | + function: A function that creates a space component |
| 35 | + """ |
| 36 | + if backend == "matplotlib": |
| 37 | + return make_mpl_space_component( |
| 38 | + agent_portrayal, |
| 39 | + propertylayer_portrayal, |
| 40 | + post_process, |
| 41 | + **space_drawing_kwargs, |
| 42 | + ) |
| 43 | + elif backend == "altair": |
| 44 | + return make_altair_space( |
| 45 | + agent_portrayal, |
| 46 | + propertylayer_portrayal, |
| 47 | + post_process, |
| 48 | + **space_drawing_kwargs, |
| 49 | + ) |
| 50 | + else: |
| 51 | + raise ValueError( |
| 52 | + f"unknown backend {backend}, must be one of matplotlib, altair" |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +def make_plot_component( |
| 57 | + measure: str | dict[str, str] | list[str] | tuple[str], |
| 58 | + post_process: Callable | None = None, |
| 59 | + backend: str = "matplotlib", |
| 60 | + **plot_drawing_kwargs, |
| 61 | +): |
| 62 | + """Create a plotting function for a specified measure using the specified backend. |
| 63 | +
|
| 64 | + Args: |
| 65 | + measure (str | dict[str, str] | list[str] | tuple[str]): Measure(s) to plot. |
| 66 | + post_process: a user-specified callable to do post-processing called with the Axes instance. |
| 67 | + backend: the backend to use {"matplotlib", "altair"} |
| 68 | + plot_drawing_kwargs: additional keyword arguments to pass onto the backend specific function for making a plotting component |
| 69 | +
|
| 70 | + Notes: |
| 71 | + altair plotting backend is not yet implemented and planned for mesa 3.1. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + function: A function that creates a plot component |
| 75 | + """ |
| 76 | + if backend == "matplotlib": |
| 77 | + return make_mpl_plot_component(measure, post_process, **plot_drawing_kwargs) |
| 78 | + elif backend == "altair": |
| 79 | + raise NotImplementedError("altair line plots are not yet implemented") |
| 80 | + else: |
| 81 | + raise ValueError( |
| 82 | + f"unknown backend {backend}, must be one of matplotlib, altair" |
| 83 | + ) |
0 commit comments