Skip to content

Fix knn_graph self-loops with coincident points and shared network nodes - #232

Merged
yu-ta-sato merged 1 commit into
c2g-dev:mainfrom
pablo-legerensomolinos:fix/knn-graph-self-loops
Sep 21, 2026
Merged

yu-ta-sato merged 1 commit into
c2g-dev:mainfrom
pablo-legerensomolinos:fix/knn-graph-self-loops

Conversation

@pablo-legerensomolinos

@pablo-legerensomolinos pablo-legerensomolinos commented Sep 21, 2026 •

Copy link
Copy Markdown

Summary

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 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, including
    tests/test_graph.py; proximity.py at 100% coverage. The 3 new regression tests
    fail 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 with
    core.autocrlf=true, so yamllint only passes once the YAML files have LF endings;
    this is unrelated to the change.
  • Without MPLBACKEND=Agg, two plotting tests failed intermittently on my machine with
    a Tk TclError; they pass in isolation and with Agg, and the change does not touch
    plotting code.
  • Compared against a brute-force kNN on random data: 600 euclidean/manhattan trials
    (half with forced coincident points) and 120 network trials on a grid network show no
    discrepancies (the previous code showed 266 and 99).
  • Not run locally: Python 3.13 (left to CI).

Documentation

Not applicable.

Reviewer notes

  • Tests go through the public knn_graph API only.
  • The network branch now uses a stable sort so ties resolve deterministically by
    node position.
  • Not covered here: delaunay_graph, gabriel_graph, relative_neighborhood_graph
    and euclidean_minimum_spanning_tree raise QhullError for collinear points; I'll
    raise that separately.

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

Copy link
Copy Markdown
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!

@yu-ta-sato

Copy link
Copy Markdown
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!

@yu-ta-sato

Copy link
Copy Markdown
Collaborator

I don't think there's any issue regarding your approach. Thanks so much, let me merge it to the main branch :)

@yu-ta-sato
yu-ta-sato merged commit 8c34b04 into c2g-dev:main Sep 21, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

knn_graph returns self-loops (and misses real neighbours) for coincident points and points sharing a network node

3 participants