Skip to content

Commit

Permalink
DOC: add example to Graph.to_W (#701)
Browse files Browse the repository at this point in the history
* DOC: add example to Graph.to_W

* fix indentation
  • Loading branch information
martinfleis committed Apr 10, 2024
1 parent dff5383 commit 7503306
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions libpysal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,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

0 comments on commit 7503306

Please sign in to comment.