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 examples to build_distance_band #705

Merged
merged 8 commits into from
Apr 14, 2024
Merged
Changes from 4 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
85 changes: 85 additions & 0 deletions libpysal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,91 @@ def build_distance_band(
-------
Graph
libpysal.graph.Graph encoding distance band weights

Examples
-------
novotny-marek marked this conversation as resolved.
Show resolved Hide resolved
>>> 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]

Note that the function requires point geometry as the input.
novotny-marek marked this conversation as resolved.
Show resolved Hide resolved
The treshold distance is in units of the dataframe.
novotny-marek marked this conversation as resolved.
Show resolved Hide resolved
novotny-marek marked this conversation as resolved.
Show resolved Hide resolved

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

novotny-marek marked this conversation as resolved.
Show resolved Hide resolved
>>> distance_band = graph.Graph.build_distance_band(nybb.centroid, 110000)
>>> distance_band.adjacency
focal neighbor
Staten Island Queens 1
Brooklyn 1
Manhattan 1
Queens Staten Island 1
Brooklyn 1
Manhattan 1
Bronx 1
Brooklyn Staten Island 1
Queens 1
Manhattan 1
Bronx 1
Manhattan Staten Island 1
Queens 1
Brooklyn 1
Bronx 1
Bronx Queens 1
Brooklyn 1
Manhattan 1
Name: weight, dtype: int64

Instead of binary weights you can use inverse distance.

>>> distance_band = graph.Graph.build_distance_band(
... nybb.centroid,
... 45000,
... binary=False,
... )
>>> distance_band.adjacency
focal neighbor
Staten Island Staten Island 0.000000
Queens Brooklyn 0.000024
Brooklyn Queens 0.000024
Manhattan Bronx 0.000026
Bronx Manhattan 0.000026
Name: weight, dtype: float64

You can specify the kernel function.
novotny-marek marked this conversation as resolved.
Show resolved Hide resolved

>>> distance_band = graph.Graph.build_distance_band(
... nybb.centroid,
... 45000,
... binary=False,
... kernel='bisquare',
... bandwidth=60000,
... )
>>> distance_band.adjacency
focal neighbor
Staten Island Staten Island 0.000000
Queens Brooklyn 0.232079
Brooklyn Queens 0.232079
Manhattan Bronx 0.309825
Bronx Manhattan 0.309825
Name: weight, dtype: float64
"""
ids = _evaluate_index(data)

Expand Down