DGtal  1.5.beta
Histogram.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file Histogram.ih
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
21  *
22  * @date 2013/10/25
23  *
24  * Implementation of inline methods defined in Histogram.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 //-----------------------------------------------------------------------------
42 template <typename TQuantity, typename TBinner>
43 inline
44 DGtal::Histogram<TQuantity, TBinner>::~Histogram()
45 {
46  if ( myBinner != 0 ) delete myBinner;
47 }
48 //-----------------------------------------------------------------------------
49 template <typename TQuantity, typename TBinner>
50 inline
51 DGtal::Histogram<TQuantity, TBinner>::Histogram()
52  : myBinner( 0 )
53 {
54 }
55 //-----------------------------------------------------------------------------
56 template <typename TQuantity, typename TBinner>
57 inline
58 void
59 DGtal::Histogram<TQuantity, TBinner>::clear()
60 {
61  if ( myBinner != 0 )
62  {
63  delete myBinner;
64  myBinner = 0;
65  }
66  myHistogram.clear();
67  myCumulativeHistogram.clear();
68 }
69 
70 //-----------------------------------------------------------------------------
71 template <typename TQuantity, typename TBinner>
72 inline
73 void
74 DGtal::Histogram<TQuantity, TBinner>::init( Clone<Binner> binner )
75 {
76  clear();
77  myBinner = new Binner( binner );
78  prepare( myBinner->size() );
79 }
80 
81 //-----------------------------------------------------------------------------
82 template <typename TQuantity, typename TBinner>
83 void
84 DGtal::Histogram<TQuantity, TBinner>::init( Formula formula, const Statistic<Quantity> & stat )
85 {
86  clear();
87  switch( formula ) {
88  case SquareRoot: /**< Rule is k=sqrt(n) */
89  myBinner = new Binner( stat.min(), stat.max(),
90  static_cast<Bin>( ceil( sqrt( stat.samples() ) ) ) );
91  break;
92  case Sturges: /**< Rule is k=ceil(log_2(n)+1) */
93  myBinner = new Binner( stat.min(), stat.max(),
94  static_cast<Bin>( ceil( log2( stat.samples() ) + 1 ) ) );
95  break;
96  case Rice: /**< Rule is k=ceil(n^(1/3)) */
97  myBinner = new Binner( stat.min(), stat.max(),
98  static_cast<Bin>( ceil( pow( stat.samples(), 1.0/3.0 ) ) ) );
99  break;
100  case Scott: /**< Rule is h=3.5s/(n^(1/3)) */
101  double h = 3.5 * stat.unbiasedVariance() / ceil( pow( stat.samples(), 1.0/3.0 ) );
102  myBinner = new Binner( stat.min(), stat.max(),
103  static_cast<Bin>( ceil( ( stat.max() - stat.min() ) / h ) ) );
104  break;
105  }
106  if ( myBinner != 0 ) prepare( myBinner->size() );
107 }
108 //-----------------------------------------------------------------------------
109 template <typename TQuantity, typename TBinner>
110 void
111 DGtal::Histogram<TQuantity, TBinner>::init( Bin nbBins, const Statistic<Quantity> & stat )
112 {
113  clear();
114  myBinner = new Binner( stat.min(), stat.max(), nbBins );
115  prepare( myBinner->size() );
116 }
117 //-----------------------------------------------------------------------------
118 template <typename TQuantity, typename TBinner>
119 inline
120 typename DGtal::Histogram<TQuantity, TBinner>::Bin
121 DGtal::Histogram<TQuantity, TBinner>::bin( Quantity q ) const
122 {
123  ASSERT( isValid() );
124  return (*myBinner)( q );
125 }
126 //-----------------------------------------------------------------------------
127 template <typename TQuantity, typename TBinner>
128 inline
129 void
130 DGtal::Histogram<TQuantity, TBinner>::addValue( Quantity q )
131 {
132  ++myHistogram[ bin( q ) ];
133 }
134 //-----------------------------------------------------------------------------
135 template <typename TQuantity, typename TBinner>
136 template <typename TInputIterator>
137 inline
138 void
139 DGtal::Histogram<TQuantity, TBinner>::addValues( TInputIterator it, TInputIterator itE )
140 {
141  BOOST_CONCEPT_ASSERT(( boost::InputIterator< ConstIterator > ));
142  for ( ; it != itE; ++it )
143  addValue( *it );
144 }
145 //-----------------------------------------------------------------------------
146 template <typename TQuantity, typename TBinner>
147 inline
148 typename DGtal::Histogram<TQuantity, TBinner>::Bin
149 DGtal::Histogram<TQuantity, TBinner>::size() const
150 {
151  return static_cast<Bin>( myHistogram.size() );
152 }
153 //-----------------------------------------------------------------------------
154 template <typename TQuantity, typename TBinner>
155 inline
156 typename DGtal::Histogram<TQuantity, TBinner>::Size
157 DGtal::Histogram<TQuantity, TBinner>::area() const
158 {
159  return myCumulativeHistogram.back();
160 }
161 //-----------------------------------------------------------------------------
162 template <typename TQuantity, typename TBinner>
163 inline
164 typename DGtal::Histogram<TQuantity, TBinner>::Size
165 DGtal::Histogram<TQuantity, TBinner>::nb( Bin b ) const
166 {
167  ASSERT( b < size() );
168  return myHistogram[ b ];
169 }
170 //-----------------------------------------------------------------------------
171 template <typename TQuantity, typename TBinner>
172 inline
173 typename DGtal::Histogram<TQuantity, TBinner>::Size
174 DGtal::Histogram<TQuantity, TBinner>::accumulation( Bin b ) const
175 {
176  ASSERT( b < size() );
177  return myCumulativeHistogram[ b ];
178 }
179 //-----------------------------------------------------------------------------
180 template <typename TQuantity, typename TBinner>
181 inline
182 double
183 DGtal::Histogram<TQuantity, TBinner>::pdf( Bin b ) const
184 {
185  return NumberTraits<Bin>::castToDouble( nb( b ) )
186  / NumberTraits<Size>::castToDouble( area() );
187 }
188 //-----------------------------------------------------------------------------
189 template <typename TQuantity, typename TBinner>
190 inline
191 double
192 DGtal::Histogram<TQuantity, TBinner>::cdf( Bin b ) const
193 {
194  return NumberTraits<Bin>::castToDouble( accumulation( b ) )
195  / NumberTraits<Size>::castToDouble( area() );
196 }
197 //-----------------------------------------------------------------------------
198 template <typename TQuantity, typename TBinner>
199 inline
200 void
201 DGtal::Histogram<TQuantity, TBinner>::terminate()
202 {
203  ConstIterator srcIt = myHistogram.begin();
204  ConstIterator srcItE = myHistogram.end();
205  Bin b = 0;
206  myCumulativeHistogram[ b ] = *srcIt++;
207  for ( ; srcIt != srcItE; ++srcIt, ++b )
208  {
209  myCumulativeHistogram[ b+1 ] = myCumulativeHistogram[ b ] + *srcIt;
210  }
211 }
212 
213 
214 ///////////////////////////////////////////////////////////////////////////////
215 // Interface - public :
216 
217 /**
218  * Writes/Displays the object on an output stream.
219  * @param out the output stream where the object is written.
220  */
221 template <typename TQuantity, typename TBinner>
222 inline
223 void
224 DGtal::Histogram<TQuantity, TBinner>::selfDisplay ( std::ostream & out ) const
225 {
226  out << "[Histogram size=" << size() << " area=" << area() << "]";
227 }
228 
229 /**
230  * Checks the validity/consistency of the object.
231  * @return 'true' if the object is valid, 'false' otherwise.
232  */
233 template <typename TQuantity, typename TBinner>
234 inline
235 bool
236 DGtal::Histogram<TQuantity, TBinner>::isValid() const
237 {
238  return myBinner != 0;
239 }
240 
241 //-----------------------------------------------------------------------------
242 template <typename TQuantity, typename TBinner>
243 inline
244 void
245 DGtal::Histogram<TQuantity, TBinner>::prepare( Bin aSize )
246 {
247  boost::ignore_unused_variable_warning( aSize );
248  myHistogram.resize( myBinner->size(), 0 );
249  myCumulativeHistogram.resize( myBinner->size(), 0 );
250 }
251 
252 
253 ///////////////////////////////////////////////////////////////////////////////
254 // Implementation of inline functions //
255 
256 template <typename TQuantity, typename TBinner>
257 inline
258 std::ostream&
259 DGtal::operator<< ( std::ostream & out,
260  const Histogram<TQuantity, TBinner> & object )
261 {
262  object.selfDisplay( out );
263  return out;
264 }
265 
266 // //
267 ///////////////////////////////////////////////////////////////////////////////
268 
269