Skip to main content

findpeaks is for the detection of peaks and valleys in a 1D vector and 2D array (image).

Project description

findpeaks

Python PyPI Version License Downloads

  • findpeaks is Python package

Contents

Installation

  • Install findpeaks from PyPI (recommended). findpeaks is compatible with Python 3.6+ and runs on Linux, MacOS X and Windows.
  • A new environment can be created as following:
conda create -n env_findpeaks python=3.7
conda activate env_findpeaks
pip install findpeaks
  • Alternatively, install findpeaks from the GitHub source:
# Directly install from github source
pip install -e git://github.com/erdogant/findpeaks.git@0.1.0#egg=master
pip install git+https://github.com/erdogant/findpeaks#egg=master

# By cloning
pip install git+https://github.com/erdogant/findpeaks
git clone https://github.com/erdogant/findpeaks.git
cd findpeaks
python setup.py install

Import findpeaks package

import findpeaks

Example 1: 1D-vector low resolution

# Load library
from findpeaks import findpeaks
# Data
X = [9,60,377,985,1153,672,501,1068,1110,574,135,23,3,47,252,812,1182,741,263,33]
# Initialize
fp = findpeaks(lookahead=1)
results = fp.fit(X)
# Plot
fp.plot()

# Initialize with interpolation parameter
fp = findpeaks(lookahead=1, interpolate=10)
results = fp.fit(X)
fp.plot()

Example 2: 1D vector low resolution

# Load library
from findpeaks import findpeaks
# Data
X = [10,11,9,23,21,11,45,20,11,12]
# Initialize
fp = findpeaks(lookahead=1)
results = fp.fit(X)
# Plot
fp.plot(method='peakdetect')
fp.plot(method='topology')
fp.plot_peristence()

# Initialize with interpolate parameter
fp = findpeaks(lookahead=1, interpolate=10)
results = fp.fit(X)
fp.plot(method='peakdetect')
fp.plot(method='topology')

Example 3: 1D-vector high resolution

# Load library
import numpy as np
from findpeaks import findpeaks

# Data
i = 10000
xs = np.linspace(0,3.7*np.pi,i)
X = (0.3*np.sin(xs) + np.sin(1.3 * xs) + 0.9 * np.sin(4.2 * xs) + 0.06 * np.random.randn(i))

# Initialize
fp = findpeaks()
results = fp.fit(X)

# Plot
fp.plot1d(method='peakdetect')
fp.plot1d(method='topology')
fp.plot_peristence()

Example 4: 2D-array (image) using default settings

# Import library
from findpeaks import findpeaks

# Import example
X = fp.import_example()
print(X)
array([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.4, 0.4],
       [0. , 0. , 0. , 0. , 0. , 0. , 0.7, 1.4, 2.2, 1.8],
       [0. , 0. , 0. , 0. , 0. , 1.1, 4. , 6.5, 4.3, 1.8],
       [0. , 0. , 0. , 0. , 0. , 1.4, 6.1, 7.2, 3.2, 0.7],
       [..., ..., ..., ..., ..., ..., ..., ..., ..., ...],
       [0. , 0.4, 2.9, 7.9, 5.4, 1.4, 0.7, 0.4, 1.1, 1.8],
       [0. , 0. , 1.8, 5.4, 3.2, 1.8, 4.3, 3.6, 2.9, 6.1],
       [0. , 0. , 0.4, 0.7, 0.7, 2.5, 9. , 7.9, 3.6, 7.9],
       [0. , 0. , 0. , 0. , 0. , 1.1, 4.7, 4. , 1.4, 2.9],
       [0. , 0. , 0. , 0. , 0. , 0.4, 0.7, 0.7, 0.4, 0.4]])

# Initialize
fp = findpeaks(mask=0)

# Fit
fp.fit(X)

# Plot the pre-processing steps
fp.plot_preprocessing()

# Plot all
fp.plot()

The input figure

The masking approach detects the correct peaks.

fp.plot_mask()

Conversion from 2d to 3d mesh plots looks very nice. But there is a rough surface because of the low-resolution input data.

fp.plot_mesh()

The persistence plot appears to detect the right peaks.

fp.plot_peristence()

Example 5: 2D-array (image) with pre-processing steps

# Import library
from findpeaks import findpeaks

# Import example
X = fp.import_example()

# Initialize
fp = findpeaks(mask=0, scale=True, denoise=10, togray=True, imsize=(300,300), verbose=3)

# Fit
results = fp.fit(X)

# Plot all
fp.plot()

Show the plots:

fp.plot_preprocessing()

The masking does not work so well because the pre-processing steps includes some weighted smoothing which is not ideal for the masking approach.

fp.plot_mask()

The mesh plot has higher resolution because the pre-processing steps caused some smoothing.

fp.plot_mesh()

The Persistence plot does show the detection of correct peaks.

fp.plot_peristence()

Citation

Please cite findpeaks in your publications if this is useful for your research. Here is an example BibTeX entry:

@misc{erdogant2020findpeaks,
  title={findpeaks},
  author={Erdogan Taskesen},
  year={2020},
  howpublished={\url{https://github.com/erdogant/findpeaks}},
}

References

Maintainer

Erdogan Taskesen, github: [erdogant](https://github.com/erdogant)
Contributions are welcome.
See [LICENSE](LICENSE) for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

findpeaks-2.0.0.tar.gz (23.1 kB view hashes)

Uploaded Source

Built Distribution

findpeaks-2.0.0-py3-none-any.whl (32.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page