Blame view

src/ExternLib/StatisticProcesses/MinMaxMeanStatistic.hh 37.2 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * MinMaxMeanStatistic.hh
 *
 *  Created on: 04 nov. 2014
 *      Author: AKKA
 */

#ifndef MINMAXMEANSTATISTIC_HH_
#define MINMAXMEANSTATISTIC_HH_

#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "VisitorOfParamData.hh"
#include "StatisticData.hh"
#include "StatisticOperation.hh"
#include "StatisticProcess.hh"
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
17
#include <math.h>
2ed0b752   Hacene SI HADJ MOHAND   first last ok
18
#include <type_traits>
fbe3c2bb   Benjamin Renard   First commit
19
20
21
22

#include "TimeInterval.hh"

namespace AMDA {
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
23
24
25
26
27
28
29
30
31
    namespace Statistic {
        namespace MinMaxMean {

            using namespace AMDA::Parameters;

            typedef enum {
                FT_MIN,
                FT_MAX,
                FT_MEAN,
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
32
                FT_RMS,
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
33
                FT_MEDIAN,
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
34
                FT_VARIANCE,
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
35
36
                FT_SKEWNESS,
                FT_KURTOSIS,
fdea19dd   Hacene SI HADJ MOHAND   adding prcess
37
38
39
                FT_FIRST_VALUE,
                FT_MIDDLE_VALUE,
                FT_LAST_VALUE
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
40
41
42
43
44
45
46
47
48
49
            } FUNC_TYPE;

            template<typename TParamData, typename TResultData>
            class MinMaxMeanStatisticOperation : public StatisticOperation<TResultData> {
            public:
                /**
                 * @brief  Element type of paramData
                 */
                typedef typename TParamData::ElementType ElementType;

2f07adf4   Hacene SI HADJ MOHAND   us ok
50
51

                using TimeDataList = std::vector<std::pair<double, ElementType>>;
2ed0b752   Hacene SI HADJ MOHAND   first last ok
52

65af22fe   Hacene SI HADJ MOHAND   ok for median var...
53
54
55
56
57
58
                MinMaxMeanStatisticOperation(StatisticProcess& process,
                        TimeIntervalListSPtr pTimeIntervalList, TParamData &param, FUNC_TYPE funcType) :
                StatisticOperation<TResultData>(process),
                _paramInput(param), _timeIntervalList(pTimeIntervalList),
                _currentTimeInterval(_timeIntervalList->begin()),
                _funcType(funcType), _dimDef("unknown") {
233c5960   Benjamin Renard   Fix catalog size ...
59
60
61
                    ElementType fillValue;
                    fillValue << NotANumber();
                    initDimSize(fillValue);
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
62
63
64
65
66
67
68
69
70
71
72
73
                    resetData(StatisticOperation<TResultData>::_resultData);
                }

                virtual ~MinMaxMeanStatisticOperation(void) {
                }

                virtual void compute(ParamDataIndexInfo &pParamDataIndexInfo) {
                    for (unsigned int index = pParamDataIndexInfo._startIndex;
                            index < pParamDataIndexInfo._startIndex
                            + pParamDataIndexInfo._nbDataToProcess;
                            index++) {
                        _val = _paramInput.get(index);
2ed0b752   Hacene SI HADJ MOHAND   first last ok
74
                        double crtTime = _paramInput.getTime(index);
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
75
76
77
78
79
80
81
82
83
84
                        switch (_funcType) {
                            case FT_MIN:
                                computeMin(_val);
                                break;
                            case FT_MAX:
                                computeMax(_val);
                                break;
                            case FT_MEAN:
                                addForMean(_val);
                                break;
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
85
86
87
                            case FT_RMS:
                                addForRMS(_val);
                                break;
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
88
                            case FT_MEDIAN:
8b82976b   Hacene SI HADJ MOHAND   correcting stats
89
                                addForMean(_val);
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
90
91
92
                                generateVector(_val);
                                break;
                            case FT_VARIANCE:
4b695af7   Hacene SI HADJ MOHAND   tested ok
93
                                addForMean(_val);
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
94
95
                                generateVector(_val);
                                break;
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
96
                            case FT_SKEWNESS:
4b695af7   Hacene SI HADJ MOHAND   tested ok
97
                                addForMean(_val);
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
98
99
100
                                generateVector(_val);
                                break;
                            case FT_KURTOSIS:
4b695af7   Hacene SI HADJ MOHAND   tested ok
101
                                addForMean(_val);
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
102
103
                                generateVector(_val);
                                break;
fdea19dd   Hacene SI HADJ MOHAND   adding prcess
104
                            case FT_MIDDLE_VALUE:
2ed0b752   Hacene SI HADJ MOHAND   first last ok
105
106
107
                                addForMean(_val);
                                generateTimeDataVector(crtTime, _val);
                            case FT_FIRST_VALUE:
fdea19dd   Hacene SI HADJ MOHAND   adding prcess
108
                            case FT_LAST_VALUE:
2ed0b752   Hacene SI HADJ MOHAND   first last ok
109
                                addForMean(_val);
fdea19dd   Hacene SI HADJ MOHAND   adding prcess
110
111
                                generateVector(_val);
                                break;
2ed0b752   Hacene SI HADJ MOHAND   first last ok
112

65af22fe   Hacene SI HADJ MOHAND   ok for median var...
113
114
115
116
117
                        }
                    }
                }

