Skip to content

Commit

Permalink
gis: update three examples to use solara viz (projectmesa#226)
Browse files Browse the repository at this point in the history
This PR updates the remaining three gis examples to use Solara viz, partly addressing projectmesa#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.
  • Loading branch information
wang-boyu authored Oct 17, 2024
1 parent 2215880 commit 5655bac
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 151 deletions.
2 changes: 1 addition & 1 deletion gis/geo_schelling/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def schelling_draw(agent):
model,
[
make_geospace_leaflet(schelling_draw, zoom=4),
make_plot_happiness,
make_plot_measure(["happy"]),
make_plot_happiness,
],
model_params=model_params,
name="GeoSchelling",
Expand Down
2 changes: 1 addition & 1 deletion gis/geo_schelling_points/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def schelling_draw(agent):
model,
[
make_geospace_leaflet(schelling_draw, zoom=4),
make_plot_happiness,
make_plot_measure(["happy", "unhappy"]),
make_plot_happiness,
],
model_params=model_params,
name="GeoSchellingPoints",
Expand Down
8 changes: 4 additions & 4 deletions gis/population/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ The GeoSpace consists of both a raster and a vector layer. The raster layer cont

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.

## How to run
## How to Run

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

```bash
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 `Start`.
Then open your browser to [http://127.0.0.1:8765/](http://127.0.0.1:8765/) and press the play button ``.

## License

Expand Down
32 changes: 17 additions & 15 deletions gis/population/population/server.py → gis/population/app.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import mesa
import mesa_geo as mg
import solara
from mesa.visualization import SolaraViz
from mesa_geo.visualization import make_geospace_leaflet
from population.model import Population
from population.space import UgandaCell
from shapely.geometry import Point, Polygon

from .model import Population
from .space import UgandaCell


class NumAgentsElement(mesa.visualization.TextElement):
def __init__(self):
super().__init__()

def render(self, model):
return f"Number of Agents: {len(model.space.agents)}"
def make_plot_num_agents(model):
return solara.Markdown(f"**Number of Agents: {len(model.space.agents)}**")


def agent_portrayal(agent):
Expand All @@ -32,9 +29,14 @@ def agent_portrayal(agent):
return (agent.population, agent.population, agent.population, 1)


geospace_element = mg.visualization.MapModule(agent_portrayal)
num_agents_element = NumAgentsElement()

server = mesa.visualization.ModularServer(
Population, [geospace_element, num_agents_element], "Population Model"
model = Population()
page = SolaraViz(
model,
[
make_geospace_leaflet(agent_portrayal),
make_plot_num_agents,
],
name="Population Model",
)

page # noqa
Empty file.
2 changes: 1 addition & 1 deletion gis/population/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mesa-geo~=0.7
mesa-geo~=0.9.0a1
3 changes: 0 additions & 3 deletions gis/population/run.py

This file was deleted.

8 changes: 4 additions & 4 deletions gis/rainfall/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ The GeoSpace contains a raster layer representing elevations. It is this elevati

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.

## How to run
## How to Run

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

```bash
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 `Start`.
Then open your browser to [http://127.0.0.1:8765/](http://127.0.0.1:8765/) and press the play button ``.

## License

Expand Down
44 changes: 44 additions & 0 deletions gis/rainfall/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import Tuple

from mesa.visualization import Slider, SolaraViz, make_plot_measure
from mesa_geo.visualization import make_geospace_leaflet
from rainfall.model import Rainfall
from rainfall.space import LakeCell

model_params = {
"rain_rate": Slider("rain rate", 500, 0, 500, 5),
"water_height": Slider("water height", 5, 1, 5, 1),
"num_steps": Slider("total number of steps", 20, 1, 100, 1),
"export_data": False,
}


def cell_portrayal(cell: LakeCell) -> Tuple[float, float, float, float]:
if cell.water_level == 0:
return cell.elevation, cell.elevation, cell.elevation, 1
else:
# return a blue color gradient based on the normalized water level
# from the lowest water level colored as RGBA: (74, 141, 255, 1)
# to the highest water level colored as RGBA: (0, 0, 255, 1)
return (
(1 - cell.water_level_normalized) * 74,
(1 - cell.water_level_normalized) * 141,
255,
1,
)


model = Rainfall()
page = SolaraViz(
model,
[
make_geospace_leaflet(cell_portrayal, zoom=11),
make_plot_measure(
["Total Amount of Water", "Total Contained", "Total Outflow"]
),
],
name="Rainfall Model",
model_params=model_params,
)

page # noqa
Empty file.
47 changes: 0 additions & 47 deletions gis/rainfall/rainfall/server.py

This file was deleted.

2 changes: 1 addition & 1 deletion gis/rainfall/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mesa-geo~=0.7
mesa-geo~=0.9.0a1
3 changes: 0 additions & 3 deletions gis/rainfall/run.py

This file was deleted.

8 changes: 4 additions & 4 deletions gis/urban_growth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

This is an implementation of the [UrbanGrowth Model](https://github.com/abmgis/abmgis/tree/master/Chapter06-IntegratingABMandGIS/Models/UrbanGrowth) in Python, using [Mesa](https://github.com/projectmesa/mesa) and [Mesa-Geo](https://github.com/projectmesa/mesa-geo).

## How to run
## How to Run

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

```bash
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 `Start`.
Then open your browser to [http://127.0.0.1:8765/](http://127.0.0.1:8765/) and press the play button ``.

## License

Expand Down
47 changes: 47 additions & 0 deletions gis/urban_growth/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Tuple

import solara
from mesa.visualization import Slider, SolaraViz, make_plot_measure
from mesa_geo.visualization import make_geospace_leaflet
from urban_growth.model import UrbanGrowth
from urban_growth.space import UrbanCell


def cell_portrayal(cell: UrbanCell) -> Tuple[float, float, float, float]:
if cell.urban:
if cell.old_urbanized:
return 0, 0, 255, 1
else:
return 255, 0, 0, 1
else:
return 0, 0, 0, 0


def make_plot_urbanized(model):
return solara.Markdown(f"**Percentage Urbanized: {model.pct_urbanized:.2f}%**")


model_params = {
"max_coefficient": 100,
"dispersion_coefficient": Slider("dispersion_coefficient", 20, 0, 100, 1),
"spread_coefficient": Slider("spread_coefficient", 27, 0, 100, 1),
"breed_coefficient": Slider("breed_coefficient", 5, 0, 100, 1),
"rg_coefficient": Slider("rg_coefficient", 10, 0, 100, 1),
"slope_coefficient": Slider("slope_coefficient", 50, 0, 100, 1),
"critical_slope": Slider("critical_slope", 25, 0, 100, 1),
"road_influence": False,
}

model = UrbanGrowth()
page = SolaraViz(
model,
[
make_geospace_leaflet(cell_portrayal, zoom=12.1),
make_plot_measure(["Percentage Urbanized"]),
make_plot_urbanized,
],
name="Urban Growth Model",
model_params=model_params,
)

page # noqa
2 changes: 1 addition & 1 deletion gis/urban_growth/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mesa-geo~=0.7
mesa-geo~=0.9.0a1
3 changes: 0 additions & 3 deletions gis/urban_growth/run.py

This file was deleted.

Empty file.
63 changes: 0 additions & 63 deletions gis/urban_growth/urban_growth/server.py

This file was deleted.

0 comments on commit 5655bac

Please sign in to comment.