From c6284bf542112b85ff4053d1db1368fdd38a22ad Mon Sep 17 00:00:00 2001 From: Anna Brazdova <131773776+Kryndlea@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:26:18 +0200 Subject: [PATCH 1/3] added examples to graph --- libpysal/graph/base.py | 270 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 265 insertions(+), 5 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index 491493cee..81b4a896d 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -203,6 +203,27 @@ def from_W(cls, w): # noqa: N802 ------- Graph libpysal.graph.Graph from W + + 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] + + >>> queen_w = weights.Queen.from_dataframe(nybb, use_index=True) + >>> queen_graph = graph.Graph.from_W(queen_w) + >>> queen_graph + """ return cls.from_weights_dict(dict(w)) @@ -218,7 +239,7 @@ def to_W(self): # noqa: N802 -------- >>> import geopandas as gpd >>> from geodatasets import get_path - >>> nybb = gpd.read_file(get_path('nybb')).set_index("BoroName") + >>> nybb = gpd.read_file(get_path("nybb")).set_index("BoroName") >>> nybb BoroCode ... geometry BoroName ... @@ -557,7 +578,7 @@ def build_contiguity(cls, geometry, rook=True, by_perimeter=False, strict=False) >>> import geopandas as gpd >>> from geodatasets import get_path - >>> nybb = gpd.read_file(get_path('nybb')).set_index("BoroName") + >>> nybb = gpd.read_file(get_path("nybb")).set_index("BoroName") >>> nybb BoroCode ... geometry BoroName ... @@ -666,7 +687,7 @@ def build_distance_band( -------- >>> import geopandas as gpd >>> from geodatasets import get_path - >>> nybb = gpd.read_file(get_path('nybb')).set_index("BoroName") + >>> nybb = gpd.read_file(get_path("nybb")).set_index("BoroName") >>> nybb BoroCode ... geometry BoroName ... @@ -854,6 +875,44 @@ def build_fuzzy_contiguity( ------- Graph libpysal.graph.Graph encoding fuzzy contiguity + + + 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] + + Example using the default parameters: + + >>> fuzzy_contiguity = graph.Graph.build_fuzzy_contiguity(nybb) + >>> fuzzy_contiguity + + + Example using the tolerance of 0.05: + + >>> fuzzy_contiguity = graph.Graph.build_fuzzy_contiguity(nybb, tolerance=0.05) + >>> fuzzy_contiguity + + + Example using a buffer of 10000: + + >>> fuzzy_contiguity = graph.Graph.build_fuzzy_contiguity(nybb, buffer=10000) + >>> fuzzy_contiguity + + """ ids = _evaluate_index(geometry) @@ -981,7 +1040,7 @@ def build_knn(cls, data, k, metric="euclidean", p=2, coplanar="raise"): -------- >>> import geopandas as gpd >>> from geodatasets import get_path - >>> nybb = gpd.read_file(get_path('nybb')).set_index('BoroName') + >>> nybb = gpd.read_file(get_path("nybb")).set_index('BoroName') >>> nybb BoroCode ... geometry BoroName ... @@ -1187,7 +1246,7 @@ def build_triangulation( >>> import geopandas as gpd >>> from geodatasets import get_path - >>> nybb = gpd.read_file(get_path('nybb')).set_index("BoroName") + >>> nybb = gpd.read_file(get_path("nybb")).set_index("BoroName") >>> nybb BoroCode ... geometry BoroName ... @@ -1276,6 +1335,27 @@ def build_h3(cls, ids, order=1, weight="distance"): Returns ------- Graph + + Examples + -------- + >>> import geopandas as gpd + >>> from geodatasets import get_path + >>> from tobler.util import h3fy + >>> gdf = gpd.read_file(get_path("geoda guerry") + >>> h3 = h3fy(gdf, resolution=4) + >>> h3.head() + geometry + hex_id + 841f94dffffffff POLYGON ((609346.657 2195981.397, 604556.817 2... + 841fa67ffffffff POLYGON ((722074.162 2561038.244, 717442.706 2... + 84186a3ffffffff POLYGON ((353695.287 2121176.341, 329999.974 2... + 8418609ffffffff POLYGON ((387747.482 2509794.492, 364375.032 2... + 8418491ffffffff POLYGON ((320872.289 1846157.662, 296923.464 1... + + >>> G = graph.Graph.build_h3(h3.index) + >>> G + """ neigbors, weights = _build_from_h3(ids, order=order) g = cls.from_dicts(neigbors, weights) @@ -1597,6 +1677,25 @@ def higher_order(self, k=2, shortest_path=True, diagonal=False, lower_order=Fals ------- Graph higher order weights + + Examples + -------- + + >>> import geopandas as gpd + >>> from geodatasets import get_path + >>> gdf = gpd.read_file(get_path("geoda guerry")) + >>> contiguity = graph.Graph.build_contiguity(gdf) + >>> contiguity + + + >>> contiguity.higher_order(k=2) + + + >>> contiguity.higher_order(lower_order=True) + """ if not Version(scipy_version) >= Version("1.12.0"): raise ImportError("Graph.higher_order() requires scipy>=1.12.0.") @@ -1662,6 +1761,27 @@ def lag(self, y, categorical=False, ties="raise"): ------- numpy.ndarray array of numeric|categorical values for the spatial lag + + Examples + -------- + + >>> import numpy as np + >>> import pandas as pd + >>> import geopandas as gpd + >>> from geodatasets import get_path + >>> aus = gpd.read_file(get_path('abs.australia_states_territories')).set_index("STE_NAME21") + >>> aus = aus[aus.geometry.notna()] + >>> contiguity = graph.Graph.build_contiguity(aus) + + Spatial lag operator for continuous variables. + >>> y = np.arange(9) + >>> contiguity.lag(y) + array([21., 3., 9., 13., 9., 0., 9., 0., 0.]) + + You can also perform transformation of weights. + >>> contiguity_r = contiguity.transform("r") + >>> contiguity_r.lag(y) + array([4.2, 1.5, 3. , 2.6, 4.5, 0. , 3. , 0. , 0. ]) """ return _lag_spatial(self, y, categorical=categorical, ties=ties) @@ -1683,6 +1803,24 @@ def to_parquet(self, path, **kwargs): See also -------- read_parquet + + 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.to_parquet("contiguity.parquet") """ _to_parquet(self, path, **kwargs) @@ -1699,6 +1837,24 @@ def to_gal(self, path): See also -------- read_gal + + 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.to_gal("contiguity.gal") """ _to_gal(self, path) @@ -1715,6 +1871,24 @@ def to_gwt(self, path): See also -------- read_gwt + + 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).transform("r") + >>> contiguity.to_gwt("contiguity.gwt") """ _to_gwt(self, path) @@ -1727,6 +1901,24 @@ def to_networkx(self): ------- networkx.Graph | networkx.DiGraph Representation of libpysal Graph as networkx graph + + 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) + >>> nx_graph = contiguity.to_networkx """ try: import networkx as nx @@ -1906,6 +2098,26 @@ def subgraph(self, ids): ------- Graph A new Graph that is a subset of the original + + 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.subgraph(["Queens", "Brooklyn", "Manhattan", "Bronx"]) + Notes ----- @@ -1962,6 +2174,42 @@ def assign_self_weight(self, weight=1): ------- Graph A new ``Graph`` with added self-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] + + >>> contiguity = graph.Graph.build_contiguity(nybb) + >>> contiguity_weights = contiguity.assign_self_weight(weight = 0.5) + >>> contiguity_weights.adjacency + focal neighbor + Staten Island Staten Island 0.5 + Queens Queens 0.5 + Brooklyn 1.0 + Manhattan 1.0 + Bronx 1.0 + Brooklyn Queens 1.0 + Brooklyn 0.5 + Manhattan 1.0 + Manhattan Queens 1.0 + Brooklyn 1.0 + Manhattan 0.5 + Bronx 1.0 + Bronx Queens 1.0 + Manhattan 1.0 + Bronx 0.5 + Name: weight, dtype: float64 """ addition = pd.Series( weight, @@ -2136,6 +2384,10 @@ def read_parquet(path, **kwargs): ------- Graph deserialized Graph + + Examples + -------- + >>> graph.read_parquet("contiguity.parquet") """ adjacency, transformation = _read_parquet(path, **kwargs) return Graph(adjacency, transformation, is_sorted=True) @@ -2156,6 +2408,10 @@ def read_gal(path): ------- Graph deserialized Graph + + Examples + -------- + >>> graph.read_parquet("contiguity.gal") """ neighbors = _read_gal(path) return Graph.from_dicts(neighbors) @@ -2173,6 +2429,10 @@ def read_gwt(path): ------- Graph deserialized Graph + + Examples + -------- + >>> graph.read_parquet("contiguity.gwt") """ head, tail, weight = _read_gwt(path) return Graph.from_arrays(head, tail, weight) From 6e4214aa0d1aa95e3e8d2444d8b9f6601d154e64 Mon Sep 17 00:00:00 2001 From: Anna Brazdova <131773776+Kryndlea@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:41:29 +0200 Subject: [PATCH 2/3] ruff --- libpysal/graph/base.py | 84 +++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index 81b4a896d..ab35cafae 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -203,7 +203,7 @@ def from_W(cls, w): # noqa: N802 ------- Graph libpysal.graph.Graph from W - + Examples -------- >>> import geopandas as gpd @@ -218,7 +218,7 @@ def from_W(cls, w): # noqa: N802 Manhattan 1 ... MULTIPOLYGON (((981219.056 188655.316, 980940.... Bronx 2 ... MULTIPOLYGON (((1012821.806 229228.265, 101278... [5 rows x 4 columns] - + >>> queen_w = weights.Queen.from_dataframe(nybb, use_index=True) >>> queen_graph = graph.Graph.from_W(queen_w) >>> queen_graph @@ -875,8 +875,8 @@ def build_fuzzy_contiguity( ------- Graph libpysal.graph.Graph encoding fuzzy contiguity - - + + Examples -------- >>> import geopandas as gpd @@ -891,28 +891,28 @@ def build_fuzzy_contiguity( Manhattan 1 ... MULTIPOLYGON (((981219.056 188655.316, 980940.... Bronx 2 ... MULTIPOLYGON (((1012821.806 229228.265, 101278... [5 rows x 4 columns] - + Example using the default parameters: - + >>> fuzzy_contiguity = graph.Graph.build_fuzzy_contiguity(nybb) >>> fuzzy_contiguity - + Example using the tolerance of 0.05: - + >>> fuzzy_contiguity = graph.Graph.build_fuzzy_contiguity(nybb, tolerance=0.05) >>> fuzzy_contiguity - + Example using a buffer of 10000: - + >>> fuzzy_contiguity = graph.Graph.build_fuzzy_contiguity(nybb, buffer=10000) >>> fuzzy_contiguity - + """ ids = _evaluate_index(geometry) @@ -1335,7 +1335,7 @@ def build_h3(cls, ids, order=1, weight="distance"): Returns ------- Graph - + Examples -------- >>> import geopandas as gpd @@ -1345,7 +1345,7 @@ def build_h3(cls, ids, order=1, weight="distance"): >>> h3 = h3fy(gdf, resolution=4) >>> h3.head() geometry - hex_id + hex_id 841f94dffffffff POLYGON ((609346.657 2195981.397, 604556.817 2... 841fa67ffffffff POLYGON ((722074.162 2561038.244, 717442.706 2... 84186a3ffffffff POLYGON ((353695.287 2121176.341, 329999.974 2... @@ -1356,7 +1356,7 @@ def build_h3(cls, ids, order=1, weight="distance"): >>> G - """ + """ # noqa: E501 neigbors, weights = _build_from_h3(ids, order=order) g = cls.from_dicts(neigbors, weights) @@ -1677,25 +1677,25 @@ def higher_order(self, k=2, shortest_path=True, diagonal=False, lower_order=Fals ------- Graph higher order weights - + Examples -------- - + >>> import geopandas as gpd >>> from geodatasets import get_path >>> gdf = gpd.read_file(get_path("geoda guerry")) >>> contiguity = graph.Graph.build_contiguity(gdf) >>> contiguity - + [0, 1, 2, 3, 4, ...]> + >>> contiguity.higher_order(k=2) - + >>> contiguity.higher_order(lower_order=True) + [0, 1, 2, 3, 4, ...]> """ if not Version(scipy_version) >= Version("1.12.0"): raise ImportError("Graph.higher_order() requires scipy>=1.12.0.") @@ -1761,23 +1761,25 @@ def lag(self, y, categorical=False, ties="raise"): ------- numpy.ndarray array of numeric|categorical values for the spatial lag - + Examples -------- - + >>> import numpy as np >>> import pandas as pd >>> import geopandas as gpd >>> from geodatasets import get_path - >>> aus = gpd.read_file(get_path('abs.australia_states_territories')).set_index("STE_NAME21") + >>> aus = gpd.read_file(get_path("abs.australia_states_territories")).set_index( + ... "STE_NAME21" + ... ) >>> aus = aus[aus.geometry.notna()] >>> contiguity = graph.Graph.build_contiguity(aus) - + Spatial lag operator for continuous variables. >>> y = np.arange(9) >>> contiguity.lag(y) array([21., 3., 9., 13., 9., 0., 9., 0., 0.]) - + You can also perform transformation of weights. >>> contiguity_r = contiguity.transform("r") >>> contiguity_r.lag(y) @@ -1803,7 +1805,7 @@ def to_parquet(self, path, **kwargs): See also -------- read_parquet - + Examples -------- >>> import geopandas as gpd @@ -1818,7 +1820,7 @@ def to_parquet(self, path, **kwargs): 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.to_parquet("contiguity.parquet") """ @@ -1837,7 +1839,7 @@ def to_gal(self, path): See also -------- read_gal - + Examples -------- >>> import geopandas as gpd @@ -1852,7 +1854,7 @@ def to_gal(self, path): 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.to_gal("contiguity.gal") """ @@ -1871,7 +1873,7 @@ def to_gwt(self, path): See also -------- read_gwt - + Examples -------- >>> import geopandas as gpd @@ -1886,7 +1888,7 @@ def to_gwt(self, path): 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).transform("r") >>> contiguity.to_gwt("contiguity.gwt") """ @@ -1901,7 +1903,7 @@ def to_networkx(self): ------- networkx.Graph | networkx.DiGraph Representation of libpysal Graph as networkx graph - + Examples -------- >>> import geopandas as gpd @@ -1916,7 +1918,7 @@ def to_networkx(self): 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) >>> nx_graph = contiguity.to_networkx """ @@ -2098,7 +2100,7 @@ def subgraph(self, ids): ------- Graph A new Graph that is a subset of the original - + Examples -------- >>> import geopandas as gpd @@ -2113,7 +2115,7 @@ def subgraph(self, ids): 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.subgraph(["Queens", "Brooklyn", "Manhattan", "Bronx"]) >> import geopandas as gpd @@ -2189,11 +2191,11 @@ def assign_self_weight(self, weight=1): 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_weights = contiguity.assign_self_weight(weight = 0.5) >>> contiguity_weights.adjacency - focal neighbor + focal neighbor Staten Island Staten Island 0.5 Queens Queens 0.5 Brooklyn 1.0 @@ -2384,7 +2386,7 @@ def read_parquet(path, **kwargs): ------- Graph deserialized Graph - + Examples -------- >>> graph.read_parquet("contiguity.parquet") @@ -2408,7 +2410,7 @@ def read_gal(path): ------- Graph deserialized Graph - + Examples -------- >>> graph.read_parquet("contiguity.gal") @@ -2429,7 +2431,7 @@ def read_gwt(path): ------- Graph deserialized Graph - + Examples -------- >>> graph.read_parquet("contiguity.gwt") From e92e77b4ec617d2b124a4e1320cac18cf608b8cf Mon Sep 17 00:00:00 2001 From: Anna Brazdova <131773776+Kryndlea@users.noreply.github.com> Date: Tue, 9 Jul 2024 16:06:40 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Martin Fleischmann --- libpysal/graph/base.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index ab35cafae..7a06951d5 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -223,7 +223,7 @@ def from_W(cls, w): # noqa: N802 >>> queen_graph = graph.Graph.from_W(queen_w) >>> queen_graph + ['Staten Island', 'Queens', 'Brooklyn', 'Manhattan', 'Bronx']> """ return cls.from_weights_dict(dict(w)) @@ -906,7 +906,7 @@ def build_fuzzy_contiguity( - Example using a buffer of 10000: + Example using a buffer of 10000 feet (CRS of nybb is in feet): >>> fuzzy_contiguity = graph.Graph.build_fuzzy_contiguity(nybb, buffer=10000) >>> fuzzy_contiguity @@ -1680,7 +1680,6 @@ def higher_order(self, k=2, shortest_path=True, diagonal=False, lower_order=Fals Examples -------- - >>> import geopandas as gpd >>> from geodatasets import get_path >>> gdf = gpd.read_file(get_path("geoda guerry")) @@ -1764,7 +1763,6 @@ def lag(self, y, categorical=False, ties="raise"): Examples -------- - >>> import numpy as np >>> import pandas as pd >>> import geopandas as gpd @@ -1776,11 +1774,13 @@ def lag(self, y, categorical=False, ties="raise"): >>> contiguity = graph.Graph.build_contiguity(aus) Spatial lag operator for continuous variables. + >>> y = np.arange(9) >>> contiguity.lag(y) array([21., 3., 9., 13., 9., 0., 9., 0., 0.]) You can also perform transformation of weights. + >>> contiguity_r = contiguity.transform("r") >>> contiguity_r.lag(y) array([4.2, 1.5, 3. , 2.6, 4.5, 0. , 3. , 0. , 0. ]) @@ -1920,7 +1920,7 @@ def to_networkx(self): [5 rows x 4 columns] >>> contiguity = graph.Graph.build_contiguity(nybb) - >>> nx_graph = contiguity.to_networkx + >>> nx_graph = contiguity.to_networkx() """ try: import networkx as nx @@ -2193,7 +2193,7 @@ def assign_self_weight(self, weight=1): [5 rows x 4 columns] >>> contiguity = graph.Graph.build_contiguity(nybb) - >>> contiguity_weights = contiguity.assign_self_weight(weight = 0.5) + >>> contiguity_weights = contiguity.assign_self_weight(0.5) >>> contiguity_weights.adjacency focal neighbor Staten Island Staten Island 0.5