How can I interpret the parameter 'width' in scipy peak detection?

Viewed 81

I'm still not pretty sure how I can understand the parameter 'width' in detecting peaks using scipy. Here's a histogram I'm considering: enter image description here If I set

peaks, _ = find_peaks(probsuc,width=4)
peak_prominences(probsuc, peaks, wlen=7)

Then the output looks like

(array([0.20229167, 0.25791667]), array([ 4, 39]), array([10, 45]))

However, if I set width=5, then there's only one peak detectable:

(array([0.20229167]), array([4]), array([10]))

And no peak could be detected if width=6. I'm wondering what width exactly means? (I do not quite understand the description on the documentation) If I want to detect the two major peaks on the histogram, is there a general strategy to set the value of width?

Thanks:)

1 Answers

The width parameter tells the function to only consider a peak as a local maximum if a certain number of consecutive samples or bins exceeds the threshold and height parameters.

The number of peaks decreases as you increase the value of width because you are adding an increasingly stringent condition. In your example, one of the peaks is not found with width=5 because the height of the five bins does not meet the conditions to be considered a local max.

With width being a hyperparameter, there is no single correct way of finding its best value. One way you could find a good width candidate would be to generate many sequences and manually count the number of peaks in order to create a labeled dataset. Then you could run a grid search to find the most accurate width, such as the width that leads to the lowest mean squared error.

Related