                virtual void finalizeCompute(void) {
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
118
                    if (_funcType == FT_MEAN)
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
119
                        finalizeMeanResult(StatisticOperation<TResultData>::_resultData);
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
120
121
                    if (_funcType == FT_RMS)
                        finalizeRMSResult(StatisticOperation<TResultData>::_resultData);
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
122
123
                    if (_funcType == FT_MEDIAN)
                        finalizeMedianResult(StatisticOperation<TResultData>::_resultData);
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
124
                    if (_funcType == FT_VARIANCE) {
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
125
                        finalizeMeanResult(StatisticOperation<TResultData>::_resultData);
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
126
127
                        finalizeVarianceResult(StatisticOperation<TResultData>::_resultData);
                    } else if (_funcType == FT_SKEWNESS) {
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
128
                        finalizeMeanResult(StatisticOperation<TResultData>::_resultData);
4b695af7   Hacene SI HADJ MOHAND   tested ok
129
                        finalizeVarianceResult(StatisticOperation<TResultData>::_resultData);
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
130
                        finalizeSkewnessResult(StatisticOperation<TResultData>::_resultData);
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
131
                    } else if (_funcType == FT_KURTOSIS) {
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
132
                        finalizeMeanResult(StatisticOperation<TResultData>::_resultData);
4b695af7   Hacene SI HADJ MOHAND   tested ok
133
                        finalizeVarianceResult(StatisticOperation<TResultData>::_resultData);
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
134
                        finalizeKurtosisResult(StatisticOperation<TResultData>::_resultData);
2ed0b752   Hacene SI HADJ MOHAND   first last ok
135
136
                    } else if (_funcType == FT_FIRST_VALUE) {
                        finalizeFirstValueResult(StatisticOperation<TResultData>::_resultData);
2f07adf4   Hacene SI HADJ MOHAND   us ok
137
138
139
                    }else if (_funcType == FT_MIDDLE_VALUE) {
                        finalizeMiddleValueResult(StatisticOperation<TResultData>::_resultData);
                    } else if (_funcType == FT_LAST_VALUE) {
2ed0b752   Hacene SI HADJ MOHAND   first last ok
140
                        finalizeLastValueResult(StatisticOperation<TResultData>::_resultData);
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
141
                    }
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
142
143
144
145
146
                }

                virtual void reset() {
                    StatisticOperation<TResultData>::reset();
                    resetData(StatisticOperation<TResultData>::_resultData);
74f4d2f6   Benjamin Renard   Fix start / stop ...
147
148
149
                    _dataList.clear();
                    _timeDataList.clear();
                    ++_currentTimeInterval;
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
150
151
152
153
154
155
156
157
158
159
                }

                /**
                 * @brief Get the result dimensiond efinition.
                 */
                virtual std::string getResultDimDefinition(bool /* forCoverage */) {
                    return _dimDef.str();
                }

            private:
233c5960   Benjamin Renard   Fix catalog size ...
160
161
162
163
164
165
166
167
168
169
                template<typename Type>
                void initDimSize(Type &/*a*/) {
                    _dimDef.str("1");
                }

                template<typename Type>
                void initDimSize(std::vector<Type> &/*a*/) {
                    _dimDef.str("unknown");
                }

65af22fe   Hacene SI HADJ MOHAND   ok for median var...
170
171
172
173
174
175
176
177
178
179
180
181
182
183

                template<typename Type>
                void resetData(Type &a) {
                    a._result << NotANumber();
                    a._nbDataProcessed = 0;
                }

