Skip to content

Commit 9897884

Browse files
committed
gis: update four examples to use solara viz
1 parent 8f9254b commit 9897884

File tree

26 files changed

+278
-323
lines changed

26 files changed

+278
-323
lines changed

gis/agents_and_networks/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ python3 -m pip install -r requirements.txt
2828
Then run the model:
2929

3030
```bash
31-
python3 scripts/run.py --campus ub
31+
solara run app.py -- --campus ub
3232
```
3333

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

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

3838
## License
3939

gis/agents_and_networks/app.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import sys
2+
3+
from mesa.visualization import Slider, SolaraViz, make_plot_measure
4+
from mesa_geo.visualization import make_geospace_leaflet
5+
from src.model.model import AgentsAndNetworks
6+
from src.visualization.utils import agent_draw, make_plot_clock
7+
8+
9+
def parse_args():
10+
campus = "ub"
11+
if "--campus" in sys.argv:
12+
campus = sys.argv[sys.argv.index("--campus") + 1]
13+
return campus
14+
15+
16+
if __name__ == "__main__":
17+
campus = parse_args()
18+
19+
if campus == "ub":
20+
data_file_prefix = "UB"
21+
elif campus == "gmu":
22+
data_file_prefix = "Mason"
23+
else:
24+
raise ValueError("Invalid campus name. Choose from ub or gmu.")
25+
26+
campus_params = {
27+
"ub": {"data_crs": "epsg:4326", "commuter_speed": 0.5, "zoom": 14},
28+
"gmu": {"data_crs": "epsg:2283", "commuter_speed": 0.4, "zoom": 16},
29+
}
30+
model_params = {
31+
"campus": campus,
32+
"data_crs": campus_params[campus]["data_crs"],
33+
"buildings_file": f"data/{campus}/{data_file_prefix}_bld.zip",
34+
"walkway_file": f"data/{campus}/{data_file_prefix}_walkway_line.zip",
35+
"lakes_file": f"data/{campus}/hydrop.zip",
36+
"rivers_file": f"data/{campus}/hydrol.zip",
37+
"driveway_file": f"data/{campus}/{data_file_prefix}_Rds.zip",
38+
"output_dir": "outputs",
39+
"show_walkway": True,
40+
"show_lakes_and_rivers": True,
41+
"show_driveway": True,
42+
"num_commuters": Slider(
43+
"Number of Commuters", value=50, min=10, max=150, step=10
44+
),
45+
"commuter_speed": Slider(
46+
"Commuter Walking Speed (m/s)",
47+
value=campus_params[campus]["commuter_speed"],
48+
min=0.1,
49+
max=1.5,
50+
step=0.1,
51+
),
52+
}
53+
model = AgentsAndNetworks()
54+
page = SolaraViz(
55+
model,
56+
[
57+
make_geospace_leaflet(agent_draw, zoom=campus_params[campus]["zoom"]),
58+
make_plot_clock,
59+
make_plot_measure(["status_home", "status_work", "status_traveling"]),
60+
make_plot_measure(["friendship_home", "friendship_work"]),
61+
],
62+
name="Agents and Networks",
63+
model_params=model_params,
64+
)
65+
66+
page # noqa

gis/agents_and_networks/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-e .
33

44
# external requirements
5-
mesa-geo~=0.7
5+
mesa-geo~=0.9.0a0
66
geopandas
77
numpy
88
pandas

gis/agents_and_networks/scripts/run.py

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

gis/agents_and_networks/src/visualization/server.py

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

gis/agents_and_networks/src/visualization/utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,48 @@
33
import matplotlib.pyplot as plt
44
import pandas as pd
55
import seaborn as sns
6+
import solara
7+
8+
from ..agent.building import Building
9+
from ..agent.commuter import Commuter
10+
from ..agent.geo_agents import Driveway, LakeAndRiver, Walkway
11+
12+
13+
def make_plot_clock(model):
14+
return solara.Markdown(f"**Day {model.day}, {model.hour:02d}:{model.minute:02d}**")
15+
16+
17+
def agent_draw(agent):
18+
portrayal = {}
19+
portrayal["color"] = "White"
20+
if isinstance(agent, Driveway):
21+
portrayal["color"] = "#D08004"
22+
elif isinstance(agent, Walkway):
23+
portrayal["color"] = "Brown"
24+
elif isinstance(agent, LakeAndRiver):
25+
portrayal["color"] = "#04D0CD"
26+
elif isinstance(agent, Building):
27+
portrayal["color"] = "Grey"
28+
# if agent.function is None:
29+
# portrayal["color"] = "Grey"
30+
# elif agent.function == 1.0:
31+
# portrayal["color"] = "Blue"
32+
# elif agent.function == 2.0:
33+
# portrayal["color"] = "Green"
34+
# else:
35+
# portrayal["color"] = "Grey"
36+
elif isinstance(agent, Commuter):
37+
if agent.status == "home":
38+
portrayal["color"] = "Green"
39+
elif agent.status == "work":
40+
portrayal["color"] = "Blue"
41+
elif agent.status == "transport":
42+
portrayal["color"] = "Red"
43+
else:
44+
portrayal["color"] = "Grey"
45+
portrayal["radius"] = "5"
46+
portrayal["fillOpacity"] = 1
47+
return portrayal
648

749

850
def plot_commuter_status_count(model_vars_df: pd.DataFrame) -> None:

gis/geo_schelling/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ NUTS-2 regions are the GeoAgents. The neighbors of a polygon are considered thos
1616

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

gis/geo_schelling/app.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import solara
2+
from mesa.visualization import Slider, SolaraViz, make_plot_measure
3+
from mesa_geo.visualization import make_geospace_leaflet
4+
from model import GeoSchelling
5+
6+
7+
def make_plot_happiness(model):
8+
return solara.Markdown(f"**Happy agents: {model.happy}**")
9+
10+
11+
model_params = {
12+
"density": Slider("Agent density", 0.6, 0.1, 1.0, 0.1),
13+
"minority_pc": Slider("Fraction minority", 0.2, 0.00, 1.0, 0.05),
14+
"export_data": False,
15+
}
16+
17+
18+
def schelling_draw(agent):
19+
"""
20+
Portrayal Method for canvas
21+
"""
22+
portrayal = {}
23+
if agent.atype is None:
24+
portrayal["color"] = "Grey"
25+
elif agent.atype == 0:
26+
portrayal["color"] = "Red"
27+
else:
28+
portrayal["color"] = "Blue"
29+
return portrayal
30+
31+
32+
model = GeoSchelling()
33+
page = SolaraViz(
34+
model,
35+
[
36+
make_geospace_leaflet(schelling_draw, zoom=4),
37+
make_plot_happiness,
38+
make_plot_measure(["happy"]),
39+
],
40+
model_params=model_params,
41+
name="GeoSchelling",
42+
)
43+
44+
page # noqa

gis/geo_schelling/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.0a0

gis/geo_schelling/run.py

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

0 commit comments

Comments
 (0)