Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
Histogram.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-2017.
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: Timo Sachsenberg$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_MATH_STATISTICS_HISTOGRAM_H
36 #define OPENMS_MATH_STATISTICS_HISTOGRAM_H
37 
38 //OpenMS
39 #include <OpenMS/CONCEPT/Types.h>
41 
42 //STL
43 #include <vector>
44 #include <cmath>
45 #include <limits>
46 #include <iostream>
47 #include <algorithm>
48 
49 namespace OpenMS
50 {
51  namespace Math
52  {
53 
63  template <typename ValueType = UInt, typename BinSizeType = double>
64  class Histogram
65  {
66 public:
67 
69  typedef typename std::vector<ValueType>::const_iterator ConstIterator;
70 
74  Histogram() :
76  min_(0),
77  max_(0),
78  bin_size_(0)
79  {
80  }
81 
83  Histogram(const Histogram & histogram) :
84  min_(histogram.min_),
85  max_(histogram.max_),
86  bin_size_(histogram.bin_size_),
87  bins_(histogram.bins_)
88  {
89  }
90 
96  Histogram(BinSizeType min, BinSizeType max, BinSizeType bin_size) :
97  min_(min),
98  max_(max),
99  bin_size_(bin_size)
100  {
101  this->initBins_();
102  }
103 
104 
110  template <typename DataIterator>
111  Histogram(DataIterator begin, DataIterator end, BinSizeType min, BinSizeType max, BinSizeType bin_size) :
112  min_(min),
113  max_(max),
114  bin_size_(bin_size)
115  {
116  this->initBins_();
117  for (DataIterator it = begin; it != end; ++it)
118  {
119  this->inc((BinSizeType) *it);
120  }
121  }
122 
123 
125  virtual ~Histogram()
126  {
127  }
128 
130 
132  BinSizeType minBound() const
133  {
134  return min_;
135  }
136 
138  BinSizeType maxBound() const
139  {
140  return max_;
141  }
142 
144  ValueType maxValue() const
145  {
146  return *(std::max_element(bins_.begin(), bins_.end()));
147  }
148 
150  ValueType minValue() const
151  {
152  return *(std::min_element(bins_.begin(), bins_.end()));
153  }
154 
156  BinSizeType binSize() const
157  {
158  return bin_size_;
159  }
160 
162  Size size() const
163  {
164  return bins_.size();
165  }
166 
172  ValueType operator[](Size index) const
173  {
174  if (index >= bins_.size())
175  {
176  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
177  }
178  return bins_[index];
179  }
180 
186  BinSizeType centerOfBin(Size bin_index) const
187  {
188  if (bin_index >= bins_.size())
189  {
190  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
191  }
192 
193  return (BinSizeType)(min_ + ((BinSizeType)bin_index + 0.5) * bin_size_);
194  }
195 
201  ValueType binValue(BinSizeType val) const
202  {
203  return bins_[valToBin_(val)];
204  }
205 
213  Size inc(BinSizeType val, ValueType increment = 1)
214  {
215  Size bin_index = this->valToBin_(val);
216  this->bins_[bin_index] += increment;
217  return bin_index;
218  }
219 
220 
221  Size incUntil(BinSizeType val, bool inclusive, ValueType increment = 1)
222  {
223  Size bin_index = this->valToBin_(val);
224  for (Size i = 0; i < bin_index; ++i)
225  {
226  this->bins_[i] += increment;
227  }
228  if (inclusive)
229  {
230  this->bins_[bin_index] += increment;
231  }
232  return bin_index;
233  }
234 
235  Size incFrom(BinSizeType val, bool inclusive, ValueType increment = 1)
236  {
237  Size bin_index = this->valToBin_(val);
238  for (Size i = bin_index + 1; i < this->bins_.size(); ++i)
239  {
240  this->bins_[i] += increment;
241  }
242  if (inclusive)
243  {
244  this->bins_[bin_index] += increment;
245  }
246  return bin_index;
247  }
248 
249  template< typename DataIterator >
250  static void getCumulativeHistogram(DataIterator begin, DataIterator end,
251  bool complement,
252  bool inclusive,
254  {
255  for (DataIterator it = begin; it != end; ++it)
256  {
257  if (complement)
258  {
259  histogram.incUntil(*it, inclusive);
260  }
261  else
262  {
263  histogram.incFrom(*it, inclusive);
264  }
265  }
266  }
267 
268 
274  void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
275  {
276  min_ = min;
277  max_ = max;
278  bin_size_ = bin_size;
279  bins_.clear();
280 
281  if (bin_size_ <= 0)
282  {
283  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
284  }
285  else
286  {
287  // if max_ == min_ there is only one bin
288  if (max_ != min_)
289  {
290  bins_.resize(Size(ceil((max_ - min_) / bin_size_)), 0);
291  }
292  else
293  {
294  bins_.resize(1, 0);
295  }
296  }
297  }
298 
302  bool operator==(const Histogram & histogram) const
304  {
305  return min_ == histogram.min_ &&
306  max_ == histogram.max_ &&
307  bin_size_ == histogram.bin_size_ &&
308  bins_ == histogram.bins_;
309  }
310 
312  bool operator!=(const Histogram & histogram) const
313  {
314  return !operator==(histogram);
315  }
316 
318  Histogram & operator=(const Histogram & histogram)
319  {
320  if (&histogram == this) return *this;
321 
322  min_ = histogram.min_;
323  max_ = histogram.max_;
324  bin_size_ = histogram.bin_size_;
325  bins_ = histogram.bins_;
326 
327  return *this;
328  }
329 
331 
335  inline ConstIterator begin() const { return bins_.begin(); }
337 
339  inline ConstIterator end() const { return bins_.end(); }
341 
343  void applyLogTransformation(BinSizeType multiplier)
344  {
345  for (typename std::vector<ValueType>::iterator it = bins_.begin(); it != bins_.end(); ++it)
346  {
347  *it = (ValueType)(multiplier * log((BinSizeType)(*it + 1.0f)));
348  }
349  }
350 
351 protected:
353  BinSizeType min_;
355  BinSizeType max_;
357  BinSizeType bin_size_;
359  std::vector<ValueType> bins_;
365  Size valToBin_(BinSizeType val) const
366  {
367  //std::cout << "val: " << val << " (min: " << min_ << " max: " << max_ << ")" << std::endl;
368  if (val < min_ || val > max_)
369  {
370  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
371  }
372  if (val == max_)
373  {
374  return Size(bins_.size() - 1);
375  }
376  else
377  {
378  return (Size) floor((val - min_) / (max_ - min_) * bins_.size());
379  }
380  }
381 
383  void initBins_()
384  {
385  if (this->bin_size_ <= 0)
386  {
387  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
388  }
389  else
390  {
391  // if max_ == min_ there is only one bin
392  if (this->max_ != this->min_)
393  {
394  this->bins_ = std::vector<ValueType>(Size(ceil((max_ - min_) / bin_size_)), 0);
395  }
396  else
397  {
398  this->bins_ = std::vector<ValueType>(1, 0);
399  }
400  }
401  }
402  };
403 
405  template <typename ValueType, typename BinSizeType>
406  std::ostream & operator<<(std::ostream & os, const Histogram<ValueType, BinSizeType> & hist)
407  {
408  for (Size i = 0; i < hist.size(); ++i)
409  {
410  os << hist.centerOfBin(i) << "\t"<< hist[i] << std::endl;
411  }
412  return os;
413  }
414 
415  } // namespace Math
416 
417 } // namespace OpenMS
418 
419 #endif // OPENMS_MATH_STATISTICS_HISTOGRAM_H
Histogram()
default constructor
Definition: Histogram.h:75
Out of range exception.
Definition: Exception.h:320
Size size() const
returns the number of bins
Definition: Histogram.h:162
Histogram(const Histogram &histogram)
copy constructor
Definition: Histogram.h:83
BinSizeType minBound() const
returns the lower bound
Definition: Histogram.h:132
Histogram & operator=(const Histogram &histogram)
Assignment.
Definition: Histogram.h:318
Size inc(BinSizeType val, ValueType increment=1)
increases the bin corresponding to value val by increment
Definition: Histogram.h:213
BinSizeType binSize() const
returns the bin size
Definition: Histogram.h:156
std::vector< ValueType >::const_iterator ConstIterator
Non-mutable iterator of the bins.
Definition: Histogram.h:69
void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
resets the histogram with the given range and bin size
Definition: Histogram.h:274
Int overflow exception.
Definition: Exception.h:255
ValueType maxValue() const
returns the highest value of all bins
Definition: Histogram.h:144
ValueType operator[](Size index) const
returns the value of bin index
Definition: Histogram.h:172
bool operator!=(const Histogram &histogram) const
Inequality operator.
Definition: Histogram.h:312
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
BinSizeType centerOfBin(Size bin_index) const
returns the center position of the bin with the index bin_index
Definition: Histogram.h:186
ValueType minValue() const
returns the lowest value of all bins
Definition: Histogram.h:150
ConstIterator end() const
Non-mutable iterator pointing after the last bin.
Definition: Histogram.h:339
ValueType binValue(BinSizeType val) const
returns the value of bin corresponding to the value val
Definition: Histogram.h:201
BinSizeType bin_size_
Bin size.
Definition: Histogram.h:357
Size incFrom(BinSizeType val, bool inclusive, ValueType increment=1)
Definition: Histogram.h:235
virtual ~Histogram()
destructor
Definition: Histogram.h:125
Representation of a histogram.
Definition: Histogram.h:64
Histogram(BinSizeType min, BinSizeType max, BinSizeType bin_size)
constructor with min, max and bin size
Definition: Histogram.h:96
Size incUntil(BinSizeType val, bool inclusive, ValueType increment=1)
Definition: Histogram.h:221
std::vector< ValueType > bins_
Vector of bins.
Definition: Histogram.h:359
static void getCumulativeHistogram(DataIterator begin, DataIterator end, bool complement, bool inclusive, Histogram< ValueType, BinSizeType > &histogram)
Definition: Histogram.h:250
bool operator==(const Histogram &histogram) const
Equality operator.
Definition: Histogram.h:303
void applyLogTransformation(BinSizeType multiplier)
Transforms the bin values with f(x)=multiplier*log(x+1)
Definition: Histogram.h:343
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
Size valToBin_(BinSizeType val) const
Returns the bin a given value belongs to.
Definition: Histogram.h:365
void initBins_()
initialize the bins
Definition: Histogram.h:383
BinSizeType maxBound() const
returns the upper bound
Definition: Histogram.h:138
ConstIterator begin() const
Non-mutable iterator pointing to the first bin.
Definition: Histogram.h:336
BinSizeType min_
Lower bound.
Definition: Histogram.h:353
BinSizeType max_
Upper bound.
Definition: Histogram.h:355
Histogram(DataIterator begin, DataIterator end, BinSizeType min, BinSizeType max, BinSizeType bin_size)
constructor with data iterator and min, max, bin_size parameters
Definition: Histogram.h:111

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:00 using doxygen 1.8.13