Skip to content

Commit 9c30387

Browse files
authored
update examples to use new SolaraViz API (#193)
1 parent 2b43544 commit 9c30387

File tree

8 files changed

+64
-47
lines changed

8 files changed

+64
-47
lines changed

examples/aco_tsp/app.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import solara
77
from aco_tsp.model import AcoTspModel, TSPGraph
88
from matplotlib.figure import Figure
9-
from mesa.visualization import SolaraViz
9+
from mesa.visualization import SolaraViz, make_plot_measure
1010

1111

1212
def circle_portrayal_example(agent):
@@ -35,6 +35,8 @@ def circle_portrayal_example(agent):
3535
},
3636
}
3737

38+
model = AcoTspModel()
39+
3840

3941
def make_graph(model):
4042
fig = Figure()
@@ -55,7 +57,7 @@ def make_graph(model):
5557
edge_color="gray",
5658
)
5759

58-
solara.FigureMatplotlib(fig)
60+
return solara.FigureMatplotlib(fig)
5961

6062

6163
def ant_level_distances(model):
@@ -67,10 +69,8 @@ def ant_level_distances(model):
6769

6870

6971
page = SolaraViz(
70-
AcoTspModel,
71-
model_params,
72-
space_drawer=None,
73-
measures=["best_distance_iter", "best_distance", make_graph],
74-
agent_portrayal=circle_portrayal_example,
72+
model,
73+
components=[make_plot_measure(["best_distance_iter", "best_distance"]), make_graph],
74+
model_params=model_params,
7575
play_interval=1,
7676
)

examples/boid_flockers/app.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from boid_flockers.model import BoidFlockers
2-
from mesa.visualization import SolaraViz
2+
from mesa.visualization import SolaraViz, make_space_matplotlib
33

44

55
def boid_draw(agent):
@@ -15,11 +15,12 @@ def boid_draw(agent):
1515
"separation": 2,
1616
}
1717

18+
model = BoidFlockers(100, 100, 100, 5, 10, 2)
19+
1820
page = SolaraViz(
19-
model_class=BoidFlockers,
21+
model,
22+
[make_space_matplotlib(agent_portrayal=boid_draw)],
2023
model_params=model_params,
21-
measures=[],
2224
name="BoidFlockers",
23-
agent_portrayal=boid_draw,
2425
)
2526
page # noqa

examples/boltzmann_wealth_model_experimental/app.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from mesa.visualization import SolaraViz, make_plot_measure, make_space_matplotlib
1+
from mesa.visualization import (
2+
SolaraViz,
3+
make_plot_measure,
4+
make_space_matplotlib,
5+
)
26
from model import BoltzmannWealthModel
37

48

@@ -29,9 +33,9 @@ def agent_portrayal(agent):
2933

3034
# Create visualization elements. The visualization elements are solara components
3135
# that receive the model instance as a "prop" and display it in a certain way.
32-
# Under the hood these are just functions that receive the model instance.
33-
# You can also author your own visualization elements, they just have to return
34-
# a valid solara component or an ipywidget.
36+
# Under the hood these are just classes that receive the model instance.
37+
# You can also author your own visualization elements, which can also be functions
38+
# that receive the model instance and return a valid solara component.
3539
SpaceGraph = make_space_matplotlib(agent_portrayal)
3640
GiniPlot = make_plot_measure("Gini")
3741

examples/conways_game_of_life_fast/app.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mesa.visualization import SolaraViz
1+
from mesa.visualization import SolaraViz, make_plot_measure
22
from model import GameOfLifeModel
33

44
model_params = {
@@ -20,11 +20,16 @@
2020
},
2121
}
2222

23+
gol = GameOfLifeModel(10, 10)
24+
25+
TotalAlivePlot = make_plot_measure("Cells alive")
26+
FractionAlivePlot = make_plot_measure("Fraction alive")
27+
28+
2329
page = SolaraViz(
24-
GameOfLifeModel,
25-
model_params,
26-
measures=["Cells alive", "Fraction alive"],
27-
space_drawer=None,
30+
gol,
31+
components=[TotalAlivePlot, FractionAlivePlot],
32+
model_params=model_params,
2833
name="Game of Life Model",
2934
)
3035
page # noqa

examples/hotelling_law/app.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from hotelling_law.agents import ConsumerAgent, StoreAgent
66
from hotelling_law.model import HotellingModel
77
from matplotlib.figure import Figure
8-
from mesa.visualization import SolaraViz
8+
from mesa.visualization import SolaraViz, make_plot_measure
99

