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.
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.
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/>.
19 * @author Bertrand Kerautret (\c kerautre@loria.fr )
20 * LORIA (CNRS, UMR 7503), University of Nancy, France
21 * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
22 * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
26 * Implementation of inline methods defined in Profile.h
28 * This file is part of the DGtal library.
31///////////////////////////////////////////////////////////////////////////////
32// IMPLEMENTATION of inline methods.
33///////////////////////////////////////////////////////////////////////////////
35//////////////////////////////////////////////////////////////////////////////
37#include "DGtal/math/SimpleLinearRegression.h"
38//////////////////////////////////////////////////////////////////////////////
42///////////////////////////////////////////////////////////////////////////////
43// Implementation of inline methods //
45template<typename TValueFunctor, typename TValue>
46DGtal::Profile<TValueFunctor, TValue>::~Profile()
48 if ( myXsamples != 0 ) delete myXsamples;
49 if ( myStats != 0 ) delete myStats;
52template<typename TValueFunctor, typename TValue>
53DGtal::Profile<TValueFunctor, TValue>::Profile()
54 : myXsamples( 0 ), myStats( 0 ), myProfileDef(MEAN), myStoreValInStats(false)
59template<typename TValueFunctor, typename TValue>
60DGtal::Profile<TValueFunctor, TValue>::Profile(ProfileType type)
61 : myXsamples( 0 ), myStats( 0 ), myProfileDef(type), myStoreValInStats(false)
66template<typename TValueFunctor, typename TValue>
68DGtal::Profile<TValueFunctor, TValue>::clear()
70 if ( myXsamples != 0 )
84template<typename TValueFunctor, typename TValue>
86DGtal::Profile<TValueFunctor, TValue>::init(const unsigned int nb, const bool storeValsInStats )
89 myXsamples = new std::vector<TValue>( nb );
90 myStats = new std::vector< Statistic<TValue> >;
91 myStoreValInStats= storeValsInStats;
92 for ( unsigned int i = 0; i < nb; ++i )
94 (*myXsamples)[ i ] = (TValue) ( i + 1 );
95 myStats->push_back(Statistic<TValue>(storeValsInStats));
100template<typename TValueFunctor, typename TValue>
102DGtal::Profile<TValueFunctor, TValue>::addValue( const unsigned int indexX, const TValue value )
104 ASSERT( isValid() && ( indexX < myXsamples->size() ) );
105 (*myStats)[ indexX ].addValue( value );
108template<typename TValueFunctor, typename TValue>
110DGtal::Profile<TValueFunctor, TValue>::addStatistic
111( const unsigned int indexX, const Statistic<TValue> & stat )
113 ASSERT( isValid() && ( indexX < myXsamples->size() ) );
114 (*myStats)[ indexX ] += stat;
118template<typename TValueFunctor, typename TValue>
120DGtal::Profile<TValueFunctor, TValue>::getProfile
121( std::vector<TValue> & x, std::vector<TValue> & y ) const
124 for ( unsigned int i = 0; i < myXsamples->size(); ++i)
126 x.push_back( myFunctor( (*myXsamples)[ i ] ) );
127 switch (myProfileDef){
129 y.push_back( myFunctor( (*myStats)[ i ].max() ) );
132 y.push_back( myFunctor( (*myStats)[ i ].min() ) );
135 y.push_back (myFunctor( (*myStats)[ i ].median() ));
139 y.push_back( myFunctor( (*myStats)[ i ].mean() ) );
151template<typename TValueFunctor, typename TValue>
153DGtal::Profile<TValueFunctor, TValue>::stopStatsSaving()
155 for ( unsigned int i = 0; i < myStats->size(); ++i )
157 ((*myStats).at( i )).terminate();
161template<typename TValueFunctor, typename TValue>
163DGtal::Profile<TValueFunctor, TValue>::Profile( const Profile & other ){
164 if(other.myStats!=0 && other.myXsamples!=0)
166 myXsamples = new std::vector<TValue>( other.myXsamples->size() );
167 myStats = new std::vector< Statistic<TValue> > ( other.myXsamples->size());
168 myProfileDef = other.myProfileDef;
169 myStoreValInStats= other.myStoreValInStats;
170 for ( unsigned int i = 0; i < other.myXsamples->size(); ++i )
172 (*myXsamples)[ i ] = (*other.myXsamples)[i];
173 (*myStats)[ i ] = (*other.myStats)[i];
180 myProfileDef = other.myProfileDef;
181 myStoreValInStats = false;
185template<typename TValueFunctor, typename TValue>
187DGtal::Profile<TValueFunctor, TValue> &
188DGtal::Profile<TValueFunctor, TValue>::operator=
189( const DGtal::Profile<TValueFunctor, TValue> & other )
191 if ( this != &other )
193 if(other.myStats!=0 && other.myXsamples!=0)
195 myXsamples = new std::vector<TValue>( other.myXsamples->size() );
196 myStats = new std::vector< Statistic<TValue> > ( other.myXsamples->size());
197 myProfileDef = other.myProfileDef;
198 myStoreValInStats= other.myStoreValInStats;
199 for ( unsigned int i = 0; i < other.myXsamples->size(); ++i )
201 (*myXsamples)[ i ] = (*other.myXsamples)[i];
202 (*myStats)[ i ] = (*other.myStats)[i];
209 myProfileDef = other.myProfileDef;
210 myStoreValInStats=false;
219template<typename TValueFunctor, typename TValue>
220template <typename Iterator>
223DGtal::Profile<TValueFunctor, TValue>::init( Iterator beginXvalues, Iterator endXvalues,
224 const bool storeValsInStats )
227 myStoreValInStats= storeValsInStats;
228 myXsamples = new std::vector<TValue>;
229 myStats = new std::vector< Statistic<TValue> >;
232 for ( ; beginXvalues != endXvalues; ++ beginXvalues, ++nb )
234 myXsamples->push_back( * beginXvalues );
235 myStats->push_back(Statistic<TValue>( myStoreValInStats));
240template<typename TValueFunctor, typename TValue>
242DGtal::Profile<TValueFunctor, TValue>::selfDisplay( std::ostream& out ) const
247template<typename TValueFunctor, typename TValue>
249DGtal::Profile<TValueFunctor, TValue>::isValid() const
251 return myXsamples != 0;
256///////////////////////////////////////////////////////////////////////////////
257// Implementation of inline functions and external operators //
260 * Overloads 'operator<<' for displaying objects of class 'Profile'.
261 * @param out the output stream where the object is written.
262 * @param object the object of class 'Profile' to write.
263 * @return the output stream after the writing.
265template<typename TValueFunctor, typename TValue>
268DGtal::operator<< ( std::ostream & out,
269 const Profile<TValueFunctor, TValue> & object )
271 object.selfDisplay ( out );
276template<typename TValueFunctor, typename TValue>
279DGtal::Profile<TValueFunctor, TValue>::setType(ProfileType type){
280 ASSERT(myProfileDef==MEAN || myProfileDef==MAX || myProfileDef==MIN || myProfileDef==MEDIAN);
286///////////////////////////////////////////////////////////////////////////////