Skip to content

Commit d18d06f

Browse files
committed
Drop support for 3.7
2 parents f056a77 + 8286c8f commit d18d06f

File tree

5 files changed

+78
-32
lines changed

5 files changed

+78
-32
lines changed

.github/workflows/unittests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
matrix:
2121
os: ['ubuntu-latest']
2222
environment-file: [
23-
ci/38-minimal.yaml,
2423
ci/38.yaml,
2524
ci/39.yaml,
2625
ci/310.yaml,

ci/38-minimal.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

libpysal/weights/tests/test_weights.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import tempfile
33

44
import unittest
5+
import pytest
56
from ..weights import W, WSP
67
from .. import util
78
from ..util import WSP2W, lat2W
@@ -373,6 +374,10 @@ def test_to_sparse(self):
373374
sparse = self.w_islands.to_sparse()
374375
self.assertIsInstance(sparse, scipy.sparse._arrays.coo_array)
375376

377+
def test_sparse_fmt(self):
378+
with pytest.raises(ValueError) as exc_info:
379+
sparse = self.w_islands.to_sparse("dog")
380+
376381
def test_from_sparse(self):
377382
sparse = self.w_islands.to_sparse()
378383
w = W.from_sparse(sparse)
@@ -714,6 +719,11 @@ def test_trcWtW_WW(self):
714719
def test_s0(self):
715720
self.assertEqual(self.w3x3.s0, 24.0)
716721

722+
def test_from_WSP(self):
723+
w = W.from_WSP(self.wsp)
724+
self.assertEqual(w.n, 100)
725+
self.assertEqual(w.pct_nonzero, 4.62)
726+
717727

718728
if __name__ == "__main__":
719729
unittest.main()

libpysal/weights/util.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
try:
2626
from shapely.geometry.base import BaseGeometry
27+
2728
HAS_SHAPELY = True
2829
except ImportError:
2930
HAS_SHAPELY = False
@@ -534,17 +535,17 @@ def higher_order_sp(
534535
)
535536

536537
if lower_order:
537-
wk = sum(map(lambda x: w ** x, range(2, k + 1)))
538+
wk = sum(map(lambda x: w**x, range(2, k + 1)))
538539
shortest_path = False
539540
else:
540-
wk = w ** k
541+
wk = w**k
541542

542543
rk, ck = wk.nonzero()
543544
sk = set(zip(rk, ck))
544545

545546
if shortest_path:
546547
for j in range(1, k):
547-
wj = w ** j
548+
wj = w**j
548549
rj, cj = wj.nonzero()
549550
sj = set(zip(rj, cj))
550551
sk.difference_update(sj)
@@ -826,14 +827,12 @@ def WSP2W(wsp, **kwargs):
826827
827828
828829
"""
829-
wsp.sparse
830-
indices = wsp.sparse.indices
831830
data = wsp.sparse.data
832831
indptr = wsp.sparse.indptr
833832
id_order = wsp.id_order
834833
if id_order:
835834
# replace indices with user IDs
836-
indices = [id_order[i] for i in indices]
835+
indices = [id_order[i] for i in wsp.sparse.indices]
837836
else:
838837
id_order = list(range(wsp.n))
839838
neighbors, weights = {}, {}
@@ -1086,12 +1085,7 @@ def get_points_array(iterable):
10861085
]
10871086
)
10881087
else:
1089-
data = np.vstack(
1090-
[
1091-
np.array(shape.centroid)
1092-
for shape in first_choice
1093-
]
1094-
)
1088+
data = np.vstack([np.array(shape.centroid) for shape in first_choice])
10951089
except AttributeError:
10961090
data = np.vstack([shape for shape in backup])
10971091
return data

libpysal/weights/weights.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,67 @@ def from_shapefile(cls, *args, **kwargs):
234234

235235
@classmethod
236236
def from_WSP(cls, WSP, silence_warnings=True):
237-
return WSP2W(WSP, silence_warnings=silence_warnings)
237+
"""Create a pysal W from a pysal WSP object (thin weights matrix).
238+
239+
Parameters
240+
----------
241+
wsp : WSP
242+
PySAL sparse weights object
243+
244+
silence_warnings : bool
245+
By default ``libpysal`` will print a warning if the dataset contains
246+
any disconnected components or islands. To silence this warning set this
247+
parameter to ``True``.
248+
249+
250+
Returns
251+
-------
252+
w : W
253+
PySAL weights object
254+
255+
Examples
256+
--------
257+
>>> from libpysal.weights import lat2W, WSP, W
258+
259+
Build a 10x10 scipy.sparse matrix for a rectangular 2x5 region of cells
260+
(rook contiguity), then construct a PySAL sparse weights object (wsp).
261+
262+
>>> sp = lat2SW(2, 5)
263+
>>> wsp = WSP(sp)
264+
>>> wsp.n
265+
10
266+
>>> wsp.sparse[0].todense()
267+
matrix([[0, 1, 0, 0, 0, 1, 0, 0, 0, 0]], dtype=int8)
268+
269+
Create a standard PySAL W from this sparse weights object.
270+
271+
>>> w = W.from_WSP(wsp)
272+
>>> w.n
273+
10
274+
>>> print(w.full()[0][0])
275+
[0 1 0 0 0 1 0 0 0 0]
276+
"""
277+
data = WSP.sparse.data
278+
indptr = WSP.sparse.indptr
279+
id_order = WSP.id_order
280+
if id_order:
281+
# replace indices with user IDs
282+
indices = [id_order[i] for i in WSP.sparse.indices]
283+
else:
284+
id_order = list(range(WSP.n))
285+
neighbors, weights = {}, {}
286+
start = indptr[0]
287+
for i in range(WSP.n):
288+
oid = id_order[i]
289+
end = indptr[i + 1]
290+
neighbors[oid] = indices[start:end]
291+
weights[oid] = data[start:end]
292+
start = end
293+
ids = copy.copy(WSP.id_order)
294+
w = W(neighbors, weights, ids, silence_warnings=silence_warnings)
295+
w._sparse = copy.deepcopy(WSP.sparse)
296+
w._cache["sparse"] = w._sparse
297+
return w
238298

239299
@classmethod
240300
def from_adjlist(
@@ -452,7 +512,7 @@ def to_sparse(self, fmt="coo"):
452512
n = self.n
453513
return disp[fmt_l]((data, (row, col)), shape=(n, n))
454514
else:
455-
warnings.warn(f"{fmt} not supported.")
515+
raise ValueError(f"unsupported sparse format: {fmt}")
456516

457517
@property
458518
def n_components(self):

0 commit comments

Comments
 (0)