1

In the graph below, I want to find the major peaks of gray line. I used scipy.signal.find_peak and got the blue line as a result.

I used threshold to filter some minor peaks out, but that also filtered out peaks in the yellow circle, who has large height on one side but a small one on the other.

Source Code

After running this code, you will see a crosshair & will see the value of each point when hovering mouse over it.

line={'x':[3405.024, 3405.312, 3406.176, 3406.464, 3406.752, 3407.04, 3407.328, 3408.192, 3408.48, 3408.768, 3409.056, 3409.344, 3410.208, 3410.496, 3410.784, 3411.072, 3411.36, 3412.224, 3412.512, 3412.8, 3413.088, 3413.376, 3414.24, 3414.528, 3414.816, 3415.104, 3415.392, 3416.256, 3416.544, 3416.832, 3417.12, 3417.408, 3418.272, 3418.56, 3418.848, 3419.136, 3419.424, 3420.288, 3420.576, 3420.864, 3421.152, 3421.44, 3422.304, 3422.592, 3422.88, 3423.168, 3423.456, 3424.32, 3424.608, 3424.896, 3425.184, 3425.472, 3426.336, 3426.624, 3426.912, 3427.2, 3427.488, 3428.352, 3428.64, 3428.928, 3429.216, 3429.504, 3430.368, 3430.656, 3430.944, 3431.232, 3431.52, 3432.384, 3432.672, 3432.96, 3433.248, 3433.536, 3434.4, 3434.688, 3434.976, 3435.264, 3435.552, 3436.416, 3436.704, 3436.992, 3437.28, 3437.568, 3438.432, 3438.72, 3439.008, 3439.296, 3439.584, 3440.448, 3440.736, 3441.024, 3441.312, 3441.6, 3442.464, 3442.752, 3443.04, 3443.328, 3443.616, 3446.784, 3447.072, 3447.36, 3447.648, 3448.512, 3448.8, 3449.088, 3449.376, 3449.664, 3450.528, 3450.816, 3451.104, 3451.392, 3451.68, 3452.544, 3452.832, 3453.12, 3453.408, 3453.696, 3454.56, 3454.848, 3455.136, 3455.424, 3455.712, 3456.576, 3456.864, 3457.152, 3457.44, 3457.728, 3458.592, 3458.88, 3459.168, 3459.456, 3459.744, 3460.608, 3460.896, 3461.184, 3461.472, 3461.76, 3462.624, 3462.912, 3463.2, 3463.488, 3463.776, 3464.64, 3464.928, 3465.216, 3465.504, 3465.792, 3466.656, 3466.944, 3467.232, 3467.52, 3467.808, 3468.672, 3468.96, 3469.248, 3469.536, 3469.824, 3470.688, 3470.976, 3471.552, 3471.84, 3472.704, 3472.992, 3473.28, 3473.568, 3473.856, 3474.72, 3475.008, 3475.296, 3475.584, 3475.872, 3476.736, 3477.024, 3477.312, 3477.6, 3477.888, 3478.752, 3479.04, 3479.328, 3482.784, 3483.072, 3483.36, 3483.648, 3483.936, 3484.8, 3485.088, 3485.376, 3485.664, 3485.952, 3486.816, 3487.104, 3487.392, 3487.68, 3487.968, 3488.832, 3489.12, 3489.408, 3489.696, 3489.984, 3490.848, 3491.136],'y':[2.8704988033738155, 2.855552848626523, 2.8394408991556035, 2.8760938091928283, 2.839230155752019, 2.842457658803069, 2.8410261433228166, 2.8573861450756612, 2.857387021980881, 2.806297422602761, 2.7971283818671346, 2.7816507103799193, 2.731688047826982, 2.7418725745971857, 2.727028143376022, 2.776373497840072, 2.801856492953382, 2.7920703904952764, 2.8003712817530464, 2.7848618280702757, 2.7786797981369897, 2.7352797514124574, 2.741999007214465, 2.7920810188363783, 2.801841637046689, 2.832213052750878, 2.8819813363787383, 2.9318568069410413, 2.981497613758153, 3.0011931879816136, 2.9941795591480016, 2.9941645557792027, 2.9478162494706575, 2.9775519414015075, 2.982208419457967, 2.956031947959084, 2.9583227973910504, 2.964161293518658, 2.9712130327007737, 2.9498071809929405, 2.947371456134651, 2.9521622178702605, 2.9371266647017547, 2.93215460629238, 2.9514305014085975, 2.949006587519915, 2.9596092363229705, 2.934544963099511, 2.9259999819162847, 2.9197764455993918, 2.9089066976394196, 2.9100614536904934, 2.9161729891245227, 2.911935999891451, 2.896417726428179, 2.898940273843373, 2.8951579879203897, 2.886189355260204, 2.9091151063692093, 2.8992706703011177, 2.895453863515079, 2.8788995783497784, 2.8789845854022316, 2.89047933320601, 2.8979224192980593, 2.8881360937889298, 2.896751710195652, 2.8790824784924367, 2.8969734235605875, 2.898082683905878, 2.909342251831121, 2.9070673254146855, 2.8872077101208267, 2.891123474503871, 2.888572314334519, 2.8817986291265023, 2.8753337226251725, 2.8737598581078565, 2.8783575789921154, 2.867808253209416, 2.8497945212357005, 2.8392950365274237, 2.8153576260700426, 2.8185456929134998, 2.8253019924080207, 2.8288712405608827, 2.8221710322062323, 2.8100183794152946, 2.7681498575125953, 2.742390211943808, 2.792849100488907, 2.790026871755891, 2.792735260255324, 2.7746751189360492, 2.7928607427598386, 2.778956818305892, 2.774587510597919, 2.7629222212014386, 2.7385934473474234, 2.745969768703785, 2.7503317074577964, 2.7649130418783114, 2.767732897488769, 2.772028865305056, 2.7619817721377706, 2.7706064899620073, 2.7763126454998215, 2.786212313698737, 2.802673026727067, 2.78340104867985, 2.758183878749916, 2.743656235653572, 2.7701133956928436, 2.760025238913161, 2.7642904134655, 2.7599647903480373, 2.768544885546413, 2.7913152045520464, 2.7854886131704686, 2.7812735815987657, 2.7320161850757247, 2.7231157434265394, 2.6751482107427886, 2.681411567472767, 2.637660451225097, 2.6278522128296027, 2.596465035825378, 2.6169615566163595, 2.5683539101775836, 2.5225643565877998, 2.5427818533230595, 2.5698387209196802, 2.5203688447388806, 2.5314195096680696, 2.5623730621802134, 2.5853420641486036, 2.540136269265092, 2.565455618183025, 2.575464093057478, 2.578758890008093, 2.6105825154513993, 2.5903245474265875, 2.5696223783321663, 2.557233685735461, 2.5978400470167387, 2.611454530182371, 2.606400722406021, 2.6182183639355108, 2.606302545314764, 2.6113411921707956, 2.614691105551711, 2.6279869932301434, 2.619688293530369, 2.6263497361070973, 2.5762707320437723, 2.5516833894135105, 2.5372918619107634, 2.5171641021619755, 2.4983723964184352, 2.5153086713506463, 2.5448086235658294, 2.539356448084917, 2.5895495696020316, 2.6015979885697456, 2.5793537898839447, 2.596757942183393, 2.634237347235472, 2.62573269109998, 2.648973280370734, 2.653850410062133, 2.667541954741026, 2.65954303961076, 2.673108862263467, 2.6476972988331102, 2.6453257760740194, 2.667986307466183, 2.6675609678227357, 2.66105845423031, 2.647232437711875, 2.649478337561501, 2.6590103208820617, 2.6683168473509338, 2.6639166879519123, 2.6527879524755393, 2.6491881433080122, 2.6554116420393234, 2.659090848486373, 2.6190258290305173, 2.6189826877638294, 2.6471901347633344, 2.646685092454985, 2.6367726108298832, 2.6446671748512705, 2.6461789397586024, 2.6534272148910043, 2.640544131372565, 2.6259370611098025, 2.6192824324516666, 2.577783385627213, 2.581229718064362]}

