OpenMS  2.4.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-2018.
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 
73  Histogram() :
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 
122 
124  virtual ~Histogram()
125  {
126  }
127 
129 
131  BinSizeType minBound() const
132  {
133  return min_;
134  }
135 
137  BinSizeType maxBound() const
138  {
139  return max_;
140  }
141 
143  ValueType maxValue() const
144  {
145  return *(std::max_element(bins_.begin(), bins_.end()));
146  }
147 
149  ValueType minValue() const
150  {
151  return *(std::min_element(bins_.begin(), bins_.end()));
152  }
153 
155  BinSizeType binSize() const
156  {
157  return bin_size_;
158  }
159 
161  Size size() const
162  {
163  return bins_.size();
164  }
165 
171  ValueType operator[](Size index) const
172  {
173  if (index >= bins_.size())
174  {
175  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
176  }
177  return bins_[index];
178  }
179 
185  BinSizeType centerOfBin(Size bin_index) const
186  {
187  if (bin_index >= bins_.size())
188  {
189  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
190  }
191 
192  return (BinSizeType)(min_ + ((BinSizeType)bin_index + 0.5) * bin_size_);
193  }
194 
200  ValueType binValue(BinSizeType val) const
201  {
202  return bins_[valToBin_(val)];
203  }
204 
212  Size inc(BinSizeType val, ValueType increment = 1)
213  {
214  Size bin_index = this->valToBin_(val);
215  this->bins_[bin_index] += increment;
216  return bin_index;
217  }
218 
219 
220  Size incUntil(BinSizeType val, bool inclusive, ValueType increment = 1)
221  {
222  Size bin_index = this->valToBin_(val);
223  for (Size i = 0; i < bin_index; ++i)
224  {
225  this->bins_[i] += increment;
226  }
227  if (inclusive)
228  {
229  this->bins_[bin_index] += increment;
230  }
231  return bin_index;
232  }
233 
234  Size incFrom(BinSizeType val, bool inclusive, ValueType increment = 1)
235  {
236  Size bin_index = this->valToBin_(val);
237  for (Size i = bin_index + 1; i < this->bins_.size(); ++i)
238  {
239  this->bins_[i] += increment;
240  }
241  if (inclusive)
242  {
243  this->bins_[bin_index] += increment;
244  }
245  return bin_index;
246  }
247 
248  template< typename DataIterator >
249  static void getCumulativeHistogram(DataIterator begin, DataIterator end,
250  bool complement,
251  bool inclusive,
253  {
254  for (DataIterator it = begin; it != end; ++it)
255  {
256  if (complement)
257  {
258  histogram.incUntil(*it, inclusive);
259  }
260  else
261  {
262  histogram.incFrom(*it, inclusive);
263  }
264  }
265  }
266 
267 
273  void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
274  {
275  min_ = min;
276  max_ = max;
277  bin_size_ = bin_size;
278  bins_.clear();
279 
280  if (bin_size_ <= 0)
281  {
282  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
283  }
284  else
285  {
286  // if max_ == min_ there is only one bin
287  if (max_ != min_)
288  {
289  bins_.resize(Size(ceil((max_ - min_) / bin_size_)), 0);
290  }
291  else
292  {
293  bins_.resize(1, 0);
294  }
295  }
296  }
297 
301  bool operator==(const Histogram & histogram) const
303  {
304  return min_ == histogram.min_ &&
305  max_ == histogram.max_ &&
306  bin_size_ == histogram.bin_size_ &&
307  bins_ == histogram.bins_;
308  }
309 
311  bool operator!=(const Histogram & histogram) const
312  {
313  return !operator==(histogram);
314  }
315 
317  Histogram & operator=(const Histogram & histogram)
318  {
319  if (&histogram == this) return *this;
320 
321  min_ = histogram.min_;
322  max_ = histogram.max_;
323  bin_size_ = histogram.bin_size_;
324  bins_ = histogram.bins_;
325 
326  return *this;
327  }
328 
330 
334  inline ConstIterator begin() const { return bins_.begin(); }
336 
338  inline ConstIterator end() const { return bins_.end(); }
340 
342  void applyLogTransformation(BinSizeType multiplier)
343  {
344  for (typename std::vector<ValueType>::iterator it = bins_.begin(); it != bins_.end(); ++it)
345  {
346  *it = (ValueType)(multiplier * log((BinSizeType)(*it + 1.0f)));
347  }
348  }
349 
350 protected:
352  BinSizeType min_;
354  BinSizeType max_;
356  BinSizeType bin_size_;
358  std::vector<ValueType> bins_;
364  Size valToBin_(BinSizeType val) const
365  {
366  //std::cout << "val: " << val << " (min: " << min_ << " max: " << max_ << ")" << std::endl;
367  if (val < min_ || val > max_)
368  {
369  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
370  }
371  if (val == max_)
372  {
373  return Size(bins_.size() - 1);
374  }
375  else
376  {
377  return (Size) floor((val - min_) / (max_ - min_) * bins_.size());
378  }
379  }
380 
382  void initBins_()
383  {
384  if (this->bin_size_ <= 0)
385  {
386  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
387  }
388  else
389  {
390  // if max_ == min_ there is only one bin
391  if (this->max_ != this->min_)
392  {
393  this->bins_ = std::vector<ValueType>(Size(ceil((max_ - min_) / bin_size_)), 0);
394  }
395  else
396  {
397  this->bins_ = std::vector<ValueType>(1, 0);
398  }
399  }
400  }
401  };
402 
404  template <typename ValueType, typename BinSizeType>
405  std::ostream & operator<<(std::ostream & os, const Histogram<ValueType, BinSizeType> & hist)
406  {
407  for (Size i = 0; i < hist.size(); ++i)
408  {
409  os << hist.centerOfBin(i) << "\t"<< hist[i] << std::endl;
410  }
411  return os;
412  }
413 
414  } // namespace Math
415 
416 } // namespace OpenMS
417 
Histogram()
default constructor
Definition: Histogram.h:74
Out of range exception.
Definition: Exception.h:319
Size size() const
returns the number of bins
Definition: Histogram.h:161
Histogram(const Histogram &histogram)
copy constructor
Definition: Histogram.h:82
BinSizeType minBound() const
returns the lower bound
Definition: Histogram.h:131
Histogram & operator=(const Histogram &histogram)
Assignment.
Definition: Histogram.h:317
Size inc(BinSizeType val, ValueType increment=1)
increases the bin corresponding to value val by increment
Definition: Histogram.h:212
BinSizeType binSize() const
returns the bin size
Definition: Histogram.h:155
std::vector< ValueType >::const_iterator ConstIterator
Non-mutable iterator of the bins.
Definition: Histogram.h:68
void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
resets the histogram with the given range and bin size
Definition: Histogram.h:273
Int overflow exception.
Definition: Exception.h:254
ValueType maxValue() const
returns the highest value of all bins
Definition: Histogram.h:143
ValueType operator[](Size index) const
returns the value of bin index
Definition: Histogram.h:171
bool operator!=(const Histogram &histogram) const
Inequality operator.
Definition: Histogram.h:311
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
BinSizeType centerOfBin(Size bin_index) const
returns the center position of the bin with the index bin_index
Definition: Histogram.h:185
ValueType minValue() const
returns the lowest value of all bins
Definition: Histogram.h:149
ConstIterator end() const
Non-mutable iterator pointing after the last bin.
Definition: Histogram.h:338
ValueType binValue(BinSizeType val) const
returns the value of bin corresponding to the value val
Definition: Histogram.h:200
BinSizeType bin_size_
Bin size.
Definition: Histogram.h:356
Size incFrom(BinSizeType val, bool inclusive, ValueType increment=1)
Definition: Histogram.h:234
virtual ~Histogram()
destructor
Definition: Histogram.h:124
Representation of a histogram.
Definition: Histogram.h:63
Histogram(BinSizeType min, BinSizeType max, BinSizeType bin_size)
constructor with min, max and bin size
Definition: Histogram.h:95
Size incUntil(BinSizeType val, bool inclusive, ValueType increment=1)
Definition: Histogram.h:220
std::vector< ValueType > bins_
Vector of bins.
Definition: Histogram.h:358
static void getCumulativeHistogram(DataIterator begin, DataIterator end, bool complement, bool inclusive, Histogram< ValueType, BinSizeType > &histogram)
Definition: Histogram.h:249
bool operator==(const Histogram &histogram) const
Equality operator.
Definition: Histogram.h:302
void applyLogTransformation(BinSizeType multiplier)
Transforms the bin values with f(x)=multiplier*log(x+1)
Definition: Histogram.h:342
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Size valToBin_(BinSizeType val) const
Returns the bin a given value belongs to.
Definition: Histogram.h:364
void initBins_()
initialize the bins
Definition: Histogram.h:382
BinSizeType maxBound() const
returns the upper bound
Definition: Histogram.h:137
ConstIterator begin() const
Non-mutable iterator pointing to the first bin.
Definition: Histogram.h:335
BinSizeType min_
Lower bound.
Definition: Histogram.h:352
BinSizeType max_
Upper bound.
Definition: Histogram.h:354
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