Skip to content

Commit 1d63261

Browse files
authored
Merge pull request #321 from ljwolf/adjlist
represent islands as self-joins with zero weight in adjacency lists
2 parents 5b386cf + 3cd1b29 commit 1d63261

File tree

2 files changed

+114
-100
lines changed

2 files changed

+114
-100
lines changed

libpysal/weights/tests/test_adjlist.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,30 @@ def test_filter(self):
3434
alist = grid.to_adjlist(remove_symmetric=True)
3535
assert len(alist) == 4
3636
with self.assertRaises(AssertionError):
37-
badgrid = weights.W.from_adjlist(alist)
38-
np.testing.assert_allclose(badgrid.sparse.toarray(), grid.sparse.toarray())
37+
# build this manually because of bug libpysal#322
38+
alist_neighbors = alist.groupby('focal').neighbor.apply(list).to_dict()
39+
all_ids = set(alist_neighbors.keys()).union(*map(set, alist_neighbors.values()))
40+
for idx in set(all_ids).difference(set(alist_neighbors.keys())):
41+
alist_neighbors[idx] = []
42+
badgrid = weights.W(alist_neighbors)
43+
np.testing.assert_allclose(badgrid.sparse.toarray(),
44+
grid.sparse.toarray())
3945
assert set(alist.focal.unique()) == {0, 1, 2}
4046
assert set(alist.neighbor.unique()) == {1, 2, 3}
4147
assert alist.weight.unique().item() == 1
4248
grid = lat2W(2, 2, id_type="string")
4349
alist = grid.to_adjlist(remove_symmetric=True)
4450
assert len(alist) == 4
4551
with self.assertRaises(AssertionError):
46-
badgrid = weights.W.from_adjlist(alist)
47-
np.testing.assert_allclose(badgrid.sparse.toarray(), grid.sparse.toarray())
48-
print(alist)
49-
tuples = set([tuple(t) for t in alist[["focal", "neighbor"]].values])
52+
# build this manually because of bug libpysal#322
53+
alist_neighbors = alist.groupby('focal').neighbor.apply(list).to_dict()
54+
all_ids = set(alist_neighbors.keys()).union(*map(set, alist_neighbors.values()))
55+
for idx in set(all_ids).difference(set(alist_neighbors.keys())):
56+
alist_neighbors[idx] = []
57+
badgrid = weights.W(alist_neighbors)
58+
np.testing.assert_allclose(badgrid.sparse.toarray(),
59+
grid.sparse.toarray())
60+
tuples = set([tuple(t) for t in alist[['focal','neighbor']].values])
5061
full_alist = grid.to_adjlist()
5162
all_possible = set([tuple(t) for t in full_alist[["focal", "neighbor"]].values])
5263
assert tuples.issubset(all_possible), (

0 commit comments

Comments
 (0)