Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: add example to Graph.to_W #701

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions libpysal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,45 @@ def to_W(self): # noqa: N802
-------
libpysal.weights.W
representation of graph as a weights.W object

Examples
--------
>>> import geopandas as gpd
>>> from geodatasets import get_path
>>> nybb = gpd.read_file(get_path('nybb')).set_index("BoroName")
>>> nybb
BoroCode ... geometry
BoroName ...
Staten Island 5 ... MULTIPOLYGON (((970217.022 145643.332, 970227....
Queens 4 ... MULTIPOLYGON (((1029606.077 156073.814, 102957...
Brooklyn 3 ... MULTIPOLYGON (((1021176.479 151374.797, 102100...
Manhattan 1 ... MULTIPOLYGON (((981219.056 188655.316, 980940....
Bronx 2 ... MULTIPOLYGON (((1012821.806 229228.265, 101278...
[5 rows x 4 columns]

>>> contiguity = graph.Graph.build_contiguity(nybb)
>>> contiguity.adjacency
focal neighbor
Staten Island Staten Island 0
Queens Brooklyn 1
Manhattan 1
Bronx 1
Brooklyn Queens 1
Manhattan 1
Manhattan Queens 1
Brooklyn 1
Bronx 1
Bronx Queens 1
Manhattan 1
Name: weight, dtype: int64

>>> w = contiguity.to_W()
>>> w.neighbors
{'Bronx': ['Queens', 'Manhattan'],
'Brooklyn': ['Queens', 'Manhattan'],
'Manhattan': ['Queens', 'Brooklyn', 'Bronx'],
'Queens': ['Brooklyn', 'Manhattan', 'Bronx'],
'Staten Island': []}
"""
grouper = self._adjacency.groupby(level=0)
neighbors = {}
Expand Down