Skip to content

Commit

Permalink
gis: update four examples to use solara viz
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-boyu committed Sep 28, 2024
1 parent 84542d4 commit 707cd0b
Show file tree
Hide file tree
Showing 26 changed files with 278 additions and 323 deletions.
4 changes: 2 additions & 2 deletions gis/agents_and_networks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ python3 -m pip install -r requirements.txt
Then run the model:

```bash
python3 scripts/run.py --campus ub
solara run app.py -- --campus ub
```

Change `ub` to `gmu` for a different campus map.

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
66 changes: 66 additions & 0 deletions gis/agents_and_networks/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import sys

from mesa.visualization import Slider, SolaraViz, make_plot_measure
from mesa_geo.visualization import make_geospace_leaflet
from src.model.model import AgentsAndNetworks
from src.visualization.utils import agent_draw, make_plot_clock


def parse_args():
campus = "ub"
if "--campus" in sys.argv:
campus = sys.argv[sys.argv.index("--campus") + 1]
return campus


if __name__ == "__main__":
campus = parse_args()

if campus == "ub":
data_file_prefix = "UB"
elif campus == "gmu":
data_file_prefix = "Mason"
else:
raise ValueError("Invalid campus name. Choose from ub or gmu.")

campus_params = {
"ub": {"data_crs": "epsg:4326", "commuter_speed": 0.5, "zoom": 14},
"gmu": {"data_crs": "epsg:2283", "commuter_speed": 0.4, "zoom": 16},
}
model_params = {
"campus": campus,
"data_crs": campus_params[campus]["data_crs"],
"buildings_file": f"data/{campus}/{data_file_prefix}_bld.zip",
"walkway_file": f"data/{campus}/{data_file_prefix}_walkway_line.zip",
"lakes_file": f"data/{campus}/hydrop.zip",
"rivers_file": f"data/{campus}/hydrol.zip",
"driveway_file": f"data/{campus}/{data_file_prefix}_Rds.zip",
"output_dir": "outputs",
"show_walkway": True,
"show_lakes_and_rivers": True,
"show_driveway": True,
"num_commuters": Slider(
"Number of Commuters", value=50, min=10, max=150, step=10
),
"commuter_speed": Slider(
"Commuter Walking Speed (m/s)",
value=campus_params[campus]["commuter_speed"],
min=0.1,
max=1.5,
step=0.1,
),
}
model = AgentsAndNetworks()
page = SolaraViz(
model,
[
make_geospace_leaflet(agent_draw, zoom=campus_params[campus]["zoom"]),
make_plot_clock,
make_plot_measure(["status_home", "status_work", "status_traveling"]),
make_plot_measure(["friendship_home", "friendship_work"]),
],
name="Agents and Networks",
model_params=model_params,
)

page # noqa
2 changes: 1 addition & 1 deletion gis/agents_and_networks/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-e .

# external requirements
mesa-geo~=0.7
mesa-geo~=0.9.0a0
geopandas
numpy
pandas
Expand Down
64 changes: 0 additions & 64 deletions gis/agents_and_networks/scripts/run.py

This file was deleted.

64 changes: 0 additions & 64 deletions gis/agents_and_networks/src/visualization/server.py

This file was deleted.

42 changes: 42 additions & 0 deletions gis/agents_and_networks/src/visualization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,48 @@
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import solara

from ..agent.building import Building
from ..agent.commuter import Commuter
from ..agent.geo_agents import Driveway, LakeAndRiver, Walkway


def make_plot_clock(model):
return solara.Markdown(f"**Day {model.day}, {model.hour:02d}:{model.minute:02d}**")


def agent_draw(agent):
portrayal = {}
portrayal["color"] = "White"
if isinstance(agent, Driveway):
portrayal["color"] = "#D08004"
elif isinstance(agent, Walkway):
portrayal["color"] = "Brown"
elif isinstance(agent, LakeAndRiver):
portrayal["color"] = "#04D0CD"
elif isinstance(agent, Building):
portrayal["color"] = "Grey"
# if agent.function is None:
# portrayal["color"] = "Grey"
# elif agent.function == 1.0:
# portrayal["color"] = "Blue"
# elif agent.function == 2.0:
# portrayal["color"] = "Green"
# else:
# portrayal["color"] = "Grey"
elif isinstance(agent, Commuter):
if agent.status == "home":
portrayal["color"] = "Green"
elif agent.status == "work":
portrayal["color"] = "Blue"
elif agent.status == "transport":
portrayal["color"] = "Red"
else:
portrayal["color"] = "Grey"
portrayal["radius"] = "5"
portrayal["fillOpacity"] = 1
return portrayal


def plot_commuter_status_count(model_vars_df: pd.DataFrame) -> None:
Expand Down
6 changes: 3 additions & 3 deletions gis/geo_schelling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ NUTS-2 regions are the GeoAgents. The neighbors of a polygon are considered thos

## 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 ``.
44 changes: 44 additions & 0 deletions gis/geo_schelling/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import solara
from mesa.visualization import Slider, SolaraViz, make_plot_measure
from mesa_geo.visualization import make_geospace_leaflet
from model import GeoSchelling


def make_plot_happiness(model):
return solara.Markdown(f"**Happy agents: {model.happy}**")


model_params = {
"density": Slider("Agent density", 0.6, 0.1, 1.0, 0.1),
"minority_pc": Slider("Fraction minority", 0.2, 0.00, 1.0, 0.05),
"export_data": False,
}


def schelling_draw(agent):
"""
Portrayal Method for canvas
"""
portrayal = {}
if agent.atype is None:
portrayal["color"] = "Grey"
elif agent.atype == 0:
portrayal["color"] = "Red"
else:
portrayal["color"] = "Blue"
return portrayal


model = GeoSchelling()
page = SolaraViz(
model,
[
make_geospace_leaflet(schelling_draw, zoom=4),
make_plot_happiness,
make_plot_measure(["happy"]),
],
model_params=model_params,
name="GeoSchelling",
)

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

This file was deleted.

45 changes: 0 additions & 45 deletions gis/geo_schelling/server.py

This file was deleted.

8 changes: 4 additions & 4 deletions gis/geo_schelling_points/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ The NUTS-2 regions are considered as a shared definition of neighborhood among a

There are two types of GeoAgents: people and regions. Each person resides in a randomly assigned region, and checks the color ratio of its region against a pre-defined "happiness" threshold at every time step. If the ratio falls below a certain threshold (e.g., 40%), the agent is found to be "unhappy", and randomly moves to another region. People are represented as points, with locations randomly chosen within their regions. The color of a region depends on the color of the majority population it contains (i.e., point in polygon calculations).

## 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 ``.
Loading

0 comments on commit 707cd0b

Please sign in to comment.