xboa
_window_peak_finder.py
Go to the documentation of this file.
1 # This file is a part of xboa
2 #
3 # xboa is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # xboa is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with xboa in the doc folder. If not, see
15 # <http://www.gnu.org/licenses/>.
16 
17 class WindowPeakFinder(object):
18  """
19  Find peaks in a list of data points by looking for the peak within a window
20 
21  This algorithm will ignore subpeaks that are close to the main peak
22  (assuming e.g. that they are noise)
23  """
24  def __init__(self, window_size, threshold_over_mean, window_step):
25  """
26  Initialise the peak finder
27  - window_size integer size of the window over which we look for peaks.
28  - threshold_over_mean float, require the peak to be greater than a
29  multiple of the data mean within the window to be registered
30  - window_step float, move the window by window_step on each iteration
31  """
32  self.window_size = int(window_size)
33  self.threshold = float(threshold_over_mean)
34  self.window_step = int(window_step)
35 
36  def find_peaks(self, data):
37  """
38  Find peaks in the data
39 
40  - data list of floats that contains data within which we seek to find
41  peaks
42 
43  Makes a window and looks for the highest value within that window. If
44  the highest value is at the boundary of the window, it is ignored; else
45  the peak is saved. The index corresponding to window start is then
46  incremented by one and the routine is repeated.
47 
48  uses window_size that is the smallest of self.window_size and len(data)
49 
50  Returns a list of indices, each index corresponding to the location of a
51  peak
52  """
53  peaks = []
54  window_size = self.window_size
55  if len(data) < self.window_size:
56  window_size = len(data)
57  win_sz_float = float(window_size)
58  for i in range(0, len(data)-window_size+1, self.window_step):
59  # optimisation - we can update max_value and max_index by just
60  # checking the next entry, rather than checking the entire data set
61  test = data[i:i+window_size]
62  max_value = max(test)
63  max_index = test.index(max_value)
64  if (i != 0 and max_index == 0) or \
65  (i != len(data)-window_size and \
66  max_index == window_size-1): # on bounds
67  pass
68  else:
69  max_index += i
70  if max_index not in peaks:
71  if self.threshold < 1.:
72  peaks.append(max_index)
73  else: # computationally expensive, so try to leave it til last
74  mean = sum(test)/win_sz_float
75  sigma = (sum([x**2 for x in test])-mean**2)**0.5\
76  /win_sz_float
77  if max_value-mean > sigma*self.threshold:
78  peaks.append(max_index)
79  return peaks
80 
Find peaks in a list of data points by looking for the peak within a window.