From fc6dfba2cea5cb9b5d71a2c0e768275fb57b9457 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Wed, 13 Apr 2022 19:26:08 +0100 Subject: [PATCH 1/3] vectorize centroid in W.plot --- libpysal/weights/weights.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/libpysal/weights/weights.py b/libpysal/weights/weights.py index 55d38c202..87f27c72c 100644 --- a/libpysal/weights/weights.py +++ b/libpysal/weights/weights.py @@ -584,7 +584,7 @@ def trcWtW_WW(self): def pct_nonzero(self): """Percentage of nonzero weights.""" if "pct_nonzero" not in self._cache: - self._pct_nonzero = 100.0 * self.sparse.nnz / (1.0 * self._n ** 2) + self._pct_nonzero = 100.0 * self.sparse.nnz / (1.0 * self._n**2) self._cache["pct_nonzero"] = self._pct_nonzero return self._pct_nonzero @@ -1293,8 +1293,8 @@ def plot( if indexed_on is not None: neighbors = gdf[gdf[indexed_on].isin(neighbors)].index.tolist() idx = gdf[gdf[indexed_on] == idx].index.tolist()[0] - centroids = gdf.loc[neighbors].centroid.apply(lambda p: (p.x, p.y)) - centroids = np.vstack(centroids.values) + centroids = gdf.loc[neighbors].centroid + centroids = np.stack([centroids.x, centroids.y], axis=1) focal = np.hstack(gdf.loc[idx].geometry.centroid.xy) seen = set() for nidx, neighbor in zip(neighbors, centroids): @@ -1303,11 +1303,8 @@ def plot( ax.plot(*list(zip(focal, neighbor)), marker=None, **edge_kws) seen.update((idx, nidx)) seen.update((nidx, idx)) - ax.scatter( - gdf.centroid.apply(lambda p: p.x), - gdf.centroid.apply(lambda p: p.y), - **node_kws - ) + centroids = gdf.centroid + ax.scatter(centroids.x, centroids.y, **node_kws) return f, ax From d1041c3a96f0faf3a9f28d630c187d4f45d5aa63 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Sat, 3 Sep 2022 13:53:19 +0200 Subject: [PATCH 2/3] ensure we index using list --- libpysal/weights/weights.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libpysal/weights/weights.py b/libpysal/weights/weights.py index 87f27c72c..c55ad4c47 100644 --- a/libpysal/weights/weights.py +++ b/libpysal/weights/weights.py @@ -1293,6 +1293,8 @@ def plot( if indexed_on is not None: neighbors = gdf[gdf[indexed_on].isin(neighbors)].index.tolist() idx = gdf[gdf[indexed_on] == idx].index.tolist()[0] + else: + neighbors = list(neighbors) centroids = gdf.loc[neighbors].centroid centroids = np.stack([centroids.x, centroids.y], axis=1) focal = np.hstack(gdf.loc[idx].geometry.centroid.xy) From afe4f129d54776549dc47aab30308ec4a9d8f10c Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Sat, 3 Sep 2022 14:03:44 +0200 Subject: [PATCH 3/3] add test --- libpysal/weights/tests/test_weights.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libpysal/weights/tests/test_weights.py b/libpysal/weights/tests/test_weights.py index 83f8decf1..e44722daf 100644 --- a/libpysal/weights/tests/test_weights.py +++ b/libpysal/weights/tests/test_weights.py @@ -1,5 +1,6 @@ import os import tempfile +import warnings import unittest from ..weights import W, WSP @@ -13,6 +14,20 @@ NPTA3E = np.testing.assert_array_almost_equal +try: + import geopandas + + GEOPANDAS_EXTINCT = False +except ImportError: + GEOPANDAS_EXTINCT = True + +try: + import matplotlib + + MPL_EXTINCT = False +except ImportError: + MPL_EXTINCT = True + class TestW(unittest.TestCase): def setUp(self): @@ -355,6 +370,13 @@ def test_roundtrip_write(self): new = W.from_file(path) np.testing.assert_array_equal(self.w.sparse.toarray(), new.sparse.toarray()) + @unittest.skipIf(GEOPANDAS_EXTINCT or MPL_EXTINCT, "Missing dependencies") + def test_plot(self): + df = geopandas.read_file(examples.get_path("10740.shp")) + with warnings.catch_warnings(record=True) as record: + self.w.plot(df) + assert len(record) == 0 + class Test_WSP_Back_To_W(unittest.TestCase): # Test to make sure we get back to the same W functionality