DGtal 1.4.0
Loading...
Searching...
No Matches
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//-----------------------------------------------------------------------------
42template <typename TQuantity, typename TBinner>
43inline
44DGtal::Histogram<TQuantity, TBinner>::~Histogram()
45{
46 if ( myBinner != 0 ) delete myBinner;
47}
48//-----------------------------------------------------------------------------
49template <typename TQuantity, typename TBinner>
50inline
51DGtal::Histogram<TQuantity, TBinner>::Histogram()
52 : myBinner( 0 )
53{
54}
55//-----------------------------------------------------------------------------
56template <typename TQuantity, typename TBinner>
57inline
58void
59DGtal::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//-----------------------------------------------------------------------------
71template <typename TQuantity, typename TBinner>
72inline
73void
74DGtal::Histogram<TQuantity, TBinner>::init( Clone<Binner> binner )
75{
76 clear();
77 myBinner = new Binner( binner );
78 prepare( myBinner->size() );
79}
80
81//-----------------------------------------------------------------------------
82template <typename TQuantity, typename TBinner>
83void
84DGtal::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//-----------------------------------------------------------------------------
109template <typename TQuantity, typename TBinner>
110void
111DGtal::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//-----------------------------------------------------------------------------
118template <typename TQuantity, typename TBinner>
119inline
120typename DGtal::Histogram<TQuantity, TBinner>::Bin
121DGtal::Histogram<TQuantity, TBinner>::bin( Quantity q ) const
122{
123 ASSERT( isValid() );
124 return (*myBinner)( q );
125}
126//-----------------------------------------------------------------------------
127template <typename TQuantity, typename TBinner>
128inline
129void
130DGtal::Histogram<TQuantity, TBinner>::addValue( Quantity q )
131{
132 ++myHistogram[ bin( q ) ];
133}
134//-----------------------------------------------------------------------------
135template <typename TQuantity, typename TBinner>
136template <typename TInputIterator>
137inline
138void
139DGtal::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//-----------------------------------------------------------------------------
146template <typename TQuantity, typename TBinner>
147inline
148typename DGtal::Histogram<TQuantity, TBinner>::Bin
149DGtal::Histogram<TQuantity, TBinner>::size() const
150{
151 return static_cast<Bin>( myHistogram.size() );
152}
153//-----------------------------------------------------------------------------
154template <typename TQuantity, typename TBinner>
155inline
156typename DGtal::Histogram<TQuantity, TBinner>::Size
157DGtal::Histogram<TQuantity, TBinner>::area() const
158{
159 return myCumulativeHistogram.back();
160}
161//-----------------------------------------------------------------------------
162template <typename TQuantity, typename TBinner>
163inline
164typename DGtal::Histogram<TQuantity, TBinner>::Size
165DGtal::Histogram<TQuantity, TBinner>::nb( Bin b ) const
166{
167 ASSERT( b < size() );
168 return myHistogram[ b ];
169}
170//-----------------------------------------------------------------------------
171template <typename TQuantity, typename TBinner>
172inline
173typename DGtal::Histogram<TQuantity, TBinner>::Size
174DGtal::Histogram<TQuantity, TBinner>::accumulation( Bin b ) const
175{
176 ASSERT( b < size() );
177 return myCumulativeHistogram[ b ];
178}
179//-----------------------------------------------------------------------------
180template <typename TQuantity, typename TBinner>
181inline
182double
183DGtal::Histogram<TQuantity, TBinner>::pdf( Bin b ) const
184{
185 return NumberTraits<Bin>::castToDouble( nb( b ) )
186 / NumberTraits<Size>::castToDouble( area() );
187}
188//-----------------------------------------------------------------------------
189template <typename TQuantity, typename TBinner>
190inline
191double
192DGtal::Histogram<TQuantity, TBinner>::cdf( Bin b ) const
193{
194 return NumberTraits<Bin>::castToDouble( accumulation( b ) )
195 / NumberTraits<Size>::castToDouble( area() );
196}
197//-----------------------------------------------------------------------------
198template <typename TQuantity, typename TBinner>
199inline
200void
201DGtal::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 */
221template <typename TQuantity, typename TBinner>
222inline
223void
224DGtal::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 */
233template <typename TQuantity, typename TBinner>
234inline
235bool
236DGtal::Histogram<TQuantity, TBinner>::isValid() const
237{
238 return myBinner != 0;
239}
240
241//-----------------------------------------------------------------------------
242template <typename TQuantity, typename TBinner>
243inline
244void
245DGtal::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
256template <typename TQuantity, typename TBinner>
257inline
258std::ostream&
259DGtal::operator<< ( std::ostream & out,
260 const Histogram<TQuantity, TBinner> & object )
261{
262 object.selfDisplay( out );
263 return out;
264}
265
266// //
267///////////////////////////////////////////////////////////////////////////////
268
269