DGtal  1.0.0
Histogram.h
1
17 #pragma once
18
31 #if defined(Histogram_RECURSES)
32 #error Recursive header files inclusion detected in Histogram.h
33 #else // defined(Histogram_RECURSES)
34
35 #define Histogram_RECURSES
36
37 #if !defined Histogram_h
38
39 #define Histogram_h
40
42 // Inclusions
43 #include <iostream>
44 #include "DGtal/base/Common.h"
45 #include "DGtal/base/Clone.h"
46 #include "DGtal/math/CBinner.h"
47 #include "DGtal/kernel/CEuclideanRing.h"
48 #include "DGtal/math/Statistic.h"
50
51 namespace DGtal
52 {
63  template <typename TQuantity>
64  struct RegularBinner {
65  typedef TQuantity Quantity;
68
84  RegularBinner( const Quantity & min, const Quantity & max, Bin n )
85  : myMin( min ), myMax( max ), myNb( n )
86  {
87  ASSERT( max > min );
88  ASSERT( n > 0 );
89  myWidth = ( max - min ) / static_cast<Quantity>( n );
90  }
91
95  inline Bin size() const
96  {
97  return myNb;
98  }
99
105  inline Bin operator()( const Quantity & q ) const
106  {
107  if ( q <= myMin ) return NumberTraits<Bin>::ZERO;
108  else if ( q >= myMax ) return myNb - NumberTraits<Bin>::ONE;
109  else return static_cast<Bin>( floor( NumberTraits<Quantity>::castToDouble( ( q - myMin ) / myWidth ) ) );
110  }
111  };
112
114  // template class Histogram
143  template < typename TQuantity,
144  typename TBinner = RegularBinner< TQuantity > >
145  class Histogram
146  {
147  public:
150  BOOST_STATIC_ASSERT(( boost::is_same< TQuantity, typename TBinner::Quantity >::value ));
151
152  // ----------------------- public types ------------------------------
153  public:
154  typedef TQuantity Quantity;
155  typedef TBinner Binner;
157  typedef typename Binner::Bin Bin;
159  typedef std::vector<Size> Container;
160  typedef Container::const_iterator ConstIterator;
161
169  Scott };
170  // ----------------------- Standard services ------------------------------
171  public:
172
176  ~Histogram();
177
181  Histogram();
182
186  void clear();
187
191  void init( Clone<Binner> binner );
192
197  void init( Formula formula, const Statistic<Quantity> & stat );
198
203  void init( Bin nbBins, const Statistic<Quantity> & stat );
204
209  void addValue( Quantity q );
210
217  template <typename TInputIterator>
218  void addValues( TInputIterator it, TInputIterator itE );
219
223  void terminate();
224
229  Bin bin( Quantity q ) const;
230
234  Bin size() const;
235
240  Size area() const;
241
246  Size nb( Bin b ) const;
247
253  Size accumulation( Bin b ) const;
254
259  double pdf( Bin b ) const;
260
265  double cdf( Bin b ) const;
266
267  // ----------------------- Interface --------------------------------------
268  public:
269
274  void selfDisplay ( std::ostream & out ) const;
275
280  bool isValid() const;
281
282  // ------------------------- Protected Datas ------------------------------
283  protected:
285  const Binner* myBinner;
290
291  // ------------------------- Private Datas --------------------------------
292  private:
293
294  // ------------------------- Hidden services ------------------------------
295  private:
296
302  Histogram ( const Histogram & other );
303
310  Histogram & operator= ( const Histogram & other );
311
312  // ------------------------- Internals ------------------------------------
313  private:
317  void prepare( Bin size );
318
319
320  }; // end of class Histogram
321
322
329  template <typename TQuantity, typename TBinner>
330  std::ostream&
331  operator<< ( std::ostream & out, const Histogram<TQuantity, TBinner> & object );
332
333 } // namespace DGtal
334
335
337 // Includes inline functions.
338 #include "DGtal/math/Histogram.ih"
339
340 // //
342
343 #endif // !defined Histogram_h
344
345 #undef Histogram_RECURSES
346 #endif // else defined(Histogram_RECURSES)
const Binner * myBinner
The binner that places quantities into a bin.
Definition: Histogram.h:285
Container::const_iterator ConstIterator
Definition: Histogram.h:160
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
Bin size() const
Size area() const
Binner::Bin Bin
Definition: Histogram.h:157
Aim: Represents a typical histogram in statistics, which is a discrete estimate of the probability di...
Definition: Histogram.h:145
Histogram & operator=(const Histogram &other)
double cdf(Bin b) const
Size nb(Bin b) const
Aim: This class processes a set of sample values for one variable and can then compute different stat...
Definition: Statistic.h:69
Bin size() const
Definition: Histogram.h:95
void prepare(Bin size)
Aim: Defines the mathematical concept equivalent to a unitary commutative ring with a division operat...
DGtal::uint64_t Size
Definition: Histogram.h:158
Aim: This class encapsulates its parameter class to indicate that the given parameter is required to ...
Definition: Clone.h:266
TQuantity Quantity
Definition: Histogram.h:154
double pdf(Bin b) const
Histogram< Quantity, Binner > Self
Definition: Histogram.h:156
Bin bin(Quantity q) const
Bin operator()(const Quantity &q) const
Definition: Histogram.h:105
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
Aim: The traits class for all models of Cinteger.
Definition: NumberTraits.h:531
BOOST_CONCEPT_ASSERT((concepts::CEuclideanRing< TQuantity >))
RegularBinner(const Quantity &min, const Quantity &max, Bin n)
Definition: Histogram.h:84
DGtal is the top-level namespace which contains all DGtal functions and types.
TBinner Binner
Definition: Histogram.h:155
DGtal::uint32_t Bin
Definition: Histogram.h:67
Container myHistogram
The histogram data.
Definition: Histogram.h:287
BOOST_STATIC_ASSERT((boost::is_same< TQuantity, typename TBinner::Quantity >::value))
int max(int a, int b)
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
std::vector< Size > Container
Definition: Histogram.h:159
Container myCumulativeHistogram
The cumulative histogram data.
Definition: Histogram.h:289
Size accumulation(Bin b) const
void selfDisplay(std::ostream &out) const
TQuantity Quantity
Definition: Histogram.h:65
void init(Clone< Binner > binner)
Aim: Represents an object that places a quantity into a bin, i.e. a functor that associates a natural...
Definition: CBinner.h:97
Aim: Represents an elementary functor that partitions quantities into regular intervals,...
Definition: Histogram.h:64
bool isValid() const
RegularBinner< Quantity > Self
Definition: Histogram.h:66