/usr/include/OpenMS/MATH/STATISTICS/Histogram.h is in libopenms-dev 1.11.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | // --------------------------------------------------------------------------
// OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
//
// This software is released under a three-clause BSD license:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of any author or any participating institution
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// --------------------------------------------------------------------------
// $Maintainer: Stephan Aiche$
// $Authors: Marc Sturm $
// --------------------------------------------------------------------------
#ifndef OPENMS_MATH_STATISTICS_HISTOGRAM_H
#define OPENMS_MATH_STATISTICS_HISTOGRAM_H
//OpenMS
#include <OpenMS/CONCEPT/Types.h>
#include <OpenMS/CONCEPT/Exception.h>
//STL
#include <vector>
#include <cmath>
#include <limits>
#include <iostream>
#include <algorithm>
namespace OpenMS
{
namespace Math
{
/**
@brief Representation of a histogram
The first template argument gives the Type of the
values that are stored in the bins. The second argument
gives the type for the bin size and range.
@ingroup Math
*/
template <typename ValueType = UInt, typename BinSizeType = DoubleReal>
class Histogram
{
public:
/// Non-mutable iterator of the bins
typedef typename std::vector<ValueType>::const_iterator ConstIterator;
/** @name Constructors and Destructors
*/
//@{
///default constructor
Histogram() :
min_(0),
max_(0),
bin_size_(0)
{
}
///copy constructor
Histogram(const Histogram & histogram) :
min_(histogram.min_),
max_(histogram.max_),
bin_size_(histogram.bin_size_),
bins_(histogram.bins_)
{
}
/**
@brief constructor with min, max and bin size
@exception Exception::OutOfRange is thrown if @p bin_size negative or zero
*/
Histogram(BinSizeType min, BinSizeType max, BinSizeType bin_size) :
min_(min),
max_(max),
bin_size_(bin_size)
{
if (bin_size_ <= 0)
{
throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
else
{
// if max_ == min_ there is only one bin
if (max_ != min_)
{
bins_ = std::vector<ValueType>(Size(ceil((max_ - min_) / bin_size_)), 0);
}
else
{
bins_ = std::vector<ValueType>(1, 0);
}
}
}
///destructor
virtual ~Histogram()
{
}
//@}
///returns the lower bound
BinSizeType minBound() const
{
return min_;
}
///returns the upper bound
BinSizeType maxBound() const
{
return max_;
}
///returns the highest value of all bins
ValueType maxValue() const
{
return *(std::max_element(bins_.begin(), bins_.end()));
}
///returns the lowest value of all bins
ValueType minValue() const
{
return *(std::min_element(bins_.begin(), bins_.end()));
}
///returns the bin size
BinSizeType binSize() const
{
return bin_size_;
}
///returns the number of bins
Size size() const
{
return bins_.size();
}
/**
@brief returns the value of bin @p index
@exception Exception::IndexOverflow is thrown for invalid indices
*/
ValueType operator[](Size index) const
{
if (index >= bins_.size())
{
throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
return bins_[index];
}
/**
@brief returns the center position of the bin with the index @p bin_index
@exception Exception::IndexOverflow is thrown for invalid indices
*/
BinSizeType centerOfBin(Size bin_index) const
{
if (bin_index >= bins_.size())
{
throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
return (BinSizeType)(min_ + ((BinSizeType)bin_index + 0.5) * bin_size_);
}
/**
@brief returns the value of bin corresponding to the value @p val
@exception Exception::OutOfRange is thrown if the value is out of valid range
*/
ValueType binValue(BinSizeType val) const
{
return bins_[valToBin_(val)];
}
/**
@brief increases the bin corresponding to value @p val by @p increment
@return The index of the increased bin.
@exception Exception::OutOfRange is thrown if the value is out of valid range
*/
Size inc(BinSizeType val, ValueType increment = 1)
{
Size bin_index = valToBin_(val);
bins_[bin_index] += increment;
return bin_index;
}
/**
@brief resets the histogram with the given range and bin size
@exception Exception::OutOfRange is thrown if @p bin_size negative or zero
*/
void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
{
min_ = min;
max_ = max;
bin_size_ = bin_size;
bins_.clear();
if (bin_size_ <= 0)
{
throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
else
{
// if max_ == min_ there is only one bin
if (max_ != min_)
{
bins_.resize(Size(ceil((max_ - min_) / bin_size_)), 0);
}
else
{
bins_.resize(1, 0);
}
}
}
/** @name Assignment and equality operators
*/
//@{
///Equality operator
bool operator==(const Histogram & histogram) const
{
return min_ == histogram.min_ &&
max_ == histogram.max_ &&
bin_size_ == histogram.bin_size_ &&
bins_ == histogram.bins_;
}
///Inequality operator
bool operator!=(const Histogram & histogram) const
{
return !operator==(histogram);
}
///Assignment
Histogram & operator=(const Histogram & histogram)
{
if (&histogram == this) return *this;
min_ = histogram.min_;
max_ = histogram.max_;
bin_size_ = histogram.bin_size_;
bins_ = histogram.bins_;
return *this;
}
//@}
/** @name Iterators
*/
//@{
/// Non-mutable iterator pointing to the first bin
inline ConstIterator begin() const { return bins_.begin(); }
/// Non-mutable iterator pointing after the last bin
inline ConstIterator end() const { return bins_.end(); }
//@}
/// Transforms the bin values with f(x)=multiplier*log(x+1)
void applyLogTransformation(BinSizeType multiplier)
{
for (typename std::vector<ValueType>::iterator it = bins_.begin(); it != bins_.end(); ++it)
{
*it = (ValueType)(multiplier * log((BinSizeType)(*it + 1.0f)));
}
}
protected:
/// Lower bound
BinSizeType min_;
/// Upper bound
BinSizeType max_;
/// Bin size
BinSizeType bin_size_;
/// Vector of bins
std::vector<ValueType> bins_;
/**
@brief Returns the bin a given value belongs to
@exception Exception::OutOfRange is thrown if the value is out of valid range
*/
Size valToBin_(BinSizeType val) const
{
//std::cout << "val: " << val << " (min: " << min_ << " max: " << max_ << ")" << std::endl;
if (val < min_ || val > max_)
{
throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
if (val == max_)
{
return Size(bins_.size() - 1);
}
else
{
return (Size) floor((val - min_) / (max_ - min_) * bins_.size());
}
}
};
///Print the contents to a stream.
template <typename ValueType, typename BinSizeType>
std::ostream & operator<<(std::ostream & os, const Histogram<ValueType, BinSizeType> & hist)
{
for (Size i = 0; i < hist.size(); ++i)
{
os << hist.centerOfBin(i) << "\t"<< hist[i] << std::endl;
}
return os;
}
} // namespace Math
} // namespace OpenMS
#endif // OPENMS_MATH_STATISTICS_HISTOGRAM_H
|