                template<typename Type>
                void resetData(std::vector<Type> &a) {
                    a.clear();
                }

                template<typename Type>
                void computeMin(Type &a) {
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
                    if (isNAN(a))
                        return;

                    if (isNAN(StatisticOperation<TResultData>::_resultData._result))
                        StatisticOperation<TResultData>::_resultData._result = a;
                    else if (a < StatisticOperation<TResultData>::_resultData._result)
                        StatisticOperation<TResultData>::_resultData._result = a;
                    ++StatisticOperation<TResultData>::_resultData._nbDataProcessed;
                }

                template<typename Type>
                void computeMin(std::vector<Type> &a) {
                    if (StatisticOperation<TResultData>::_resultData.empty()) {
                        _dimDef.str("");
                        _dimDef << a.size();
                        for (unsigned int i = 0; i < a.size(); ++i) {
                            StatisticDataScalar<Type> data;
                            resetData(data);
                            data._result = a[i];
                            if (!isNAN(a))
                                ++data._nbDataProcessed;
                            StatisticOperation<TResultData>::_resultData.push_back(data);
                        }
                        return;
                    }

                    for (unsigned int i = 0; i < StatisticOperation<TResultData>::_resultData.size(); ++i) {
                        if (isNAN(a[i]))
                            continue;

                        if (isNAN(StatisticOperation<TResultData>::_resultData[i]._result))
                            StatisticOperation<TResultData>::_resultData[i]._result = a[i];
                        else if (a[i] < StatisticOperation<TResultData>::_resultData[i]._result)
                            StatisticOperation<TResultData>::_resultData[i]._result = a[i];
                        ++StatisticOperation<TResultData>::_resultData[i]._nbDataProcessed;
                    }
                }

                template<typename Type>
                void computeMax(Type &a) {
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
                    if (isNAN(a))
                        return;

                    if (isNAN(StatisticOperation<TResultData>::_resultData._result))
                        StatisticOperation<TResultData>::_resultData._result = a;
                    else if (a > StatisticOperation<TResultData>::_resultData._result)
                        StatisticOperation<TResultData>::_resultData._result = a;
                    ++StatisticOperation<TResultData>::_resultData._nbDataProcessed;
                }

                template<typename Type>
                void computeMax(std::vector<Type> &a) {
                    if (StatisticOperation<TResultData>::_resultData.empty()) {
                        _dimDef.str("");
                        _dimDef << a.size();

                        for (unsigned int i = 0; i < a.size(); ++i) {
                            StatisticDataScalar<Type> data;
                            resetData(data);
                            data._result = a[i];
                            if (!isNAN(a))
                                ++data._nbDataProcessed;
                            StatisticOperation<TResultData>::_resultData.push_back(data);
                        }
                        return;
                    }

                    for (unsigned int i = 0; i < StatisticOperation<TResultData>::_resultData.size(); ++i) {
                        if (isNAN(a[i]))
                            continue;

                        if (isNAN(StatisticOperation<TResultData>::_resultData[i]._result))
                            StatisticOperation<TResultData>::_resultData[i]._result = a[i];
                        else if (a[i] > StatisticOperation<TResultData>::_resultData[i]._result)
                            StatisticOperation<TResultData>::_resultData[i]._result = a[i];
                        ++StatisticOperation<TResultData>::_resultData[i]._nbDataProcessed;
                    }
                }

                template<typename Type>
                void addForMean(Type &a) {
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
                    if (isNAN(a))
                        return;

                    if (isNAN(StatisticOperation<TResultData>::_resultData._result))
                        StatisticOperation<TResultData>::_resultData._result = a;
                    else
                        StatisticOperation<TResultData>::_resultData._result = StatisticOperation<TResultData>::_resultData._result + a;
                    ++StatisticOperation<TResultData>::_resultData._nbDataProcessed;
                }

