Skip to content

Commit 5655bac

Browse files
authored
gis: update three examples to use solara viz (#226)
This PR updates the remaining three gis examples to use Solara viz, partly addressing #154. It depends on projectmesa/mesa-geo#254 to be released, as these examples use raster layers. Also made some small adjustments in ordering components to account for the grid layout, instead of the previous column layout in Solara viz.
1 parent 2215880 commit 5655bac

File tree

19 files changed

+125
-151
lines changed

19 files changed

+125
-151
lines changed

gis/geo_schelling/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def schelling_draw(agent):
3434
model,
3535
[
3636
make_geospace_leaflet(schelling_draw, zoom=4),
37-
make_plot_happiness,
3837
make_plot_measure(["happy"]),
38+
make_plot_happiness,
3939
],
4040
model_params=model_params,
4141
name="GeoSchelling",

gis/geo_schelling_points/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def schelling_draw(agent):
3636
model,
3737
[
3838
make_geospace_leaflet(schelling_draw, zoom=4),
39-
make_plot_happiness,
4039
make_plot_measure(["happy", "unhappy"]),
40+
make_plot_happiness,
4141
],
4242
model_params=model_params,
4343
name="GeoSchellingPoints",

gis/population/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ The GeoSpace consists of both a raster and a vector layer. The raster layer cont
1414

1515
The GeoAgents are people, created based on the population data. As this is a simple example model, the agents only move randomly to neighboring cells at each time step. To make the simulation more realistic and visually appealing, the agents in the same cell have a randomized position within the cell, so that they don’t stand on top of each other at exactly the same coordinate.
1616

17-
## How to run
17+
## How to Run
1818

19-
To run the model interactively, run `mesa runserver` in this directory. e.g.
19+
To run the model interactively, run `solara run app.py` in this directory. e.g.
2020

2121
```bash
22-
mesa runserver
22+
solara run app.py
2323
```
2424

25-
Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press `Start`.
25+
Then open your browser to [http://127.0.0.1:8765/](http://127.0.0.1:8765/) and press the play button ``.
2626

2727
## License
2828

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import mesa
21
import mesa_geo as mg
2+
import solara
3+
from mesa.visualization import SolaraViz
4+
from mesa_geo.visualization import make_geospace_leaflet
5+
from population.model import Population
6+
from population.space import UgandaCell
37
from shapely.geometry import Point, Polygon
48

5-
from .model import Population
6-
from .space import UgandaCell
79

8-
9-
class NumAgentsElement(mesa.visualization.TextElement):
10-
def __init__(self):
11-
super().__init__()
12-
13-
def render(self, model):
14-
return f"Number of Agents: {len(model.space.agents)}"
10+
def make_plot_num_agents(model):
11+
return solara.Markdown(f"**Number of Agents: {len(model.space.agents)}**")
1512

1613

1714
def agent_portrayal(agent):
@@ -32,9 +29,14 @@ def agent_portrayal(agent):
3229
return (agent.population, agent.population, agent.population, 1)
3330

3431

35-
geospace_element = mg.visualization.MapModule(agent_portrayal)
36-
num_agents_element = NumAgentsElement()
37-
38-
server = mesa.visualization.ModularServer(
39-
Population, [geospace_element, num_agents_element], "Population Model"
32+
model = Population()
33+
page = SolaraViz(
34+
model,
35+
[
36+
make_geospace_leaflet(agent_portrayal),
37+
make_plot_num_agents,
38+
],
39+
name="Population Model",
4040
)
41+
42+
page # noqa

gis/population/population/__init__.py

Whitespace-only changes.

gis/population/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mesa-geo~=0.7
1+
mesa-geo~=0.9.0a1

gis/population/run.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

gis/rainfall/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ The GeoSpace contains a raster layer representing elevations. It is this elevati
1414

1515
In this example, the raindrops are the GeoAgents. At each time step, raindrops are randomly created across the landscape to simulate rainfall. The raindrops flow from cells of higher elevation to lower elevation based on their eight surrounding cells (i.e., Moore neighbourhood). The raindrop also has its own height, which allows them to accumulate, gain height and flow if they are trapped at places such as potholes, pools, or depressions. When they reach the boundary of the GeoSpace, they are removed from the model as outflow.
1616

17-
## How to run
17+
## How to Run
1818

19-
To run the model interactively, run `mesa runserver` in this directory. e.g.
19+
To run the model interactively, run `solara run app.py` in this directory. e.g.
2020

2121
```bash
22-
mesa runserver
22+
solara run app.py
2323
```
2424

25-
Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press `Start`.
25+
Then open your browser to [http://127.0.0.1:8765/](http://127.0.0.1:8765/) and press the play button ``.
2626

2727
## License
2828

gis/rainfall/app.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import Tuple
2+
3+
from mesa.visualization import Slider, SolaraViz, make_plot_measure
4+
from mesa_geo.visualization import make_geospace_leaflet
5+
from rainfall.model import Rainfall
6+
from rainfall.space import LakeCell
7+
8+
model_params = {
9+
"rain_rate": Slider("rain rate", 500, 0, 500, 5),
10+
"water_height": Slider("water height", 5, 1, 5, 1),
11+
"num_steps": Slider("total number of steps", 20, 1, 100, 1),
12+
"export_data": False,
13+
}
14+
15+
16+
def cell_portrayal(cell: LakeCell) -> Tuple[float, float, float, float]:
17+
if cell.water_level == 0:
18+
return cell.elevation, cell.elevation, cell.elevation, 1
19+
else:
20+
# return a blue color gradient based on the normalized water level
21+
# from the lowest water level colored as RGBA: (74, 141, 255, 1)
22+
# to the highest water level colored as RGBA: (0, 0, 255, 1)
23+
return (
24+
(1 - cell.water_level_normalized) * 74,
25+
(1 - cell.water_level_normalized) * 141,
26+
255,
27+
1,
28+
)
29+
30+
31+
model = Rainfall()
32+
page = SolaraViz(
33+
model,
34+
[
35+
make_geospace_leaflet(cell_portrayal, zoom=11),
36+
make_plot_measure(
37+
["Total Amount of Water", "Total Contained", "Total Outflow"]
38+
),
39+
],
40+
name="Rainfall Model",
41+
model_params=model_params,
42+
)
43+
44+
page # noqa

gis/rainfall/rainfall/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)