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

vectorize centroid in W.plot #465

Merged
merged 4 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions libpysal/weights/tests/test_weights.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import tempfile
import warnings

import unittest
import pytest
Expand All @@ -15,6 +16,20 @@

NPTA3E = np.testing.assert_array_almost_equal

try:
import geopandas

GEOPANDAS_EXTINCT = False
except ImportError:
GEOPANDAS_EXTINCT = True

Check warning on line 24 in libpysal/weights/tests/test_weights.py

View check run for this annotation

Codecov / codecov/patch

libpysal/weights/tests/test_weights.py#L23-L24

Added lines #L23 - L24 were not covered by tests

try:
import matplotlib

MPL_EXTINCT = False
except ImportError:
MPL_EXTINCT = True

Check warning on line 31 in libpysal/weights/tests/test_weights.py

View check run for this annotation

Codecov / codecov/patch

libpysal/weights/tests/test_weights.py#L30-L31

Added lines #L30 - L31 were not covered by tests


class TestW(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -408,6 +423,13 @@
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

def test_to_sparse(self):
sparse = self.w_islands.to_sparse()
np.testing.assert_array_equal(sparse.data, [1, 1, 1, 1, 0])
Expand Down
13 changes: 6 additions & 7 deletions libpysal/weights/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -1490,8 +1490,10 @@ 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)
else:
neighbors = list(neighbors)
centroids = gdf.loc[neighbors].centroid
martinfleis marked this conversation as resolved.
Show resolved Hide resolved
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):
Expand All @@ -1500,11 +1502,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


Expand Down
Loading