                template<typename Type>
                void addForMean(std::vector<Type> &a) {
                    if (StatisticOperation<TResultData>::_resultData.empty()) {
                        _dimDef.str("");
                        _dimDef << a.size();

                        for (unsigned int i = 0; i < a.size(); ++i) {
                            StatisticDataScalar<Type> data;
                            resetData(data);
                            data._result = a[i];
                            if (!isNAN(a))
                                ++data._nbDataProcessed;
                            StatisticOperation<TResultData>::_resultData.push_back(data);
                        }
                        return;
                    }

                    for (unsigned int i = 0; i < StatisticOperation<TResultData>::_resultData.size(); ++i) {
                        if (isNAN(a[i]))
                            continue;

                        if (isNAN(StatisticOperation<TResultData>::_resultData[i]._result))
                            StatisticOperation<TResultData>::_resultData[i]._result = a[i];
                        else
                            StatisticOperation<TResultData>::_resultData[i]._result = StatisticOperation<TResultData>::_resultData[i]._result + a[i];
                        ++StatisticOperation<TResultData>::_resultData[i]._nbDataProcessed;
                    }
                }

                template<typename Type>
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
305
                void addForRMS(Type &a) {
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
                    if (isNAN(a))
                        return;

                    if (isNAN(StatisticOperation<TResultData>::_resultData._result))
                        StatisticOperation<TResultData>::_resultData._result = a * a;
                    else
                        StatisticOperation<TResultData>::_resultData._result = StatisticOperation<TResultData>::_resultData._result + a*a;
                    ++StatisticOperation<TResultData>::_resultData._nbDataProcessed;
                }

                template<typename Type>
                void addForRMS(std::vector<Type> &a) {
                    if (StatisticOperation<TResultData>::_resultData.empty()) {
                        _dimDef.str("");
                        _dimDef << a.size();

                        for (unsigned int i = 0; i < a.size(); ++i) {
                            StatisticDataScalar<Type> data;
                            resetData(data);
                            data._result = a[i] * a[i];
                            if (!isNAN(a))
                                ++data._nbDataProcessed;
                            StatisticOperation<TResultData>::_resultData.push_back(data);
                        }
                        return;
                    }

                    for (unsigned int i = 0; i < StatisticOperation<TResultData>::_resultData.size(); ++i) {
                        if (isNAN(a[i]))
                            continue;

                        if (isNAN(StatisticOperation<TResultData>::_resultData[i]._result))
                            StatisticOperation<TResultData>::_resultData[i]._result = a[i] * a[i];
                        else
                            StatisticOperation<TResultData>::_resultData[i]._result = StatisticOperation<TResultData>::_resultData[i]._result + a[i] * a[i];
                        ++StatisticOperation<TResultData>::_resultData[i]._nbDataProcessed;
                    }
                }

                template<typename Type>
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
346
                void generateVector(Type &a) {
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
347
348
349
350
351
                    if (isNAN(a))
                        return;
                    if (isNAN(StatisticOperation<TResultData>::_resultData._result))
                        StatisticOperation<TResultData>::_resultData._result = a;
                    _dataList.push_back(a);
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
                }

                template<typename Type>
                void generateVector(std::vector<Type> &a) {
                    if (StatisticOperation<TResultData>::_resultData.empty()) {
                        _dimDef.str("");
                        _dimDef << a.size();

                        for (unsigned int i = 0; i < a.size(); ++i) {
                            StatisticDataScalar<Type> data;
                            resetData(data);
                            data._result = a[i];
                            if (!isNAN(a))
                                ++data._nbDataProcessed;
                            StatisticOperation<TResultData>::_resultData.push_back(data);
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
367
368
369
370
371
372
373
374
375
376
377
                        }
                    }

                    if (_dataList.empty()) {
                        // init _dataList  
                        for (int i = 0; i < a.size(); i++) {
                            std::vector<Type> vec;
                            if (!isNAN(a))
                                vec.push_back(a[i]);
                            _dataList.push_back(vec);
                            vec.clear();
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
378
379
380
381
382
383
                        }
                    } else {
                        //remplir dataList
                        for (int i = 0; i < a.size(); i++) {
                            if (!isNAN(a[i]))
                                _dataList[i].push_back(a[i]);
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
384
385
386
387
388
389
                        }
                    }

                }

                template<typename Type>
2ed0b752   Hacene SI HADJ MOHAND   first last ok
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
                void generateTimeDataVector(double time, Type &a) {
                    _dimDef.str("1");
                    if (isNAN(a))
                        return;
                    if (isNAN(StatisticOperation<TResultData>::_resultData._result))
                        StatisticOperation<TResultData>::_resultData._result = a;
                    _timeDataList.push_back(std::make_pair(time, a));
                }