from scipy.signal import find_peaks
from bokeh.plotting import figure
from bokeh.io import output_notebook, show
from bokeh.models import HoverTool, CrosshairTool

max_index, _ = find_peaks(line['y'], prominence=0.0464)
max_x = [line['x'][i] for i in max_index]
max_y = [line['y'][i] for i in max_index]
peak = {'x':max_x,'y':max_y}

hover_tool = HoverTool(tooltips=[('x','@x{0,0.00}'),('y','@y{0,0.00}')], formatters={'@x':'datetime'}, mode='vline')
crosshair = CrosshairTool(dimensions="both",line_color='magenta', line_width=0.5)
plot = figure()
plot.line(line['x'],line['y'],legend_label='line',line_color='gray')
plot.circle(line['x'],line['y'],legend_label='line',color='gray')
plot.line(peak['x'],peak['y'],legend_label='peak',line_color='red')
plot.circle(peak['x'],peak['y'],legend_label='peak',color='red')
plot.legend.click_policy="hide"
plot.add_tools(hover_tool)
plot.add_tools(crosshair)
output_notebook()
show(plot)

enter image description here

Quesiton

Is there a way to detect peaks in the yellow circles, which has big height on one side and small on on the other, to make the peak line goes as the blue one? It doesn't have to be SciPy.

enter image description here

David H. J.
  • 340
  • 2
  • 12
  • 1
    It would be better if you shared 1) your latest/greatest code 2) a list of the data points and 3) your expected output. Thank you. – Mark Setchell Jul 21 '22 at 19:47
  • 1
    @MarkSetchell I added source code, raw data, and expected output. – David H. J. Jul 22 '22 at 07:02
  • What are the numeric values of the yellow points? – Dani Mesejo Jul 22 '22 at 07:25
  • 1
    Maybe you could run the function with the `Prominence` set to `None` and get the `Properties` returned and then select peaks where **either** prominence is large, rather than both prominences? – Mark Setchell Jul 22 '22 at 07:31
  • @DaniMesejo first is (3433,2.91), second is (3436,2.88). Also, I update the code so you can see the value on graph. – David H. J. Jul 22 '22 at 07:37
  • @MarkSetchell I tried your way, `max_index, properties = find_peaks(line['y'])`, and the result of `print(properties)` is an empty dictionary `{}`. How should fix it? – David H. J. Jul 22 '22 at 08:16
  • I'm not near a real computer, but think you need `find_peaks(..., prominence=None)` – Mark Setchell Jul 22 '22 at 08:24
  • @MarkSetchell Neither that works unfortunately. In [this question](https://stackoverflow.com/questions/73077442/scipy-signal-find-peaks-return-empty-properties) it says that I need to set prominence to a value. After that, could you show me the following steps to filter peaks with one prominence being large? – David H. J. Jul 22 '22 at 09:09

0 Answers0