OpenMS  2.4.0
SignalToNoiseEstimator.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2018.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow $
32 // $Authors: $
33 // --------------------------------------------------------------------------
34 //
35 
36 #pragma once
37 
41 
42 #include <vector>
43 #include <cmath>
44 #include <map>
45 
46 namespace OpenMS
47 {
55  template <typename Container = MSSpectrum>
57  public DefaultParamHandler, public ProgressLogger
58  {
59 public:
60 
64  typedef typename Container::const_iterator PeakIterator;
65  typedef typename PeakIterator::value_type PeakType;
66 
67 
69 
72  DefaultParamHandler("SignalToNoiseEstimator"),
74  first_(),
75  last_(),
76  is_result_valid_(false)
77  {
78  }
79 
82  DefaultParamHandler(source),
83  ProgressLogger(source),
85  first_(source.first_),
86  last_(source.last_),
88  {}
89 
92  {
93  if (&source == this) return *this;
94 
98  first_ = source.first_;
99  last_ = source.last_;
100  return *this;
101  }
102 
105  {}
106 
107 
109  virtual void init(const PeakIterator & it_begin, const PeakIterator & it_end)
110  {
111  first_ = it_begin;
112  last_ = it_end;
114  is_result_valid_ = true;
115  }
116 
118  virtual void init(const Container & c)
119  {
120  init(c.begin(), c.end());
121  }
122 
128  virtual double getSignalToNoise(const PeakIterator & data_point)
129  {
130  if (!is_result_valid_)
131  {
132  // recompute ...
133  init(first_, last_);
134  }
135 
136  return stn_estimates_[*data_point];
137  }
138 
139  virtual double getSignalToNoise(const PeakType & data_point)
140  {
141  if (!is_result_valid_)
142  {
143  // recompute ...
144  init(first_, last_);
145  }
146 
147  return stn_estimates_[data_point];
148  }
149 
150 protected:
151 
157  virtual void computeSTN_(const PeakIterator & scan_first_, const PeakIterator & scan_last_) = 0;
158 
159 
160 
167  {
168  double mean;
169  double variance;
170  };
171 
172 
174  inline GaussianEstimate estimate_(const PeakIterator & scan_first_, const PeakIterator & scan_last_) const
175  {
176  int size = 0;
177  // add up
178  double v = 0;
179  double m = 0;
180  PeakIterator run = scan_first_;
181  while (run != scan_last_)
182  {
183  m += (*run).getIntensity();
184  ++size;
185  ++run;
186  }
187  //average
188  m = m / size;
189 
190  //determine variance
191  run = scan_first_;
192  while (run != scan_last_)
193  {
194  double tmp(m - (*run).getIntensity());
195  v += tmp * tmp;
196  ++run;
197  }
198  v = v / ((double)size); // divide by n
199 
200  GaussianEstimate value = {m, v};
201  return value;
202  }
203 
204  //MEMBERS:
205 
207  std::map<PeakType, double, typename PeakType::PositionLess> stn_estimates_;
208 
214  mutable bool is_result_valid_;
215  };
216 
217 } // namespace OpenMS
218 
virtual double getSignalToNoise(const PeakIterator &data_point)
Definition: SignalToNoiseEstimator.h:128
ProgressLogger & operator=(const ProgressLogger &other)
Assignment Operator.
virtual void computeSTN_(const PeakIterator &scan_first_, const PeakIterator &scan_last_)=0
computes the S/N values when init() is called
SignalToNoiseEstimator & operator=(const SignalToNoiseEstimator &source)
Assignment operator.
Definition: SignalToNoiseEstimator.h:91
Container::const_iterator PeakIterator
Definition: SignalToNoiseEstimator.h:64
double variance
mean of estimated Gaussian
Definition: SignalToNoiseEstimator.h:169
const double c
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
double mean
Definition: SignalToNoiseEstimator.h:168
bool is_result_valid_
flag: set to true if SignalToNoise estimates are calculated and none of the params were changed...
Definition: SignalToNoiseEstimator.h:214
virtual void init(const Container &c)
Set the start and endpoint of the raw data interval, for which signal to noise ratios will be estimat...
Definition: SignalToNoiseEstimator.h:118
GaussianEstimate estimate_(const PeakIterator &scan_first_, const PeakIterator &scan_last_) const
calculate mean & stdev of intensities of a spectrum
Definition: SignalToNoiseEstimator.h:174
protected struct to store parameters my, sigma for a Gaussian distribution
Definition: SignalToNoiseEstimator.h:166
virtual void init(const PeakIterator &it_begin, const PeakIterator &it_end)
Set the start and endpoint of the raw data interval, for which signal to noise ratios will be estimat...
Definition: SignalToNoiseEstimator.h:109
PeakIterator last_
points to the right position next to the last raw data point in the interval
Definition: SignalToNoiseEstimator.h:212
PeakIterator first_
points to the first raw data point in the interval
Definition: SignalToNoiseEstimator.h:210
~SignalToNoiseEstimator() override
Destructor.
Definition: SignalToNoiseEstimator.h:104
PeakIterator::value_type PeakType
Definition: SignalToNoiseEstimator.h:65
SignalToNoiseEstimator()
Constructor.
Definition: SignalToNoiseEstimator.h:71
virtual double getSignalToNoise(const PeakType &data_point)
Definition: SignalToNoiseEstimator.h:139
This class represents the abstract base class of a signal to noise estimator.
Definition: SignalToNoiseEstimator.h:56
std::map< PeakType, double, typename PeakType::PositionLess > stn_estimates_
stores the noise estimate for each peak
Definition: SignalToNoiseEstimator.h:207
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:54
virtual DefaultParamHandler & operator=(const DefaultParamHandler &rhs)
Assignment operator.
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91
SignalToNoiseEstimator(const SignalToNoiseEstimator &source)
Copy constructor.
Definition: SignalToNoiseEstimator.h:81