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

get_unit_adaptive_window leads to recursion errors if there are no peaks. #3581

Open
zm711 opened this issue Dec 16, 2024 · 0 comments
Open
Labels
bug Something isn't working curation Related to curation module

Comments

@zm711
Copy link
Collaborator

zm711 commented Dec 16, 2024

In the adaptive window code it tries to find the win_size by taking the initial threshold set by the user and if that doesn't work then it divides by 2 to try a smaller threshold and continues. But if there is no threshold then we end up with reaching the maximum recursion depth because 0/2 will keep being 0. I don't know this bit of the code well enough to know what our base condition should be in the case that there are no peaks (maybe if a unit with bad correlograms?) so I'm no completely sure how to fix this. What do think @yger since you've updated this extensively.

function below for convenient check:

def get_unit_adaptive_window(auto_corr: np.ndarray, threshold: float) -> int:
"""
Computes an adaptive window to correlogram (basically corresponds to the first peak).
Based on a minimum threshold and minimum of second derivative.
If no peak is found over threshold, recomputes with threshold/2.
Parameters
----------
auto_corr : np.ndarray
Correlogram used for adaptive window.
threshold : float
Minimum threshold of correlogram (all peaks under this threshold are discarded).
Returns
-------
unit_window : int
Index at which the adaptive window has been calculated.
"""
import scipy.signal
if np.sum(np.abs(auto_corr)) == 0:
return 20.0
derivative_2 = -np.gradient(np.gradient(auto_corr))
peaks = scipy.signal.find_peaks(derivative_2)[0]
keep = auto_corr[peaks] >= threshold
peaks = peaks[keep]
keep = peaks < (auto_corr.shape[0] // 2)
peaks = peaks[keep]
if peaks.size == 0:
# If none of the peaks crossed the threshold, redo with threshold/2.
return get_unit_adaptive_window(auto_corr, threshold / 2)
# keep the last peak (nearest to center)
win_size = auto_corr.shape[0] // 2 - peaks[-1]
return win_size

@zm711 zm711 added curation Related to curation module bug Something isn't working labels Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working curation Related to curation module
Projects
None yet
Development

No branches or pull requests

1 participant