                template<typename Type>
                void generateTimeDataVector(double time, std::vector<Type> &a) {
                    if (StatisticOperation<TResultData>::_resultData.empty()) {
                        _dimDef.str("");
                        _dimDef << a.size();

                        for (unsigned int i = 0; i < a.size(); ++i) {
                            StatisticDataScalar<Type> data;
                            resetData(data);
                            data._result = a[i];
                            if (!isNAN(a))
                                ++data._nbDataProcessed;
                            StatisticOperation<TResultData>::_resultData.push_back(data);
                        }
                    }
                    // fill timeDataList
2f07adf4   Hacene SI HADJ MOHAND   us ok
415
416
417
418
419
                    std::vector<Type> vec;
                    for (int i = 0; i < a.size(); i++) 
                         vec.push_back(a[i]);
                     _timeDataList.push_back(std::make_pair(time, vec));
                     vec.clear();
2ed0b752   Hacene SI HADJ MOHAND   first last ok
420
421
422
                }

                template<typename Type>
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
423
                void finalizeMeanResult(Type& a) {
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
424
425
                    if (!isNAN(a._result) && a._nbDataProcessed > 0)
                        a._result /= a._nbDataProcessed;
4b695af7   Hacene SI HADJ MOHAND   tested ok
426
                    _mean = a._result;
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
427

65af22fe   Hacene SI HADJ MOHAND   ok for median var...
428
429
430
431
                }

                template<typename Type>
                void finalizeMeanResult(std::vector<Type>& a) {
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
                    for (unsigned int i = 0; i < a.size(); ++i) {
                        if (!isNAN(a[i]._result) && a[i]._nbDataProcessed > 0) {
                            a[i]._result /= a[i]._nbDataProcessed;
                            _mean.push_back(a[i]._result);
                        }
                    }
                }

                template<typename Type>
                void finalizeRMSResult(Type& a) {
                    if (!isNAN(a._result) && a._nbDataProcessed > 0)
                        a._result = sqrt(a._result / a._nbDataProcessed);
                }

                template<typename Type>
                void finalizeRMSResult(std::vector<Type>& a) {
                    for (int i = 0; i < a.size(); i++) {
                        if (!isNAN(a[i]._result) && a[i]._nbDataProcessed > 0)
                            a[i]._result = sqrt(a[i]._result / a[i]._nbDataProcessed);
4b695af7   Hacene SI HADJ MOHAND   tested ok
451
                    }
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
                }

                template<typename Type>
                void finalizeMedianResult(Type & a) {
                    std::size_t size = _dataList.size();
                    if (size == 0)
                        return;
                    std::sort(_dataList.begin(), _dataList.end());
                    if (size % 2 == 0) {
                        a._result = (_dataList[size / 2 - 1] + _dataList[size / 2]) / 2;
                    } else {
                        a._result = _dataList[floor(size / 2)];
                    }
                }

                template<typename Type>
                void finalizeMedianResult(std::vector<Type>& a) {
                    for (int i = 0; i < a.size(); i++) {
                        std::size_t size = _dataList[i].size();
                        if (size == 0)
                            return;
                        std::sort(_dataList[i].begin(), _dataList[i].end());
                        if (size % 2 == 0) {
                            a[i]._result = (_dataList[i][size / 2 - 1] + _dataList[i][size / 2]) / 2;
                        } else {
                            a[i]._result = _dataList[i][floor(size / 2)];
                        }
                    }
                }

