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-2013.
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: Stephan Aiche$
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 = DoubleReal>
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  if (bin_size_ <= 0)
102  {
103  throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
104  }
105  else
106  {
107  // if max_ == min_ there is only one bin
108  if (max_ != min_)
109  {
110  bins_ = std::vector<ValueType>(Size(ceil((max_ - min_) / bin_size_)), 0);
111  }
112  else
113  {
114  bins_ = std::vector<ValueType>(1, 0);
115  }
116  }
117  }
118 
120  virtual ~Histogram()
121  {
122  }
123 
125 
127  BinSizeType minBound() const
128  {
129  return min_;
130  }
131 
133  BinSizeType maxBound() const
134  {
135  return max_;
136  }
137 
139  ValueType maxValue() const
140  {
141  return *(std::max_element(bins_.begin(), bins_.end()));
142  }
143 
145  ValueType minValue() const
146  {
147  return *(std::min_element(bins_.begin(), bins_.end()));
148  }
149 
151  BinSizeType binSize() const
152  {
153  return bin_size_;
154  }
155 
157  Size size() const
158  {
159  return bins_.size();
160  }
161 
167  ValueType operator[](Size index) const
168  {
169  if (index >= bins_.size())
170  {
171  throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__);
172  }
173  return bins_[index];
174  }
175 
181  BinSizeType centerOfBin(Size bin_index) const
182  {
183  if (bin_index >= bins_.size())
184  {
185  throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__);
186  }
187 
188  return (BinSizeType)(min_ + ((BinSizeType)bin_index + 0.5) * bin_size_);
189  }
190 
196  ValueType binValue(BinSizeType val) const
197  {
198  return bins_[valToBin_(val)];
199  }
200 
208  Size inc(BinSizeType val, ValueType increment = 1)
209  {
210  Size bin_index = valToBin_(val);
211  bins_[bin_index] += increment;
212  return bin_index;
213  }
214 
220  void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
221  {
222  min_ = min;
223  max_ = max;
224  bin_size_ = bin_size;
225  bins_.clear();
226 
227  if (bin_size_ <= 0)
228  {
229  throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
230  }
231  else
232  {
233  // if max_ == min_ there is only one bin
234  if (max_ != min_)
235  {
236  bins_.resize(Size(ceil((max_ - min_) / bin_size_)), 0);
237  }
238  else
239  {
240  bins_.resize(1, 0);
241  }
242  }
243  }
244 
248  bool operator==(const Histogram & histogram) const
250  {
251  return min_ == histogram.min_ &&
252  max_ == histogram.max_ &&
253  bin_size_ == histogram.bin_size_ &&
254  bins_ == histogram.bins_;
255  }
256 
258  bool operator!=(const Histogram & histogram) const
259  {
260  return !operator==(histogram);
261  }
262 
264  Histogram & operator=(const Histogram & histogram)
265  {
266  if (&histogram == this) return *this;
267 
268  min_ = histogram.min_;
269  max_ = histogram.max_;
270  bin_size_ = histogram.bin_size_;
271  bins_ = histogram.bins_;
272 
273  return *this;
274  }
275 
277 
281  inline ConstIterator begin() const { return bins_.begin(); }
283 
285  inline ConstIterator end() const { return bins_.end(); }
287 
289  void applyLogTransformation(BinSizeType multiplier)
290  {
291  for (typename std::vector<ValueType>::iterator it = bins_.begin(); it != bins_.end(); ++it)
292  {
293  *it = (ValueType)(multiplier * log((BinSizeType)(*it + 1.0f)));
294  }
295  }
296 
297 protected:
299  BinSizeType min_;
301  BinSizeType max_;
303  BinSizeType bin_size_;
305  std::vector<ValueType> bins_;
311  Size valToBin_(BinSizeType val) const
312  {
313  //std::cout << "val: " << val << " (min: " << min_ << " max: " << max_ << ")" << std::endl;
314  if (val < min_ || val > max_)
315  {
316  throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
317  }
318  if (val == max_)
319  {
320  return Size(bins_.size() - 1);
321  }
322  else
323  {
324  return (Size) floor((val - min_) / (max_ - min_) * bins_.size());
325  }
326  }
327 
328  };
329 
331  template <typename ValueType, typename BinSizeType>
332  std::ostream & operator<<(std::ostream & os, const Histogram<ValueType, BinSizeType> & hist)
333  {
334  for (Size i = 0; i < hist.size(); ++i)
335  {
336  os << hist.centerOfBin(i) << "\t"<< hist[i] << std::endl;
337  }
338  return os;
339  }
340 
341  } // namespace Math
342 
343 } // namespace OpenMS
344 
345 #endif // OPENMS_MATH_STATISTICS_HISTOGRAM_H
Histogram()
default constructor
Definition: Histogram.h:75
Out of range exception.
Definition: Exception.h:320
Histogram(const Histogram &histogram)
copy constructor
Definition: Histogram.h:83
BinSizeType maxBound() const
returns the upper bound
Definition: Histogram.h:133
Histogram & operator=(const Histogram &histogram)
Assignment.
Definition: Histogram.h:264
Size inc(BinSizeType val, ValueType increment=1)
increases the bin corresponding to value val by increment
Definition: Histogram.h:208
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:220
Int overflow exception.
Definition: Exception.h:255
ValueType maxValue() const
returns the highest value of all bins
Definition: Histogram.h:139
BinSizeType centerOfBin(Size bin_index) const
returns the center position of the bin with the index bin_index
Definition: Histogram.h:181
BinSizeType bin_size_
Bin size.
Definition: Histogram.h:303
bool operator!=(const Histogram &histogram) const
Inequality operator.
Definition: Histogram.h:258
Size size() const
returns the number of bins
Definition: Histogram.h:157
virtual ~Histogram()
destructor
Definition: Histogram.h:120
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
ValueType binValue(BinSizeType val) const
returns the value of bin corresponding to the value val
Definition: Histogram.h:196
std::vector< ValueType > bins_
Vector of bins.
Definition: Histogram.h:305
BinSizeType minBound() const
returns the lower bound
Definition: Histogram.h:127
ConstIterator end() const
Non-mutable iterator pointing after the last bin.
Definition: Histogram.h:285
void applyLogTransformation(BinSizeType multiplier)
Transforms the bin values with f(x)=multiplier*log(x+1)
Definition: Histogram.h:289
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:144
ValueType minValue() const
returns the lowest value of all bins
Definition: Histogram.h:145
Size valToBin_(BinSizeType val) const
Returns the bin a given value belongs to.
Definition: Histogram.h:311
bool operator==(const Histogram &histogram) const
Equality operator.
Definition: Histogram.h:249
BinSizeType min_
Lower bound.
Definition: Histogram.h:299
BinSizeType binSize() const
returns the bin size
Definition: Histogram.h:151
ConstIterator begin() const
Non-mutable iterator pointing to the first bin.
Definition: Histogram.h:282
BinSizeType max_
Upper bound.
Definition: Histogram.h:301
ValueType operator[](Size index) const
returns the value of bin index
Definition: Histogram.h:167

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:14 using doxygen 1.8.5