Skip to content

Commit 9de5595

Browse files
Update examples to use updated space drawing (#2442)
* Update app.py * Update app.py * ruff fixes * advanced examples updated * ruff related fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e605a1f commit 9de5595

File tree

6 files changed

+51
-57
lines changed

6 files changed

+51
-57
lines changed

mesa/examples/advanced/epstein_civil_violence/app.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def citizen_cop_portrayal(agent):
2525
return
2626

2727
portrayal = {
28-
"size": 25,
28+
"size": 50,
2929
}
3030

3131
if isinstance(agent, Citizen):
@@ -36,6 +36,13 @@ def citizen_cop_portrayal(agent):
3636
return portrayal
3737

3838

39+
def post_process(ax):
40+
ax.set_aspect("equal")
41+
ax.set_xticks([])
42+
ax.set_yticks([])
43+
ax.get_figure().set_size_inches(10, 10)
44+
45+
3946
model_params = {
4047
"height": 40,
4148
"width": 40,
@@ -47,8 +54,13 @@ def citizen_cop_portrayal(agent):
4754
"max_jail_term": Slider("Max Jail Term", 30, 0, 50, 1),
4855
}
4956

50-
space_component = make_space_component(citizen_cop_portrayal)
51-
chart_component = make_plot_measure([state.name.lower() for state in CitizenState])
57+
space_component = make_space_component(
58+
citizen_cop_portrayal, post_process=post_process, draw_grid=False
59+
)
60+
61+
chart_component = make_plot_measure(
62+
{state.name.lower(): agent_colors[state] for state in CitizenState}
63+
)
5264

5365
epstein_model = EpsteinCivilViolence()
5466

mesa/examples/advanced/pd_grid/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def pd_agent_portrayal(agent):
1313
"""
1414
return {
1515
"color": "blue" if agent.move == "C" else "red",
16-
"shape": "s", # square marker
16+
"marker": "s", # square marker
1717
"size": 25,
1818
}
1919

mesa/examples/advanced/sugarscape_g1mt/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import numpy as np
22
import solara
33
from matplotlib.figure import Figure
4-
from sugarscape_g1mt.model import SugarscapeG1mt
5-
from sugarscape_g1mt.trader_agents import Trader
64

5+
from mesa.examples.advanced.sugarscape_g1mt.agents import Trader
6+
from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt
77
from mesa.visualization import SolaraViz, make_plot_measure
88

99

@@ -57,6 +57,6 @@ def portray(g):
5757
model1,
5858
components=[SpaceDrawer, make_plot_measure(["Trader", "Price"])],
5959
name="Sugarscape {G1, M, T}",
60-
play_interval=1500,
60+
play_interval=150,
6161
)
6262
page # noqa

mesa/examples/basic/conways_game_of_life/app.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@
66

77

88
def agent_portrayal(agent):
9-
return {"c": "white" if agent.state == 0 else "black", "marker": "s"}
9+
return {
10+
"color": "white" if agent.state == 0 else "black",
11+
"marker": "s",
12+
"size": 25,
13+
}
14+
15+
16+
def post_process(ax):
17+
ax.set_aspect("equal")
18+
ax.set_xticks([])
19+
ax.set_yticks([])
1020

1121

1222
model_params = {
@@ -22,7 +32,9 @@ def agent_portrayal(agent):
2232
# Under the hood these are just classes that receive the model instance.
2333
# You can also author your own visualization elements, which can also be functions
2434
# that receive the model instance and return a valid solara component.
25-
SpaceGraph = make_space_component(agent_portrayal)
35+
SpaceGraph = make_space_component(
36+
agent_portrayal, post_process=post_process, draw_grid=False
37+
)
2638

2739

2840
# Create the SolaraViz page. This will automatically create a server and display the

mesa/examples/basic/schelling/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ def agent_portrayal(agent):
2828

2929
model1 = Schelling(20, 20, 0.8, 0.2, 3)
3030

31-
HappyPlot = make_plot_measure("happy")
31+
HappyPlot = make_plot_measure({"happy": "tab:green"})
3232

3333
page = SolaraViz(
3434
model1,
3535
components=[
3636
make_space_component(agent_portrayal),
37-
make_plot_measure("happy"),
37+
HappyPlot,
3838
get_happy_agents,
3939
],
4040
model_params=model_params,

mesa/examples/basic/virus_on_network/app.py

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,37 @@
11
import math
22

33
import solara
4-
from matplotlib.figure import Figure
5-
from matplotlib.ticker import MaxNLocator
64

75
from mesa.examples.basic.virus_on_network.model import (
86
State,
97
VirusOnNetwork,
108
number_infected,
119
)
12-
from mesa.visualization import Slider, SolaraViz, make_space_component
13-
10+
from mesa.visualization import (
11+
Slider,
12+
SolaraViz,
13+
make_plot_measure,
14+
make_space_component,
15+
)
1416

15-
def agent_portrayal(graph):
16-
def get_agent(node):
17-
return graph.nodes[node]["agent"][0]
1817

19-
edge_width = []
20-
edge_color = []
21-
for u, v in graph.edges():
22-
agent1 = get_agent(u)
23-
agent2 = get_agent(v)
24-
w = 2
25-
ec = "#e8e8e8"
26-
if State.RESISTANT in (agent1.state, agent2.state):
27-
w = 3
28-
ec = "black"
29-
edge_width.append(w)
30-
edge_color.append(ec)
18+
def agent_portrayal(agent):
3119
node_color_dict = {
3220
State.INFECTED: "tab:red",
3321
State.SUSCEPTIBLE: "tab:green",
3422
State.RESISTANT: "tab:gray",
3523
}
36-
node_color = [node_color_dict[get_agent(node).state] for node in graph.nodes()]
37-
return {
38-
"width": edge_width,
39-
"edge_color": edge_color,
40-
"node_color": node_color,
41-
}
24+
return {"color": node_color_dict[agent.state], "size": 10}
4225

4326

4427
def get_resistant_susceptible_ratio(model):
4528
ratio = model.resistant_susceptible_ratio()
4629
ratio_text = r"$\infty$" if ratio is math.inf else f"{ratio:.2f}"
4730
infected_text = str(number_infected(model))
4831

49-
return f"Resistant/Susceptible Ratio: {ratio_text}<br>Infected Remaining: {infected_text}"
50-
51-
52-
def make_plot(model):
53-
# This is for the case when we want to plot multiple measures in 1 figure.
54-
fig = Figure()
55-
ax = fig.subplots()
56-
measures = ["Infected", "Susceptible", "Resistant"]
57-
colors = ["tab:red", "tab:green", "tab:gray"]
58-
for i, m in enumerate(measures):
59-
color = colors[i]
60-
df = model.datacollector.get_model_vars_dataframe()
61-
ax.plot(df.loc[:, m], label=m, color=color)
62-
fig.legend()
63-
# Set integer x axis
64-
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
65-
ax.set_xlabel("Step")
66-
ax.set_ylabel("Number of Agents")
67-
return solara.FigureMatplotlib(fig)
32+
return solara.Markdown(
33+
f"Resistant/Susceptible Ratio: {ratio_text}<br>Infected Remaining: {infected_text}"
34+
)
6835

6936

7037
model_params = {
@@ -120,15 +87,18 @@ def make_plot(model):
12087
}
12188

12289
SpacePlot = make_space_component(agent_portrayal)
90+
StatePlot = make_plot_measure(
91+
{"Infected": "tab:red", "Susceptible": "tab:green", "Resistant": "tab:gray"}
92+
)
12393

12494
model1 = VirusOnNetwork()
12595

12696
page = SolaraViz(
12797
model1,
12898
[
12999
SpacePlot,
130-
make_plot,
131-
# get_resistant_susceptible_ratio, # TODO: Fix and uncomment
100+
StatePlot,
101+
get_resistant_susceptible_ratio,
132102
],
133103
model_params=model_params,
134104
name="Virus Model",

0 commit comments

Comments
 (0)