Go to the documentation of this file.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
00029
00030
00031
00032
00033
00034
00035 #ifndef OPENMS_MATH_STATISTICS_HISTOGRAM_H
00036 #define OPENMS_MATH_STATISTICS_HISTOGRAM_H
00037
00038
00039 #include <OpenMS/CONCEPT/Types.h>
00040 #include <OpenMS/CONCEPT/Exception.h>
00041
00042
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
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
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
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 }
00342
00343 }
00344
00345 #endif // OPENMS_MATH_STATISTICS_HISTOGRAM_H