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

Histogram.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: Stephan Aiche$
00032 // $Authors: Marc Sturm $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_MATH_STATISTICS_HISTOGRAM_H
00036 #define OPENMS_MATH_STATISTICS_HISTOGRAM_H
00037 
00038 //OpenMS
00039 #include <OpenMS/CONCEPT/Types.h>
00040 #include <OpenMS/CONCEPT/Exception.h>
00041 
00042 //STL
00043 #include <vector>
00044 #include <cmath>
00045 #include <limits>
00046 #include <iostream>
00047 #include <algorithm>
00048 
00049 namespace OpenMS
00050 {
00051   namespace Math
00052   {
00053 
00063     template <typename ValueType = UInt, typename BinSizeType = DoubleReal>
00064     class Histogram
00065     {
00066 public:
00067 
00069       typedef typename std::vector<ValueType>::const_iterator ConstIterator;
00070 
00074 
00075       Histogram() :
00076         min_(0),
00077         max_(0),
00078         bin_size_(0)
00079       {
00080       }
00081 
00083       Histogram(const Histogram & histogram) :
00084         min_(histogram.min_),
00085         max_(histogram.max_),
00086         bin_size_(histogram.bin_size_),
00087         bins_(histogram.bins_)
00088       {
00089       }
00090 
00096       Histogram(BinSizeType min, BinSizeType max, BinSizeType bin_size) :
00097         min_(min),
00098         max_(max),
00099         bin_size_(bin_size)
00100       {
00101         if (bin_size_ <= 0)
00102         {
00103           throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
00104         }
00105         else
00106         {
00107           // if max_ == min_ there is only one bin
00108           if (max_ != min_)
00109           {
00110             bins_ = std::vector<ValueType>(Size(ceil((max_ - min_) / bin_size_)), 0);
00111           }
00112           else
00113           {
00114             bins_ = std::vector<ValueType>(1, 0);
00115           }
00116         }
00117       }
00118 
00120       virtual ~Histogram()
00121       {
00122       }
00123 
00125 
00127       BinSizeType minBound() const
00128       {
00129         return min_;
00130       }
00131 
00133       BinSizeType maxBound() const
00134       {
00135         return max_;
00136       }
00137 
00139       ValueType maxValue() const
00140       {
00141         return *(std::max_element(bins_.begin(), bins_.end()));
00142       }
00143 
00145       ValueType minValue() const
00146       {
00147         return *(std::min_element(bins_.begin(), bins_.end()));
00148       }
00149 
00151       BinSizeType binSize() const
00152       {
00153         return bin_size_;
00154       }
00155 
00157       Size size() const
00158       {
00159         return bins_.size();
00160       }
00161 
00167       ValueType operator[](Size index) const
00168       {
00169         if (index >= bins_.size())
00170         {
00171           throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__);
00172         }
00173         return bins_[index];
00174       }
00175 
00181       BinSizeType centerOfBin(Size bin_index) const
00182       {
00183         if (bin_index >= bins_.size())
00184         {
00185           throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__);
00186         }
00187 
00188         return (BinSizeType)(min_ + ((BinSizeType)bin_index + 0.5) * bin_size_);
00189       }
00190 
00196       ValueType binValue(BinSizeType val) const
00197       {
00198         return bins_[valToBin_(val)];
00199       }
00200 
00208       Size inc(BinSizeType val, ValueType increment = 1)
00209       {
00210         Size bin_index = valToBin_(val);
00211         bins_[bin_index] += increment;
00212         return bin_index;
00213       }
00214 
00220       void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
00221       {
00222         min_ = min;
00223         max_ = max;
00224         bin_size_ = bin_size;
00225         bins_.clear();
00226 
00227         if (bin_size_ <= 0)
00228         {
00229           throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
00230         }
00231         else
00232         {
00233           // if max_ == min_ there is only one bin
00234           if (max_ != min_)
00235           {
00236             bins_.resize(Size(ceil((max_ - min_) / bin_size_)), 0);
00237           }
00238           else
00239           {
00240             bins_.resize(1, 0);
00241           }
00242         }
00243       }
00244 
00248 
00249       bool operator==(const Histogram & histogram) const
00250       {
00251         return min_ == histogram.min_ &&
00252                max_ == histogram.max_ &&
00253                bin_size_ == histogram.bin_size_ &&
00254                bins_ == histogram.bins_;
00255       }
00256 
00258       bool operator!=(const Histogram & histogram) const
00259       {
00260         return !operator==(histogram);
00261       }
00262 
00264       Histogram & operator=(const Histogram & histogram)
00265       {
00266         if (&histogram == this) return *this;
00267 
00268         min_ = histogram.min_;
00269         max_ = histogram.max_;
00270         bin_size_ = histogram.bin_size_;
00271         bins_ = histogram.bins_;
00272 
00273         return *this;
00274       }
00275 
00277 
00281 
00282       inline ConstIterator begin() const { return bins_.begin(); }
00283 
00285       inline ConstIterator end() const { return bins_.end(); }
00287 
00289       void applyLogTransformation(BinSizeType multiplier)
00290       {
00291         for (typename std::vector<ValueType>::iterator it = bins_.begin(); it != bins_.end(); ++it)
00292         {
00293           *it = (ValueType)(multiplier * log((BinSizeType)(*it + 1.0f)));
00294         }
00295       }
00296 
00297 protected:
00299       BinSizeType min_;
00301       BinSizeType max_;
00303       BinSizeType bin_size_;
00305       std::vector<ValueType> bins_;
00311       Size valToBin_(BinSizeType val) const
00312       {
00313         //std::cout << "val: " << val << " (min: " << min_ << " max: " << max_ << ")" << std::endl;
00314         if (val < min_ || val > max_)
00315         {
00316           throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
00317         }
00318         if (val == max_)
00319         {
00320           return Size(bins_.size() - 1);
00321         }
00322         else
00323         {
00324           return (Size) floor((val - min_) / (max_ - min_) * bins_.size());
00325         }
00326       }
00327 
00328     };
00329 
00331     template <typename ValueType, typename BinSizeType>
00332     std::ostream & operator<<(std::ostream & os, const Histogram<ValueType, BinSizeType> & hist)
00333     {
00334       for (Size i = 0; i < hist.size(); ++i)
00335       {
00336         os << hist.centerOfBin(i) << "\t"<< hist[i] << std::endl;
00337       }
00338       return os;
00339     }
00340 
00341   }   // namespace Math
00342 
00343 } // namespace OpenMS
00344 
00345 #endif // OPENMS_MATH_STATISTICS_HISTOGRAM_H

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