                template<typename Type>
fdea19dd   Hacene SI HADJ MOHAND   adding prcess
483
                void finalizeFirstValueResult(Type & a) {
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
484
485
                    if (_dataList.size() == 0)
                        return;
2ed0b752   Hacene SI HADJ MOHAND   first last ok
486
                    a._result = _dataList[0];
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
487
                }
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
488

65af22fe   Hacene SI HADJ MOHAND   ok for median var...
489
                template<typename Type>
fdea19dd   Hacene SI HADJ MOHAND   adding prcess
490
                void finalizeFirstValueResult(std::vector<Type>& a) {
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
491
492
493
                    for (int i = 0; i < a.size(); i++) {
                        if (_dataList[i].size() == 0)
                            return;
2ed0b752   Hacene SI HADJ MOHAND   first last ok
494
                        a[i]._result = _dataList[i][0];
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
495
496
497
                    }
                }

1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
498
                template<typename Type>
2ed0b752   Hacene SI HADJ MOHAND   first last ok
499
500
                void finalizeLastValueResult(Type & a) {
                    if (_dataList.size() == 0)
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
501
                        return;
2ed0b752   Hacene SI HADJ MOHAND   first last ok
502
                    a._result = _dataList.back();
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
503
                }
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
504

1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
505
                template<typename Type>
2ed0b752   Hacene SI HADJ MOHAND   first last ok
506
507
508
                void finalizeLastValueResult(std::vector<Type>& a) {
                    for (int i = 0; i < a.size(); i++) {
                        if (_dataList[i].size() == 0)
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
509
                            return;
2ed0b752   Hacene SI HADJ MOHAND   first last ok
510
                        a[i]._result = _dataList[i].back();
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
511
512
                    }
                }
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
513

1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
514
                template<typename Type>
2ed0b752   Hacene SI HADJ MOHAND   first last ok
515
516
                void finalizeMiddleValueResult(Type & a) {
                    if (_timeDataList.size() == 0)
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
517
                        return;
2ed0b752   Hacene SI HADJ MOHAND   first last ok
518
519
520
521
                    if (_timeDataList.size() == 1) {
                        a._result = _timeDataList[0].second;
                        return;
                    } else {
74f4d2f6   Benjamin Renard   Fix start / stop ...
522
                        double middeTime = (_currentTimeInterval->_startTime + _currentTimeInterval->_stopTime) / 2;
2f07adf4   Hacene SI HADJ MOHAND   us ok
523
524
525
526
527
528
529
530
531
532
                        for (int i = 0; i < _timeDataList.size(); i++) {
                            if (_timeDataList[i].first == middeTime) {
                                a._result = _timeDataList[i].second;
                                return;
                            } else if (_timeDataList[i].first > middeTime) {
                                a._result = _timeDataList[i - 1].second + (_timeDataList[i].second - _timeDataList[i - 1].second)*
                                        ((middeTime - _timeDataList[i - 1].first) / (_timeDataList[i].first - _timeDataList[i - 1].first));
                                return;
                            }
                        }
2ed0b752   Hacene SI HADJ MOHAND   first last ok
533
                    }
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
534
                }
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
535

1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
536
                template<typename Type>
2ed0b752   Hacene SI HADJ MOHAND   first last ok
537
538
539
                void finalizeMiddleValueResult(std::vector<Type> & a) {

                    //vector of pair <time dataScalar>
2f07adf4   Hacene SI HADJ MOHAND   us ok
540
                    std::vector<std::pair<double, double>> timeDataList;
6f170ffb   Hacene SI HADJ MOHAND   Ok rms
541
                    for (int i = 0; i < a.size(); i++) {
2ed0b752   Hacene SI HADJ MOHAND   first last ok
542
                        for (auto val : _timeDataList) {
2f07adf4   Hacene SI HADJ MOHAND   us ok
543
544
                            if (!isNAN(val.second[i]))
                                timeDataList.push_back(std::make_pair(val.first, (double) val.second[i]));
2ed0b752   Hacene SI HADJ MOHAND   first last ok
545
                        }
2f07adf4   Hacene SI HADJ MOHAND   us ok
546
547
548
549
550
551
552
553
554
555
556
557
558
                    
                    if (timeDataList.size() == 0)
                        continue;
                    a[i]._result = getMiddleValue(timeDataList);
                    timeDataList.clear();
                }
                }
            

            double getMiddleValue(std::vector<std::pair<double, double >> time1DDataList) {
                if (time1DDataList.size() == 1) {
                    return time1DDataList[0].second;
                }
74f4d2f6   Benjamin Renard   Fix start / stop ...
559
                double middeTime = (_currentTimeInterval->_startTime + _currentTimeInterval->_stopTime) / 2;
2f07adf4   Hacene SI HADJ MOHAND   us ok
560
561
562
563
564
565
566
567
568
                double res = std::numeric_limits<double>::quiet_NaN();
                for (int i = 0; i < time1DDataList.size(); i++) {
                    if (time1DDataList[i].first == middeTime) {
                        res= time1DDataList[i].second;
                        break;
                    } else if (time1DDataList[i].first > middeTime) {
                        res = time1DDataList[i - 1].second + (time1DDataList[i].second - time1DDataList[i - 1].second)*
                                ((middeTime - time1DDataList[i - 1].first) / (time1DDataList[i].first - time1DDataList[i - 1].first));
                        break;
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
569
570
                    }
                }
2f07adf4   Hacene SI HADJ MOHAND   us ok
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
                return res;
            }

