Blame view

src/ExternLib/StatisticFunctions/CorrelationFunctions.hh 18.4 KB
e8db9c26   Hacene SI HADJ MOHAND   structure ok
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   CorrelationFunctions.hh
 * Author: hacene
 *
 * Created on September 27, 2021, 3:35 PM
 */

#ifndef CORRELATIONFUNCTIONS_HH
#define CORRELATIONFUNCTIONS_HH

403992c8   Hacene SI HADJ MOHAND   scalr ok tested
17
18
#include "DicError.hh"
#include "AMDA_exception.hh"
e8db9c26   Hacene SI HADJ MOHAND   structure ok
19
20
21
22
#include "Parameter.hh"
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "Operation.hh"
e6a241e7   Hacene SI HADJ MOHAND   structure ok
23
24
#include <vector>
#include <iostream>
e8db9c26   Hacene SI HADJ MOHAND   structure ok
25
26
27
#include <iterator>
#include <c++/4.8.2/bits/stl_vector.h>
#include <c++/4.8.2/bits/stl_pair.h>
6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
28
#include <list>
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
29
#include "Toolbox.hh"
e6a241e7   Hacene SI HADJ MOHAND   structure ok
30
#include "AbstractFunc.hh"
e8db9c26   Hacene SI HADJ MOHAND   structure ok
31
32
33
34
35
36
37

namespace AMDA {
    namespace Parameters {
        namespace StatisticFunctions {

#define AVERAGE_TIME 1200 // (seconds)
#define MAX_GAP_SIZE 3600 // (seconds)
e6a241e7   Hacene SI HADJ MOHAND   structure ok
38

403992c8   Hacene SI HADJ MOHAND   scalr ok tested
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
            enum COEFS {
                COVARIANCE = 1,
                PAERSON = 2,
                SPEARMAN = 3,
                KENDALL = 4,
            };
            static std::map<std::string, COEFS> coefsToStr = {
                {"covariance", COEFS::COVARIANCE},
                {"pearson", COEFS::PAERSON},
                {"spearman", COEFS::SPEARMAN},
                {"kendall", COEFS::KENDALL},
                {"1", COEFS::COVARIANCE},
                {"2", COEFS::PAERSON},
                {"3", COEFS::SPEARMAN},
                {"4", COEFS::KENDALL},
e8db9c26   Hacene SI HADJ MOHAND   structure ok
54
55
56
            };

            template <typename InputElemType, typename OutputElemType>
e6a241e7   Hacene SI HADJ MOHAND   structure ok
57
            class AbstractCorrelationFunc : public AbstractFuncBase {
e8db9c26   Hacene SI HADJ MOHAND   structure ok
58
59
60
61
62
63
            public:

                /**
                 * @brief Constructor.
                 * @details Create the ParamData type of the input ParamData.
                 */
6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
64
                AbstractCorrelationFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& firstParamInput, ParamDataSpec<InputElemType>& secondParamInput, double windowtime, std::string correlationType)
e6a241e7   Hacene SI HADJ MOHAND   structure ok
65
                : AbstractFuncBase(pProcess, pTimeIntervalList, windowtime),
6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
66
                _correlationType(correlationType),
e8db9c26   Hacene SI HADJ MOHAND   structure ok
67
68
                _firstParamInput(firstParamInput),
                _secondParamInput(secondParamInput),
e8db9c26   Hacene SI HADJ MOHAND   structure ok
69
70
71
72
73
74
75
                _paramOutput(new ParamDataSpec<OutputElemType>) {
                    _paramDataOutput = _paramOutput;
                }

                virtual ~AbstractCorrelationFunc() {
                }

6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
76
                virtual OutputElemType computeCorrelation(std::list<std::pair<double, std::pair<InputElemType, InputElemType>>>&mem, OutputElemType& nanVal, std::string type) = 0;
e8db9c26   Hacene SI HADJ MOHAND   structure ok
77

6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                virtual void init() {
                    AbstractCorrelationFunc<InputElemType, OutputElemType>::setTarget(AbstractCorrelationFunc<InputElemType, OutputElemType>::getIntStartTime());
                    AbstractCorrelationFunc<InputElemType, OutputElemType>::setNeedInit(false);
                }

                virtual bool nextTarget() {
                    double target = AbstractCorrelationFunc<InputElemType, OutputElemType>::getTarget() + AbstractCorrelationFunc<InputElemType, OutputElemType>::getWindowTime();
                    bool res = AbstractCorrelationFunc<InputElemType, OutputElemType>::setTarget(target);
                    while (!_mem.empty() && !AbstractCorrelationFunc<InputElemType, OutputElemType>::inWindow(_mem.front().first)) {
                        _mem.pop_front();
                    }
                    return res;
                }

