Skip to content

Commit

Permalink
Closes #5 Add mlpy find peaks functions mlpy.findpeaks_dist and mlpy.…
Browse files Browse the repository at this point in the history
…findpeaks_win
  • Loading branch information
MonsieurV committed Nov 5, 2018
1 parent b4e3106 commit 7c3f583
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ docs/_build/
# PyBuilder
target/

venv/
venv/

tests/mlpy-code
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ This is an overview of all the ready-to-use algorithms I've found to perform pea
| [Octave-Forge findpeaks](#octave-forge-findpeaks) | Requires an Octave-Forge distribution<br>+ PyPI package oct2py<br>Depends on Scipy | Minimum distance<br>Minimum height<br>Minimum peak width ||
| [Janko Slavic findpeaks](#janko-slavic-findpeaks) | Single function<br>Depends on Numpy | Minimum distance<br>Minimum height ||
| [Tony Beltramelli detect_peaks](#tony-beltramelli-detect_peaks) | Single function<br>Depends on Numpy | Amplitude threshold ||
| [mlpy.findpeaks_dist](#mlpyfindpeaks_dist) | Included in mlpy<br>Depends on Scipy and GSL | Minimum distance ||
| [mlpy.findpeaks_win](#mlpyfindpeaks_win) | Single function<br>Depends on Scipy and GSL | Sliding window width ||

## How to make your choice?

Expand Down Expand Up @@ -251,6 +253,44 @@ Straightforward, simple and lightweight peak detection algorithm, with minimum d

No minimum peak height filtering support.

## mlpy.findpeaks_dist

![](/images/mlpy_findpeaks_dist.png?raw=true "mlpy.findpeaks_dist")

```python
import numpy as np
import scipy.signal
vector = [0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8,
13, 8, 10, 3, 1, 20, 7, 3, 0]
print('Detect peaks with minimum distance filter.')
indexes = mlpy.findpeaks_dist(vector, mindist=2.1)
print('Peaks are: %s' % (indexes))
```

[Documentation](http://mlpy.sourceforge.net/docs/3.5/findpeaks.html#mlpy.findpeaks_dist).
[Sample code](/tests/mlpy_findpeaks_dist.py).

Find peaks, with a minimum distance filter between peaks. Code written by Davide Albanese.

## mlpy.findpeaks_win

![](/images/mlpy_findpeaks_win.png?raw=true "mlpy.findpeaks_win")

```python
import numpy as np
import scipy.signal
vector = [0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8,
13, 8, 10, 3, 1, 20, 7, 3, 0]
print('Detect peaks with sliding window of 5.')
indexes = mlpy.findpeaks_win(vector, span=5)
print('Peaks are: %s' % (indexes))
```

[Documentation](http://mlpy.sourceforge.net/docs/3.5/findpeaks.html#mlpy.findpeaks_win).
[Sample code](/tests/mlpy_findpeaks_win.py).

Find peaks, with a sliding window of specified width. Code written by Davide Albanese.

----------------------------------

# How to find both lows and highs?
Expand Down
Binary file added images/mlpy_findpeaks_dist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/mlpy_findpeaks_win.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions tests/install_mlpy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
set -e

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
MLPY_DIR="$DIR/mlpy-code"

# Pulling code.
if [ -d "$MLPY_DIR" ]; then
cd $MLPY_DIR && hg pull
else
hg clone http://hg.code.sf.net/p/mlpy/code $MLPY_DIR
fi

# We try to install mlpy on Python 2, as the install is not compatible with Python 3.7+.
# (CPython must be updated to support Python 3.7+. See https://github.com/cython/cython/issues/1978)

# Installating dependencies.
# We also install matplotlib for our tests.
pip2 install -U --user numpy scipy matplotlib

# Also requires http://www.gnu.org/software/gsl/ to be installed.

# Installing.
# http://mlpy.sourceforge.net/docs/3.5/install.html
cd $MLPY_DIR
python2 setup.py build_ext --include-dirs=/usr/include/ --rpath=/usr/lib/
python2 setup.py install --user
27 changes: 27 additions & 0 deletions tests/mlpy_findpeaks_dist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
To install the mlpy, you may read (and run) /tests/install_mlpy.sh
"""
import numpy as np
from vector import vector, plot_peaks
import mlpy

print('Detect peaks without any filters.')
indexes = mlpy.findpeaks_dist(vector)
print('Peaks are: {}'.format(indexes))
plot_peaks(
np.array(vector),
indexes,
algorithm='mlpy.findpeaks_dist'
)

print('Detect peaks with minimum distance filter.')
mdp = 2.1
indexes = mlpy.findpeaks_dist(vector, mindist=mdp)
print('Peaks are: {}'.format(indexes))
plot_peaks(
np.array(vector),
indexes,
mpd=mdp, algorithm='mlpy.findpeaks_dist'
)
28 changes: 28 additions & 0 deletions tests/mlpy_findpeaks_win.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
To install the mlpy, you may read (and run) /tests/install_mlpy.sh
"""
import numpy as np
from vector import vector, plot_peaks
import mlpy

span = 3
print('Detect peaks with a sliding window of {} (minimum possible).'.format(span))
indexes = mlpy.findpeaks_win(vector, span=span)
print('Peaks are: {}'.format(indexes))
plot_peaks(
np.array(vector),
indexes,
mpd=span, algorithm='mlpy.findpeaks_win'
)

span = 5
print('Detect peaks with a sliding window of {}.'.format(span))
indexes = mlpy.findpeaks_win(vector, span=span)
print('Peaks are: {}'.format(indexes))
plot_peaks(
np.array(vector),
indexes,
mpd=span, algorithm='mlpy.findpeaks_win'
)

0 comments on commit 7c3f583

Please sign in to comment.