            template<typename Type>
            void finalizeVarianceResult(Type & a) {
                if (_dataList.size() == 0)
                    return;
                double accum = 0.0;
                std::for_each(std::begin(_dataList), std::end(_dataList), [&](const double d) {
                    accum += (d - _mean) * (d - _mean);
                });
                a._result = accum / (_dataList.size());
                _standardDeviation = sqrt(a._result);
            }

            template<typename Type>
            void finalizeVarianceResult(std::vector<Type> & a) {
                for (int i = 0; i < a.size(); i++) {
                    if (_dataList[i].size() == 0)
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
590
591
                        return;
                    double accum = 0.0;
2f07adf4   Hacene SI HADJ MOHAND   us ok
592
593
                    std::for_each(std::begin(_dataList[i]), std::end(_dataList[i]), [&](const double d) {
                        accum += (d - _mean[i]) * (d - _mean[i]);
65af22fe   Hacene SI HADJ MOHAND   ok for median var...
594
                    });
2f07adf4   Hacene SI HADJ MOHAND   us ok
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
                    a[i]._result = accum / (_dataList[i].size());
                    _standardDeviation.push_back(sqrt(a[i]._result));
                }
            }

            template<typename Type>
            void finalizeSkewnessResult(Type & a) {
                int n = _dataList.size();
                if (n == 0)
                    return;
                double accum = 0.0;
                std::for_each(std::begin(_dataList), std::end(_dataList), [&](const double d) {
                    accum += (d - _mean) * (d - _mean) * (d - _mean);
                });
                a._result = n / ((double) (n - 1)*(n - 2)) * accum / pow(_standardDeviation, 3);
            }

            template<typename Type>
            void finalizeSkewnessResult(std::vector<Type> & a) {
                for (int i = 0; i < a.size(); i++) {
                    int n = _dataList[i].size();
4b695af7   Hacene SI HADJ MOHAND   tested ok
616
                    if (n == 0)
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
617
618
                        return;
                    double accum = 0.0;
2f07adf4   Hacene SI HADJ MOHAND   us ok
619
620
                    std::for_each(std::begin(_dataList[i]), std::end(_dataList[i]), [&](const double d) {
                        accum += (d - _mean[i]) * (d - _mean[i]) * (d - _mean[i]);
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
621
                    });
2f07adf4   Hacene SI HADJ MOHAND   us ok
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
                    a[i]._result = n / ((double) (n - 1)*(n - 2)) * accum / pow(_standardDeviation[i], 3);
                }
            }

            template<typename Type>
            void finalizeKurtosisResult(Type & a) {
                int n = _dataList.size();
                if (n == 0)
                    return;
                double accum = 0.0;
                std::for_each(std::begin(_dataList), std::end(_dataList), [&](const double d) {
                    accum += (d - _mean) * (d - _mean) * (d - _mean) * (d - _mean) / pow(_standardDeviation, 4);
                });
                a._result = n * (n + 1) / ((double) (n - 1)*(n - 2)*(n - 3)) * accum - 3 * (n - 1)*(n - 1) / ((double) (n - 2)*(n - 3));
            }

            template<typename Type>
            void finalizeKurtosisResult(std::vector<Type> & a) {
                for (int i = 0; i < a.size(); i++) {
                    int n = _dataList[i].size();
4b695af7   Hacene SI HADJ MOHAND   tested ok
642
                    if (n == 0)
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
643
644
                        return;
                    double accum = 0.0;
2f07adf4   Hacene SI HADJ MOHAND   us ok
645
646
                    std::for_each(std::begin(_dataList[i]), std::end(_dataList[i]), [&](const double d) {
                        accum += (d - _mean[i]) * (d - _mean[i]) * (d - _mean[i]) * (d - _mean[i]) / pow(_standardDeviation[i], 4);
1d0339b8   Hacene SI HADJ MOHAND   Ok pour skewness ...
647
                    });
2f07adf4   Hacene SI HADJ MOHAND   us ok
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
                    a[i]._result = n * (n + 1) / ((double) (n - 1)*(n - 2)*(n - 3)) * accum - 3 * (n - 1)*(n - 1) / ((double) (n - 2)*(n - 3));
                }
            }

            /**
             * @brief real ParamData Input
             */
            TParamData& _paramInput;

            TimeIntervalListSPtr _timeIntervalList;

            TimeIntervalList::iterator _currentTimeInterval;

            ElementType _val;

            ElementType _mean;

            ElementType _standardDeviation;

            std::vector<ElementType> _dataList;

