Code Monkey home page Code Monkey logo

peaks.jl's Introduction

Peaks.jl

version pkgeval stable-docs dev-docs CI codecov Project Status: Active – The project has reached a stable, usable state and is being actively developed.

Peaks.jl contains peak (local extrema) finding functions for vector data. Contributions welcome.

signal-with-peaks-prominences-and-widths

julia> using Peaks

julia> t = 0:1/100:1;

julia> y = 2*sin.(5*t)+3*sin.(10*t)+2*sin.(30*t);

julia> pks, vals = findmaxima(y)
([8, 26, 48, 70, 88], [4.344867409921723, 5.5693856245725195, 0.42179571038522123, 3.050541716751975,
1.765468536605815])

julia> pks, proms = peakproms(pks, y)
([8, 26, 48, 70, 88], [1.9441651653930858, 5.5693856245725195, 2.203426259167901, 6.0957723300230855,
2.195991801053836])

julia> pks, widths, leftedge, rightedge = peakwidths(pks, y, proms)
([8, 26, 48, 70, 88], [7.168551512183585, 13.02544712081329, 8.262715646139178, 13.80559202119737,
7.663187146933097], [4.916043956211862, 18.50125024651451, 43.35170982447645, 63.83409366134414, 84.28425741824285],
[12.084595468395447, 31.5266973673278, 51.61442547061563, 77.63968568254151, 91.94744456517594])

julia> _, proms = peakproms!(pks, y; minprom=1)
([8, 26, 48, 70, 88], [1.9441651653930858, 5.5693856245725195, 2.203426259167901, 6.0957723300230855, 2.195991801053836])

julia> using Plots
julia> plotpeaks(t, y, peaks=pks, prominences=true, widths=true)  # see above plot for result

Features

  • Find peaks (maxima or minima), peak prominence, and peak width
    • Filter peaks by peak spacing (window size), prominence, and width
    • Compute "Full Width Half Maximum" (FWHM) of discrete or sampled functions using peakwidths
  • Fully supports NaN/missing with optional tolerance using keyword arg strict:
    • Conventional handling/propagation of NaN/missing when strict = true (the default)
      julia> argmaxima([missing,2,0,1,1,0]) # equivalent to [2,0,1,1,0]
      1-element Vector{Int64}:
       4
      
      julia> peakproms([2,4], [NaN,2,0,1,1,0])
      ([2, 4], [NaN, 1.0])
      
      julia> peakwidths([2,4], [NaN,2,0,1,1,0], [2,1])
      ([2, 4], [NaN, 2.0], [NaN, 3.5], [2.5, 5.5])
    • Reasonable alternatives when strict = false
      julia> argmaxima([missing,2,0,1,1,0]; strict=false)
      2-element Vector{Int64}:
       2
       4
      
      julia> peakproms([2,4], [NaN,2,0,1,1,0]; strict=false)
      ([2, 4], [2.0, 1.0])
      
      julia> peakwidths([2,4], [NaN,2,0,1,1,0], [2,1]; strict=false)
      ([2, 4], [1.5, 2.0], [1.0, 3.5], [2.5, 5.5])

Related

peaks.jl's People

Contributors

github-actions[bot] avatar halleysfifthinc avatar jishnub avatar kronosthelate avatar mrhenning avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

peaks.jl's Issues

Performance

I am a little bit surprised about the performance of argmaxima compared with a direct implementation:

using Test, BenchmarkTools
using Peaks

function localmaxmin!(y, maxs::Vector{Int}, mins::Vector{Int})
   empty!(maxs)
   empty!(mins)
   for i in 2:length(y)-1
       if y[i+1] < y[i] > y[i-1]
           push!(maxs, i)
       elseif y[i+1] > y[i] < y[i-1]
           push!(mins, i)
       end
   end
end

function localmaxmin2!(x, maxs, mins)
   append!(maxs2, argmaxima(x))
   append!(mins2, argminima(x))
   maxs2, mins2
end

function localmax(y)
   maxs = Int[]
   for i in 2:length(y)-1
       if y[i+1] < y[i] > y[i-1]
           push!(maxs, i)
       end
   end
   maxs
end

function localmax2(x)
   maxs2 = argmaxima(x)
end

maxs = Int[]
mins  = Int[]
maxs2= Int[]
mins2 = Int[]
x = zeros(100000)
x_max = 2:100:100000
x[x_max] .= 1
x_min = 5:100:100000
x[x_min] .= -1
localmaxmin!(x, maxs, mins)
localmaxmin2!(x, maxs2, mins2)

@test maxs == maxs2
@test mins == mins2

@btime localmax(x)
@btime localmax2(x)
@test localmax(x) == localmax2(x)

On my laptop, this returns

102.428 μs (10 allocations: 16.39 KiB)
380.387 μs (6 allocations: 398.92 KiB)
Test Passed

which is 3 times slower.

add 4 to compat?

from a cursory glance there shouldn't be any issues with that

peakprom can't find a peak within the minimum distance `w` of the boundary

Calculating the extrema of an array with peakprom it would be nice to be able to switch off the strictbounds imposed by the maxima/minima.
Maybe something like

...
@eval begin
    function peakprom(x::AbstractVector{T}, ::$Extrema, w=1, minprom::T=zero(T), strictbounds::Bool=true) where T
        if ($Extrema) === Maxima
            m = maxima(x, w, strictbounds)
        else
            m = minima(x, w, strictbounds)
        end
