From 6b57ee9ad79a33b914c8ca27b598b9002c708944 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 30 Oct 2024 13:46:00 +0100 Subject: [PATCH 1/6] Update app.py --- mesa/examples/basic/conways_game_of_life/app.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mesa/examples/basic/conways_game_of_life/app.py b/mesa/examples/basic/conways_game_of_life/app.py index 7a45125a30a..7d620add9a0 100644 --- a/mesa/examples/basic/conways_game_of_life/app.py +++ b/mesa/examples/basic/conways_game_of_life/app.py @@ -6,9 +6,14 @@ def agent_portrayal(agent): - return {"c": "white" if agent.state == 0 else "black", "marker": "s"} + return {"color": "white" if agent.state == 0 else "black", "marker": "s", 'size':25} +def post_process(ax): + ax.set_aspect('equal') + ax.set_xticks([]) + ax.set_yticks([]) + model_params = { "width": 50, "height": 50, @@ -22,7 +27,7 @@ def agent_portrayal(agent): # Under the hood these are just classes that receive the model instance. # You can also author your own visualization elements, which can also be functions # that receive the model instance and return a valid solara component. -SpaceGraph = make_space_component(agent_portrayal) +SpaceGraph = make_space_component(agent_portrayal, post_process=post_process, draw_grid=False) # Create the SolaraViz page. This will automatically create a server and display the From a876028746a936af25b63c433188c30fea959a8f Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 30 Oct 2024 13:53:41 +0100 Subject: [PATCH 2/6] Update app.py --- mesa/examples/basic/schelling/app.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mesa/examples/basic/schelling/app.py b/mesa/examples/basic/schelling/app.py index 53fab7ba0f0..278742bf6f1 100644 --- a/mesa/examples/basic/schelling/app.py +++ b/mesa/examples/basic/schelling/app.py @@ -1,5 +1,4 @@ import solara - from mesa.examples.basic.schelling.model import Schelling from mesa.visualization import ( Slider, @@ -28,13 +27,13 @@ def agent_portrayal(agent): model1 = Schelling(20, 20, 0.8, 0.2, 3) -HappyPlot = make_plot_measure("happy") +HappyPlot = make_plot_measure({"happy": "tab:green"}) page = SolaraViz( model1, components=[ make_space_component(agent_portrayal), - make_plot_measure("happy"), + HappyPlot, get_happy_agents, ], model_params=model_params, From 6fedd7971a8b4160b23080669edc6e77716fe387 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 30 Oct 2024 14:02:11 +0100 Subject: [PATCH 3/6] ruff fixes --- .../basic/conways_game_of_life/app.py | 4 +- mesa/examples/basic/schelling/app.py | 1 + mesa/examples/basic/virus_on_network/app.py | 60 ++++--------------- 3 files changed, 15 insertions(+), 50 deletions(-) diff --git a/mesa/examples/basic/conways_game_of_life/app.py b/mesa/examples/basic/conways_game_of_life/app.py index 7d620add9a0..6dfbd62b3e0 100644 --- a/mesa/examples/basic/conways_game_of_life/app.py +++ b/mesa/examples/basic/conways_game_of_life/app.py @@ -6,11 +6,11 @@ def agent_portrayal(agent): - return {"color": "white" if agent.state == 0 else "black", "marker": "s", 'size':25} + return {"color": "white" if agent.state == 0 else "black", "marker": "s", "size":25} def post_process(ax): - ax.set_aspect('equal') + ax.set_aspect("equal") ax.set_xticks([]) ax.set_yticks([]) diff --git a/mesa/examples/basic/schelling/app.py b/mesa/examples/basic/schelling/app.py index 278742bf6f1..86f5a2941fd 100644 --- a/mesa/examples/basic/schelling/app.py +++ b/mesa/examples/basic/schelling/app.py @@ -1,4 +1,5 @@ import solara + from mesa.examples.basic.schelling.model import Schelling from mesa.visualization import ( Slider, diff --git a/mesa/examples/basic/virus_on_network/app.py b/mesa/examples/basic/virus_on_network/app.py index 7cf54f308d5..5e0cfebb6a8 100644 --- a/mesa/examples/basic/virus_on_network/app.py +++ b/mesa/examples/basic/virus_on_network/app.py @@ -1,71 +1,34 @@ import math import solara -from matplotlib.figure import Figure -from matplotlib.ticker import MaxNLocator from mesa.examples.basic.virus_on_network.model import ( State, VirusOnNetwork, number_infected, ) -from mesa.visualization import Slider, SolaraViz, make_space_component - +from mesa.visualization import ( + Slider, + SolaraViz, + make_plot_measure, + make_space_component, +) -def agent_portrayal(graph): - def get_agent(node): - return graph.nodes[node]["agent"][0] - edge_width = [] - edge_color = [] - for u, v in graph.edges(): - agent1 = get_agent(u) - agent2 = get_agent(v) - w = 2 - ec = "#e8e8e8" - if State.RESISTANT in (agent1.state, agent2.state): - w = 3 - ec = "black" - edge_width.append(w) - edge_color.append(ec) +def agent_portrayal(agent): node_color_dict = { State.INFECTED: "tab:red", State.SUSCEPTIBLE: "tab:green", State.RESISTANT: "tab:gray", } - node_color = [node_color_dict[get_agent(node).state] for node in graph.nodes()] - return { - "width": edge_width, - "edge_color": edge_color, - "node_color": node_color, - } - + return {"color": node_color_dict[agent.state], "size":10} def get_resistant_susceptible_ratio(model): ratio = model.resistant_susceptible_ratio() ratio_text = r"$\infty$" if ratio is math.inf else f"{ratio:.2f}" infected_text = str(number_infected(model)) - return f"Resistant/Susceptible Ratio: {ratio_text}
Infected Remaining: {infected_text}" - - -def make_plot(model): - # This is for the case when we want to plot multiple measures in 1 figure. - fig = Figure() - ax = fig.subplots() - measures = ["Infected", "Susceptible", "Resistant"] - colors = ["tab:red", "tab:green", "tab:gray"] - for i, m in enumerate(measures): - color = colors[i] - df = model.datacollector.get_model_vars_dataframe() - ax.plot(df.loc[:, m], label=m, color=color) - fig.legend() - # Set integer x axis - ax.xaxis.set_major_locator(MaxNLocator(integer=True)) - ax.set_xlabel("Step") - ax.set_ylabel("Number of Agents") - return solara.FigureMatplotlib(fig) - + return solara.Markdown(f"Resistant/Susceptible Ratio: {ratio_text}
Infected Remaining: {infected_text}") model_params = { "num_nodes": Slider( @@ -120,6 +83,7 @@ def make_plot(model): } SpacePlot = make_space_component(agent_portrayal) +StatePlot = make_plot_measure({"Infected":"tab:red", "Susceptible":"tab:green", "Resistant":"tab:gray"}) model1 = VirusOnNetwork() @@ -127,8 +91,8 @@ def make_plot(model): model1, [ SpacePlot, - make_plot, - # get_resistant_susceptible_ratio, # TODO: Fix and uncomment + StatePlot, + get_resistant_susceptible_ratio, ], model_params=model_params, name="Virus Model", From d95f56cfe988d01d1a1c2cb04c068b073536c8c6 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 30 Oct 2024 14:10:05 +0100 Subject: [PATCH 4/6] advanced examples updated --- .../examples/advanced/epstein_civil_violence/app.py | 13 ++++++++++--- mesa/examples/advanced/pd_grid/app.py | 3 +-- mesa/examples/advanced/sugarscape_g1mt/app.py | 6 +++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mesa/examples/advanced/epstein_civil_violence/app.py b/mesa/examples/advanced/epstein_civil_violence/app.py index 538ef186f57..a31b18cff62 100644 --- a/mesa/examples/advanced/epstein_civil_violence/app.py +++ b/mesa/examples/advanced/epstein_civil_violence/app.py @@ -25,7 +25,7 @@ def citizen_cop_portrayal(agent): return portrayal = { - "size": 25, + "size": 50, } if isinstance(agent, Citizen): @@ -36,6 +36,12 @@ def citizen_cop_portrayal(agent): return portrayal +def post_process(ax): + ax.set_aspect('equal') + ax.set_xticks([]) + ax.set_yticks([]) + ax.get_figure().set_size_inches(10, 10) + model_params = { "height": 40, "width": 40, @@ -47,8 +53,9 @@ def citizen_cop_portrayal(agent): "max_jail_term": Slider("Max Jail Term", 30, 0, 50, 1), } -space_component = make_space_component(citizen_cop_portrayal) -chart_component = make_plot_measure([state.name.lower() for state in CitizenState]) +space_component = make_space_component(citizen_cop_portrayal, post_process=post_process, draw_grid=False) + +chart_component = make_plot_measure({state.name.lower():agent_colors[state] for state in CitizenState}) epstein_model = EpsteinCivilViolence() diff --git a/mesa/examples/advanced/pd_grid/app.py b/mesa/examples/advanced/pd_grid/app.py index 6edf8140536..a3fc88213e4 100644 --- a/mesa/examples/advanced/pd_grid/app.py +++ b/mesa/examples/advanced/pd_grid/app.py @@ -1,7 +1,6 @@ """ Solara-based visualization for the Spatial Prisoner's Dilemma Model. """ - from mesa.examples.advanced.pd_grid.model import PdGrid from mesa.visualization import SolaraViz, make_plot_measure, make_space_component from mesa.visualization.UserParam import Slider @@ -13,7 +12,7 @@ def pd_agent_portrayal(agent): """ return { "color": "blue" if agent.move == "C" else "red", - "shape": "s", # square marker + "marker": "s", # square marker "size": 25, } diff --git a/mesa/examples/advanced/sugarscape_g1mt/app.py b/mesa/examples/advanced/sugarscape_g1mt/app.py index 752998891bd..227a8cc721f 100644 --- a/mesa/examples/advanced/sugarscape_g1mt/app.py +++ b/mesa/examples/advanced/sugarscape_g1mt/app.py @@ -1,9 +1,9 @@ import numpy as np import solara from matplotlib.figure import Figure -from sugarscape_g1mt.model import SugarscapeG1mt -from sugarscape_g1mt.trader_agents import Trader +from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt +from mesa.examples.advanced.sugarscape_g1mt.agents import Trader from mesa.visualization import SolaraViz, make_plot_measure @@ -57,6 +57,6 @@ def portray(g): model1, components=[SpaceDrawer, make_plot_measure(["Trader", "Price"])], name="Sugarscape {G1, M, T}", - play_interval=1500, + play_interval=150, ) page # noqa From c640085f9d768a8a0c4b5ad0ead2a96160017661 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 30 Oct 2024 14:10:17 +0100 Subject: [PATCH 5/6] ruff related fixes --- mesa/examples/advanced/epstein_civil_violence/app.py | 2 +- mesa/examples/advanced/sugarscape_g1mt/app.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mesa/examples/advanced/epstein_civil_violence/app.py b/mesa/examples/advanced/epstein_civil_violence/app.py index a31b18cff62..ddb75394303 100644 --- a/mesa/examples/advanced/epstein_civil_violence/app.py +++ b/mesa/examples/advanced/epstein_civil_violence/app.py @@ -37,7 +37,7 @@ def citizen_cop_portrayal(agent): def post_process(ax): - ax.set_aspect('equal') + ax.set_aspect("equal") ax.set_xticks([]) ax.set_yticks([]) ax.get_figure().set_size_inches(10, 10) diff --git a/mesa/examples/advanced/sugarscape_g1mt/app.py b/mesa/examples/advanced/sugarscape_g1mt/app.py index 227a8cc721f..39969e24079 100644 --- a/mesa/examples/advanced/sugarscape_g1mt/app.py +++ b/mesa/examples/advanced/sugarscape_g1mt/app.py @@ -2,8 +2,8 @@ import solara from matplotlib.figure import Figure -from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt from mesa.examples.advanced.sugarscape_g1mt.agents import Trader +from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt from mesa.visualization import SolaraViz, make_plot_measure From 1806b347b847366ee4a2dbc286cad861abf8bd9f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:13:47 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/examples/advanced/epstein_civil_violence/app.py | 9 +++++++-- mesa/examples/advanced/pd_grid/app.py | 1 + mesa/examples/basic/conways_game_of_life/app.py | 11 +++++++++-- mesa/examples/basic/virus_on_network/app.py | 12 +++++++++--- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/mesa/examples/advanced/epstein_civil_violence/app.py b/mesa/examples/advanced/epstein_civil_violence/app.py index ddb75394303..99304cd4618 100644 --- a/mesa/examples/advanced/epstein_civil_violence/app.py +++ b/mesa/examples/advanced/epstein_civil_violence/app.py @@ -42,6 +42,7 @@ def post_process(ax): ax.set_yticks([]) ax.get_figure().set_size_inches(10, 10) + model_params = { "height": 40, "width": 40, @@ -53,9 +54,13 @@ def post_process(ax): "max_jail_term": Slider("Max Jail Term", 30, 0, 50, 1), } -space_component = make_space_component(citizen_cop_portrayal, post_process=post_process, draw_grid=False) +space_component = make_space_component( + citizen_cop_portrayal, post_process=post_process, draw_grid=False +) -chart_component = make_plot_measure({state.name.lower():agent_colors[state] for state in CitizenState}) +chart_component = make_plot_measure( + {state.name.lower(): agent_colors[state] for state in CitizenState} +) epstein_model = EpsteinCivilViolence() diff --git a/mesa/examples/advanced/pd_grid/app.py b/mesa/examples/advanced/pd_grid/app.py index a3fc88213e4..d5bfd626e3c 100644 --- a/mesa/examples/advanced/pd_grid/app.py +++ b/mesa/examples/advanced/pd_grid/app.py @@ -1,6 +1,7 @@ """ Solara-based visualization for the Spatial Prisoner's Dilemma Model. """ + from mesa.examples.advanced.pd_grid.model import PdGrid from mesa.visualization import SolaraViz, make_plot_measure, make_space_component from mesa.visualization.UserParam import Slider diff --git a/mesa/examples/basic/conways_game_of_life/app.py b/mesa/examples/basic/conways_game_of_life/app.py index 6dfbd62b3e0..168681b7ba6 100644 --- a/mesa/examples/basic/conways_game_of_life/app.py +++ b/mesa/examples/basic/conways_game_of_life/app.py @@ -6,7 +6,11 @@ def agent_portrayal(agent): - return {"color": "white" if agent.state == 0 else "black", "marker": "s", "size":25} + return { + "color": "white" if agent.state == 0 else "black", + "marker": "s", + "size": 25, + } def post_process(ax): @@ -14,6 +18,7 @@ def post_process(ax): ax.set_xticks([]) ax.set_yticks([]) + model_params = { "width": 50, "height": 50, @@ -27,7 +32,9 @@ def post_process(ax): # Under the hood these are just classes that receive the model instance. # You can also author your own visualization elements, which can also be functions # that receive the model instance and return a valid solara component. -SpaceGraph = make_space_component(agent_portrayal, post_process=post_process, draw_grid=False) +SpaceGraph = make_space_component( + agent_portrayal, post_process=post_process, draw_grid=False +) # Create the SolaraViz page. This will automatically create a server and display the diff --git a/mesa/examples/basic/virus_on_network/app.py b/mesa/examples/basic/virus_on_network/app.py index 5e0cfebb6a8..8e82a72830c 100644 --- a/mesa/examples/basic/virus_on_network/app.py +++ b/mesa/examples/basic/virus_on_network/app.py @@ -21,14 +21,18 @@ def agent_portrayal(agent): State.SUSCEPTIBLE: "tab:green", State.RESISTANT: "tab:gray", } - return {"color": node_color_dict[agent.state], "size":10} + return {"color": node_color_dict[agent.state], "size": 10} + def get_resistant_susceptible_ratio(model): ratio = model.resistant_susceptible_ratio() ratio_text = r"$\infty$" if ratio is math.inf else f"{ratio:.2f}" infected_text = str(number_infected(model)) - return solara.Markdown(f"Resistant/Susceptible Ratio: {ratio_text}
Infected Remaining: {infected_text}") + return solara.Markdown( + f"Resistant/Susceptible Ratio: {ratio_text}
Infected Remaining: {infected_text}" + ) + model_params = { "num_nodes": Slider( @@ -83,7 +87,9 @@ def get_resistant_susceptible_ratio(model): } SpacePlot = make_space_component(agent_portrayal) -StatePlot = make_plot_measure({"Infected":"tab:red", "Susceptible":"tab:green", "Resistant":"tab:gray"}) +StatePlot = make_plot_measure( + {"Infected": "tab:red", "Susceptible": "tab:green", "Resistant": "tab:gray"} +) model1 = VirusOnNetwork()