OpenMS  2.7.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 
251 
252  Size incUntil(BinSizeType val, bool inclusive, ValueType increment = 1)
253  {
254  Size bin_index = this->valueToBin(val);
255  for (Size i = 0; i < bin_index; ++i)
256  {
257  this->bins_[i] += increment;
258  }
259  if (inclusive)
260  {
261  this->bins_[bin_index] += increment;
262  }
263  return bin_index;
264  }
265 
266  Size incFrom(BinSizeType val, bool inclusive, ValueType increment = 1)
267  {
268  Size bin_index = this->valueToBin(val);
269  for (Size i = bin_index + 1; i < this->bins_.size(); ++i)
270  {
271  this->bins_[i] += increment;
272  }
273  if (inclusive)
274  {
275  this->bins_[bin_index] += increment;
276  }
277  return bin_index;
278  }
279 
280  template< typename DataIterator >
281  static void getCumulativeHistogram(DataIterator begin, DataIterator end,
282  bool complement,
283  bool inclusive,
285  {
286  for (DataIterator it = begin; it != end; ++it)
287  {
288  if (complement)
289  {
290  histogram.incUntil(*it, inclusive);
291  }
292  else
293  {
294  histogram.incFrom(*it, inclusive);
295  }
296  }
297  }
298 
299 
305  void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
306  {
307  min_ = min;
308  max_ = max;
309  bin_size_ = bin_size;
310  bins_.clear();
311 
312  if (bin_size_ <= 0)
313  {
314  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
315  }
316  else
317  {
318  // if max_ == min_ there is only one bin
319  if (max_ != min_)
320  {
321  bins_.resize(Size(ceil((max_ - min_) / bin_size_)), 0);
322  }
323  else
324  {
325  bins_.resize(1, 0);
326  }
327  }
328  }
329 
334  bool operator==(const Histogram & histogram) const
335  {
336  return min_ == histogram.min_ &&
337  max_ == histogram.max_ &&
338  bin_size_ == histogram.bin_size_ &&
339  bins_ == histogram.bins_;
340  }
341 
343  bool operator!=(const Histogram & histogram) const
344  {
345  return !operator==(histogram);
346  }
347 
349  Histogram & operator=(const Histogram & histogram)
350  {
351  if (&histogram == this) return *this;
352 
353  min_ = histogram.min_;
354  max_ = histogram.max_;
355  bin_size_ = histogram.bin_size_;
356  bins_ = histogram.bins_;
357 
358  return *this;
359  }
360 
362 
367  inline ConstIterator begin() const { return bins_.begin(); }
368 
370  inline ConstIterator end() const { return bins_.end(); }
372 
374  void applyLogTransformation(BinSizeType multiplier)
375  {
376  for (typename std::vector<ValueType>::iterator it = bins_.begin(); it != bins_.end(); ++it)
377  {
378  *it = (ValueType)(multiplier * log((BinSizeType)(*it + 1.0f)));
379  }
380  }
381 
387  Size valueToBin(BinSizeType val) const
388  {
389  //std::cout << "val: " << val << " (min: " << min_ << " max: " << max_ << ")" << std::endl;
390  if (val < min_ || val > max_)
391  {
392  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
393  }
394  if (val == max_)
395  {
396  return Size(bins_.size() - 1);
397  }
398  else
399  {
400  return (Size) floor((val - min_) / bin_size_);
401  }
402  }
403 
404 protected:
406  BinSizeType min_;
408  BinSizeType max_;
410  BinSizeType bin_size_;
412  std::vector<ValueType> bins_;
413 
414 
416  void initBins_()
417  {
418  if (this->bin_size_ <= 0)
419  {
420  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
421  }
422  else
423  {
424  if (this->max_ != this->min_)
425  {
426  this->bins_ = std::vector<ValueType>(Size(ceil((max_ - min_) / bin_size_)), 0);
427  }
428  else
429  { // if max_ == min_ there is only one bin
430  this->bins_ = std::vector<ValueType>(1, 0);
431  }
432  }
433  }
434  };
435 
437  template <typename ValueType, typename BinSizeType>
438  std::ostream & operator<<(std::ostream & os, const Histogram<ValueType, BinSizeType> & hist)
439  {
440  for (Size i = 0; i < hist.size(); ++i)
441  {
442  os << hist.centerOfBin(i) << "\t"<< hist[i] << std::endl;
443  }
444  return os;
445  }
446 
447  } // namespace Math
448 
449 } // namespace OpenMS
450 
Int overflow exception.
Definition: Exception.h:248
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:412
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:374
BinSizeType bin_size_
Bin size.
Definition: Histogram.h:410
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:343
BinSizeType max_
Upper bound.
Definition: Histogram.h:408
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:370
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:334
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:349
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:281
void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
resets the histogram with the given range and bin size
Definition: Histogram.h:305
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:367
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)
Definition: Histogram.h:266
Histogram(const Histogram &histogram)
copy constructor
Definition: Histogram.h:82
void initBins_()
initialize the bins
Definition: Histogram.h:416
Size incUntil(BinSizeType val, bool inclusive, ValueType increment=1)
Definition: Histogram.h:252
Size valueToBin(BinSizeType val) const
Returns the bin index a given value belongs to.
Definition: Histogram.h:387
BinSizeType min_
Lower bound.
Definition: Histogram.h:406
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:438
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47