            /*
             * vector of pair<time, data> 
             */
            TimeDataList _timeDataList;


            FUNC_TYPE _funcType;

            std::stringstream _dimDef;
        };

        class CreateMinMaxMeanStatistic : public VisitorOfParamData {
        public:

            /**
             * @brief constructor
             */
            CreateMinMaxMeanStatistic(StatisticProcess& pProcess,
                    TimeIntervalListSPtr pTimeIntervalList,
                    ParamData &paramData, FUNC_TYPE type) :
            _process(pProcess), _timeIntervalList(pTimeIntervalList),
            _paramData(paramData), _operation(NULL), _type(type) {
                _paramData.accept(*this);
            }

            StatisticOperationBase* getStatisticOperation(void) {
                return _operation;
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataScalaireShort *)
             */
            virtual void visit(ParamDataScalaireShort *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataScalaireShort, StatisticDataScalar<short>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataScalaireShort &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataScalaireFloat *)
             */
            virtual void visit(ParamDataScalaireFloat *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataScalaireFloat, StatisticDataScalar<float>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataScalaireFloat &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataScalaireDouble *)
             */
            virtual void visit(ParamDataScalaireDouble *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataScalaireDouble, StatisticDataScalar<double>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataScalaireDouble &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataScalaireLongDouble *)
             */
            virtual void visit(ParamDataScalaireLongDouble *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataScalaireLongDouble, StatisticDataScalar<long double>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataScalaireLongDouble &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataScalaireInt *)
             */
            virtual void visit(ParamDataScalaireInt *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataScalaireInt, StatisticDataScalar<int>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataScalaireInt &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataLogicalData *)
             */
            virtual void visit(ParamDataLogicalData *) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN)
                        << AMDA::ex_msg(
                        "CreateStatistic operation not supported"));
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab1DShort *)
             */
            virtual void visit(ParamDataTab1DShort *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataTab1DShort, StatisticDataVector<short>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataTab1DShort &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab1DFloat *)
             */
            virtual void visit(ParamDataTab1DFloat *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataTab1DFloat, StatisticDataVector<float>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataTab1DFloat &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab1DDouble *)
             */
            virtual void visit(ParamDataTab1DDouble *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataTab1DDouble, StatisticDataVector<double>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataTab1DDouble &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab1DLongDouble *)
             */
            virtual void visit(ParamDataTab1DLongDouble *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataTab1DLongDouble, StatisticDataVector<long double>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataTab1DLongDouble &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab1DInt *)
             */
            virtual void visit(ParamDataTab1DInt *) {
                _operation = new MinMaxMeanStatisticOperation<ParamDataTab1DInt, StatisticDataVector<int>>(_process,
                        _timeIntervalList, dynamic_cast<ParamDataTab1DInt &> (_paramData), _type);
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab1DLogicalData *)
             */
            virtual void visit(ParamDataTab1DLogicalData *) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN)
                        << AMDA::ex_msg(
                        "CreateStatistic operation not supported"));
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab2DShort *)
             */
            virtual void visit(ParamDataTab2DShort *) {
                BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamDataTab2DShort data not supported"));
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab2DFloat *)
             */
            virtual void visit(ParamDataTab2DFloat *) {
                BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamDataTab2DFloat data not supported"));
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab2DDouble *)
             */
            virtual void visit(ParamDataTab2DDouble *) {
                BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamDataTab2DDouble data not supported"));
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab2DLongDouble *)
             */
            virtual void visit(ParamDataTab2DLongDouble *) {
                BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamDataTab2DLongDouble data not supported"));
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab2DInt *)
             */
            virtual void visit(ParamDataTab2DInt *) {
                BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamDataTab2DInt data not supported"));
            }

            /**
             * @overload VisitorOfParamData::visit(ParamDataTab2DLogicalData *)
             */
            virtual void visit(ParamDataTab2DLogicalData *) {
                BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamDataTab2DLogicalData data not supported"));
            }

        private:
            StatisticProcess& _process;

            TimeIntervalListSPtr& _timeIntervalList;

            ParamData &_paramData;

            StatisticOperationBase *_operation;

            FUNC_TYPE _type;
        };

    } /* namespace MinMaxMean */
} /* namespace Statistic */
fbe3c2bb   Benjamin Renard   First commit
854
855
856
} /* namespace AMDA */

#endif /* MINMAXMEANSTATISTIC_HH_ */