_peak_finding.py 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. """
  2. Functions for identifying peaks in signals.
  3. """
  4. import math
  5. import numpy as np
  6. from scipy.signal._wavelets import cwt, ricker
  7. from scipy.stats import scoreatpercentile
  8. from ._peak_finding_utils import (
  9. _local_maxima_1d,
  10. _select_by_peak_distance,
  11. _peak_prominences,
  12. _peak_widths
  13. )
  14. __all__ = ['argrelmin', 'argrelmax', 'argrelextrema', 'peak_prominences',
  15. 'peak_widths', 'find_peaks', 'find_peaks_cwt']
  16. def _boolrelextrema(data, comparator, axis=0, order=1, mode='clip'):
  17. """
  18. Calculate the relative extrema of `data`.
  19. Relative extrema are calculated by finding locations where
  20. ``comparator(data[n], data[n+1:n+order+1])`` is True.
  21. Parameters
  22. ----------
  23. data : ndarray
  24. Array in which to find the relative extrema.
  25. comparator : callable
  26. Function to use to compare two data points.
  27. Should take two arrays as arguments.
  28. axis : int, optional
  29. Axis over which to select from `data`. Default is 0.
  30. order : int, optional
  31. How many points on each side to use for the comparison
  32. to consider ``comparator(n,n+x)`` to be True.
  33. mode : str, optional
  34. How the edges of the vector are treated. 'wrap' (wrap around) or
  35. 'clip' (treat overflow as the same as the last (or first) element).
  36. Default 'clip'. See numpy.take.
  37. Returns
  38. -------
  39. extrema : ndarray
  40. Boolean array of the same shape as `data` that is True at an extrema,
  41. False otherwise.
  42. See also
  43. --------
  44. argrelmax, argrelmin
  45. Examples
  46. --------
  47. >>> import numpy as np
  48. >>> testdata = np.array([1,2,3,2,1])
  49. >>> _boolrelextrema(testdata, np.greater, axis=0)
  50. array([False, False, True, False, False], dtype=bool)
  51. """
  52. if (int(order) != order) or (order < 1):
  53. raise ValueError('Order must be an int >= 1')
  54. datalen = data.shape[axis]
  55. locs = np.arange(0, datalen)
  56. results = np.ones(data.shape, dtype=bool)
  57. main = data.take(locs, axis=axis, mode=mode)
  58. for shift in range(1, order + 1):
  59. plus = data.take(locs + shift, axis=axis, mode=mode)
  60. minus = data.take(locs - shift, axis=axis, mode=mode)
  61. results &= comparator(main, plus)
  62. results &= comparator(main, minus)
  63. if ~results.any():
  64. return results
  65. return results
  66. def argrelmin(data, axis=0, order=1, mode='clip'):
  67. """
  68. Calculate the relative minima of `data`.
  69. Parameters
  70. ----------
  71. data : ndarray
  72. Array in which to find the relative minima.
  73. axis : int, optional
  74. Axis over which to select from `data`. Default is 0.
  75. order : int, optional
  76. How many points on each side to use for the comparison
  77. to consider ``comparator(n, n+x)`` to be True.
  78. mode : str, optional
  79. How the edges of the vector are treated.
  80. Available options are 'wrap' (wrap around) or 'clip' (treat overflow
  81. as the same as the last (or first) element).
  82. Default 'clip'. See numpy.take.
  83. Returns
  84. -------
  85. extrema : tuple of ndarrays
  86. Indices of the minima in arrays of integers. ``extrema[k]`` is
  87. the array of indices of axis `k` of `data`. Note that the
  88. return value is a tuple even when `data` is 1-D.
  89. See Also
  90. --------
  91. argrelextrema, argrelmax, find_peaks
  92. Notes
  93. -----
  94. This function uses `argrelextrema` with np.less as comparator. Therefore, it
  95. requires a strict inequality on both sides of a value to consider it a
  96. minimum. This means flat minima (more than one sample wide) are not detected.
  97. In case of 1-D `data` `find_peaks` can be used to detect all
  98. local minima, including flat ones, by calling it with negated `data`.
  99. .. versionadded:: 0.11.0
  100. Examples
  101. --------
  102. >>> import numpy as np
  103. >>> from scipy.signal import argrelmin
  104. >>> x = np.array([2, 1, 2, 3, 2, 0, 1, 0])
  105. >>> argrelmin(x)
  106. (array([1, 5]),)
  107. >>> y = np.array([[1, 2, 1, 2],
  108. ... [2, 2, 0, 0],
  109. ... [5, 3, 4, 4]])
  110. ...
  111. >>> argrelmin(y, axis=1)
  112. (array([0, 2]), array([2, 1]))
  113. """
  114. return argrelextrema(data, np.less, axis, order, mode)
  115. def argrelmax(data, axis=0, order=1, mode='clip'):
  116. """
  117. Calculate the relative maxima of `data`.
  118. Parameters
  119. ----------
  120. data : ndarray
  121. Array in which to find the relative maxima.
  122. axis : int, optional
  123. Axis over which to select from `data`. Default is 0.
  124. order : int, optional
  125. How many points on each side to use for the comparison
  126. to consider ``comparator(n, n+x)`` to be True.
  127. mode : str, optional
  128. How the edges of the vector are treated.
  129. Available options are 'wrap' (wrap around) or 'clip' (treat overflow
  130. as the same as the last (or first) element).
  131. Default 'clip'. See `numpy.take`.
  132. Returns
  133. -------
  134. extrema : tuple of ndarrays
  135. Indices of the maxima in arrays of integers. ``extrema[k]`` is
  136. the array of indices of axis `k` of `data`. Note that the
  137. return value is a tuple even when `data` is 1-D.
  138. See Also
  139. --------
  140. argrelextrema, argrelmin, find_peaks
  141. Notes
  142. -----
  143. This function uses `argrelextrema` with np.greater as comparator. Therefore,
  144. it requires a strict inequality on both sides of a value to consider it a
  145. maximum. This means flat maxima (more than one sample wide) are not detected.
  146. In case of 1-D `data` `find_peaks` can be used to detect all
  147. local maxima, including flat ones.
  148. .. versionadded:: 0.11.0
  149. Examples
  150. --------
  151. >>> import numpy as np
  152. >>> from scipy.signal import argrelmax
  153. >>> x = np.array([2, 1, 2, 3, 2, 0, 1, 0])
  154. >>> argrelmax(x)
  155. (array([3, 6]),)
  156. >>> y = np.array([[1, 2, 1, 2],
  157. ... [2, 2, 0, 0],
  158. ... [5, 3, 4, 4]])
  159. ...
  160. >>> argrelmax(y, axis=1)
  161. (array([0]), array([1]))
  162. """
  163. return argrelextrema(data, np.greater, axis, order, mode)
  164. def argrelextrema(data, comparator, axis=0, order=1, mode='clip'):
  165. """
  166. Calculate the relative extrema of `data`.
  167. Parameters
  168. ----------
  169. data : ndarray
  170. Array in which to find the relative extrema.
  171. comparator : callable
  172. Function to use to compare two data points.
  173. Should take two arrays as arguments.
  174. axis : int, optional
  175. Axis over which to select from `data`. Default is 0.
  176. order : int, optional
  177. How many points on each side to use for the comparison
  178. to consider ``comparator(n, n+x)`` to be True.
  179. mode : str, optional
  180. How the edges of the vector are treated. 'wrap' (wrap around) or
  181. 'clip' (treat overflow as the same as the last (or first) element).
  182. Default is 'clip'. See `numpy.take`.
  183. Returns
  184. -------
  185. extrema : tuple of ndarrays
  186. Indices of the maxima in arrays of integers. ``extrema[k]`` is
  187. the array of indices of axis `k` of `data`. Note that the
  188. return value is a tuple even when `data` is 1-D.
  189. See Also
  190. --------
  191. argrelmin, argrelmax
  192. Notes
  193. -----
  194. .. versionadded:: 0.11.0
  195. Examples
  196. --------
  197. >>> import numpy as np
  198. >>> from scipy.signal import argrelextrema
  199. >>> x = np.array([2, 1, 2, 3, 2, 0, 1, 0])
  200. >>> argrelextrema(x, np.greater)
  201. (array([3, 6]),)
  202. >>> y = np.array([[1, 2, 1, 2],
  203. ... [2, 2, 0, 0],
  204. ... [5, 3, 4, 4]])
  205. ...
  206. >>> argrelextrema(y, np.less, axis=1)
  207. (array([0, 2]), array([2, 1]))
  208. """
  209. results = _boolrelextrema(data, comparator,
  210. axis, order, mode)
  211. return np.nonzero(results)
  212. def _arg_x_as_expected(value):
  213. """Ensure argument `x` is a 1-D C-contiguous array of dtype('float64').
  214. Used in `find_peaks`, `peak_prominences` and `peak_widths` to make `x`
  215. compatible with the signature of the wrapped Cython functions.
  216. Returns
  217. -------
  218. value : ndarray
  219. A 1-D C-contiguous array with dtype('float64').
  220. """
  221. value = np.asarray(value, order='C', dtype=np.float64)
  222. if value.ndim != 1:
  223. raise ValueError('`x` must be a 1-D array')
  224. return value
  225. def _arg_peaks_as_expected(value):
  226. """Ensure argument `peaks` is a 1-D C-contiguous array of dtype('intp').
  227. Used in `peak_prominences` and `peak_widths` to make `peaks` compatible
  228. with the signature of the wrapped Cython functions.
  229. Returns
  230. -------
  231. value : ndarray
  232. A 1-D C-contiguous array with dtype('intp').
  233. """
  234. value = np.asarray(value)
  235. if value.size == 0:
  236. # Empty arrays default to np.float64 but are valid input
  237. value = np.array([], dtype=np.intp)
  238. try:
  239. # Safely convert to C-contiguous array of type np.intp
  240. value = value.astype(np.intp, order='C', casting='safe',
  241. subok=False, copy=False)
  242. except TypeError as e:
  243. raise TypeError("cannot safely cast `peaks` to dtype('intp')") from e
  244. if value.ndim != 1:
  245. raise ValueError('`peaks` must be a 1-D array')
  246. return value
  247. def _arg_wlen_as_expected(value):
  248. """Ensure argument `wlen` is of type `np.intp` and larger than 1.
  249. Used in `peak_prominences` and `peak_widths`.
  250. Returns
  251. -------
  252. value : np.intp
  253. The original `value` rounded up to an integer or -1 if `value` was
  254. None.
  255. """
  256. if value is None:
  257. # _peak_prominences expects an intp; -1 signals that no value was
  258. # supplied by the user
  259. value = -1
  260. elif 1 < value:
  261. # Round up to a positive integer
  262. if not np.can_cast(value, np.intp, "safe"):
  263. value = math.ceil(value)
  264. value = np.intp(value)
  265. else:
  266. raise ValueError('`wlen` must be larger than 1, was {}'
  267. .format(value))
  268. return value
  269. def peak_prominences(x, peaks, wlen=None):
  270. """
  271. Calculate the prominence of each peak in a signal.
  272. The prominence of a peak measures how much a peak stands out from the
  273. surrounding baseline of the signal and is defined as the vertical distance
  274. between the peak and its lowest contour line.
  275. Parameters
  276. ----------
  277. x : sequence
  278. A signal with peaks.
  279. peaks : sequence
  280. Indices of peaks in `x`.
  281. wlen : int, optional
  282. A window length in samples that optionally limits the evaluated area for
  283. each peak to a subset of `x`. The peak is always placed in the middle of
  284. the window therefore the given length is rounded up to the next odd
  285. integer. This parameter can speed up the calculation (see Notes).
  286. Returns
  287. -------
  288. prominences : ndarray
  289. The calculated prominences for each peak in `peaks`.
  290. left_bases, right_bases : ndarray
  291. The peaks' bases as indices in `x` to the left and right of each peak.
  292. The higher base of each pair is a peak's lowest contour line.
  293. Raises
  294. ------
  295. ValueError
  296. If a value in `peaks` is an invalid index for `x`.
  297. Warns
  298. -----
  299. PeakPropertyWarning
  300. For indices in `peaks` that don't point to valid local maxima in `x`,
  301. the returned prominence will be 0 and this warning is raised. This
  302. also happens if `wlen` is smaller than the plateau size of a peak.
  303. Warnings
  304. --------
  305. This function may return unexpected results for data containing NaNs. To
  306. avoid this, NaNs should either be removed or replaced.
  307. See Also
  308. --------
  309. find_peaks
  310. Find peaks inside a signal based on peak properties.
  311. peak_widths
  312. Calculate the width of peaks.
  313. Notes
  314. -----
  315. Strategy to compute a peak's prominence:
  316. 1. Extend a horizontal line from the current peak to the left and right
  317. until the line either reaches the window border (see `wlen`) or
  318. intersects the signal again at the slope of a higher peak. An
  319. intersection with a peak of the same height is ignored.
  320. 2. On each side find the minimal signal value within the interval defined
  321. above. These points are the peak's bases.
  322. 3. The higher one of the two bases marks the peak's lowest contour line. The
  323. prominence can then be calculated as the vertical difference between the
  324. peaks height itself and its lowest contour line.
  325. Searching for the peak's bases can be slow for large `x` with periodic
  326. behavior because large chunks or even the full signal need to be evaluated
  327. for the first algorithmic step. This evaluation area can be limited with the
  328. parameter `wlen` which restricts the algorithm to a window around the
  329. current peak and can shorten the calculation time if the window length is
  330. short in relation to `x`.
  331. However, this may stop the algorithm from finding the true global contour
  332. line if the peak's true bases are outside this window. Instead, a higher
  333. contour line is found within the restricted window leading to a smaller
  334. calculated prominence. In practice, this is only relevant for the highest set
  335. of peaks in `x`. This behavior may even be used intentionally to calculate
  336. "local" prominences.
  337. .. versionadded:: 1.1.0
  338. References
  339. ----------
  340. .. [1] Wikipedia Article for Topographic Prominence:
  341. https://en.wikipedia.org/wiki/Topographic_prominence
  342. Examples
  343. --------
  344. >>> import numpy as np
  345. >>> from scipy.signal import find_peaks, peak_prominences
  346. >>> import matplotlib.pyplot as plt
  347. Create a test signal with two overlayed harmonics
  348. >>> x = np.linspace(0, 6 * np.pi, 1000)
  349. >>> x = np.sin(x) + 0.6 * np.sin(2.6 * x)
  350. Find all peaks and calculate prominences
  351. >>> peaks, _ = find_peaks(x)
  352. >>> prominences = peak_prominences(x, peaks)[0]
  353. >>> prominences
  354. array([1.24159486, 0.47840168, 0.28470524, 3.10716793, 0.284603 ,
  355. 0.47822491, 2.48340261, 0.47822491])
  356. Calculate the height of each peak's contour line and plot the results
  357. >>> contour_heights = x[peaks] - prominences
  358. >>> plt.plot(x)
  359. >>> plt.plot(peaks, x[peaks], "x")
  360. >>> plt.vlines(x=peaks, ymin=contour_heights, ymax=x[peaks])
  361. >>> plt.show()
  362. Let's evaluate a second example that demonstrates several edge cases for
  363. one peak at index 5.
  364. >>> x = np.array([0, 1, 0, 3, 1, 3, 0, 4, 0])
  365. >>> peaks = np.array([5])
  366. >>> plt.plot(x)
  367. >>> plt.plot(peaks, x[peaks], "x")
  368. >>> plt.show()
  369. >>> peak_prominences(x, peaks) # -> (prominences, left_bases, right_bases)
  370. (array([3.]), array([2]), array([6]))
  371. Note how the peak at index 3 of the same height is not considered as a
  372. border while searching for the left base. Instead, two minima at 0 and 2
  373. are found in which case the one closer to the evaluated peak is always
  374. chosen. On the right side, however, the base must be placed at 6 because the
  375. higher peak represents the right border to the evaluated area.
  376. >>> peak_prominences(x, peaks, wlen=3.1)
  377. (array([2.]), array([4]), array([6]))
  378. Here, we restricted the algorithm to a window from 3 to 7 (the length is 5
  379. samples because `wlen` was rounded up to the next odd integer). Thus, the
  380. only two candidates in the evaluated area are the two neighboring samples
  381. and a smaller prominence is calculated.
  382. """
  383. x = _arg_x_as_expected(x)
  384. peaks = _arg_peaks_as_expected(peaks)
  385. wlen = _arg_wlen_as_expected(wlen)
  386. return _peak_prominences(x, peaks, wlen)
  387. def peak_widths(x, peaks, rel_height=0.5, prominence_data=None, wlen=None):
  388. """
  389. Calculate the width of each peak in a signal.
  390. This function calculates the width of a peak in samples at a relative
  391. distance to the peak's height and prominence.
  392. Parameters
  393. ----------
  394. x : sequence
  395. A signal with peaks.
  396. peaks : sequence
  397. Indices of peaks in `x`.
  398. rel_height : float, optional
  399. Chooses the relative height at which the peak width is measured as a
  400. percentage of its prominence. 1.0 calculates the width of the peak at
  401. its lowest contour line while 0.5 evaluates at half the prominence
  402. height. Must be at least 0. See notes for further explanation.
  403. prominence_data : tuple, optional
  404. A tuple of three arrays matching the output of `peak_prominences` when
  405. called with the same arguments `x` and `peaks`. This data are calculated
  406. internally if not provided.
  407. wlen : int, optional
  408. A window length in samples passed to `peak_prominences` as an optional
  409. argument for internal calculation of `prominence_data`. This argument
  410. is ignored if `prominence_data` is given.
  411. Returns
  412. -------
  413. widths : ndarray
  414. The widths for each peak in samples.
  415. width_heights : ndarray
  416. The height of the contour lines at which the `widths` where evaluated.
  417. left_ips, right_ips : ndarray
  418. Interpolated positions of left and right intersection points of a
  419. horizontal line at the respective evaluation height.
  420. Raises
  421. ------
  422. ValueError
  423. If `prominence_data` is supplied but doesn't satisfy the condition
  424. ``0 <= left_base <= peak <= right_base < x.shape[0]`` for each peak,
  425. has the wrong dtype, is not C-contiguous or does not have the same
  426. shape.
  427. Warns
  428. -----
  429. PeakPropertyWarning
  430. Raised if any calculated width is 0. This may stem from the supplied
  431. `prominence_data` or if `rel_height` is set to 0.
  432. Warnings
  433. --------
  434. This function may return unexpected results for data containing NaNs. To
  435. avoid this, NaNs should either be removed or replaced.
  436. See Also
  437. --------
  438. find_peaks
  439. Find peaks inside a signal based on peak properties.
  440. peak_prominences
  441. Calculate the prominence of peaks.
  442. Notes
  443. -----
  444. The basic algorithm to calculate a peak's width is as follows:
  445. * Calculate the evaluation height :math:`h_{eval}` with the formula
  446. :math:`h_{eval} = h_{Peak} - P \\cdot R`, where :math:`h_{Peak}` is the
  447. height of the peak itself, :math:`P` is the peak's prominence and
  448. :math:`R` a positive ratio specified with the argument `rel_height`.
  449. * Draw a horizontal line at the evaluation height to both sides, starting at
  450. the peak's current vertical position until the lines either intersect a
  451. slope, the signal border or cross the vertical position of the peak's
  452. base (see `peak_prominences` for an definition). For the first case,
  453. intersection with the signal, the true intersection point is estimated
  454. with linear interpolation.
  455. * Calculate the width as the horizontal distance between the chosen
  456. endpoints on both sides. As a consequence of this the maximal possible
  457. width for each peak is the horizontal distance between its bases.
  458. As shown above to calculate a peak's width its prominence and bases must be
  459. known. You can supply these yourself with the argument `prominence_data`.
  460. Otherwise, they are internally calculated (see `peak_prominences`).
  461. .. versionadded:: 1.1.0
  462. Examples
  463. --------
  464. >>> import numpy as np
  465. >>> from scipy.signal import chirp, find_peaks, peak_widths
  466. >>> import matplotlib.pyplot as plt
  467. Create a test signal with two overlayed harmonics
  468. >>> x = np.linspace(0, 6 * np.pi, 1000)
  469. >>> x = np.sin(x) + 0.6 * np.sin(2.6 * x)
  470. Find all peaks and calculate their widths at the relative height of 0.5
  471. (contour line at half the prominence height) and 1 (at the lowest contour
  472. line at full prominence height).
  473. >>> peaks, _ = find_peaks(x)
  474. >>> results_half = peak_widths(x, peaks, rel_height=0.5)
  475. >>> results_half[0] # widths
  476. array([ 64.25172825, 41.29465463, 35.46943289, 104.71586081,
  477. 35.46729324, 41.30429622, 181.93835853, 45.37078546])
  478. >>> results_full = peak_widths(x, peaks, rel_height=1)
  479. >>> results_full[0] # widths
  480. array([181.9396084 , 72.99284945, 61.28657872, 373.84622694,
  481. 61.78404617, 72.48822812, 253.09161876, 79.36860878])
  482. Plot signal, peaks and contour lines at which the widths where calculated
  483. >>> plt.plot(x)
  484. >>> plt.plot(peaks, x[peaks], "x")
  485. >>> plt.hlines(*results_half[1:], color="C2")
  486. >>> plt.hlines(*results_full[1:], color="C3")
  487. >>> plt.show()
  488. """
  489. x = _arg_x_as_expected(x)
  490. peaks = _arg_peaks_as_expected(peaks)
  491. if prominence_data is None:
  492. # Calculate prominence if not supplied and use wlen if supplied.
  493. wlen = _arg_wlen_as_expected(wlen)
  494. prominence_data = _peak_prominences(x, peaks, wlen)
  495. return _peak_widths(x, peaks, rel_height, *prominence_data)
  496. def _unpack_condition_args(interval, x, peaks):
  497. """
  498. Parse condition arguments for `find_peaks`.
  499. Parameters
  500. ----------
  501. interval : number or ndarray or sequence
  502. Either a number or ndarray or a 2-element sequence of the former. The
  503. first value is always interpreted as `imin` and the second, if supplied,
  504. as `imax`.
  505. x : ndarray
  506. The signal with `peaks`.
  507. peaks : ndarray
  508. An array with indices used to reduce `imin` and / or `imax` if those are
  509. arrays.
  510. Returns
  511. -------
  512. imin, imax : number or ndarray or None
  513. Minimal and maximal value in `argument`.
  514. Raises
  515. ------
  516. ValueError :
  517. If interval border is given as array and its size does not match the size
  518. of `x`.
  519. Notes
  520. -----
  521. .. versionadded:: 1.1.0
  522. """
  523. try:
  524. imin, imax = interval
  525. except (TypeError, ValueError):
  526. imin, imax = (interval, None)
  527. # Reduce arrays if arrays
  528. if isinstance(imin, np.ndarray):
  529. if imin.size != x.size:
  530. raise ValueError('array size of lower interval border must match x')
  531. imin = imin[peaks]
  532. if isinstance(imax, np.ndarray):
  533. if imax.size != x.size:
  534. raise ValueError('array size of upper interval border must match x')
  535. imax = imax[peaks]
  536. return imin, imax
  537. def _select_by_property(peak_properties, pmin, pmax):
  538. """
  539. Evaluate where the generic property of peaks confirms to an interval.
  540. Parameters
  541. ----------
  542. peak_properties : ndarray
  543. An array with properties for each peak.
  544. pmin : None or number or ndarray
  545. Lower interval boundary for `peak_properties`. ``None`` is interpreted as
  546. an open border.
  547. pmax : None or number or ndarray
  548. Upper interval boundary for `peak_properties`. ``None`` is interpreted as
  549. an open border.
  550. Returns
  551. -------
  552. keep : bool
  553. A boolean mask evaluating to true where `peak_properties` confirms to the
  554. interval.
  555. See Also
  556. --------
  557. find_peaks
  558. Notes
  559. -----
  560. .. versionadded:: 1.1.0
  561. """
  562. keep = np.ones(peak_properties.size, dtype=bool)
  563. if pmin is not None:
  564. keep &= (pmin <= peak_properties)
  565. if pmax is not None:
  566. keep &= (peak_properties <= pmax)
  567. return keep
  568. def _select_by_peak_threshold(x, peaks, tmin, tmax):
  569. """
  570. Evaluate which peaks fulfill the threshold condition.
  571. Parameters
  572. ----------
  573. x : ndarray
  574. A 1-D array which is indexable by `peaks`.
  575. peaks : ndarray
  576. Indices of peaks in `x`.
  577. tmin, tmax : scalar or ndarray or None
  578. Minimal and / or maximal required thresholds. If supplied as ndarrays
  579. their size must match `peaks`. ``None`` is interpreted as an open
  580. border.
  581. Returns
  582. -------
  583. keep : bool
  584. A boolean mask evaluating to true where `peaks` fulfill the threshold
  585. condition.
  586. left_thresholds, right_thresholds : ndarray
  587. Array matching `peak` containing the thresholds of each peak on
  588. both sides.
  589. Notes
  590. -----
  591. .. versionadded:: 1.1.0
  592. """
  593. # Stack thresholds on both sides to make min / max operations easier:
  594. # tmin is compared with the smaller, and tmax with the greater thresold to
  595. # each peak's side
  596. stacked_thresholds = np.vstack([x[peaks] - x[peaks - 1],
  597. x[peaks] - x[peaks + 1]])
  598. keep = np.ones(peaks.size, dtype=bool)
  599. if tmin is not None:
  600. min_thresholds = np.min(stacked_thresholds, axis=0)
  601. keep &= (tmin <= min_thresholds)
  602. if tmax is not None:
  603. max_thresholds = np.max(stacked_thresholds, axis=0)
  604. keep &= (max_thresholds <= tmax)
  605. return keep, stacked_thresholds[0], stacked_thresholds[1]
  606. def find_peaks(x, height=None, threshold=None, distance=None,
  607. prominence=None, width=None, wlen=None, rel_height=0.5,
  608. plateau_size=None):
  609. """
  610. Find peaks inside a signal based on peak properties.
  611. This function takes a 1-D array and finds all local maxima by
  612. simple comparison of neighboring values. Optionally, a subset of these
  613. peaks can be selected by specifying conditions for a peak's properties.
  614. Parameters
  615. ----------
  616. x : sequence
  617. A signal with peaks.
  618. height : number or ndarray or sequence, optional
  619. Required height of peaks. Either a number, ``None``, an array matching
  620. `x` or a 2-element sequence of the former. The first element is
  621. always interpreted as the minimal and the second, if supplied, as the
  622. maximal required height.
  623. threshold : number or ndarray or sequence, optional
  624. Required threshold of peaks, the vertical distance to its neighboring
  625. samples. Either a number, ``None``, an array matching `x` or a
  626. 2-element sequence of the former. The first element is always
  627. interpreted as the minimal and the second, if supplied, as the maximal
  628. required threshold.
  629. distance : number, optional
  630. Required minimal horizontal distance (>= 1) in samples between
  631. neighbouring peaks. Smaller peaks are removed first until the condition
  632. is fulfilled for all remaining peaks.
  633. prominence : number or ndarray or sequence, optional
  634. Required prominence of peaks. Either a number, ``None``, an array
  635. matching `x` or a 2-element sequence of the former. The first
  636. element is always interpreted as the minimal and the second, if
  637. supplied, as the maximal required prominence.
  638. width : number or ndarray or sequence, optional
  639. Required width of peaks in samples. Either a number, ``None``, an array
  640. matching `x` or a 2-element sequence of the former. The first
  641. element is always interpreted as the minimal and the second, if
  642. supplied, as the maximal required width.
  643. wlen : int, optional
  644. Used for calculation of the peaks prominences, thus it is only used if
  645. one of the arguments `prominence` or `width` is given. See argument
  646. `wlen` in `peak_prominences` for a full description of its effects.
  647. rel_height : float, optional
  648. Used for calculation of the peaks width, thus it is only used if `width`
  649. is given. See argument `rel_height` in `peak_widths` for a full
  650. description of its effects.
  651. plateau_size : number or ndarray or sequence, optional
  652. Required size of the flat top of peaks in samples. Either a number,
  653. ``None``, an array matching `x` or a 2-element sequence of the former.
  654. The first element is always interpreted as the minimal and the second,
  655. if supplied as the maximal required plateau size.
  656. .. versionadded:: 1.2.0
  657. Returns
  658. -------
  659. peaks : ndarray
  660. Indices of peaks in `x` that satisfy all given conditions.
  661. properties : dict
  662. A dictionary containing properties of the returned peaks which were
  663. calculated as intermediate results during evaluation of the specified
  664. conditions:
  665. * 'peak_heights'
  666. If `height` is given, the height of each peak in `x`.
  667. * 'left_thresholds', 'right_thresholds'
  668. If `threshold` is given, these keys contain a peaks vertical
  669. distance to its neighbouring samples.
  670. * 'prominences', 'right_bases', 'left_bases'
  671. If `prominence` is given, these keys are accessible. See
  672. `peak_prominences` for a description of their content.
  673. * 'width_heights', 'left_ips', 'right_ips'
  674. If `width` is given, these keys are accessible. See `peak_widths`
  675. for a description of their content.
  676. * 'plateau_sizes', left_edges', 'right_edges'
  677. If `plateau_size` is given, these keys are accessible and contain
  678. the indices of a peak's edges (edges are still part of the
  679. plateau) and the calculated plateau sizes.
  680. .. versionadded:: 1.2.0
  681. To calculate and return properties without excluding peaks, provide the
  682. open interval ``(None, None)`` as a value to the appropriate argument
  683. (excluding `distance`).
  684. Warns
  685. -----
  686. PeakPropertyWarning
  687. Raised if a peak's properties have unexpected values (see
  688. `peak_prominences` and `peak_widths`).
  689. Warnings
  690. --------
  691. This function may return unexpected results for data containing NaNs. To
  692. avoid this, NaNs should either be removed or replaced.
  693. See Also
  694. --------
  695. find_peaks_cwt
  696. Find peaks using the wavelet transformation.
  697. peak_prominences
  698. Directly calculate the prominence of peaks.
  699. peak_widths
  700. Directly calculate the width of peaks.
  701. Notes
  702. -----
  703. In the context of this function, a peak or local maximum is defined as any
  704. sample whose two direct neighbours have a smaller amplitude. For flat peaks
  705. (more than one sample of equal amplitude wide) the index of the middle
  706. sample is returned (rounded down in case the number of samples is even).
  707. For noisy signals the peak locations can be off because the noise might
  708. change the position of local maxima. In those cases consider smoothing the
  709. signal before searching for peaks or use other peak finding and fitting
  710. methods (like `find_peaks_cwt`).
  711. Some additional comments on specifying conditions:
  712. * Almost all conditions (excluding `distance`) can be given as half-open or
  713. closed intervals, e.g., ``1`` or ``(1, None)`` defines the half-open
  714. interval :math:`[1, \\infty]` while ``(None, 1)`` defines the interval
  715. :math:`[-\\infty, 1]`. The open interval ``(None, None)`` can be specified
  716. as well, which returns the matching properties without exclusion of peaks.
  717. * The border is always included in the interval used to select valid peaks.
  718. * For several conditions the interval borders can be specified with
  719. arrays matching `x` in shape which enables dynamic constrains based on
  720. the sample position.
  721. * The conditions are evaluated in the following order: `plateau_size`,
  722. `height`, `threshold`, `distance`, `prominence`, `width`. In most cases
  723. this order is the fastest one because faster operations are applied first
  724. to reduce the number of peaks that need to be evaluated later.
  725. * While indices in `peaks` are guaranteed to be at least `distance` samples
  726. apart, edges of flat peaks may be closer than the allowed `distance`.
  727. * Use `wlen` to reduce the time it takes to evaluate the conditions for
  728. `prominence` or `width` if `x` is large or has many local maxima
  729. (see `peak_prominences`).
  730. .. versionadded:: 1.1.0
  731. Examples
  732. --------
  733. To demonstrate this function's usage we use a signal `x` supplied with
  734. SciPy (see `scipy.datasets.electrocardiogram`). Let's find all peaks (local
  735. maxima) in `x` whose amplitude lies above 0.
  736. >>> import numpy as np
  737. >>> import matplotlib.pyplot as plt
  738. >>> from scipy.datasets import electrocardiogram
  739. >>> from scipy.signal import find_peaks
  740. >>> x = electrocardiogram()[2000:4000]
  741. >>> peaks, _ = find_peaks(x, height=0)
  742. >>> plt.plot(x)
  743. >>> plt.plot(peaks, x[peaks], "x")
  744. >>> plt.plot(np.zeros_like(x), "--", color="gray")
  745. >>> plt.show()
  746. We can select peaks below 0 with ``height=(None, 0)`` or use arrays matching
  747. `x` in size to reflect a changing condition for different parts of the
  748. signal.
  749. >>> border = np.sin(np.linspace(0, 3 * np.pi, x.size))
  750. >>> peaks, _ = find_peaks(x, height=(-border, border))
  751. >>> plt.plot(x)
  752. >>> plt.plot(-border, "--", color="gray")
  753. >>> plt.plot(border, ":", color="gray")
  754. >>> plt.plot(peaks, x[peaks], "x")
  755. >>> plt.show()
  756. Another useful condition for periodic signals can be given with the
  757. `distance` argument. In this case, we can easily select the positions of
  758. QRS complexes within the electrocardiogram (ECG) by demanding a distance of
  759. at least 150 samples.
  760. >>> peaks, _ = find_peaks(x, distance=150)
  761. >>> np.diff(peaks)
  762. array([186, 180, 177, 171, 177, 169, 167, 164, 158, 162, 172])
  763. >>> plt.plot(x)
  764. >>> plt.plot(peaks, x[peaks], "x")
  765. >>> plt.show()
  766. Especially for noisy signals peaks can be easily grouped by their
  767. prominence (see `peak_prominences`). E.g., we can select all peaks except
  768. for the mentioned QRS complexes by limiting the allowed prominence to 0.6.
  769. >>> peaks, properties = find_peaks(x, prominence=(None, 0.6))
  770. >>> properties["prominences"].max()
  771. 0.5049999999999999
  772. >>> plt.plot(x)
  773. >>> plt.plot(peaks, x[peaks], "x")
  774. >>> plt.show()
  775. And, finally, let's examine a different section of the ECG which contains
  776. beat forms of different shape. To select only the atypical heart beats, we
  777. combine two conditions: a minimal prominence of 1 and width of at least 20
  778. samples.
  779. >>> x = electrocardiogram()[17000:18000]
  780. >>> peaks, properties = find_peaks(x, prominence=1, width=20)
  781. >>> properties["prominences"], properties["widths"]
  782. (array([1.495, 2.3 ]), array([36.93773946, 39.32723577]))
  783. >>> plt.plot(x)
  784. >>> plt.plot(peaks, x[peaks], "x")
  785. >>> plt.vlines(x=peaks, ymin=x[peaks] - properties["prominences"],
  786. ... ymax = x[peaks], color = "C1")
  787. >>> plt.hlines(y=properties["width_heights"], xmin=properties["left_ips"],
  788. ... xmax=properties["right_ips"], color = "C1")
  789. >>> plt.show()
  790. """
  791. # _argmaxima1d expects array of dtype 'float64'
  792. x = _arg_x_as_expected(x)
  793. if distance is not None and distance < 1:
  794. raise ValueError('`distance` must be greater or equal to 1')
  795. peaks, left_edges, right_edges = _local_maxima_1d(x)
  796. properties = {}
  797. if plateau_size is not None:
  798. # Evaluate plateau size
  799. plateau_sizes = right_edges - left_edges + 1
  800. pmin, pmax = _unpack_condition_args(plateau_size, x, peaks)
  801. keep = _select_by_property(plateau_sizes, pmin, pmax)
  802. peaks = peaks[keep]
  803. properties["plateau_sizes"] = plateau_sizes
  804. properties["left_edges"] = left_edges
  805. properties["right_edges"] = right_edges
  806. properties = {key: array[keep] for key, array in properties.items()}
  807. if height is not None:
  808. # Evaluate height condition
  809. peak_heights = x[peaks]
  810. hmin, hmax = _unpack_condition_args(height, x, peaks)
  811. keep = _select_by_property(peak_heights, hmin, hmax)
  812. peaks = peaks[keep]
  813. properties["peak_heights"] = peak_heights
  814. properties = {key: array[keep] for key, array in properties.items()}
  815. if threshold is not None:
  816. # Evaluate threshold condition
  817. tmin, tmax = _unpack_condition_args(threshold, x, peaks)
  818. keep, left_thresholds, right_thresholds = _select_by_peak_threshold(
  819. x, peaks, tmin, tmax)
  820. peaks = peaks[keep]
  821. properties["left_thresholds"] = left_thresholds
  822. properties["right_thresholds"] = right_thresholds
  823. properties = {key: array[keep] for key, array in properties.items()}
  824. if distance is not None:
  825. # Evaluate distance condition
  826. keep = _select_by_peak_distance(peaks, x[peaks], distance)
  827. peaks = peaks[keep]
  828. properties = {key: array[keep] for key, array in properties.items()}
  829. if prominence is not None or width is not None:
  830. # Calculate prominence (required for both conditions)
  831. wlen = _arg_wlen_as_expected(wlen)
  832. properties.update(zip(
  833. ['prominences', 'left_bases', 'right_bases'],
  834. _peak_prominences(x, peaks, wlen=wlen)
  835. ))
  836. if prominence is not None:
  837. # Evaluate prominence condition
  838. pmin, pmax = _unpack_condition_args(prominence, x, peaks)
  839. keep = _select_by_property(properties['prominences'], pmin, pmax)
  840. peaks = peaks[keep]
  841. properties = {key: array[keep] for key, array in properties.items()}
  842. if width is not None:
  843. # Calculate widths
  844. properties.update(zip(
  845. ['widths', 'width_heights', 'left_ips', 'right_ips'],
  846. _peak_widths(x, peaks, rel_height, properties['prominences'],
  847. properties['left_bases'], properties['right_bases'])
  848. ))
  849. # Evaluate width condition
  850. wmin, wmax = _unpack_condition_args(width, x, peaks)
  851. keep = _select_by_property(properties['widths'], wmin, wmax)
  852. peaks = peaks[keep]
  853. properties = {key: array[keep] for key, array in properties.items()}
  854. return peaks, properties
  855. def _identify_ridge_lines(matr, max_distances, gap_thresh):
  856. """
  857. Identify ridges in the 2-D matrix.
  858. Expect that the width of the wavelet feature increases with increasing row
  859. number.
  860. Parameters
  861. ----------
  862. matr : 2-D ndarray
  863. Matrix in which to identify ridge lines.
  864. max_distances : 1-D sequence
  865. At each row, a ridge line is only connected
  866. if the relative max at row[n] is within
  867. `max_distances`[n] from the relative max at row[n+1].
  868. gap_thresh : int
  869. If a relative maximum is not found within `max_distances`,
  870. there will be a gap. A ridge line is discontinued if
  871. there are more than `gap_thresh` points without connecting
  872. a new relative maximum.
  873. Returns
  874. -------
  875. ridge_lines : tuple
  876. Tuple of 2 1-D sequences. `ridge_lines`[ii][0] are the rows of the
  877. ii-th ridge-line, `ridge_lines`[ii][1] are the columns. Empty if none
  878. found. Each ridge-line will be sorted by row (increasing), but the
  879. order of the ridge lines is not specified.
  880. References
  881. ----------
  882. .. [1] Bioinformatics (2006) 22 (17): 2059-2065.
  883. :doi:`10.1093/bioinformatics/btl355`
  884. Examples
  885. --------
  886. >>> import numpy as np
  887. >>> rng = np.random.default_rng()
  888. >>> data = rng.random((5,5))
  889. >>> max_dist = 3
  890. >>> max_distances = np.full(20, max_dist)
  891. >>> ridge_lines = _identify_ridge_lines(data, max_distances, 1)
  892. Notes
  893. -----
  894. This function is intended to be used in conjunction with `cwt`
  895. as part of `find_peaks_cwt`.
  896. """
  897. if len(max_distances) < matr.shape[0]:
  898. raise ValueError('Max_distances must have at least as many rows '
  899. 'as matr')
  900. all_max_cols = _boolrelextrema(matr, np.greater, axis=1, order=1)
  901. # Highest row for which there are any relative maxima
  902. has_relmax = np.nonzero(all_max_cols.any(axis=1))[0]
  903. if len(has_relmax) == 0:
  904. return []
  905. start_row = has_relmax[-1]
  906. # Each ridge line is a 3-tuple:
  907. # rows, cols,Gap number
  908. ridge_lines = [[[start_row],
  909. [col],
  910. 0] for col in np.nonzero(all_max_cols[start_row])[0]]
  911. final_lines = []
  912. rows = np.arange(start_row - 1, -1, -1)
  913. cols = np.arange(0, matr.shape[1])
  914. for row in rows:
  915. this_max_cols = cols[all_max_cols[row]]
  916. # Increment gap number of each line,
  917. # set it to zero later if appropriate
  918. for line in ridge_lines:
  919. line[2] += 1
  920. # XXX These should always be all_max_cols[row]
  921. # But the order might be different. Might be an efficiency gain
  922. # to make sure the order is the same and avoid this iteration
  923. prev_ridge_cols = np.array([line[1][-1] for line in ridge_lines])
  924. # Look through every relative maximum found at current row
  925. # Attempt to connect them with existing ridge lines.
  926. for ind, col in enumerate(this_max_cols):
  927. # If there is a previous ridge line within
  928. # the max_distance to connect to, do so.
  929. # Otherwise start a new one.
  930. line = None
  931. if len(prev_ridge_cols) > 0:
  932. diffs = np.abs(col - prev_ridge_cols)
  933. closest = np.argmin(diffs)
  934. if diffs[closest] <= max_distances[row]:
  935. line = ridge_lines[closest]
  936. if line is not None:
  937. # Found a point close enough, extend current ridge line
  938. line[1].append(col)
  939. line[0].append(row)
  940. line[2] = 0
  941. else:
  942. new_line = [[row],
  943. [col],
  944. 0]
  945. ridge_lines.append(new_line)
  946. # Remove the ridge lines with gap_number too high
  947. # XXX Modifying a list while iterating over it.
  948. # Should be safe, since we iterate backwards, but
  949. # still tacky.
  950. for ind in range(len(ridge_lines) - 1, -1, -1):
  951. line = ridge_lines[ind]
  952. if line[2] > gap_thresh:
  953. final_lines.append(line)
  954. del ridge_lines[ind]
  955. out_lines = []
  956. for line in (final_lines + ridge_lines):
  957. sortargs = np.array(np.argsort(line[0]))
  958. rows, cols = np.zeros_like(sortargs), np.zeros_like(sortargs)
  959. rows[sortargs] = line[0]
  960. cols[sortargs] = line[1]
  961. out_lines.append([rows, cols])
  962. return out_lines
  963. def _filter_ridge_lines(cwt, ridge_lines, window_size=None, min_length=None,
  964. min_snr=1, noise_perc=10):
  965. """
  966. Filter ridge lines according to prescribed criteria. Intended
  967. to be used for finding relative maxima.
  968. Parameters
  969. ----------
  970. cwt : 2-D ndarray
  971. Continuous wavelet transform from which the `ridge_lines` were defined.
  972. ridge_lines : 1-D sequence
  973. Each element should contain 2 sequences, the rows and columns
  974. of the ridge line (respectively).
  975. window_size : int, optional
  976. Size of window to use to calculate noise floor.
  977. Default is ``cwt.shape[1] / 20``.
  978. min_length : int, optional
  979. Minimum length a ridge line needs to be acceptable.
  980. Default is ``cwt.shape[0] / 4``, ie 1/4-th the number of widths.
  981. min_snr : float, optional
  982. Minimum SNR ratio. Default 1. The signal is the value of
  983. the cwt matrix at the shortest length scale (``cwt[0, loc]``), the
  984. noise is the `noise_perc`th percentile of datapoints contained within a
  985. window of `window_size` around ``cwt[0, loc]``.
  986. noise_perc : float, optional
  987. When calculating the noise floor, percentile of data points
  988. examined below which to consider noise. Calculated using
  989. scipy.stats.scoreatpercentile.
  990. References
  991. ----------
  992. .. [1] Bioinformatics (2006) 22 (17): 2059-2065.
  993. :doi:`10.1093/bioinformatics/btl355`
  994. """
  995. num_points = cwt.shape[1]
  996. if min_length is None:
  997. min_length = np.ceil(cwt.shape[0] / 4)
  998. if window_size is None:
  999. window_size = np.ceil(num_points / 20)
  1000. window_size = int(window_size)
  1001. hf_window, odd = divmod(window_size, 2)
  1002. # Filter based on SNR
  1003. row_one = cwt[0, :]
  1004. noises = np.empty_like(row_one)
  1005. for ind, val in enumerate(row_one):
  1006. window_start = max(ind - hf_window, 0)
  1007. window_end = min(ind + hf_window + odd, num_points)
  1008. noises[ind] = scoreatpercentile(row_one[window_start:window_end],
  1009. per=noise_perc)
  1010. def filt_func(line):
  1011. if len(line[0]) < min_length:
  1012. return False
  1013. snr = abs(cwt[line[0][0], line[1][0]] / noises[line[1][0]])
  1014. if snr < min_snr:
  1015. return False
  1016. return True
  1017. return list(filter(filt_func, ridge_lines))
  1018. def find_peaks_cwt(vector, widths, wavelet=None, max_distances=None,
  1019. gap_thresh=None, min_length=None,
  1020. min_snr=1, noise_perc=10, window_size=None):
  1021. """
  1022. Find peaks in a 1-D array with wavelet transformation.
  1023. The general approach is to smooth `vector` by convolving it with
  1024. `wavelet(width)` for each width in `widths`. Relative maxima which
  1025. appear at enough length scales, and with sufficiently high SNR, are
  1026. accepted.
  1027. Parameters
  1028. ----------
  1029. vector : ndarray
  1030. 1-D array in which to find the peaks.
  1031. widths : float or sequence
  1032. Single width or 1-D array-like of widths to use for calculating
  1033. the CWT matrix. In general,
  1034. this range should cover the expected width of peaks of interest.
  1035. wavelet : callable, optional
  1036. Should take two parameters and return a 1-D array to convolve
  1037. with `vector`. The first parameter determines the number of points
  1038. of the returned wavelet array, the second parameter is the scale
  1039. (`width`) of the wavelet. Should be normalized and symmetric.
  1040. Default is the ricker wavelet.
  1041. max_distances : ndarray, optional
  1042. At each row, a ridge line is only connected if the relative max at
  1043. row[n] is within ``max_distances[n]`` from the relative max at
  1044. ``row[n+1]``. Default value is ``widths/4``.
  1045. gap_thresh : float, optional
  1046. If a relative maximum is not found within `max_distances`,
  1047. there will be a gap. A ridge line is discontinued if there are more
  1048. than `gap_thresh` points without connecting a new relative maximum.
  1049. Default is the first value of the widths array i.e. widths[0].
  1050. min_length : int, optional
  1051. Minimum length a ridge line needs to be acceptable.
  1052. Default is ``cwt.shape[0] / 4``, ie 1/4-th the number of widths.
  1053. min_snr : float, optional
  1054. Minimum SNR ratio. Default 1. The signal is the maximum CWT coefficient
  1055. on the largest ridge line. The noise is `noise_perc` th percentile of
  1056. datapoints contained within the same ridge line.
  1057. noise_perc : float, optional
  1058. When calculating the noise floor, percentile of data points
  1059. examined below which to consider noise. Calculated using
  1060. `stats.scoreatpercentile`. Default is 10.
  1061. window_size : int, optional
  1062. Size of window to use to calculate noise floor.
  1063. Default is ``cwt.shape[1] / 20``.
  1064. Returns
  1065. -------
  1066. peaks_indices : ndarray
  1067. Indices of the locations in the `vector` where peaks were found.
  1068. The list is sorted.
  1069. See Also
  1070. --------
  1071. cwt
  1072. Continuous wavelet transform.
  1073. find_peaks
  1074. Find peaks inside a signal based on peak properties.
  1075. Notes
  1076. -----
  1077. This approach was designed for finding sharp peaks among noisy data,
  1078. however with proper parameter selection it should function well for
  1079. different peak shapes.
  1080. The algorithm is as follows:
  1081. 1. Perform a continuous wavelet transform on `vector`, for the supplied
  1082. `widths`. This is a convolution of `vector` with `wavelet(width)` for
  1083. each width in `widths`. See `cwt`.
  1084. 2. Identify "ridge lines" in the cwt matrix. These are relative maxima
  1085. at each row, connected across adjacent rows. See identify_ridge_lines
  1086. 3. Filter the ridge_lines using filter_ridge_lines.
  1087. .. versionadded:: 0.11.0
  1088. References
  1089. ----------
  1090. .. [1] Bioinformatics (2006) 22 (17): 2059-2065.
  1091. :doi:`10.1093/bioinformatics/btl355`
  1092. Examples
  1093. --------
  1094. >>> import numpy as np
  1095. >>> from scipy import signal
  1096. >>> xs = np.arange(0, np.pi, 0.05)
  1097. >>> data = np.sin(xs)
  1098. >>> peakind = signal.find_peaks_cwt(data, np.arange(1,10))
  1099. >>> peakind, xs[peakind], data[peakind]
  1100. ([32], array([ 1.6]), array([ 0.9995736]))
  1101. """
  1102. widths = np.array(widths, copy=False, ndmin=1)
  1103. if gap_thresh is None:
  1104. gap_thresh = np.ceil(widths[0])
  1105. if max_distances is None:
  1106. max_distances = widths / 4.0
  1107. if wavelet is None:
  1108. wavelet = ricker
  1109. cwt_dat = cwt(vector, wavelet, widths)
  1110. ridge_lines = _identify_ridge_lines(cwt_dat, max_distances, gap_thresh)
  1111. filtered = _filter_ridge_lines(cwt_dat, ridge_lines, min_length=min_length,
  1112. window_size=window_size, min_snr=min_snr,
  1113. noise_perc=noise_perc)
  1114. max_locs = np.asarray([x[1][0] for x in filtered])
  1115. max_locs.sort()
  1116. return max_locs