Skip to content

Commit

Permalink
gis: fix user warning when testing geo schelling example
Browse files Browse the repository at this point in the history
[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

update
  • Loading branch information
wang-boyu authored and EwoutH committed Sep 3, 2024
1 parent c3bffc5 commit 8101ad3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 20 additions & 1 deletion gis/geo_schelling/model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import random
from pathlib import Path

import geopandas as gpd
import libpysal
import mesa
import mesa_geo as mg

script_directory = Path(__file__).resolve().parent


def get_largest_connected_components(gdf):
"""Get the largest connected component of a GeoDataFrame."""
# create spatial weights matrix
W = libpysal.weights.Queen.from_dataframe(
gdf, use_index=True, silence_warnings=True
)
# get component labels
gdf["component"] = W.component_labels
# get the largest component
largest_component = gdf["component"].value_counts().idxmax()
# subset the GeoDataFrame
gdf = gdf[gdf["component"] == largest_component]
return gdf


class SchellingAgent(mg.GeoAgent):
"""Schelling segregation agent."""

Expand Down Expand Up @@ -71,7 +88,9 @@ def __init__(self, density=0.6, minority_pc=0.2, export_data=False):
# Set up the grid with patches for every NUTS region
ac = mg.AgentCreator(SchellingAgent, model=self)
data_path = script_directory / "data/nuts_rg_60M_2013_lvl_2.geojson"
agents = ac.from_file(filename=data_path)
agents_gdf = gpd.read_file(data_path)
agents_gdf = get_largest_connected_components(agents_gdf)
agents = ac.from_GeoDataFrame(agents_gdf, unique_id="index")
self.space.add_agents(agents)

# Set up agents
Expand Down
4 changes: 1 addition & 3 deletions gis/geo_schelling/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def schelling_draw(agent):


happy_element = HappyElement()
map_element = mg.visualization.MapModule(
schelling_draw, [52, 12], 4, tiles=xyz.CartoDB.Positron
)
map_element = mg.visualization.MapModule(schelling_draw, tiles=xyz.CartoDB.Positron)
happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}])
server = mesa.visualization.ModularServer(
GeoSchelling, [map_element, happy_element, happy_chart], "Schelling", model_params
Expand Down

0 comments on commit 8101ad3

Please sign in to comment.