Fix knn_graph self-loops with coincident points and shared network nodes - #232
Merged
yu-ta-sato merged 1 commit intoSep 21, 2026
Merged
Conversation
knn_graph dropped each point's own entry from its neighbour list by position (the first result) instead of by index. When several points share the same location, or snap to the same network node, all their distances are 0, so a point is not necessarily returned first. The result was self-loops (u, u) and a missing real neighbour. Drop the point by index in both the euclidean/manhattan and the network branches, and add regression tests for both.
pablo-legerensomolinos
force-pushed
the
fix/knn-graph-self-loops
branch
from
September 21, 2026 14:49
acff5fa to
87171f8
Compare
Author
|
Hi! The two workflows on this PR are waiting for maintainer approval ("action_required", since this is my first contribution from a fork). Could someone approve and run them when convenient? I ran the full suite locally (941 passed, incl. tests/test_graph.py`) and all pre-commit hooks pass, but Python 3.13 and the docs build are only covered by CI. Thanks! |
Collaborator
|
Hi @pablo-legerensomolinos, thanks so much for raising this PR! It totally makes sense and yes the algorithm relied on the position-based identification which should have been improved! Let me see the scripts and run them on my env, and if no problem let me merge it immediately! |
Collaborator
|
I don't think there's any issue regarding your approach. Thanks so much, let me merge it to the main branch :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
knn_graphdropped each point's own entry from its neighbour list by position(the first result) instead of by index. When several points share a location, or
snap to the same network node, all distances are 0, so a point is not necessarily
returned first. This produced self-loops
(u, u)and dropped a real neighbour.The point is now removed by index in both the euclidean/manhattan branch and the
network branch. Behaviour is unchanged when no ties are involved.
Related issues
Closes #231
Testing
uv run pytest -q(Python 3.14.3,MPLBACKEND=Agg): 941 passed, includingtests/test_graph.py;proximity.pyat 100% coverage. The 3 new regression testsfail without the fix. Also run on Python 3.12 (805 passed, excluding
tests/test_graph.py).uv run pre-commit run --all-files: all hooks pass. I run Windows withcore.autocrlf=true, soyamllintonly passes once the YAML files have LF endings;this is unrelated to the change.
MPLBACKEND=Agg, two plotting tests failed intermittently on my machine witha Tk
TclError; they pass in isolation and with Agg, and the change does not touchplotting code.
(half with forced coincident points) and 120 network trials on a grid network show no
discrepancies (the previous code showed 266 and 99).
Documentation
Not applicable.
Reviewer notes
knn_graphAPI only.node position.
delaunay_graph,gabriel_graph,relative_neighborhood_graphand
euclidean_minimum_spanning_treeraiseQhullErrorfor collinear points; I'llraise that separately.