pynapple.TsdFrame.find_peaks#

TsdFrame.find_peaks(epochs=None, return_prop=False, *args, **kwargs)[source]#

Find peaks based on peak properties.

This function wraps scipy.signal.find_peaks().

Parameters:
  • return_prop (bool, optional) – Whether to return the peak properties in the columns. See scipy.signal.find_peaks() for the list of properties.

  • height (number or ndarray or sequence, optional) – Required height of peaks. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied, as the maximal required height.

  • threshold (number or ndarray or sequence, optional) – Required threshold of peaks, the vertical distance to its neighboring samples. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied, as the maximal required threshold.

  • distance (number, optional) – Required minimal horizontal distance (>= 1) in samples between neighbouring peaks. Smaller peaks are removed first until the condition is fulfilled for all remaining peaks.

  • prominence (number or ndarray or sequence, optional) – Required prominence of peaks. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied, as the maximal required prominence.

  • width (number or ndarray or sequence, optional) – Required width of peaks in samples. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied, as the maximal required width.

  • wlen (int, optional) – Used for calculation of the peaks prominences, thus it is only used if one of the arguments prominence or width is given. See argument wlen in peak_prominences for a full description of its effects.

  • rel_height (float, optional) – Used for calculation of the peaks width, thus it is only used if width is given. See argument rel_height in peak_widths for a full description of its effects.

  • plateau_size (number or ndarray or sequence, optional) – Required size of the flat top of peaks in samples. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied as the maximal required plateau size.

Returns:

peaks – The time points and values of the peaks per column. Peak properties are included in the entry columns if return_prop=True.

Return type:

TsGroup

Examples

>>> import pynapple as nap
>>> import numpy as np
>>> times = np.arange(0, 10, 0.1)
>>> tsdframe = nap.TsdFrame(t=times, d=np.stack([np.sin(times), np.cos(times)], axis=1))
>>> peaks = tsdframe.find_peaks()
>>> peaks
  Index     rate
-------  -------
      0  0.20202
      1  0.10101
>>> peaks[0]
Time (s)
----------  --------
1.6         0.999574
7.9         0.998941
dtype: float64, shape: (2,)

You can set various requirements for finding peaks, for example a minimum width:

>>> peaks = tsdframe.find_peaks(width=21)
>>> peaks
  Index     rate
-------  -------
      0  0.10101
      1  0.10101
>>> peaks[0]
Time (s)
----------  --------
7.9         0.998941
dtype: float64, shape: (1,)

If you further want the peak properties returned, you can pass return_prop=True:

>>> peaks = tsdframe.find_peaks(return_prop=True, width=21)
>>> peaks
  Index     rate
-------  -------
      0  0.10101
      1  0.10101
>>> peaks[0]
Time (s)      peak_value    prominences    left_bases    right_bases    widths  ...
----------  ------------  -------------  ------------  -------------  --------  -----
7.9             0.998941        1.45648            47             99   25.9266  ...
dtype: float64, shape: (1, 8)