DGtal 1.4.0
Loading...
Searching...
No Matches
Profile.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 Profile.ih
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
23 *
24 * @date 2015/11/08
25 *
26 * Implementation of inline methods defined in Profile.h
27 *
28 * This file is part of the DGtal library.
29 */
30
31///////////////////////////////////////////////////////////////////////////////
32// IMPLEMENTATION of inline methods.
33///////////////////////////////////////////////////////////////////////////////
34
35//////////////////////////////////////////////////////////////////////////////
36#include <cstdlib>
37#include "DGtal/math/SimpleLinearRegression.h"
38//////////////////////////////////////////////////////////////////////////////
39
40
41
42///////////////////////////////////////////////////////////////////////////////
43// Implementation of inline methods //
44
45template<typename TValueFunctor, typename TValue>
46DGtal::Profile<TValueFunctor, TValue>::~Profile()
47{
48 if ( myXsamples != 0 ) delete myXsamples;
49 if ( myStats != 0 ) delete myStats;
50}
51
52template<typename TValueFunctor, typename TValue>
53DGtal::Profile<TValueFunctor, TValue>::Profile()
54 : myXsamples( 0 ), myStats( 0 ), myProfileDef(MEAN), myStoreValInStats(false)
55{
56}
57
58
59template<typename TValueFunctor, typename TValue>
60DGtal::Profile<TValueFunctor, TValue>::Profile(ProfileType type)
61 : myXsamples( 0 ), myStats( 0 ), myProfileDef(type), myStoreValInStats(false)
62{
63}
64
65
66template<typename TValueFunctor, typename TValue>
67void
68DGtal::Profile<TValueFunctor, TValue>::clear()
69{
70 if ( myXsamples != 0 )
71 {
72 delete myXsamples;
73 myXsamples = 0;
74 }
75 if ( myStats != 0 )
76 {
77 delete myStats;
78 myStats = 0;
79 }
80
81}
82
83
84template<typename TValueFunctor, typename TValue>
85void
86DGtal::Profile<TValueFunctor, TValue>::init(const unsigned int nb, const bool storeValsInStats )
87{
88 clear();
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 )
93 {
94 (*myXsamples)[ i ] = (TValue) ( i + 1 );
95 myStats->push_back(Statistic<TValue>(storeValsInStats));
96 }
97}
98
99
100template<typename TValueFunctor, typename TValue>
101void
102DGtal::Profile<TValueFunctor, TValue>::addValue( const unsigned int indexX, const TValue value )
103{
104 ASSERT( isValid() && ( indexX < myXsamples->size() ) );
105 (*myStats)[ indexX ].addValue( value );
106}
107
108template<typename TValueFunctor, typename TValue>
109void
110DGtal::Profile<TValueFunctor, TValue>::addStatistic
111( const unsigned int indexX, const Statistic<TValue> & stat )
112{
113 ASSERT( isValid() && ( indexX < myXsamples->size() ) );
114 (*myStats)[ indexX ] += stat;
115}
116
117
118template<typename TValueFunctor, typename TValue>
119void
120DGtal::Profile<TValueFunctor, TValue>::getProfile
121( std::vector<TValue> & x, std::vector<TValue> & y ) const
122{
123 ASSERT( isValid() );
124 for ( unsigned int i = 0; i < myXsamples->size(); ++i)
125 {
126 x.push_back( myFunctor( (*myXsamples)[ i ] ) );
127 switch (myProfileDef){
128 case MAX:
129 y.push_back( myFunctor( (*myStats)[ i ].max() ) );
130 break;
131 case MIN:
132 y.push_back( myFunctor( (*myStats)[ i ].min() ) );
133 break;
134 case MEDIAN:
135 y.push_back (myFunctor( (*myStats)[ i ].median() ));
136 break;
137 case MEAN:
138 default:
139 y.push_back( myFunctor( (*myStats)[ i ].mean() ) );
140 break;
141
142 }
143 }
144}
145
146
147
148
149
150
151template<typename TValueFunctor, typename TValue>
152void
153DGtal::Profile<TValueFunctor, TValue>::stopStatsSaving()
154{
155 for ( unsigned int i = 0; i < myStats->size(); ++i )
156 {
157 ((*myStats).at( i )).terminate();
158 }
159}
160
161template<typename TValueFunctor, typename TValue>
162inline
163DGtal::Profile<TValueFunctor, TValue>::Profile( const Profile & other ){
164 if(other.myStats!=0 && other.myXsamples!=0)
165 {
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 )
171 {
172 (*myXsamples)[ i ] = (*other.myXsamples)[i];
173 (*myStats)[ i ] = (*other.myStats)[i];
174 }
175 }
176 else
177 {
178 myStats=0;
179 myXsamples=0;
180 myProfileDef = other.myProfileDef;
181 myStoreValInStats = false;
182 }
183}
184
185template<typename TValueFunctor, typename TValue>
186inline
187DGtal::Profile<TValueFunctor, TValue> &
188DGtal::Profile<TValueFunctor, TValue>::operator=
189( const DGtal::Profile<TValueFunctor, TValue> & other )
190{
191 if ( this != &other )
192 {
193 if(other.myStats!=0 && other.myXsamples!=0)
194 {
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 )
200 {
201 (*myXsamples)[ i ] = (*other.myXsamples)[i];
202 (*myStats)[ i ] = (*other.myStats)[i];
203 }
204 }
205 else
206 {
207 myStats=0;
208 myXsamples=0;
209 myProfileDef = other.myProfileDef;
210 myStoreValInStats=false;
211 }
212 }
213
214 return *this;
215}
216
217
218
219template<typename TValueFunctor, typename TValue>
220template <typename Iterator>
221inline
222void
223DGtal::Profile<TValueFunctor, TValue>::init( Iterator beginXvalues, Iterator endXvalues,
224 const bool storeValsInStats )
225{
226 clear();
227 myStoreValInStats= storeValsInStats;
228 myXsamples = new std::vector<TValue>;
229 myStats = new std::vector< Statistic<TValue> >;
230
231 unsigned int nb = 0;
232 for ( ; beginXvalues != endXvalues; ++ beginXvalues, ++nb )
233 {
234 myXsamples->push_back( * beginXvalues );
235 myStats->push_back(Statistic<TValue>( myStoreValInStats));
236 }
237}
238
239
240template<typename TValueFunctor, typename TValue>
241void
242DGtal::Profile<TValueFunctor, TValue>::selfDisplay( std::ostream& out ) const
243{
244 out << "[Profile]";
245}
246
247template<typename TValueFunctor, typename TValue>
248bool
249DGtal::Profile<TValueFunctor, TValue>::isValid() const
250{
251 return myXsamples != 0;
252}
253
254
255
256///////////////////////////////////////////////////////////////////////////////
257// Implementation of inline functions and external operators //
258
259/**
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.
264 */
265template<typename TValueFunctor, typename TValue>
266inline
267std::ostream&
268DGtal::operator<< ( std::ostream & out,
269 const Profile<TValueFunctor, TValue> & object )
270{
271 object.selfDisplay ( out );
272 return out;
273}
274
275
276template<typename TValueFunctor, typename TValue>
277inline
278void
279DGtal::Profile<TValueFunctor, TValue>::setType(ProfileType type){
280 ASSERT(myProfileDef==MEAN || myProfileDef==MAX || myProfileDef==MIN || myProfileDef==MEDIAN);
281 myProfileDef = type;
282}
283
284
285// //
286///////////////////////////////////////////////////////////////////////////////
287
288