Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 4 additions & 6 deletions examples/color_patches/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ An agent's state represents its "opinion" and is shown by the color of the cell

## How to Run

To run the model interactively, run ``mesa runserver` in this directory. e.g.
To run the model interactively, run Solara in this directory. e.g.

```
$ mesa runserver
$ solara run app.py
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Then open your browser to [http://localhost:8765/](http://localhost:8765/) and press Reset, then Run.

## Files

* ``color_patches/model.py``: Defines the cell and model classes. The cell class governs each cell's behavior. The model class itself controls the lattice on which the cells live and interact.
* ``color_patches/server.py``: Defines an interactive visualization.
* ``run.py``: Launches an interactive visualization
* ``app.py``: Launches an interactive SolaraViz visualization.

## Further Reading

Expand Down
32 changes: 24 additions & 8 deletions examples/color_patches/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
SolaraViz,
make_space_component,
)
from mesa.visualization.user_param import (
Slider,
)

_COLORS = [
"Aqua",
Expand Down Expand Up @@ -45,23 +48,36 @@ def color_patch_draw(cell):
:return: the portrayal dictionary.
"""
if cell is None:
raise AssertionError
portrayal = {"Shape": "rect", "w": 1, "h": 1, "Filled": "true", "Layer": 0}
portrayal["x"] = cell.get_row()
portrayal["y"] = cell.get_col()
portrayal["color"] = _COLORS[cell.state]
return portrayal
return
return {
"color": _COLORS[cell.state],
"size": 100,
}


space_component = make_space_component(
color_patch_draw,
draw_grid=False,
)
Comment on lines +52 to 61
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we not using the lastest visualization API here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Sahil-Chhoker, thanks for the review! Since this PR was originally just focused on fixing the missing height/width parameters in the interface and cleaning up the outdated comments as requested by @tpike3, I kept the existing dictionary structure to keep the scope of these changes narrow.
While looking into this, I noticed that several other examples in the repository also still need to be updated to the latest AgentPortrayalStyle visualization API.
Would it be acceptable to get these immediate fixes merged in first so this example runs correctly for users? If so, I'll gladly open a new issue to track the API migration across the whole repository, assign it to myself, and submit a separate PR to update all the examples at once. I figure this will keep the commit history clean and tackle the deprecation globally.
Let me know if this works for you!

model = ColorPatches()
model_params = {
"width": Slider(
"Grid Width",
grid_rows,
5,
100,
),
"height": Slider(
"Grid Height",
grid_cols,
5,
100,
),
}

page = SolaraViz(
model,
components=[space_component],
model_params={"width": grid_rows, "height": grid_cols},
model_params=model_params,
name="Color Patches",
)
# webbrowser.open('http://127.0.0.1:8521') # TODO: make this configurable
18 changes: 1 addition & 17 deletions examples/color_patches/color_patches/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ def __init__(self, width=20, height=20):
self._grid = OrthogonalMooreGrid(
(width, height), torus=False, random=self.random
)

# self._grid.coord_iter()
# --> should really not return content + col + row
# -->but only col & row
# for (contents, col, row) in self._grid.coord_iter():
# replaced content with _ to appease linter
for cell in self._grid.all_cells:
agent = ColorCell(self, ColorCell.OPINIONS[self.random.randrange(0, 16)])
agent.move_to(cell)
Expand All @@ -98,15 +92,5 @@ def step(self):

@property
def grid(self):
"""
/mesa/visualization/modules/CanvasGridVisualization.py
is directly accessing Model.grid
76 def render(self, model):
77 grid_state = defaultdict(list)
---> 78 for y in range(model.grid.height):
79 for x in range(model.grid.width):
80 cell_objects = model.grid.get_cell_list_contents([(x, y)])

AttributeError: 'ColorPatches' object has no attribute 'grid'
"""
"""Provide backward compatibility for accessing the grid."""
return self._grid
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason for creating a separate grid property to access the _grid attribute of the model instead of just renaming _grid to grid?

I personally think latter one is more cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Sahil-Chhoker, great catch! And that's much cleaner. I looked at the docstring and saw it was just a lingering workaround for CanvasGridVisualization backward compatibility. But now we're moving this to SolaraViz anyway, I've gone ahead and removed that legacy @Property block entirely and renamed self._grid to self.grid directly in model.py.
I just pushed the commit.
Let me know if everything looks good to go now! Thanks again for the review.

Loading