                virtual bool needToChangeTarget(double crtTime) {
                    return !AbstractCorrelationFunc<InputElemType, OutputElemType>::needInit() && !AbstractCorrelationFunc<InputElemType, OutputElemType>::inWindow(crtTime);
                }

                virtual double getSampling() {
                    return AbstractCorrelationFunc<InputElemType, OutputElemType>::getWindowTime();
                }

                virtual void pushData(double time, std::pair<InputElemType, InputElemType>& elem) {
                    _mem.push_back(std::make_pair(time, elem));
                }

                virtual void resetFunc() {
                    _mem.clear();
                }
e8db9c26   Hacene SI HADJ MOHAND   structure ok
107
108
109
110
111

                void pushSecondParamData(ParamDataIndexInfo &pParamDataIndexInfo) {
                    for (unsigned int _index = pParamDataIndexInfo._startIndex;
                            _index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
                            ++_index) {
e6a241e7   Hacene SI HADJ MOHAND   structure ok
112
                        double time = _secondParamInput.getTime(_index);
e8db9c26   Hacene SI HADJ MOHAND   structure ok
113
                        InputElemType val_ = _secondParamInput.get(_index);
e6a241e7   Hacene SI HADJ MOHAND   structure ok
114
                        _secondParamInputData.push_back(std::pair<double, InputElemType> (time, val_));
e8db9c26   Hacene SI HADJ MOHAND   structure ok
115
116
117
118
119
120
121
122
123
124
                    }
                }

                virtual InputElemType getValue(std::vector<std::pair<double, InputElemType> >& input, double time) = 0;

                /**
                 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
                 */

                void write(ParamDataIndexInfo &pParamDataIndexInfo) {
f5c74402   Hacene SI HADJ MOHAND   compilation ok
125

e8db9c26   Hacene SI HADJ MOHAND   structure ok
126
127
128
129
130
131
132
133
134
135
136
137
138
                    if ((pParamDataIndexInfo._nbDataToProcess > 0)) {
                        if (pParamDataIndexInfo._startIndex == 0) {
                            _nanVal = _firstParamInput.get(0);
                            _nanVal << NotANumber();
                        }
                        for (unsigned int _index = pParamDataIndexInfo._startIndex;
                                _index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
                                ++_index) {
                            double crtTime = _firstParamInput.getTime(_index);
                            InputElemType firstVal = _firstParamInput.get(_index);
                            // get the second element 
                            InputElemType secondVal = getValue(_secondParamInputData, crtTime);
                            std::pair<InputElemType, InputElemType> crtVal(firstVal, secondVal);
e6a241e7   Hacene SI HADJ MOHAND   structure ok
139

e8db9c26   Hacene SI HADJ MOHAND   structure ok
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
                            if (needToChangeTarget(crtTime)) {
                                _paramOutput->pushTime(getTarget());
                                _paramOutput->push(compute());
                                pushData(crtTime, crtVal);
                                nextTarget();
                                bool skip = false;
                                while (!skip && needToChangeTarget(crtTime)) {
                                    _paramOutput->pushTime(getTarget());
                                    _paramOutput->push(compute());
                                    skip = nextTarget();
                                }
                            } else {
                                pushData(crtTime, crtVal);
                                if (needInit()) {
                                    init();
                                }
                            }
                        }
                    }
                    if (pParamDataIndexInfo._timeIntToProcessChanged || pParamDataIndexInfo._noMoreTimeInt) {
                        if (!needInit()) {
                            do {
                                if (inInt(getTarget())) {
                                    _paramOutput->pushTime(getTarget());
                                    _paramOutput->push(compute());
                                }
                            } while (nextTarget());
                        }
                    }
f5c74402   Hacene SI HADJ MOHAND   compilation ok
169

e8db9c26   Hacene SI HADJ MOHAND   structure ok
170
171
172
173
174
175
                }

                double getInputParamSampling() {
                    return _firstParamInput.getMinSampling();
                }

6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
176
177
178
179
180
181
182
183
184
185
186
187
                OutputElemType compute() {
                    return computeCorrelation(_mem, AbstractCorrelationFunc<InputElemType, OutputElemType>::_nanVal, _correlationType);
                }

                
            protected:
                OutputElemType _nanVal;

                std::string _correlationType;

                std::list<std::pair<double, std::pair<InputElemType, InputElemType>> > _mem;

e8db9c26   Hacene SI HADJ MOHAND   structure ok
188
189
190
191
192
193
194
195
196
            private:
                ParamDataSpec<InputElemType>& _firstParamInput;

                ParamDataSpec<InputElemType>& _secondParamInput;

                ParamDataSpec<OutputElemType>* _paramOutput;

                std::vector<std::pair<double, InputElemType> > _secondParamInputData;

6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
197

e8db9c26   Hacene SI HADJ MOHAND   structure ok
198
199
200
201
202
203
204
205
206
207
208
209
            };

