From 6abc9e4521e4420acc449279757f34e5fe5a63dc Mon Sep 17 00:00:00 2001 From: disgruntled-penguin Date: Sun, 14 Dec 2025 16:35:48 +0530 Subject: [PATCH 1/3] fix: solarViz attributeError with component arg --- gis/urban_growth/app.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gis/urban_growth/app.py b/gis/urban_growth/app.py index 68a4bc97..337e15ef 100644 --- a/gis/urban_growth/app.py +++ b/gis/urban_growth/app.py @@ -31,10 +31,13 @@ def make_plot_urbanized(model): } model = UrbanGrowth() +map_component = make_geospace_component(cell_portrayal, zoom=12.1) + +# explicitly pass components= page = SolaraViz( model, - [ - make_geospace_component(cell_portrayal, zoom=12.1), + components=[ + map_component, make_plot_component(["Percentage Urbanized"]), make_plot_urbanized, ], From b87827386b58071ec7f525b2a349fa210efc6a8f Mon Sep 17 00:00:00 2001 From: disgruntled-penguin Date: Sun, 14 Dec 2025 16:58:11 +0530 Subject: [PATCH 2/3] fix:conways rendering --- examples/conways_game_of_life_fast/app.py | 22 ++++++++++++++++++--- examples/conways_game_of_life_fast/model.py | 21 +++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/examples/conways_game_of_life_fast/app.py b/examples/conways_game_of_life_fast/app.py index 9a1281ac..5549b614 100644 --- a/examples/conways_game_of_life_fast/app.py +++ b/examples/conways_game_of_life_fast/app.py @@ -1,10 +1,23 @@ from mesa.visualization import SolaraViz, make_plot_component, make_space_component from model import GameOfLifeModel +def cell_portrayal(agent): + if agent is None: return + + # + x, y = agent.pos + is_alive = agent.model.cell_layer.data[x, y] + + return { + # If alive Black, If dead White + "color": "black" if is_alive else "white", + "size": 50, # 50 fills the cell completely + "marker": "s", #square shape + } propertylayer_portrayal = { "cell_layer": { - "color": "Black", - "alpha": 1, + "color": "Cyan", + "alpha": "cell_layer", "colorbar": False, }, } @@ -37,8 +50,11 @@ } gol = GameOfLifeModel() +layer_viz = make_space_component( + agent_portrayal=cell_portrayal, + draw_grid=False +) -layer_viz = make_space_component(propertylayer_portrayal=propertylayer_portrayal) TotalAlivePlot = make_plot_component("Cells alive") page = SolaraViz( diff --git a/examples/conways_game_of_life_fast/model.py b/examples/conways_game_of_life_fast/model.py index 40499b64..cbaa8e95 100644 --- a/examples/conways_game_of_life_fast/model.py +++ b/examples/conways_game_of_life_fast/model.py @@ -1,7 +1,7 @@ import numpy as np -from mesa import Model +from mesa import Model, Agent from mesa.datacollection import DataCollector -from mesa.space import PropertyLayer +from mesa.space import PropertyLayer, SingleGrid from scipy.signal import convolve2d @@ -10,9 +10,20 @@ class GameOfLifeModel(Model): def __init__(self, width=10, height=10, alive_fraction=0.2): super().__init__() # Initialize the property layer for cell states - self.cell_layer = PropertyLayer("cells", width, height, False, dtype=bool) - # Randomly set cells to alive - self.cell_layer.data = np.random.choice([True, False], size=(width, height), p=[alive_fraction, 1 - alive_fraction]) + + self.grid = SingleGrid(width, height, torus=True) + + + self.cell_layer = PropertyLayer("cell_layer", width, height, False, dtype=bool) + self.cell_layer.data = np.random.choice( + [True, False], + size=(width, height), + p=[alive_fraction, 1 - alive_fraction] + ) + + for _, (x, y) in self.grid.coord_iter(): + dummy = Agent(self) + self.grid.place_agent(dummy, (x, y)) # Metrics and datacollector self.cells = width * height From af8d2c0b4ec087d9cb07d8acdfbe47fddab7ac3e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 14 Dec 2025 11:28:26 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/conways_game_of_life_fast/app.py | 23 +++++++++++---------- examples/conways_game_of_life_fast/model.py | 10 ++++----- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/examples/conways_game_of_life_fast/app.py b/examples/conways_game_of_life_fast/app.py index 5549b614..4a077097 100644 --- a/examples/conways_game_of_life_fast/app.py +++ b/examples/conways_game_of_life_fast/app.py @@ -1,19 +1,23 @@ from mesa.visualization import SolaraViz, make_plot_component, make_space_component from model import GameOfLifeModel + def cell_portrayal(agent): - if agent is None: return - - # + if agent is None: + return + + # x, y = agent.pos is_alive = agent.model.cell_layer.data[x, y] - + return { - # If alive Black, If dead White + # If alive Black, If dead White "color": "black" if is_alive else "white", - "size": 50, # 50 fills the cell completely - "marker": "s", #square shape + "size": 50, # 50 fills the cell completely + "marker": "s", # square shape } + + propertylayer_portrayal = { "cell_layer": { "color": "Cyan", @@ -50,10 +54,7 @@ def cell_portrayal(agent): } gol = GameOfLifeModel() -layer_viz = make_space_component( - agent_portrayal=cell_portrayal, - draw_grid=False -) +layer_viz = make_space_component(agent_portrayal=cell_portrayal, draw_grid=False) TotalAlivePlot = make_plot_component("Cells alive") diff --git a/examples/conways_game_of_life_fast/model.py b/examples/conways_game_of_life_fast/model.py index cbaa8e95..ff8b0efb 100644 --- a/examples/conways_game_of_life_fast/model.py +++ b/examples/conways_game_of_life_fast/model.py @@ -1,5 +1,5 @@ import numpy as np -from mesa import Model, Agent +from mesa import Agent, Model from mesa.datacollection import DataCollector from mesa.space import PropertyLayer, SingleGrid from scipy.signal import convolve2d @@ -10,19 +10,19 @@ class GameOfLifeModel(Model): def __init__(self, width=10, height=10, alive_fraction=0.2): super().__init__() # Initialize the property layer for cell states - + self.grid = SingleGrid(width, height, torus=True) self.cell_layer = PropertyLayer("cell_layer", width, height, False, dtype=bool) self.cell_layer.data = np.random.choice( - [True, False], - size=(width, height), + [True, False], + size=(width, height), p=[alive_fraction, 1 - alive_fraction] ) for _, (x, y) in self.grid.coord_iter(): - dummy = Agent(self) + dummy = Agent(self) self.grid.place_agent(dummy, (x, y)) # Metrics and datacollector