1010
model_params = {
1111
"N_stores": {
@@ -108,7 +108,8 @@ def agent_portrayal(agent):
108108
return portrayal
109109

110110

111-
def space_drawer(model, agent_portrayal):
111+
@solara.component
112+
def SpaceDrawer(model):
112113
fig = Figure(figsize=(8, 5), dpi=100)
113114
ax = fig.subplots()
114115

@@ -338,20 +339,20 @@ def make_revenue_line_chart(model):
338339
return solara.FigureMatplotlib(fig)
339340

340341

342+
model1 = HotellingModel(20, 20)
343+
341344
# Instantiate the SolaraViz component with your model
342345
page = SolaraViz(
343-
model_class=HotellingModel,
344-
model_params=model_params,
345-
measures=[
346+
model1,
347+
components=[
348+
SpaceDrawer,
346349
make_price_changes_line_chart,
347350
make_market_share_and_price_chart,
348351
make_market_share_line_chart,
349-
"Price Variance",
352+
make_plot_measure("Price Variance"),
350353
make_revenue_line_chart,
351354
],
352355
name="Hotelling's Law Model",
353-
agent_portrayal=agent_portrayal,
354-
space_drawer=space_drawer,
355356
play_interval=150,
356357
)
357358

examples/schelling_experimental/app.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mesa.visualization.solara_viz import Slider, SolaraViz, make_text
1+
from mesa.visualization import Slider, SolaraViz, make_plot_measure
22
from model import Schelling
33

44

@@ -21,10 +21,13 @@ def agent_portrayal(agent):
2121
"height": 20,
2222
}
2323

24+
model1 = Schelling(20, 20, 0.8, 0.2, 3)
25+
26+
HappyPlot = make_plot_measure("happy")
27+
2428
page = SolaraViz(
25-
Schelling,
26-
model_params,
27-
measures=["happy", make_text(get_happy_agents)],
28-
agent_portrayal=agent_portrayal,
29+
model1,
30+
components=[HappyPlot, get_happy_agents],
31+
model_params=model_params,
2932
)
3033
page # noqa

examples/sugarscape_g1mt/app.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import numpy as np
22
import solara
33
from matplotlib.figure import Figure
4-
from mesa.visualization import SolaraViz
4+
from mesa.visualization import SolaraViz, make_plot_measure
55
from sugarscape_g1mt.model import SugarscapeG1mt
66
from sugarscape_g1mt.trader_agents import Trader
77

88

9-
def space_drawer(model, agent_portrayal):
9+
def SpaceDrawer(model):
1010
def portray(g):
1111
layers = {
1212
"sugar": [[np.nan for j in range(g.height)] for i in range(g.width)],
@@ -42,20 +42,20 @@ def portray(g):
4242
# Trader
4343
ax.scatter(**out["trader"])
4444
ax.set_axis_off()
45-
solara.FigureMatplotlib(fig)
45+
return solara.FigureMatplotlib(fig)
4646

4747

4848
model_params = {
4949
"width": 50,
5050
"height": 50,
5151
}
5252

53+
model1 = SugarscapeG1mt(50, 50)
54+
5355
page = SolaraViz(
54-
SugarscapeG1mt,
55-
model_params,
56-
measures=["Trader", "Price"],
56+
model1,
57+
components=[SpaceDrawer, make_plot_measure(["Trader", "Price"])],
5758
name="Sugarscape {G1, M, T}",
58-
space_drawer=space_drawer,
5959
play_interval=1500,
6060
)
6161
page # noqa

examples/virus_on_network/app.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import solara
44
from matplotlib.figure import Figure
55
from matplotlib.ticker import MaxNLocator
6-
from mesa.visualization import SolaraViz, make_text
6+
from mesa.visualization import SolaraViz, make_space_matplotlib
77
from virus_on_network.model import State, VirusOnNetwork, number_infected
88

99

@@ -57,7 +57,7 @@ def make_plot(model):
5757
fig.legend()
5858
# Set integer x axis
5959
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
60-
solara.FigureMatplotlib(fig)
60+
return solara.FigureMatplotlib(fig)
6161

6262

6363
model_params = {
@@ -119,14 +119,17 @@ def make_plot(model):
119119
},
120120
}
121121

122+
SpacePlot = make_space_matplotlib(agent_portrayal)
123+
124+
model1 = VirusOnNetwork()
125+
122126
page = SolaraViz(
123-
VirusOnNetwork,
124-
model_params,
125-
measures=[
127+
model1,
128+
[
129+
SpacePlot,
126130
make_plot,
127-
make_text(get_resistant_susceptible_ratio),
131+
get_resistant_susceptible_ratio,
128132
],
129133
name="Virus Model",
130-
agent_portrayal=agent_portrayal,
131134
)
132135
page # noqa

0 commit comments

Comments
 (0)