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

fixes utils.detect2peaks not detecting peaks at borders #88

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions amep/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,11 @@ def detect2peaks(
Detects the two highest peaks in the given data after smoothing it
with a running average. Includes also global maxima at the boundaries.

Notes
-----
This method only produces correct results if there are at most two
peaks present! Please check the behavior in your individual case.

Parameters
----------
xdata : np.ndarray
Expand All @@ -983,7 +988,7 @@ def detect2peaks(
avydata : np.ndarray
Averaged y values.
avxdata : np.ndarray
Averaged x values (same shape as avxdata).
Averaged x values (same shape as avydata).

Examples
--------
Expand Down Expand Up @@ -1021,6 +1026,12 @@ def detect2peaks(
# smooth data with running mean
avxdata = runningmean(xdata, nav, mode='valid')
avydata = runningmean(ydata, nav, mode='valid')

# detecting maxima at the borders
# adding "virtual" points at the beginning and end of data
dx=avxdata[1]-avxdata[0]
avxdata=np.concatenate(([avxdata[0]-dx], avxdata, [avxdata[-1]+dx]))
avydata=np.concatenate(([np.min(avydata)], avydata, [np.min(avydata)]))

# determine peaks in averaged data
ind, other = signal.find_peaks(avydata, height=height, distance=distance, width=width)
Expand Down Expand Up @@ -1053,7 +1064,8 @@ def detect2peaks(
high = [np.max(avxdata[ind[largest]]),\
avydata[ind[largest]][np.argmax(avxdata[ind[largest]])]] # high x peak

return low, high, avydata, avxdata
# remove virtual points in avxdata and avydata
return low, high, avydata[1:-1], avxdata[1:-1]



Expand Down
Loading