OpenMS  2.8.0
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-2021.
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 #pragma once
36 
37 //OpenMS
38 #include <OpenMS/CONCEPT/Types.h>
40 
41 //STL
42 #include <vector>
43 #include <cmath>
44 #include <limits>
45 #include <iostream>
46 #include <algorithm>
47 
48 namespace OpenMS
49 {
50  namespace Math
51  {
52 
62  template <typename ValueType = UInt, typename BinSizeType = double>
63  class Histogram
64  {
65 public:
66 
68  typedef typename std::vector<ValueType>::const_iterator ConstIterator;
69 
75  min_(0),
76  max_(0),
77  bin_size_(0)
78  {
79  }
80 
82  Histogram(const Histogram & histogram) :
83  min_(histogram.min_),
84  max_(histogram.max_),
85  bin_size_(histogram.bin_size_),
86  bins_(histogram.bins_)
87  {
88  }
89 
95  Histogram(BinSizeType min, BinSizeType max, BinSizeType bin_size) :
96  min_(min),
97  max_(max),
98  bin_size_(bin_size)
99  {
100  this->initBins_();
101  }
102 
103 
109  template <typename DataIterator>
110  Histogram(DataIterator begin, DataIterator end, BinSizeType min, BinSizeType max, BinSizeType bin_size) :
111  min_(min),
112  max_(max),
113  bin_size_(bin_size)
114  {
115  this->initBins_();
116  for (DataIterator it = begin; it != end; ++it)
117  {
118  this->inc((BinSizeType) *it);
119  }
120  }
121 
123  virtual ~Histogram()
124  {
125  }
126 
128 
130  BinSizeType minBound() const
131  {
132  return min_;
133  }
134 
136  BinSizeType maxBound() const
137  {
138  return max_;
139  }
140 
142  ValueType maxValue() const
143  {
144  return *(std::max_element(bins_.begin(), bins_.end()));
145  }
146 
148  ValueType minValue() const
149  {
150  return *(std::min_element(bins_.begin(), bins_.end()));
151  }
152 
154  BinSizeType binSize() const
155  {
156  return bin_size_;
157  }
158 
160  Size size() const
161  {
162  return bins_.size();
163  }
164 
170  ValueType operator[](Size index) const
171  {
172  if (index >= bins_.size())
173  {
174  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
175  }
176  return bins_[index];
177  }
178 
184  BinSizeType centerOfBin(Size bin_index) const
185  {
186  if (bin_index >= bins_.size())
187  {
188  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
189  }
190 
191  return (BinSizeType)(min_ + ((BinSizeType)bin_index + 0.5) * bin_size_);
192  }
193 
199  BinSizeType rightBorderOfBin(Size bin_index) const
200  {
201  if (bin_index >= bins_.size())
202  {
203  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
204  }
205  if (bin_index + 1 == bins_.size())
206  { // rightmost bin is special. It contains the maximum value - see valueToBin().
207  return std::nextafter(maxBound(), maxBound() + 1);
208  }
209  return (BinSizeType)(min_ + ((BinSizeType) bin_index + 1) * bin_size_);
210  }
211 
217  BinSizeType leftBorderOfBin(Size bin_index) const
218  {
219  if (bin_index >= bins_.size())
220  {
221  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
222  }
223 
224  return (BinSizeType)(min_ + (BinSizeType)bin_index * bin_size_);
225  }
226 
232  ValueType binValue(BinSizeType val) const
233  {
234  return bins_[valueToBin(val)];
235  }
236 
244  Size inc(BinSizeType val, ValueType increment = 1)
245  {
246  Size bin_index = this->valueToBin(val);
247  this->bins_[bin_index] += increment;
248  return bin_index;
249  }
250 
258  Size incUntil(BinSizeType val, bool inclusive, ValueType increment = 1)
259  {
260  Size bin_index = this->valueToBin(val);
261  for (Size i = 0; i < bin_index; ++i)
262  {
263  this->bins_[i] += increment;
264  }
265  if (inclusive)
266  {
267  this->bins_[bin_index] += increment;
268  }
269  return bin_index;
270  }
271 
279  Size incFrom(BinSizeType val, bool inclusive, ValueType increment = 1)
280  {
281  Size bin_index = this->valueToBin(val);
282  for (Size i = bin_index + 1; i < this->bins_.size(); ++i)
283  {
284  this->bins_[i] += increment;
285  }
286  if (inclusive)
287  {
288  this->bins_[bin_index] += increment;
289  }
290  return bin_index;
291  }
292 
293  template< typename DataIterator >
294  static void getCumulativeHistogram(DataIterator begin, DataIterator end,
295  bool complement,
296  bool inclusive,
298  {
299  for (DataIterator it = begin; it != end; ++it)
300  {
301  if (complement)
302  {
303  histogram.incUntil(*it, inclusive);
304  }
305  else
306  {
307  histogram.incFrom(*it, inclusive);
308  }
309  }
310  }
311 
312 
318  void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
319  {
320  min_ = min;
321  max_ = max;
322  bin_size_ = bin_size;
323  bins_.clear();
324 
325  if (bin_size_ <= 0)
326  {
327  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
328  }
329  else
330  {
331  // if max_ == min_ there is only one bin
332  if (max_ != min_)
333  {
334  bins_.resize(Size(ceil((max_ - min_) / bin_size_)), 0);
335  }
336  else
337  {
338  bins_.resize(1, 0);
339  }
340  }
341  }
342 
347  bool operator==(const Histogram & histogram) const
348  {
349  return min_ == histogram.min_ &&
350  max_ == histogram.max_ &&
351  bin_size_ == histogram.bin_size_ &&
352  bins_ == histogram.bins_;
353  }
354 
356  bool operator!=(const Histogram & histogram) const
357  {
358  return !operator==(histogram);
359  }
360 
362  Histogram & operator=(const Histogram & histogram)
363  {
364  if (&histogram == this) return *this;
365 
366  min_ = histogram.min_;
367  max_ = histogram.max_;
368  bin_size_ = histogram.bin_size_;
369  bins_ = histogram.bins_;
370 
371  return *this;
372  }
373 
375 
380  inline ConstIterator begin() const { return bins_.begin(); }
381 
383  inline ConstIterator end() const { return bins_.end(); }
385 
387  void applyLogTransformation(BinSizeType multiplier)
388  {
389  for (typename std::vector<ValueType>::iterator it = bins_.begin(); it != bins_.end(); ++it)
390  {
391  *it = (ValueType)(multiplier * log((BinSizeType)(*it + 1.0f)));
392  }
393  }
394 
400  Size valueToBin(BinSizeType val) const
401  {
402  //std::cout << "val: " << val << " (min: " << min_ << " max: " << max_ << ")" << std::endl;
403  if (val < min_ || val > max_)
404  {
405  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
406  }
407  if (val == max_)
408  {
409  return Size(bins_.size() - 1);
410  }
411  else
412  {
413  return (Size) floor((val - min_) / bin_size_);
414  }
415  }
416 
417 protected:
419  BinSizeType min_;
421  BinSizeType max_;
423  BinSizeType bin_size_;
425  std::vector<ValueType> bins_;
426 
427 
429  void initBins_()
430  {
431  if (this->bin_size_ <= 0)
432  {
433  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
434  }
435  else
436  {
437  if (this->max_ != this->min_)
438  {
439  this->bins_ = std::vector<ValueType>(Size(ceil((max_ - min_) / bin_size_)), 0);
440  }
441  else
442  { // if max_ == min_ there is only one bin
443  this->bins_ = std::vector<ValueType>(1, 0);
444  }
445  }
446  }
447  };
448 
450  template <typename ValueType, typename BinSizeType>
451  std::ostream & operator<<(std::ostream & os, const Histogram<ValueType, BinSizeType> & hist)
452  {
453  for (Size i = 0; i < hist.size(); ++i)
454  {
455  os << hist.centerOfBin(i) << "\t"<< hist[i] << std::endl;
456  }
457  return os;
458  }
459 
460  } // namespace Math
461 
462 } // namespace OpenMS
463 
Int overflow exception.
Definition: Exception.h:247
Out of range exception.
Definition: Exception.h:313
Representation of a histogram.
Definition: Histogram.h:64
Histogram(BinSizeType min, BinSizeType max, BinSizeType bin_size)
constructor with min, max (inclusive) and bin width
Definition: Histogram.h:95
std::vector< ValueType > bins_
Vector of bins.
Definition: Histogram.h:425
ValueType operator[](Size index) const
returns the value of bin index
Definition: Histogram.h:170
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:110
void applyLogTransformation(BinSizeType multiplier)
Transforms the bin values with f(x)=multiplier*log(x+1)
Definition: Histogram.h:387
BinSizeType bin_size_
Bin size.
Definition: Histogram.h:423
virtual ~Histogram()
destructor
Definition: Histogram.h:123
std::vector< ValueType >::const_iterator ConstIterator
Non-mutable iterator of the bins.
Definition: Histogram.h:68
bool operator!=(const Histogram &histogram) const
Inequality operator.
Definition: Histogram.h:356
BinSizeType max_
Upper bound.
Definition: Histogram.h:421
ValueType maxValue() const
returns the bin with the highest count
Definition: Histogram.h:142
BinSizeType maxBound() const
returns the upper bound (inclusive)
Definition: Histogram.h:136
ConstIterator end() const
Non-mutable iterator pointing after the last bin.
Definition: Histogram.h:383
Size inc(BinSizeType val, ValueType increment=1)
increases the bin corresponding to value val by increment
Definition: Histogram.h:244
ValueType binValue(BinSizeType val) const
returns the value of bin corresponding to the value val
Definition: Histogram.h:232
bool operator==(const Histogram &histogram) const
Equality operator.
Definition: Histogram.h:347
BinSizeType rightBorderOfBin(Size bin_index) const
returns the first value to the right side of the bin with the index bin_index, which is not part of t...
Definition: Histogram.h:199
BinSizeType minBound() const
returns the lower bound (inclusive)
Definition: Histogram.h:130
Histogram & operator=(const Histogram &histogram)
Assignment.
Definition: Histogram.h:362
BinSizeType centerOfBin(Size bin_index) const
returns the center position of the bin with the index bin_index
Definition: Histogram.h:184
BinSizeType leftBorderOfBin(Size bin_index) const
returns the leftmost value of the bin with the index bin_index, which is part of this bin,...
Definition: Histogram.h:217
static void getCumulativeHistogram(DataIterator begin, DataIterator end, bool complement, bool inclusive, Histogram< ValueType, BinSizeType > &histogram)
Definition: Histogram.h:294
void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
resets the histogram with the given range and bin size
Definition: Histogram.h:318
Size size() const
returns the number of bins
Definition: Histogram.h:160
ConstIterator begin() const
Non-mutable iterator pointing to the first bin.
Definition: Histogram.h:380
BinSizeType binSize() const
returns the bin size
Definition: Histogram.h:154
Histogram()
default constructor
Definition: Histogram.h:74
ValueType minValue() const
returns the bin with lowest count
Definition: Histogram.h:148
Size incFrom(BinSizeType val, bool inclusive, ValueType increment=1)
Increment all bins from the bin of val to the highest(=last) bin by a certain number of counts.
Definition: Histogram.h:279
Histogram(const Histogram &histogram)
copy constructor
Definition: Histogram.h:82
void initBins_()
initialize the bins
Definition: Histogram.h:429
Size incUntil(BinSizeType val, bool inclusive, ValueType increment=1)
Increment all bins from to lowest(=first) bin up to (and including?) the bin for val by a certain num...
Definition: Histogram.h:258
Size valueToBin(BinSizeType val) const
Returns the bin index a given value belongs to.
Definition: Histogram.h:400
BinSizeType min_
Lower bound.
Definition: Histogram.h:419
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
std::ostream & operator<<(std::ostream &os, const Histogram< ValueType, BinSizeType > &hist)
Print the contents to a stream.
Definition: Histogram.h:451
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47