Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add searchsortednext and searchsortedprevious #4

Merged
merged 3 commits into from
Feb 18, 2023

Conversation

SynapticSage
Copy link
Contributor

@SynapticSage SynapticSage commented Feb 9, 2023

Adding searchsortednext and searchsortedprevious

Two common tasks involving finding the nearest point, but not the same: finding the "nearest but greater than point", ie next point, or "nearest but smaller than point", ie previous point. I added two methods for those.

We often use that sort of functionality in animal behavior data when we want a label corresponding to the beginning or the end of a trial.

It's true that one could type searchsortedfirst(search_item .<= sorted_list, true) to find the nearest next point or searchsortedfirst(search_item .<= sorted_list, true) -1 to find the previous. But encountering lines like those in someone else's code can be opaque. Better to have a function that more or less documents the action taken. Hence why it's nice to have these.

It's also worth mentioning that there's some precedence for these modes in other languages. Matlab's interp1 has method=nearest setting, but also next and previous. (see https://www.mathworks.com/help/matlab/ref/interp1.html#btwp6lt-1-method)

…and previous options in addition to nearest. Can be useful for inferring eg DIO values from a sparse record of their change point states.
@joshday
Copy link
Owner

joshday commented Feb 16, 2023

Sorry for the delay in reviewing (I'm currently on parental leave). I'll try to take a look before the end of the week.

@SynapticSage
Copy link
Contributor Author

No rush 👍

(Very minor PR - it can wait.)

@codecov
Copy link

codecov bot commented Feb 18, 2023

Codecov Report

Merging #4 (c043ff5) into main (2f58809) will decrease coverage by 9.10%.
The diff coverage is 85.71%.

@@             Coverage Diff             @@
##              main       #4      +/-   ##
===========================================
- Coverage   100.00%   90.90%   -9.10%     
===========================================
  Files            1        1              
  Lines            8       22      +14     
===========================================
+ Hits             8       20      +12     
- Misses           0        2       +2     
Impacted Files Coverage Δ
src/SearchSortedNearest.jl 90.90% <85.71%> (-9.10%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@joshday
Copy link
Owner

joshday commented Feb 18, 2023

LGTM. Thanks for the PR. I appreciate the rationale and tests!

@joshday joshday merged commit fd0276e into joshday:main Feb 18, 2023
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.

None yet

2 participants