Skip to content

Commit

Permalink
FIX: Fix geometry.nearest_neighbors when k is bigger than the numbe…
Browse files Browse the repository at this point in the history
…r of candidates
  • Loading branch information
remi-braun committed Mar 7, 2024
1 parent 7dd558d commit 538d369
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.36.1 (2024-xx-xx)

- FIX: Fix `geometry.nearest_neighbors` when k is bigger than the number of candidates
- DOC: Update some examples in documentation

## 1.36.0 (2024-02-27)
Expand Down
5 changes: 5 additions & 0 deletions CI/SCRIPTS/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,8 @@ def test_nearest_neighbors():
assert (
curr_dist[0] < radius
), f"distance superior to wanted distance: {curr_dist[0]} > {radius}"

# Ensure it works with k > nof candidates
closest, distances = nearest_neighbors(
src, candidates, method="k_neighbors", k_neighbors=len(candidates) + 10
)
4 changes: 3 additions & 1 deletion sertit/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,9 @@ def _get_k_nearest(src_points: list, candidates: list, k_neighbors: int, **kwarg
tree = BallTree(candidates, leaf_size=15)

# Find the closest points and distances
closest_dist, closest = tree.query(src_points, k=k_neighbors, **kwargs)
closest_dist, closest = tree.query(
src_points, k=min(k_neighbors, len(candidates)), **kwargs
)

# Return indices and distances
return closest, closest_dist
Expand Down

0 comments on commit 538d369

Please sign in to comment.