...

would already be enough.

Add absolute lower value

Again, this is not hard to implement manually. But it would be very nice to be able to specify a lower bound for not only the peak prominence, but also the absolute value.

Add number of peaks to look for

It is currently not hard to do manually, but it would be very convenient to have a keyword argument to some function see #24 that set the maximal and/or minimal number of peaks to look for, defaulting to 0 and Inf. I am using this in the context of calibrating a spectrogram, where I know the number of peaks I am looking for in advance, and in reality I just want the 4 most prominent peaks.

Better discoverability of functions and keyword arguments

Currently, the readme and plot therein look to me like they demo the package. However, they do not show examples of setting the minimum window width w, nor do they demo the peakheights function.

I suggest:
a) All functions are shown in the readme, perhaps in a complete list, or that
b) the readme is made more barebones, and that users are referred to the documentation for fuller examples, which should include examples using peakheight and w?

Or some combination of the two:
"The filtering functions are seen in the table below, and full examples can be found in the docs"

Bibliography

HI,
Do you have some bibliography explaining the implemented code?
I would like to understand how the algorithm of finding peaks (and the prominences) works
Thank you!
Ignacio

peakheights and other not working with array of floats

Hello,

I'm trying to use this code to find peak position from an array of floats. However I'm getting this error:
image

If I run the example code with this array x = [0,5,2,3,3,1,4,0]; I do get a result, any idea of what is the problem?

Thanks in advance!

Wrong local maximum

I tried to to find the local maxima of my data with this package. It worked very well except for two points where the results seem wrong. An example with reduced data:

maxPeakfalse

Here is the code to reproduce this with the attached data (with CSV.jl):

w = 327
CSVData = CSV.File("WrongPeak.csv")
x = CSVData.Column1
y = CSVData.Column2
pks, vals = findmaxima(y, w)
plotpeaks(x, y, peaks=pks, scale=:log10)

WrongPeak.csv

Am I doing something wrong here?

plotpeaks doesn't exist?

(@v1.8) pkg> activate --temp

(jl_H5YLNr) pkg> add Peaks Plots
--- snip ---
Precompiling project...
  41 dependencies successfully precompiled in 70 seconds. 94 already precompiled.

(jl_H5YLNr) pkg> st
Status `/tmp/jl_H5YLNr/Project.toml`
  [18e31ff7] Peaks v0.4.1
  [91a5bcdd] Plots v1.36.2

julia> using Peaks, Plots

julia> plotpeaks
ERROR: UndefVarError: plotpeaks not defined

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

Unusual behavior of peakprom?

Maxima of an array of all zeros is

julia> peakprom(zeros(10), Maxima())
([2, 4, 6, 8], [0.0, 0.0, 0.0, 0.0])

instead of (Int64[], Float64[]).

findpeaks from pracma

hi,

I've implemented findpeaks as found in the pracma R package (https://github.com/cran/pracma). The original author gave his OK for a transcription into Julia, now the question is, if you are open to accept this function as a PR. (I don't want to create/register/maintain a package just for a single function, and your package Peaks.jl looks like a good candidate to include such a function).

The API is not great

EDIT: See later comments, as the proposed API has changed significantly during this thread.

I find the current implementation of

  1. finding peaks
  2. redefining pks with the return values from peakproms and peakwidths
    to be rather weird and un-ergonomic. Something like what is implemented in https://github.com/tungli/Findpeaks.jl seems way more inntuitive for me, where there is only one function, and the peaks can be filtered by prominence and width directly by keyword arguments.

If a redo is on the table, I just want to mention an idea I have had:
With the package called "Peaks", I was expecting "findpeaks" to be the main function. It could return a FoundPeaks object (Can not be same as package, perhaps other names for the type is better) that contains the peaks, the indices, the prominence, and the widths. It would also be nice to be able to provide the x-values, and get the corresponding x-values for the peaks and widths in terms of x-values, rather than having to work with indices always. Ofc the indices should also be part of the returned object.

Then, the API could be something like

xs = 0:0.1:10
ys = rand(length(xs))
pks = findpeaks(ys)  # Only have indices as x-units
pks = findpeaks(xs, ys)  # Return x-coordinate of peaks, and width in units of the x-coordinate

# Access fields, reads almost like english words.
# Internal implementation could be pks.fieldname, allowing 
# internal changes to be non-breaking in the future to remain flexible

index(pks)
location(pks)  # for getting x-vals
width(pks)
width(pks, ind=false)  # kwarg to select indices or x-vals as unit
prominence(pks)
seperation(pks)  # Or just exemplify with  `diff(index(pks))` or `diff(location(pks)`

Functions are not importing

On Julia v1.0 I installed the package Pkg.add("Peaks") and then using Peaks and none of the functions are usable. My interpreter doesn't recognize the commands.

How to set width in `findmaxima`

If I have a vector v, and want to find all peaks so that it is the highest value in the interval [i-w:i+w], how do I do it? The docs says

findmaxima(x[, w=1; strict=true]) -> (idxs, vals)

but I really do not understand what syntax to use, e.g.

findmaxima(x[, w=1; strict=true])

does not work. I can do

findmaxima(x)

but then I have w=1, which I not necessarily want.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.