Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples to use updated space drawing #2442

Merged
merged 7 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions mesa/examples/advanced/epstein_civil_violence/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
return

portrayal = {
"size": 25,
"size": 50,
}

if isinstance(agent, Citizen):
Expand All @@ -36,6 +36,13 @@
return portrayal


def post_process(ax):
ax.set_aspect("equal")
ax.set_xticks([])
ax.set_yticks([])
ax.get_figure().set_size_inches(10, 10)

Check warning on line 43 in mesa/examples/advanced/epstein_civil_violence/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/epstein_civil_violence/app.py#L39-L43

Added lines #L39 - L43 were not covered by tests
EwoutH marked this conversation as resolved.
Show resolved Hide resolved


model_params = {
"height": 40,
"width": 40,
Expand All @@ -47,8 +54,13 @@
"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(

Check warning on line 57 in mesa/examples/advanced/epstein_civil_violence/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/epstein_civil_violence/app.py#L57

Added line #L57 was not covered by tests
citizen_cop_portrayal, post_process=post_process, draw_grid=False
)

chart_component = make_plot_measure(

Check warning on line 61 in mesa/examples/advanced/epstein_civil_violence/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/epstein_civil_violence/app.py#L61

Added line #L61 was not covered by tests
{state.name.lower(): agent_colors[state] for state in CitizenState}
)

epstein_model = EpsteinCivilViolence()

Expand Down
2 changes: 1 addition & 1 deletion mesa/examples/advanced/pd_grid/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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,
}

Expand Down
6 changes: 3 additions & 3 deletions mesa/examples/advanced/sugarscape_g1mt/app.py
Original file line number Diff line number Diff line change
@@ -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.agents import Trader
from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt

Check warning on line 6 in mesa/examples/advanced/sugarscape_g1mt/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/app.py#L5-L6

Added lines #L5 - L6 were not covered by tests
from mesa.visualization import SolaraViz, make_plot_measure


Expand Down Expand Up @@ -57,6 +57,6 @@
model1,
components=[SpaceDrawer, make_plot_measure(["Trader", "Price"])],
name="Sugarscape {G1, M, T}",
play_interval=1500,
play_interval=150,
)
page # noqa
16 changes: 14 additions & 2 deletions mesa/examples/basic/conways_game_of_life/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@


def agent_portrayal(agent):
return {"c": "white" if agent.state == 0 else "black", "marker": "s"}
return {

Check warning on line 9 in mesa/examples/basic/conways_game_of_life/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/conways_game_of_life/app.py#L9

Added line #L9 was not covered by tests
"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([])

Check warning on line 19 in mesa/examples/basic/conways_game_of_life/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/conways_game_of_life/app.py#L16-L19

Added lines #L16 - L19 were not covered by tests


model_params = {
Expand All @@ -22,7 +32,9 @@
# 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(

Check warning on line 35 in mesa/examples/basic/conways_game_of_life/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/conways_game_of_life/app.py#L35

Added line #L35 was not covered by tests
agent_portrayal, post_process=post_process, draw_grid=False
)


# Create the SolaraViz page. This will automatically create a server and display the
Expand Down
4 changes: 2 additions & 2 deletions mesa/examples/basic/schelling/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

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

HappyPlot = make_plot_measure("happy")
HappyPlot = make_plot_measure({"happy": "tab:green"})

Check warning on line 31 in mesa/examples/basic/schelling/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/schelling/app.py#L31

Added line #L31 was not covered by tests

page = SolaraViz(
model1,
components=[
make_space_component(agent_portrayal),
make_plot_measure("happy"),
HappyPlot,
get_happy_agents,
],
model_params=model_params,
Expand Down
62 changes: 16 additions & 46 deletions mesa/examples/basic/virus_on_network/app.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,37 @@
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 (

Check warning on line 10 in mesa/examples/basic/virus_on_network/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/virus_on_network/app.py#L10

Added line #L10 was not covered by tests
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):

Check warning on line 18 in mesa/examples/basic/virus_on_network/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/virus_on_network/app.py#L18

Added line #L18 was not covered by tests
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}

Check warning on line 24 in mesa/examples/basic/virus_on_network/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/virus_on_network/app.py#L24

Added line #L24 was not covered by tests


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}<br>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(

Check warning on line 32 in mesa/examples/basic/virus_on_network/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/virus_on_network/app.py#L32

Added line #L32 was not covered by tests
f"Resistant/Susceptible Ratio: {ratio_text}<br>Infected Remaining: {infected_text}"
)


model_params = {
Expand Down Expand Up @@ -120,15 +87,18 @@
}

SpacePlot = make_space_component(agent_portrayal)
StatePlot = make_plot_measure(

Check warning on line 90 in mesa/examples/basic/virus_on_network/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/virus_on_network/app.py#L90

Added line #L90 was not covered by tests
{"Infected": "tab:red", "Susceptible": "tab:green", "Resistant": "tab:gray"}
)

model1 = VirusOnNetwork()

page = SolaraViz(
model1,
[
SpacePlot,
make_plot,
# get_resistant_susceptible_ratio, # TODO: Fix and uncomment
StatePlot,
get_resistant_susceptible_ratio,
],
model_params=model_params,
name="Virus Model",
Expand Down
Loading