From d90b0f72cd65d5875eb9e4012990699cfe886c5c Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Sun, 21 Jan 2024 18:57:57 +0100 Subject: [PATCH 01/12] further updates --- benchmarks/WolfSheep/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/benchmarks/WolfSheep/__init__.py b/benchmarks/WolfSheep/__init__.py index e69de29bb2d..18b86ab19ba 100644 --- a/benchmarks/WolfSheep/__init__.py +++ b/benchmarks/WolfSheep/__init__.py @@ -0,0 +1,14 @@ +from wolf_sheep import WolfSheep + +if __name__ == "__main__": + # for profiling this benchmark model + import time + + # model = WolfSheep(15, 25, 25, 60, 40, 0.2, 0.1, 20) + model = WolfSheep(15, 100, 100, 1000, 500, 0.4, 0.2, 20) + + start_time = time.perf_counter() + for _ in range(100): + model.step() + + print(time.perf_counter() - start_time) From 95864900d9e7981c8ea7240e34672f3fe792d681 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Sun, 21 Jan 2024 19:13:00 +0100 Subject: [PATCH 02/12] Update benchmarks/WolfSheep/__init__.py --- benchmarks/WolfSheep/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/WolfSheep/__init__.py b/benchmarks/WolfSheep/__init__.py index 18b86ab19ba..98b1e9fdfed 100644 --- a/benchmarks/WolfSheep/__init__.py +++ b/benchmarks/WolfSheep/__init__.py @@ -1,4 +1,4 @@ -from wolf_sheep import WolfSheep +from .wolf_sheep import WolfSheep if __name__ == "__main__": # for profiling this benchmark model From 2759244eb350ce8e38b31b08cbea46e40998893a Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Tue, 27 Aug 2024 14:01:46 +0200 Subject: [PATCH 03/12] Update __init__.py --- benchmarks/WolfSheep/__init__.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/benchmarks/WolfSheep/__init__.py b/benchmarks/WolfSheep/__init__.py index 98b1e9fdfed..e69de29bb2d 100644 --- a/benchmarks/WolfSheep/__init__.py +++ b/benchmarks/WolfSheep/__init__.py @@ -1,14 +0,0 @@ -from .wolf_sheep import WolfSheep - -if __name__ == "__main__": - # for profiling this benchmark model - import time - - # model = WolfSheep(15, 25, 25, 60, 40, 0.2, 0.1, 20) - model = WolfSheep(15, 100, 100, 1000, 500, 0.4, 0.2, 20) - - start_time = time.perf_counter() - for _ in range(100): - model.step() - - print(time.perf_counter() - start_time) From 2c5d6f2866a2ed4fc587ee47826a5218e3ac1200 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Mon, 21 Oct 2024 19:21:24 +0200 Subject: [PATCH 04/12] add support for new style grids and old style continuous space --- mesa/visualization/components/altair.py | 101 ++++++++++++++++++++---- 1 file changed, 84 insertions(+), 17 deletions(-) diff --git a/mesa/visualization/components/altair.py b/mesa/visualization/components/altair.py index 3dcb7e2c5d9..53ed8787b3d 100644 --- a/mesa/visualization/components/altair.py +++ b/mesa/visualization/components/altair.py @@ -7,6 +7,8 @@ with contextlib.suppress(ImportError): import altair as alt +from mesa.experimental.cell_space import DiscreteSpace, Grid +from mesa.space import ContinuousSpace, _Grid from mesa.visualization.utils import update_counter @@ -29,28 +31,93 @@ def SpaceAltair(model, agent_portrayal, dependencies: list[any] | None = None): if space is None: # Sometimes the space is defined as model.space instead of model.grid space = model.space + + chart = _draw_grid(space, agent_portrayal) solara.FigureAltair(chart) +def _get_agent_data_old__discrete_space(space, agent_portrayal): + """Format agent portrayal data for old-style discrete spaces. + + Args: + space: the mesa.space._Grid instance + agent_portrayal: the agent portrayal callable + + Returns: + list of dicts + + """ + all_agent_data = [] + for content, (x, y) in space.coord_iter(): + if not content: + continue + if not hasattr(content, "__iter__"): + # Is a single grid + content = [content] # noqa: PLW2901 + for agent in content: + # use all data from agent portrayal, and add x,y coordinates + agent_data = agent_portrayal(agent) + agent_data["x"] = x + agent_data["y"] = y + all_agent_data.append(agent_data) + return all_agent_data + + +def _get_agent_data_new_discrete_space(space:DiscreteSpace, agent_portrayal): + """Format agent portrayal data for new-style discrete spaces. + + Args: + space: the mesa.experiment.cell_space.Grid instance + agent_portrayal: the agent portrayal callable + + Returns: + list of dicts + + """ + all_agent_data = [] + + for cell in space.all_cells: + for agent in cell.agents: + agent_data = agent_portrayal(agent) + agent_data["x"] = cell.coordinate[0] + agent_data["y"] = cell.coordinate[1] + all_agent_data.append(agent_data) + return all_agent_data + + +def _get_agent_data_continous_space(space:ContinuousSpace, agent_portrayal): + """Format agent portrayal data for contiuous space. + + Args: + space: the ContinuousSpace instance + agent_portrayal: the agent portrayal callable + + Returns: + list of dicts + """ + data = space.__agent_points + + all_agent_data = [] + for i, agent in space._index_to_agent.items(): + agent_data = agent_portrayal(agent) + agent_data["x"] = data[i, 0] + agent_data["y"] = data[i, 1] + all_agent_data.append(agent) + return all_agent_data + def _draw_grid(space, agent_portrayal): - def portray(g): - all_agent_data = [] - for content, (x, y) in g.coord_iter(): - if not content: - continue - if not hasattr(content, "__iter__"): - # Is a single grid - content = [content] # noqa: PLW2901 - for agent in content: - # use all data from agent portrayal, and add x,y coordinates - agent_data = agent_portrayal(agent) - agent_data["x"] = x - agent_data["y"] = y - all_agent_data.append(agent_data) - return all_agent_data - - all_agent_data = portray(space) + + match space: + case Grid(): + all_agent_data = _get_agent_data_new_discrete_space(space, agent_portrayal) + case _Grid(): + all_agent_data = _get_agent_data_old__discrete_space(space, agent_portrayal) + case ContinuousSpace(): + all_agent_data = _get_agent_data_continous_space(space, agent_portrayal) + case _: + raise NotImplementedError(f"visualizing {type(space)} is currently not supported through altair") + invalid_tooltips = ["color", "size", "x", "y"] encoding_dict = { From d7cf0ef7f97a558cea21fbe9f5babbbfeff11e92 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2024 17:24:15 +0000 Subject: [PATCH 05/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/visualization/components/altair.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mesa/visualization/components/altair.py b/mesa/visualization/components/altair.py index 53ed8787b3d..9d9dfa7df28 100644 --- a/mesa/visualization/components/altair.py +++ b/mesa/visualization/components/altair.py @@ -32,7 +32,6 @@ def SpaceAltair(model, agent_portrayal, dependencies: list[any] | None = None): # Sometimes the space is defined as model.space instead of model.grid space = model.space - chart = _draw_grid(space, agent_portrayal) solara.FigureAltair(chart) @@ -64,7 +63,7 @@ def _get_agent_data_old__discrete_space(space, agent_portrayal): return all_agent_data -def _get_agent_data_new_discrete_space(space:DiscreteSpace, agent_portrayal): +def _get_agent_data_new_discrete_space(space: DiscreteSpace, agent_portrayal): """Format agent portrayal data for new-style discrete spaces. Args: @@ -86,7 +85,7 @@ def _get_agent_data_new_discrete_space(space:DiscreteSpace, agent_portrayal): return all_agent_data -def _get_agent_data_continous_space(space:ContinuousSpace, agent_portrayal): +def _get_agent_data_continous_space(space: ContinuousSpace, agent_portrayal): """Format agent portrayal data for contiuous space. Args: @@ -106,8 +105,8 @@ def _get_agent_data_continous_space(space:ContinuousSpace, agent_portrayal): all_agent_data.append(agent) return all_agent_data -def _draw_grid(space, agent_portrayal): +def _draw_grid(space, agent_portrayal): match space: case Grid(): all_agent_data = _get_agent_data_new_discrete_space(space, agent_portrayal) @@ -116,7 +115,9 @@ def _draw_grid(space, agent_portrayal): case ContinuousSpace(): all_agent_data = _get_agent_data_continous_space(space, agent_portrayal) case _: - raise NotImplementedError(f"visualizing {type(space)} is currently not supported through altair") + raise NotImplementedError( + f"visualizing {type(space)} is currently not supported through altair" + ) invalid_tooltips = ["color", "size", "x", "y"] From 7f1b2beefccd144a451d7def49ee47cd927280f6 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Mon, 21 Oct 2024 19:27:11 +0200 Subject: [PATCH 06/12] fix typo in docstring --- mesa/visualization/components/altair.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/visualization/components/altair.py b/mesa/visualization/components/altair.py index 9d9dfa7df28..6fe152cd537 100644 --- a/mesa/visualization/components/altair.py +++ b/mesa/visualization/components/altair.py @@ -86,7 +86,7 @@ def _get_agent_data_new_discrete_space(space: DiscreteSpace, agent_portrayal): def _get_agent_data_continous_space(space: ContinuousSpace, agent_portrayal): - """Format agent portrayal data for contiuous space. + """Format agent portrayal data for continuous space. Args: space: the ContinuousSpace instance From 42ef16aa47d8f60a790ff34cd093ac92615001a8 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Mon, 21 Oct 2024 19:29:39 +0200 Subject: [PATCH 07/12] update datatype encoding for x and y --- mesa/visualization/components/altair.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mesa/visualization/components/altair.py b/mesa/visualization/components/altair.py index 6fe152cd537..87883813dd3 100644 --- a/mesa/visualization/components/altair.py +++ b/mesa/visualization/components/altair.py @@ -121,11 +121,14 @@ def _draw_grid(space, agent_portrayal): invalid_tooltips = ["color", "size", "x", "y"] + + x_y_type = "ordinal" if not isinstance(space, ContinuousSpace) else "nominal" + encoding_dict = { # no x-axis label - "x": alt.X("x", axis=None, type="ordinal"), + "x": alt.X("x", axis=None, type=x_y_type), # no y-axis label - "y": alt.Y("y", axis=None, type="ordinal"), + "y": alt.Y("y", axis=None, type=x_y_type), "tooltip": [ alt.Tooltip(key, type=alt.utils.infer_vegalite_type([value])) for key, value in all_agent_data[0].items() From 46f8d442264eaca84abd3f11bb8c543e0281ad71 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2024 17:29:47 +0000 Subject: [PATCH 08/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/visualization/components/altair.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mesa/visualization/components/altair.py b/mesa/visualization/components/altair.py index 87883813dd3..1bc44447d5d 100644 --- a/mesa/visualization/components/altair.py +++ b/mesa/visualization/components/altair.py @@ -121,7 +121,6 @@ def _draw_grid(space, agent_portrayal): invalid_tooltips = ["color", "size", "x", "y"] - x_y_type = "ordinal" if not isinstance(space, ContinuousSpace) else "nominal" encoding_dict = { From cab46ab32d05a3ecd2acac1508a5c57bab86e2a6 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Mon, 21 Oct 2024 20:31:51 +0200 Subject: [PATCH 09/12] fixes for continous space visualization --- mesa/visualization/components/altair.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/mesa/visualization/components/altair.py b/mesa/visualization/components/altair.py index 1bc44447d5d..057452675e9 100644 --- a/mesa/visualization/components/altair.py +++ b/mesa/visualization/components/altair.py @@ -95,14 +95,12 @@ def _get_agent_data_continous_space(space: ContinuousSpace, agent_portrayal): Returns: list of dicts """ - data = space.__agent_points - all_agent_data = [] - for i, agent in space._index_to_agent.items(): + for agent in space._agent_to_index.keys(): agent_data = agent_portrayal(agent) - agent_data["x"] = data[i, 0] - agent_data["y"] = data[i, 1] - all_agent_data.append(agent) + agent_data["x"] = agent.pos[0] + agent_data["y"] = agent.pos[1] + all_agent_data.append(agent_data) return all_agent_data From 3dfa996e5b5bc8ccd978d9c5fb451db4ae1dad16 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Mon, 21 Oct 2024 20:32:16 +0200 Subject: [PATCH 10/12] typo in function name --- mesa/visualization/components/altair.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/visualization/components/altair.py b/mesa/visualization/components/altair.py index 057452675e9..38f4bf8deac 100644 --- a/mesa/visualization/components/altair.py +++ b/mesa/visualization/components/altair.py @@ -85,7 +85,7 @@ def _get_agent_data_new_discrete_space(space: DiscreteSpace, agent_portrayal): return all_agent_data -def _get_agent_data_continous_space(space: ContinuousSpace, agent_portrayal): +def _get_agent_data_continuous_space(space: ContinuousSpace, agent_portrayal): """Format agent portrayal data for continuous space. Args: From 71de07c00afc5ef29c5406ff521e73cb09dd50a4 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Mon, 21 Oct 2024 20:32:28 +0200 Subject: [PATCH 11/12] Update altair.py --- mesa/visualization/components/altair.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/visualization/components/altair.py b/mesa/visualization/components/altair.py index 38f4bf8deac..8d97d1e0291 100644 --- a/mesa/visualization/components/altair.py +++ b/mesa/visualization/components/altair.py @@ -111,7 +111,7 @@ def _draw_grid(space, agent_portrayal): case _Grid(): all_agent_data = _get_agent_data_old__discrete_space(space, agent_portrayal) case ContinuousSpace(): - all_agent_data = _get_agent_data_continous_space(space, agent_portrayal) + all_agent_data = _get_agent_data_continuous_space(space, agent_portrayal) case _: raise NotImplementedError( f"visualizing {type(space)} is currently not supported through altair" From 2a49ee0015066b4312b574d2f7568fdbbf3dca34 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Mon, 21 Oct 2024 20:45:42 +0200 Subject: [PATCH 12/12] ruff fix --- mesa/visualization/components/altair.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/visualization/components/altair.py b/mesa/visualization/components/altair.py index 8d97d1e0291..5aeee84761d 100644 --- a/mesa/visualization/components/altair.py +++ b/mesa/visualization/components/altair.py @@ -96,7 +96,7 @@ def _get_agent_data_continuous_space(space: ContinuousSpace, agent_portrayal): list of dicts """ all_agent_data = [] - for agent in space._agent_to_index.keys(): + for agent in space._agent_to_index: agent_data = agent_portrayal(agent) agent_data["x"] = agent.pos[0] agent_data["y"] = agent.pos[1]