From 16205dbe3fcca1cffa87d1cefdaef7a6fcf25411 Mon Sep 17 00:00:00 2001 From: dcodrut Date: Fri, 22 Aug 2025 00:35:45 +0200 Subject: [PATCH 1/4] fix the RNG-2 condition --- libpysal/weights/gabriel.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libpysal/weights/gabriel.py b/libpysal/weights/gabriel.py index 82a111539..b758b34c7 100644 --- a/libpysal/weights/gabriel.py +++ b/libpysal/weights/gabriel.py @@ -332,7 +332,7 @@ def _filter_relativehood(edges, coordinates, return_dkmax=False): 2. for each edge of the delaunay (i,j), compute dkmax = max(d(k,i), d(k,j)) for k in 1..n, k != i, j 3. for each edge of the delaunay (i,j), prune - if any dkmax is greater than d(i,j) + if any dkmax is smaller than d(i,j) """ n = edges.max() out = [] @@ -345,11 +345,12 @@ def _filter_relativehood(edges, coordinates, return_dkmax=False): dij = ((pi - pj) ** 2).sum() ** 0.5 prune = False for k in range(n): + if k == i or k == j: + continue pk = coordinates[k] dik = ((pi - pk) ** 2).sum() ** 0.5 djk = ((pj - pk) ** 2).sum() ** 0.5 - distances = numpy.array([dik, djk, dkmax]) - dkmax = distances.max() + dkmax = max(dik, djk) prune = dkmax < dij if (not return_dkmax) & prune: break From d39206309c58bb295df6df14adcca170f050b7b3 Mon Sep 17 00:00:00 2001 From: dcodrut Date: Fri, 22 Aug 2025 00:36:34 +0200 Subject: [PATCH 2/4] include the last node as edges is zero-based --- libpysal/weights/gabriel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libpysal/weights/gabriel.py b/libpysal/weights/gabriel.py index b758b34c7..d82dcce88 100644 --- a/libpysal/weights/gabriel.py +++ b/libpysal/weights/gabriel.py @@ -334,7 +334,7 @@ def _filter_relativehood(edges, coordinates, return_dkmax=False): 3. for each edge of the delaunay (i,j), prune if any dkmax is smaller than d(i,j) """ - n = edges.max() + n = len(coordinates) out = [] r = [] for edge in edges: From 9a70b3943e567518a6a04bf59b31d0d61f175981 Mon Sep 17 00:00:00 2001 From: dcodrut Date: Fri, 22 Aug 2025 13:10:54 +0200 Subject: [PATCH 3/4] avoid multiple equality comparisons --- libpysal/weights/gabriel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libpysal/weights/gabriel.py b/libpysal/weights/gabriel.py index d82dcce88..2ef8cdaf0 100644 --- a/libpysal/weights/gabriel.py +++ b/libpysal/weights/gabriel.py @@ -345,7 +345,7 @@ def _filter_relativehood(edges, coordinates, return_dkmax=False): dij = ((pi - pj) ** 2).sum() ** 0.5 prune = False for k in range(n): - if k == i or k == j: + if k in (i, j): continue pk = coordinates[k] dik = ((pi - pk) ** 2).sum() ** 0.5 From f134a92bc5f0099109ba85b875e4ec714f0bbbb3 Mon Sep 17 00:00:00 2001 From: dcodrut Date: Fri, 22 Aug 2025 13:42:47 +0200 Subject: [PATCH 4/4] fix test and check that RNG <= D --- libpysal/weights/tests/test_gabriel.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libpysal/weights/tests/test_gabriel.py b/libpysal/weights/tests/test_gabriel.py index 9dcbc5de9..5aefa5a48 100644 --- a/libpysal/weights/tests/test_gabriel.py +++ b/libpysal/weights/tests/test_gabriel.py @@ -39,8 +39,12 @@ def test_rng(): assert e.neighbors == f.neighbors + # RNG should be a subgraph of Delaunay + for k, neighbors in e.neighbors.items(): + assert set(neighbors) <= set(dty[k]) + assert e[1] != dty[1] - assert list(e[1].keys()) == [0, 3, 6, 30, 38] + assert list(e[1].keys()) == [0, 3] for focal, neighbors in e.neighbors.items(): dneighbors = dty[focal] assert set(neighbors) <= set(dneighbors)