Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

SignalToNoiseEstimator.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Chris Bielow $
00032 // $Authors: $
00033 // --------------------------------------------------------------------------
00034 //
00035 
00036 #ifndef OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATOR_H
00037 #define OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATOR_H
00038 
00039 #include <OpenMS/KERNEL/MSSpectrum.h>
00040 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
00041 #include <OpenMS/CONCEPT/ProgressLogger.h>
00042 
00043 #include <iostream>
00044 #include <vector>
00045 #include <cmath>
00046 #include <map>
00047 
00048 namespace OpenMS
00049 {
00057   template <typename Container = MSSpectrum<> >
00058   class SignalToNoiseEstimator :
00059     public DefaultParamHandler, public ProgressLogger
00060   {
00061 public:
00062 
00066     typedef typename Container::const_iterator PeakIterator;
00067     typedef typename PeakIterator::value_type PeakType;
00068 
00069 
00071 
00073     inline SignalToNoiseEstimator() :
00074       DefaultParamHandler("SignalToNoiseEstimator"),
00075       ProgressLogger(),
00076       first_(),
00077       last_(),
00078       is_result_valid_(false)
00079     {
00080     }
00081 
00083     inline SignalToNoiseEstimator(const SignalToNoiseEstimator & source) :
00084       DefaultParamHandler(source),
00085       ProgressLogger(source),
00086       stn_estimates_(source.stn_estimates_),
00087       first_(source.first_),
00088       last_(source.last_),
00089       is_result_valid_(source.is_result_valid_)
00090     {}
00091 
00093     inline SignalToNoiseEstimator & operator=(const SignalToNoiseEstimator & source)
00094     {
00095       if (&source == this) return *this;
00096 
00097       DefaultParamHandler::operator=(source);
00098       ProgressLogger::operator=(source);
00099       stn_estimates_ = source.stn_estimates_;
00100       first_ = source.first_;
00101       last_  = source.last_;
00102       return *this;
00103     }
00104 
00106     virtual ~SignalToNoiseEstimator()
00107     {}
00108 
00109 
00111     virtual void init(const PeakIterator & it_begin, const PeakIterator & it_end)
00112     {
00113       first_ = it_begin;
00114       last_ = it_end;
00115       computeSTN_(first_, last_);
00116       is_result_valid_ = true;
00117     }
00118 
00120     virtual void init(const Container & c)
00121     {
00122       init(c.begin(), c.end());
00123     }
00124 
00130     virtual double getSignalToNoise(const PeakIterator & data_point)
00131     {
00132       if (!is_result_valid_)
00133       {
00134         // recompute ...
00135         init(first_, last_);
00136       }
00137 
00138       return stn_estimates_[*data_point];
00139     }
00140 
00141     virtual double getSignalToNoise(const PeakType & data_point)
00142     {
00143       if (!is_result_valid_)
00144       {
00145         // recompute ...
00146         init(first_, last_);
00147       }
00148 
00149       return stn_estimates_[data_point];
00150     }
00151 
00152 protected:
00153 
00159     virtual void computeSTN_(const PeakIterator & scan_first_, const PeakIterator & scan_last_) = 0;
00160 
00161 
00162 
00168     struct GaussianEstimate
00169     {
00170       double mean;   
00171       double variance; 
00172     };
00173 
00174 
00176     inline GaussianEstimate estimate_(const PeakIterator & scan_first_, const PeakIterator & scan_last_) const
00177     {
00178       int size = 0;
00179       // add up
00180       double v = 0;
00181       double m = 0;
00182       PeakIterator run = scan_first_;
00183       while (run != scan_last_)
00184       {
00185         m += (*run).getIntensity();
00186         ++size;
00187         ++run;
00188       }
00189       //average
00190       m = m / size;
00191 
00192       //determine variance
00193       run = scan_first_;
00194       while (run != scan_last_)
00195       {
00196         DoubleReal tmp(m - (*run).getIntensity());
00197         v += tmp * tmp;
00198         ++run;
00199       }
00200       v = v / ((double)size); // divide by n
00201 
00202       GaussianEstimate value = {m, v};
00203       return value;
00204     }
00205 
00206     //MEMBERS:
00207 
00209     std::map<PeakType, double, typename PeakType::PositionLess> stn_estimates_;
00210 
00212     PeakIterator first_;
00214     PeakIterator last_;
00216     mutable bool is_result_valid_;
00217   };
00218 
00219 } // namespace OpenMS
00220 
00221 #endif //OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATOR_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:44 using doxygen 1.7.1