            /**
             * 
             * @param pProcess
             * @param pTimeIntervalList
             * @param firstParamInput
             * @param secondParamInput
             * @param windowtime
             * @param type
             */
            template <typename InputElemType, typename OutputElemType>
e6a241e7   Hacene SI HADJ MOHAND   structure ok
210
            class Correlation : public AbstractCorrelationFunc<InputElemType, OutputElemType> {
e8db9c26   Hacene SI HADJ MOHAND   structure ok
211
212
            public:

f5c74402   Hacene SI HADJ MOHAND   compilation ok
213
214
                Correlation(Process & pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& firstParamInput,
                        ParamDataSpec<InputElemType>& secondParamInput, double windowtime, std::string correlationType) :
6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
215
                AbstractCorrelationFunc<InputElemType, OutputElemType> (pProcess, pTimeIntervalList, firstParamInput, secondParamInput, windowtime, correlationType) {
e8db9c26   Hacene SI HADJ MOHAND   structure ok
216
217
218

                }

e6a241e7   Hacene SI HADJ MOHAND   structure ok
219
                virtual ~Correlation() {
e8db9c26   Hacene SI HADJ MOHAND   structure ok
220
221
                }

e8db9c26   Hacene SI HADJ MOHAND   structure ok
222
223
224
225
                InputElemType getValue(std::vector<std::pair<double, InputElemType> >& input, double time) {
                    double min_t = time - AVERAGE_TIME / 2.;
                    double max_t = time + AVERAGE_TIME / 2.;
                    std::vector<std::pair<double, InputElemType> > values_for_mean;
f5c74402   Hacene SI HADJ MOHAND   compilation ok
226
                    InputElemType nanVal;
e8db9c26   Hacene SI HADJ MOHAND   structure ok
227
228
229
                    nanVal << NotANumber();
                    std::pair<double, InputElemType> prev_value(NAN, nanVal);
                    std::pair<double, InputElemType> next_value(NAN, nanVal);
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
230
                    InputElemType value = nanVal;
f5c74402   Hacene SI HADJ MOHAND   compilation ok
231
                    for (auto it = input.begin(); it != input.end(); ++it) {
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
232
233
234
235
236
                        if (it->first == time) {
                            value = it->second;
                            return value;
                            break;
                        } else if (isNAN(it->second))
e8db9c26   Hacene SI HADJ MOHAND   structure ok
237
238
239
240
241
242
243
244
245
246
                            continue;
                        else if (it->first > max_t) {
                            next_value = *it;
                            break;
                        } else if (it->first < min_t) {
                            prev_value = *it;
                        } else {
                            values_for_mean.push_back(*it);
                        }
                    }
e8db9c26   Hacene SI HADJ MOHAND   structure ok
247
248
249
                    if (!values_for_mean.empty()) {
                        //Compute mean
                        InputElemType sum = 0;
f5c74402   Hacene SI HADJ MOHAND   compilation ok
250
                        for (auto it = values_for_mean.begin(); it != values_for_mean.end(); ++it) {
e8db9c26   Hacene SI HADJ MOHAND   structure ok
251
252
253
254
255
256
257
258
259
260
261
262
                            sum += it->second;
                        }
                        value = sum / (InputElemType) values_for_mean.size();
                    } else {
                        if (!isNAN(prev_value.first) && !isNAN(next_value.first) && (next_value.first - prev_value.first <= MAX_GAP_SIZE)) {
                            //Compute interpolated value
                            value = prev_value.second + (time - prev_value.first) / (next_value.first - prev_value.first) * (next_value.second - prev_value.second);
                        }
                    }

                    return value;
                }
f5c74402   Hacene SI HADJ MOHAND   compilation ok
263
                
e6a241e7   Hacene SI HADJ MOHAND   structure ok
264
265
266
267
268
269
270
271
272
                OutputElemType computeCorrelation(std::list<std::pair<double, std::pair<InputElemType, InputElemType>>>&mem, OutputElemType& nanVal, std::string type) {
                    OutputElemType result = nanVal;
                    if (mem.empty()) {
                        return result;
                    }
                    std::list<std::pair<InputElemType, InputElemType>> list;
                    for (typename std::list<std::pair<double, std::pair < InputElemType, InputElemType>>>::iterator it = mem.begin(); it != mem.end(); ++it) {
                        list.push_back(it->second);
                    }
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
                    if (coefsToStr.find(type) == coefsToStr.end()) {
                        BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction  unknown correlation type " + type));
                    }

                    switch (coefsToStr[type]) {
                        case COEFS::COVARIANCE:
                            getCovariance(list, result);
                            break;
                        case COEFS::PAERSON:
                            getPearson(list, result);
                            break;
                        case COEFS::SPEARMAN:
                            getSpearman(list, result);
                            break;
                        case COEFS::KENDALL:
                            getKendall(list, result);
                            break;
                        default:
                            BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction  unknown correlation type :" + type));
                    }

e6a241e7   Hacene SI HADJ MOHAND   structure ok
294
                    return result;
e8db9c26   Hacene SI HADJ MOHAND   structure ok
295
                }
6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
296
297
298
299
300
301
302
303
304
305
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
346
347
348
349
350
351
352
353
354
355
356
357

            };
            
