Skip to content

Commit 969039d

Browse files
sjsreyknaaptimemartinfleis
authored
Handle set_index properly in to_adjlist (#511)
* Handle set_index properly in to_adjlist * Add option to sort/unsort return from to_adjlist * Update libpysal/weights/tests/test_adjlist.py Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> Co-authored-by: eli knaap <ek@knaaptime.com> Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
1 parent 6e805a5 commit 969039d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

libpysal/weights/tests/test_adjlist.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,45 @@ def test_map(self):
145145
np.testing.assert_allclose(
146146
data, mapped["_".join(("subtract", name))].values
147147
)
148+
149+
def test_sort(self):
150+
from libpysal import examples
151+
from libpysal.weights import Rook
152+
153+
us = geopandas.read_file(examples.get_path("us48.shp"))
154+
w = Rook.from_dataframe(us.set_index("STATE_FIPS"), use_index=True)
155+
unsorted_al = w.to_adjlist(sort_joins=False)
156+
sorted_al = w.to_adjlist(sort_joins=True)
157+
sv = ["01"] * 4
158+
sv.append("04")
159+
sv = np.array(sv)
160+
usv = np.array(["53", "53", "30", "30", "30"])
161+
np.testing.assert_array_equal(unsorted_al.focal.values[:5], usv)
162+
np.testing.assert_array_equal(sorted_al.focal.values[:5], sv)
163+
164+
def test_ids(self):
165+
df = geopandas.read_file(examples.get_path("columbus.dbf")).head()
166+
df["my_id"] = range(3, len(df) + 3)
167+
W = weights.Queen.from_dataframe(df, ids="my_id")
168+
W_adj = W.to_adjlist(drop_islands=True)
169+
for i in range(3, 8):
170+
assert i in W_adj.focal
171+
assert i in W_adj.neighbor
172+
for i in W_adj.focal:
173+
assert i in list(range(3, len(df) + 3))
174+
for i in W_adj.neighbor:
175+
assert i in list(range(3, len(df) + 3))
176+
177+
def test_str_ids(self):
178+
df = geopandas.read_file(examples.get_path("columbus.dbf")).head()
179+
snakes = ["mamba", "boa", "python", "rattlesnake", "cobra"]
180+
df["my_str_id"] = snakes
181+
W = weights.Queen.from_dataframe(df, ids="my_str_id")
182+
W_adj = W.to_adjlist(drop_islands=True)
183+
for i in snakes:
184+
(W_adj.focal == i).any()
185+
(W_adj.neighbor == i).any()
186+
for i in W_adj.focal:
187+
assert i in snakes
188+
for i in W_adj.neighbor:
189+
assert i in snakes

libpysal/weights/weights.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ def to_adjlist(
391391
focal_col="focal",
392392
neighbor_col="neighbor",
393393
weight_col="weight",
394+
sort_joins=False,
394395
):
395396
"""
396397
Compute an adjacency list representation of a weights object.
@@ -416,6 +417,10 @@ def to_adjlist(
416417
Name of the column in which to store "destination" node ids.
417418
weight_col : str
418419
Name of the column in which to store weight information.
420+
sort_joins : bool
421+
Whether or not to lexicographically sort the adjacency
422+
list by (focal_col, neighbor_col). Default is False.
423+
419424
"""
420425
try:
421426
import pandas
@@ -432,6 +437,9 @@ def to_adjlist(
432437

433438
links = []
434439
focal_ix, neighbor_ix = self.sparse.nonzero()
440+
idxs = np.array(list(self.neighbors.keys()))
441+
focal_ix = idxs[focal_ix]
442+
neighbor_ix = idxs[neighbor_ix]
435443
weights = self.sparse.data
436444
adjlist = pandas.DataFrame(
437445
{focal_col: focal_ix, neighbor_col: neighbor_ix, weight_col: weights}
@@ -443,7 +451,9 @@ def to_adjlist(
443451
{focal_col: self.islands, neighbor_col: self.islands, weight_col: 0}
444452
)
445453
adjlist = pandas.concat((adjlist, island_adjlist)).reset_index(drop=True)
446-
return adjlist.sort_values([focal_col, neighbor_col])
454+
if sort_joins:
455+
return adjlist.sort_values([focal_col, neighbor_col])
456+
return adjlist
447457

448458
def to_networkx(self):
449459
"""Convert a weights object to a ``networkx`` graph.

0 commit comments

Comments
 (0)