Repository navigation
feat(nearest): add IntervalTree.nearest() - #161
Open
SahilRajputdotexe wants to merge 1 commit into
Open
SahilRajputdotexe wants to merge 1 commit into
SahilRajputdotexe wants to merge 1 commit into
Conversation
Returns the interval closest to a point or Interval, or a list of the k closest, using the same gap as Interval.distance_to(). Ties are broken by interval order. The search widens a window around the target using the boundary table instead of scanning every interval. Refs chaimleib#114
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.
This adds
IntervalTree.nearest(target, k=None), which finds the interval closest to a point or an Interval. Closes #114.With
k=Noneit returns one Interval, orNoneif the tree is empty. With an integerkit returns a list of up tokintervals, nearest first. Distance is the existingInterval.distance_to(): 0 when the two overlap or touch, otherwise the size of the gap. Ties are broken by the normal Interval ordering, so the result is stable even when the data fields can't be compared with each other.It doesn't scan the whole tree. It looks at a window around the target, using the boundary table to include intervals exactly at the window's edge, and doubles the window until it holds
kintervals. A null Interval target raisesValueError, a negativekraisesValueError, and a non-integerkraisesTypeError.I added tests in
test/intervaltree_methods/nearest_test.py, including a randomized comparison against a brute-force sort, plus README and CHANGELOG entries. The suite passes locally on Python 3.9, 3.12 and 3.14, and the full CI matrix (2.7, 3.5 to 3.14, flake8) passes on my fork.I kept this to
nearestonly. A custom distance callable, a "within distance" query and cross-tree joins could be built on top of it if you want them, but I didn't want to widen the API in one PR.