| OpenMS 2.5.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-2020.
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
38 #include <OpenMS/KERNEL/MSSpectrum.h>
39 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
40 #include <OpenMS/CONCEPT/ProgressLogger.h>
41
42 #include <vector>
43 #include <cmath>
44 #include <map>
45
46 namespace OpenMS
47 {
55 template <typename Container = MSSpectrum>
56 class SignalToNoiseEstimator :
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
71 inline SignalToNoiseEstimator() :
72 DefaultParamHandler("SignalToNoiseEstimator"),
73 ProgressLogger(),
74 first_(),
75 last_(),
76 is_result_valid_(false)
77 {
78 }
79
81 inline SignalToNoiseEstimator(const SignalToNoiseEstimator & source) :
82 DefaultParamHandler(source),
83 ProgressLogger(source),
84 stn_estimates_(source.stn_estimates_),
85 first_(source.first_),
86 last_(source.last_),
87 is_result_valid_(source.is_result_valid_)
88 {}
89
91 inline SignalToNoiseEstimator & operator=(const SignalToNoiseEstimator & source)
92 {
93 if (&source == this) return *this;
94
95 DefaultParamHandler::operator=(source);
96 ProgressLogger::operator=(source);
97 stn_estimates_ = source.stn_estimates_;
98 first_ = source.first_;
99 last_ = source.last_;
100 return *this;
101 }
102
104 ~SignalToNoiseEstimator() override
105 {}
106
107
109 virtual void init(const PeakIterator & it_begin, const PeakIterator & it_end)
110 {
111 first_ = it_begin;
112 last_ = it_end;
113 computeSTN_(first_, last_);
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
166 struct GaussianEstimate
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
210 PeakIterator first_;
212 PeakIterator last_;
214 mutable bool is_result_valid_;
215 };
216
217 } // namespace OpenMS
218
OpenMS::SignalToNoiseEstimator::GaussianEstimate
protected struct to store parameters my, sigma for a Gaussian distribution
Definition: SignalToNoiseEstimator.h:166
OpenMS::DefaultParamHandler
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91
OpenMS::SignalToNoiseEstimator::getSignalToNoise
virtual double getSignalToNoise(const PeakType &data_point)
Definition: SignalToNoiseEstimator.h:139
OpenMS::SignalToNoiseEstimator::computeSTN_
virtual void computeSTN_(const PeakIterator &scan_first_, const PeakIterator &scan_last_)=0
computes the S/N values when init() is called
OpenMS::SignalToNoiseEstimator::init
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
OpenMS::SignalToNoiseEstimator::operator=
SignalToNoiseEstimator & operator=(const SignalToNoiseEstimator &source)
Assignment operator.
Definition: SignalToNoiseEstimator.h:91
OpenMS::SignalToNoiseEstimator
This class represents the abstract base class of a signal to noise estimator.
Definition: SignalToNoiseEstimator.h:56
OpenMS::ProgressLogger::operator=
ProgressLogger & operator=(const ProgressLogger &other)
Assignment Operator.
OpenMS::SignalToNoiseEstimator::GaussianEstimate::variance
double variance
variance of estimated Gaussian
Definition: SignalToNoiseEstimator.h:169
OpenMS::SignalToNoiseEstimator::is_result_valid_
bool is_result_valid_
flag: set to true if SignalToNoise estimates are calculated and none of the params were changed...
Definition: SignalToNoiseEstimator.h:214
OpenMS::SignalToNoiseEstimator::first_
PeakIterator first_
points to the first raw data point in the interval
Definition: SignalToNoiseEstimator.h:210
OpenMS::SignalToNoiseEstimator::last_
PeakIterator last_
points to the right position next to the last raw data point in the interval
Definition: SignalToNoiseEstimator.h:212
ProgressLogger.h
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::ProgressLogger
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:54
DefaultParamHandler.h
OpenMS::SignalToNoiseEstimator::GaussianEstimate::mean
double mean
mean of estimated Gaussian
Definition: SignalToNoiseEstimator.h:168
MSSpectrum.h
OpenMS::DefaultParamHandler::operator=
virtual DefaultParamHandler & operator=(const DefaultParamHandler &rhs)
Assignment operator.
OpenMS::SignalToNoiseEstimator::PeakType
PeakIterator::value_type PeakType
Definition: SignalToNoiseEstimator.h:65
double
OpenMS::Constants::c
const double c
OpenMS::SignalToNoiseEstimator::stn_estimates_
std::map< PeakType, double, typename PeakType::PositionLess > stn_estimates_
stores the noise estimate for each peak
Definition: SignalToNoiseEstimator.h:207
OpenMS::SignalToNoiseEstimator::estimate_
GaussianEstimate estimate_(const PeakIterator &scan_first_, const PeakIterator &scan_last_) const
calculate mean & stdev of intensities of a spectrum
Definition: SignalToNoiseEstimator.h:174
OpenMS::SignalToNoiseEstimator::init
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
OpenMS::SignalToNoiseEstimator::SignalToNoiseEstimator
SignalToNoiseEstimator()
Constructor.
Definition: SignalToNoiseEstimator.h:71
OpenMS::SignalToNoiseEstimator::~SignalToNoiseEstimator
~SignalToNoiseEstimator() override
Destructor.
Definition: SignalToNoiseEstimator.h:104
OpenMS::SignalToNoiseEstimator::getSignalToNoise
virtual double getSignalToNoise(const PeakIterator &data_point)
Definition: SignalToNoiseEstimator.h:128
OpenMS::SignalToNoiseEstimator::SignalToNoiseEstimator
SignalToNoiseEstimator(const SignalToNoiseEstimator &source)
Copy constructor.
Definition: SignalToNoiseEstimator.h:81
OpenMS::SignalToNoiseEstimator::PeakIterator
Container::const_iterator PeakIterator
Definition: SignalToNoiseEstimator.h:64