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

fix higher_order contiguity including lower order #739

Merged
merged 3 commits into from
Jul 16, 2024
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
2 changes: 1 addition & 1 deletion libpysal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ def higher_order(self, k=2, shortest_path=True, diagonal=False, lower_order=Fals
sp = binary.sparse

if lower_order:
wk = sum(sparse.linalg.matrix_power(sp, x) for x in range(2, k + 1))
wk = sum(sparse.linalg.matrix_power(sp, x) for x in range(1, k + 1))
shortest_path = False
else:
wk = sparse.linalg.matrix_power(sp, k)
Expand Down
13 changes: 13 additions & 0 deletions libpysal/graph/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,19 @@ def test_higher_order(self):
lower = cont.higher_order(2, lower_order=True)
assert lower == expected

@pytest.mark.skipif(
Version(scipy_version) < Version("1.12.0"),
reason="sparse matrix power requires scipy>=1.12.0",
)
def test_higher_order_inclusive(self): # GH738
contig = graph.Graph.from_arrays(
[0, 1, 2, 3, 3, 4, 4], [0, 3, 4, 1, 4, 2, 3], [0, 1, 1, 1, 1, 1, 1]
)
assert len(contig) == 6
higher = contig.higher_order(2, lower_order=True)
assert len(higher) == 10
assert contig < higher

def test_n_components(self):
nybb = graph.Graph.build_contiguity(self.nybb)
assert nybb.n_components == 2
Expand Down
2 changes: 1 addition & 1 deletion libpysal/weights/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def higher_order_sp(
)

if lower_order:
wk = sum(w**x for x in range(2, k + 1))
wk = sum(w**x for x in range(1, k + 1))
shortest_path = False
else:
wk = w**k
Expand Down