             template <typename InputElemType, typename OutputElemType, typename dataType>
            class Correlation1D : public AbstractCorrelationFunc<InputElemType, OutputElemType> {
            public:

                Correlation1D(Process & pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& firstParamInput,
                        ParamDataSpec<InputElemType>& secondParamInput, double windowtime, std::string correlationType) :
                AbstractCorrelationFunc<InputElemType, OutputElemType> (pProcess, pTimeIntervalList, firstParamInput, secondParamInput, windowtime, correlationType) {

                }

                virtual ~Correlation1D() {
                }

                InputElemType getValue(std::vector<std::pair<double, InputElemType> >& input, double time) {
                    double min_t = time - AVERAGE_TIME / 2.;
                    double max_t = time + AVERAGE_TIME / 2.;
                    std::vector<std::pair<double, InputElemType> > values_for_mean;
                    InputElemType nanVal;
                    nanVal << NotANumber();
                    std::pair<double, InputElemType> prev_value(NAN, nanVal);
                    std::pair<double, InputElemType> next_value(NAN, nanVal);
                    InputElemType value = nanVal;
                    for (auto it = input.begin(); it != input.end(); ++it) {
                        if (it->first == time) {
                            value = it->second;
                            return value;
                            break;
                        } else if (isNAN(it->second))
                            continue;
                        else if (it->first > max_t) {
                            next_value = *it;
                            break;
                        } else if (it->first < min_t) {
                            prev_value = *it;
                        } else {
                            values_for_mean.push_back(*it);
                        }
                    }
                    if (!values_for_mean.empty()) {
                        //Compute mean
                        InputElemType sum = values_for_mean[0].second;
                         for(int i= 0; i < sum.size();++i){
                             for (int j=1 ; j <values_for_mean.size(); ++j ){
                                
                                   sum[i]   =  sum[i] + values_for_mean[j].second[i];
                                }
                              value[i] = sum[i] / (float) values_for_mean.size();
                        }
                       
                    } else {
                        if (!isNAN(prev_value.first) && !isNAN(next_value.first) && (next_value.first - prev_value.first <= MAX_GAP_SIZE)) {
                            //Compute interpolated value
                            for(int i= 0; i < prev_value.second.size();++i)
                                value[i] = prev_value.second[i]  + (time - prev_value.first) / (next_value.first - prev_value.first) * (next_value.second[i]  - prev_value.second[i]);
                        }
                    }

                    return value;
                }
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
358
                
6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
                OutputElemType computeCorrelation(std::list<std::pair<double, std::pair<InputElemType, InputElemType>>>&mem, OutputElemType& nanVal, std::string type) {
                    OutputElemType result = nanVal;
                    if (mem.empty()) {
                        return result;
                    }
                    int n_ = mem.begin()->second.first.size();
                    for(int i = 0; i < n_; ++i){
                    std::list<std::pair<dataType, dataType>> list;
                    for (typename std::list<std::pair<double, std::pair < InputElemType, InputElemType>>>::iterator it = mem.begin(); it != mem.end(); ++it) {
                        list.push_back(std::make_pair(it->second.first[i], it->second.second[i]));
                    }
                    if (coefsToStr.find(type) == coefsToStr.end()) {
                        BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction  unknown correlation type " + type));
                    }

                    switch (coefsToStr[type]) {
                        case COEFS::COVARIANCE:
                            getCovariance(list, result[i]);
                            break;
                        case COEFS::PAERSON:
                            getPearson(list, result[i]);
                            break;
                        case COEFS::SPEARMAN:
                            getSpearman(list, result[i]);
                            break;
                        case COEFS::KENDALL:
                            getKendall(list, result[i]);
                            break;
                        default:
                            BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction  unknown correlation type :" + type));
                    }
                    list.clear();
                    }

                    return result;
                }

e6a241e7   Hacene SI HADJ MOHAND   structure ok
396
            };
6a7820dc   Hacene SI HADJ MOHAND   1d compilation ok
397
            
e8db9c26   Hacene SI HADJ MOHAND   structure ok
398
399
400
401
        }
    }
}

e6a241e7   Hacene SI HADJ MOHAND   structure ok
402

e8db9c26   Hacene SI HADJ MOHAND   structure ok
403
#endif /* CORRELATIONFUNCTIONS_HH */