From f8dbffe933085b1b32861715a26c1738e70d0b2f Mon Sep 17 00:00:00 2001 From: novotny-marek Date: Wed, 10 Apr 2024 17:18:24 +0200 Subject: [PATCH 1/8] DOC: add examples to Graph.build_disatnce_band --- libpysal/graph/base.py | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index abbd0033d..94c5c9970 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -463,6 +463,81 @@ def build_distance_band( ------- Graph libpysal.graph.Graph encoding distance band weights + + 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] + + Note that the function requires point geometry as the input. + The treshold distance is in units of the dataframe. + + >>> 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 + + >>> 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. + + >>> 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) From b9daa8f234eb08ffd4e2cc8351d5ca72b70316b7 Mon Sep 17 00:00:00 2001 From: novotny-marek Date: Wed, 10 Apr 2024 17:23:32 +0200 Subject: [PATCH 2/8] Improve formatting --- libpysal/graph/base.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index 94c5c9970..b851d67c0 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -517,7 +517,11 @@ def build_distance_band( Instead of binary weights you can use inverse distance. - >>> distance_band = graph.Graph.build_distance_band(nybb.centroid, 45000, binary=False) + >>> distance_band = graph.Graph.build_distance_band( + ... nybb.centroid, + 45000, + binary=False + ) >>> distance_band.adjacency focal neighbor Staten Island Staten Island 0.000000 @@ -529,7 +533,13 @@ def build_distance_band( You can specify the kernel function. - >>> distance_band = graph.Graph.build_distance_band(nybb.centroid, 45000, binary=False, kernel='bisquare', bandwidth=60000) + >>> 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 From c4b1d591100b25713fd474f5aeb907dd24be6c45 Mon Sep 17 00:00:00 2001 From: novotny-marek Date: Wed, 10 Apr 2024 17:27:10 +0200 Subject: [PATCH 3/8] Improve formatting --- libpysal/graph/base.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index b851d67c0..cf56bfe5c 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -519,9 +519,9 @@ def build_distance_band( >>> distance_band = graph.Graph.build_distance_band( ... nybb.centroid, - 45000, - binary=False - ) + ... 45000, + ... binary=False, + ... ) >>> distance_band.adjacency focal neighbor Staten Island Staten Island 0.000000 @@ -535,11 +535,11 @@ def build_distance_band( >>> distance_band = graph.Graph.build_distance_band( ... nybb.centroid, - 45000, - binary=False, - kernel='bisquare', - bandwidth=60000 - ) + ... 45000, + ... binary=False, + ... kernel='bisquare', + ... bandwidth=60000, + ... ) >>> distance_band.adjacency focal neighbor Staten Island Staten Island 0.000000 From a50263e8235cb343a222b826b67bbc3bece3ad92 Mon Sep 17 00:00:00 2001 From: novotny-marek Date: Sun, 14 Apr 2024 18:45:26 +0200 Subject: [PATCH 4/8] Ruff formatting --- libpysal/graph/base.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index cf56bfe5c..5f8bbe91d 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -471,20 +471,20 @@ def build_distance_band( >>> nybb = gpd.read_file(get_path('nybb')).set_index("BoroName") >>> nybb BoroCode ... geometry - BoroName ... + 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. The treshold distance is in units of the dataframe. >>> distance_band = graph.Graph.build_distance_band(nybb.centroid, 45000) >>> distance_band.adjacency - focal neighbor + focal neighbor Staten Island Staten Island 0 Queens Brooklyn 1 Brooklyn Queens 1 @@ -494,7 +494,7 @@ def build_distance_band( >>> distance_band = graph.Graph.build_distance_band(nybb.centroid, 110000) >>> distance_band.adjacency - focal neighbor + focal neighbor Staten Island Queens 1 Brooklyn 1 Manhattan 1 @@ -518,12 +518,12 @@ def build_distance_band( Instead of binary weights you can use inverse distance. >>> distance_band = graph.Graph.build_distance_band( - ... nybb.centroid, + ... nybb.centroid, ... 45000, ... binary=False, ... ) >>> distance_band.adjacency - focal neighbor + focal neighbor Staten Island Staten Island 0.000000 Queens Brooklyn 0.000024 Brooklyn Queens 0.000024 @@ -541,7 +541,7 @@ def build_distance_band( ... bandwidth=60000, ... ) >>> distance_band.adjacency - focal neighbor + focal neighbor Staten Island Staten Island 0.000000 Queens Brooklyn 0.232079 Brooklyn Queens 0.232079 From 0e715fb6f49caa86f46025318dde364f4c7b5bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Novotn=C3=BD?= <126814194+novotny-marek@users.noreply.github.com> Date: Sun, 14 Apr 2024 19:04:47 +0200 Subject: [PATCH 5/8] DOC: Apply suggestions Co-authored-by: Martin Fleischmann --- libpysal/graph/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index 5f8bbe91d..73ef5dd42 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -465,7 +465,7 @@ def build_distance_band( libpysal.graph.Graph encoding distance band weights Examples - ------- + -------- >>> import geopandas as gpd >>> from geodatasets import get_path >>> nybb = gpd.read_file(get_path('nybb')).set_index("BoroName") From 6351463fcf7e5475e7701ecfedfcc66427cf4e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Novotn=C3=BD?= <126814194+novotny-marek@users.noreply.github.com> Date: Sun, 14 Apr 2024 19:08:42 +0200 Subject: [PATCH 6/8] Apply suggestions from code review Co-authored-by: Martin Fleischmann --- libpysal/graph/base.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index 73ef5dd42..cf3c4d6d6 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -479,8 +479,11 @@ def build_distance_band( Bronx 2 ... MULTIPOLYGON (((1012821.806 229228.265, 101278... [5 rows x 4 columns] - Note that the function requires point geometry as the input. - The treshold distance is in units of the dataframe. + Note that the method requires point geometry (or an array of coordinates + representing points) as an input. + + The threshold distance is in the units of the geometry projection. You can check it + using the ``nybb.crs`` property. >>> distance_band = graph.Graph.build_distance_band(nybb.centroid, 45000) >>> distance_band.adjacency @@ -492,6 +495,8 @@ def build_distance_band( Bronx Manhattan 1 Name: weight, dtype: int64 + The larger threshold yields more neighbors. + >>> distance_band = graph.Graph.build_distance_band(nybb.centroid, 110000) >>> distance_band.adjacency focal neighbor @@ -531,7 +536,7 @@ def build_distance_band( Bronx Manhattan 0.000026 Name: weight, dtype: float64 - You can specify the kernel function. + Or specify the kernel function to derive weight from the distance. >>> distance_band = graph.Graph.build_distance_band( ... nybb.centroid, From 492148710beb17a9bbe23f051fcd4b79e3a17c72 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 14 Apr 2024 17:26:52 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- libpysal/graph/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index cf3c4d6d6..35a969965 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -479,9 +479,9 @@ def build_distance_band( Bronx 2 ... MULTIPOLYGON (((1012821.806 229228.265, 101278... [5 rows x 4 columns] - Note that the method requires point geometry (or an array of coordinates + Note that the method requires point geometry (or an array of coordinates representing points) as an input. - + The threshold distance is in the units of the geometry projection. You can check it using the ``nybb.crs`` property. @@ -496,7 +496,7 @@ def build_distance_band( Name: weight, dtype: int64 The larger threshold yields more neighbors. - + >>> distance_band = graph.Graph.build_distance_band(nybb.centroid, 110000) >>> distance_band.adjacency focal neighbor From 5016b3fdf630abb6e93bcf3c6c2c3b3d57acd7c8 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Sun, 14 Apr 2024 19:29:58 +0200 Subject: [PATCH 8/8] Update libpysal/graph/base.py --- libpysal/graph/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index 35a969965..c0338742a 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -482,8 +482,8 @@ def build_distance_band( Note that the method requires point geometry (or an array of coordinates representing points) as an input. - The threshold distance is in the units of the geometry projection. You can check it - using the ``nybb.crs`` property. + The threshold distance is in the units of the geometry projection. + You can check it using the ``nybb.crs`` property. >>> distance_band = graph.Graph.build_distance_band(nybb.centroid, 45000) >>> distance_band.adjacency