00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATOR_H
00029 #define OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATOR_H
00030
00031 #include <OpenMS/KERNEL/MSSpectrum.h>
00032 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
00033 #include <OpenMS/CONCEPT/ProgressLogger.h>
00034
00035 #include <iostream>
00036 #include <vector>
00037 #include <cmath>
00038 #include <map>
00039
00040 namespace OpenMS
00041 {
00049 template < typename Container = MSSpectrum< > >
00050 class SignalToNoiseEstimator: public DefaultParamHandler, public ProgressLogger
00051 {
00052 public:
00053
00057 typedef typename Container::const_iterator PeakIterator;
00058 typedef typename PeakIterator::value_type PeakType;
00059
00060
00062
00064 inline SignalToNoiseEstimator()
00065 : DefaultParamHandler("SignalToNoiseEstimator"),
00066 ProgressLogger(),
00067 first_(0),
00068 last_(0),
00069 is_result_valid_(false)
00070 {
00071 }
00072
00074 inline SignalToNoiseEstimator(const SignalToNoiseEstimator& source)
00075 : DefaultParamHandler(source),
00076 ProgressLogger(source),
00077 stn_estimates_( source.stn_estimates_),
00078 first_(source.first_),
00079 last_(source.last_),
00080 is_result_valid_(source.is_result_valid_)
00081 {}
00082
00084 inline SignalToNoiseEstimator& operator=(const SignalToNoiseEstimator& source)
00085 {
00086 if(&source == this) return *this;
00087
00088 DefaultParamHandler::operator=(source);
00089 ProgressLogger::operator=(source);
00090 stn_estimates_ = source.stn_estimates_;
00091 first_ = source.first_;
00092 last_ = source.last_;
00093 return *this;
00094 }
00095
00097 virtual ~SignalToNoiseEstimator()
00098 {}
00099
00100
00102 virtual void init(const PeakIterator& it_begin, const PeakIterator& it_end)
00103 {
00104 first_=it_begin;
00105 last_=it_end;
00106 computeSTN_(first_, last_);
00107 is_result_valid_ = true;
00108 }
00109
00110
00112 virtual void init(const Container& c)
00113 {
00114 init(c.begin(), c.end() );
00115 }
00116
00122 virtual double getSignalToNoise(const PeakIterator& data_point)
00123 {
00124 if (!is_result_valid_)
00125 {
00126
00127 init(first_, last_);
00128 }
00129
00130 return stn_estimates_[*data_point];
00131 }
00132
00133 virtual double getSignalToNoise(const PeakType& data_point)
00134 {
00135 if (!is_result_valid_)
00136 {
00137
00138 init(first_, last_);
00139 }
00140
00141 return stn_estimates_[data_point];
00142 }
00143
00144 protected:
00145
00146 virtual void computeSTN_(const PeakIterator& scan_first_, const PeakIterator& scan_last_) throw(Exception::InvalidValue) = 0;
00147
00148
00149
00155 struct GaussianEstimate
00156 {
00157 double mean;
00158 double variance;
00159 };
00160
00161
00163 inline GaussianEstimate estimate_(const PeakIterator& scan_first_, const PeakIterator& scan_last_) const
00164 {
00165 int size = 0;
00166
00167 double v = 0;
00168 double m = 0;
00169 PeakIterator run = scan_first_;
00170 while (run != scan_last_)
00171 {
00172 m += (*run).getIntensity();
00173 ++size;
00174 ++run;
00175 }
00176
00177 m = m/size;
00178
00179
00180 run = scan_first_;
00181 while (run != scan_last_)
00182 {
00183 v += std::pow(m - (*run).getIntensity(), 2);
00184 ++run;
00185 }
00186 v = v / ((double)size);
00187
00188 GaussianEstimate value = {m, v};
00189 return value;
00190 }
00191
00192
00193
00195 std::map< PeakType, double, typename PeakType::PositionLess > stn_estimates_;
00196
00198 PeakIterator first_;
00200 PeakIterator last_;
00202 mutable bool is_result_valid_;
00203 };
00204
00205 }
00206
00207 #endif //OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATOR_H