Blame view

src/ExternLib/StatisticFunctions/AbstractFunc.hh 13.4 KB
74a11471   Benjamin Renard   Add min, max, var...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * AbstractFunc.hh
 *
 *  Created on: Jun 21, 2018
 *      Author: benjamin
 */

#ifndef ABSTRACTFUNC_HH_
#define ABSTRACTFUNC_HH_

#include "Parameter.hh"
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "Operation.hh"

#include <iterator>

namespace AMDA {
namespace Parameters {
namespace StatisticFunctions {

/**
 * @class AbstractFunc
 * @brief 
 * @details This class implement the interface Operation.
 */

class AbstractFuncBase : public Operation {
public:
	AbstractFuncBase(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, double windowtime) : Operation(pProcess), _timeIntervalList(pTimeIntervalList), _currentTimeInterval(pTimeIntervalList->begin()), _windowtime(windowtime), _needInit(true) {

	}

	virtual ~AbstractFuncBase() {

	}

	double getWindowTime() {
		return _windowtime;
	}

	bool inWindow(double time) {
		return (time >= _minWindow && time <= _maxWindow);
	}

	double getIntStartTime() {
		return _currentTimeInterval->_startTime;
	}

	double getIntStopTime() {
		return _currentTimeInterval->_stopTime;
	}

	bool inInt(double time) {
		return (time >= _currentTimeInterval->_startTime && time <= _currentTimeInterval->_stopTime);
	}

	double getTarget() {
		return _target;
	}

	bool needInit() {
		return _needInit;
	}

	void setNeedInit(bool needInit) {
		_needInit = needInit;
	}

	bool setTarget(double time) {
		if (!inInt(time)) {
			return false;
		}

		_target = time;
		_minWindow = _target - _windowtime / 2.;
65f64dfe   Benjamin Renard   Fix bugs with cor...
77
78
		if (_minWindow < _currentTimeInterval->_startTime - _windowtime / 2.) {
			_minWindow = _currentTimeInterval->_startTime - _windowtime / 2.;
74a11471   Benjamin Renard   Add min, max, var...
79
80
		}
		_maxWindow = _target + _windowtime / 2.;
65f64dfe   Benjamin Renard   Fix bugs with cor...
81
82
		if (_maxWindow > _currentTimeInterval->_stopTime + _windowtime / 2.) {
			_maxWindow = _currentTimeInterval->_stopTime + _windowtime / 2.;
74a11471   Benjamin Renard   Add min, max, var...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
		}
		return true;
	}

	/**
	 * @overload Operation::reset(double pStartTime, double pTimeInt)
	 * @brief reset static data to process another TimeInterval
	 */
	virtual void reset() {
		Operation::reset();
		if (_currentTimeInterval == _timeIntervalList->end()) {
			return;
		}
		++_currentTimeInterval;
		_needInit = true;
65f64dfe   Benjamin Renard   Fix bugs with cor...
98
        resetFunc();
74a11471   Benjamin Renard   Add min, max, var...
99
100
101
102
103
104
105
106
	}

	virtual bool nextTarget() = 0;

	virtual bool needToChangeTarget(double crtTime) = 0;

	virtual double getSampling() = 0;

06bc83c5   Benjamin Renard   Fix bug with slid...
107
108
109
	virtual void init() = 0;
        
        virtual void resetFunc() = 0;
74a11471   Benjamin Renard   Add min, max, var...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

private:
	TimeIntervalListSPtr _timeIntervalList;

	TimeIntervalList::iterator _currentTimeInterval;

	double _windowtime;

	double _minWindow;

	double _maxWindow;

	double _target;

	bool _needInit;
};

template <typename InputElemType, typename OutputElemType> 
class AbstractFunc : public AbstractFuncBase {
public:
	/**
	 * @brief Constructor.
	 * @details Create the ParamData type of the input ParamData.
	 */
	AbstractFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& paramInput, double windowtime)
	: AbstractFuncBase(pProcess, pTimeIntervalList, windowtime),
	  _paramInput(paramInput),
	  _paramOutput(new ParamDataSpec<OutputElemType>) {
	  _paramDataOutput=_paramOutput;
	}

	virtual ~AbstractFunc() {
	}

	virtual void pushData(double time, InputElemType& elem) = 0;

	virtual OutputElemType compute() = 0;
06bc83c5   Benjamin Renard   Fix bug with slid...
147
        
74a11471   Benjamin Renard   Add min, max, var...
148
149
150
151
152
	/**
	 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
	 */
  
	void write(ParamDataIndexInfo &pParamDataIndexInfo) {
74a11471   Benjamin Renard   Add min, max, var...
153
154
155
156
157
		if ((pParamDataIndexInfo._nbDataToProcess > 0)) {
			if (pParamDataIndexInfo._startIndex == 0) {
				_nanVal = _paramInput.get(0);
				_nanVal << NotANumber();
			}
06bc83c5   Benjamin Renard   Fix bug with slid...
158
                        for (unsigned int _index = pParamDataIndexInfo._startIndex ;
74a11471   Benjamin Renard   Add min, max, var...
159
160
161
162
				_index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
				++_index)
			{
				double crtTime = _paramInput.getTime(_index);
06bc83c5   Benjamin Renard   Fix bug with slid...
163
                                InputElemType crtVal = _paramInput.get(_index);
06bc83c5   Benjamin Renard   Fix bug with slid...
164
165
166
167
                                if (needToChangeTarget(crtTime)) {
                                    _paramOutput->pushTime(getTarget());
                                    _paramOutput->push(compute());
                                    pushData(crtTime, crtVal);
65f64dfe   Benjamin Renard   Fix bugs with cor...
168
169
170
171
172
173
174
                                    if (nextTarget()) {
                                        bool skip = false;
                                        while (!skip && needToChangeTarget(crtTime)) {
                                            _paramOutput->pushTime(getTarget());
                                            _paramOutput->push(compute());
                                            skip = nextTarget();
                                        }
06bc83c5   Benjamin Renard   Fix bug with slid...
175
176
177
178
179
180
181
182
                                    }
                                }
                                else {
                                    pushData(crtTime, crtVal);
                                    if (needInit()) {
					init();
                                    }
                                }
74a11471   Benjamin Renard   Add min, max, var...
183
184
185
			}
		}
		if (pParamDataIndexInfo._timeIntToProcessChanged || pParamDataIndexInfo._noMoreTimeInt) {
65f64dfe   Benjamin Renard   Fix bugs with cor...
186
187
188
189
190
            if (!needInit()) {
			    do {
                    if (inInt(getTarget())) {
			            _paramOutput->pushTime(getTarget());
			            _paramOutput->push(compute());
06bc83c5   Benjamin Renard   Fix bug with slid...
191
                    }
65f64dfe   Benjamin Renard   Fix bugs with cor...
192
193
			    } while (nextTarget());
            }
74a11471   Benjamin Renard   Add min, max, var...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
		}
	}

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

private:
	ParamDataSpec<InputElemType>& _paramInput;

	ParamDataSpec<OutputElemType>* _paramOutput;

protected:
	OutputElemType _nanVal;
e336270a   Benjamin Renard   Fix correlation &...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
};

template <typename InputElemType, typename OutputElemType> 
class Abstract2ParamsFunc : public AbstractFuncBase {
public:
	/**
	 * @brief Constructor.
	 * @details Create the ParamData type of the input ParamData.
	 */
	Abstract2ParamsFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& param1Input, ParamDataSpec<InputElemType>& param2Input, double windowtime)
	: AbstractFuncBase(pProcess, pTimeIntervalList, windowtime),
	  _param1Input(param1Input),
	  _param2Input(param2Input),
	  _paramOutput(new ParamDataSpec<OutputElemType>) {
	  _paramDataOutput=_paramOutput;
	}

	virtual ~Abstract2ParamsFunc() {
	}

	virtual void pushData(double time, InputElemType& elem1, InputElemType& elem2) = 0;

	virtual OutputElemType compute() = 0;
        
	/**
	 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
	 */
  
	void write(ParamDataIndexInfo &pParamDataIndexInfo) {
		if ((pParamDataIndexInfo._nbDataToProcess > 0)) {
			for (unsigned int _index = pParamDataIndexInfo._startIndex ;
				_index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
				++_index)
			{
				double crtTime = _param1Input.getTime(_index);
                InputElemType crtVal1 = _param1Input.get(_index);
				InputElemType crtVal2 = _param2Input.get(_index);
                if (needToChangeTarget(crtTime)) {
                    _paramOutput->pushTime(getTarget());
                    _paramOutput->push(compute());
                    pushData(crtTime, crtVal1, crtVal2);
65f64dfe   Benjamin Renard   Fix bugs with cor...
249
250
251
252
253
254
255
                    if (nextTarget()) {
                        bool skip = false;
                        while (!skip && needToChangeTarget(crtTime)) {
                            _paramOutput->pushTime(getTarget());
                            _paramOutput->push(compute());
                            skip = nextTarget();
                        }
e336270a   Benjamin Renard   Fix correlation &...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
                    }
                }
                else {
                    pushData(crtTime, crtVal1, crtVal2);
                    if (needInit()) {
						init();
                    }
                }
			}
		}
		if (pParamDataIndexInfo._timeIntToProcessChanged || pParamDataIndexInfo._noMoreTimeInt) {
            if (!needInit()) {
				do {
                    if (inInt(getTarget())) {
						_paramOutput->pushTime(getTarget());
						_paramOutput->push(compute());
                    }
				} while (nextTarget());
            }
		}
	}

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

public:
	ParamDataSpec<InputElemType>& _param1Input;

	ParamDataSpec<InputElemType>& _param2Input;

	ParamDataSpec<OutputElemType>* _paramOutput;
};
74a11471   Benjamin Renard   Add min, max, var...
289
290
291
292
293
294
295
296
297
298
299

template <typename InputElemType, typename OutputElemType>
class ClassicAbstractFunc : public AbstractFunc<InputElemType,OutputElemType> {
public:
	ClassicAbstractFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& paramInput, double windowtime)
          : AbstractFunc<InputElemType,OutputElemType>(pProcess, pTimeIntervalList, paramInput, windowtime) {
	}

	virtual ~ClassicAbstractFunc() {
	}

06bc83c5   Benjamin Renard   Fix bug with slid...
300
	virtual void init() {
74a11471   Benjamin Renard   Add min, max, var...
301
302
303
304
305
306
		AbstractFunc<InputElemType,OutputElemType>::setTarget(AbstractFunc<InputElemType,OutputElemType>::getIntStartTime());
		AbstractFunc<InputElemType,OutputElemType>::setNeedInit(false);
	}

	virtual bool nextTarget() {
		double target = AbstractFunc<InputElemType,OutputElemType>::getTarget() + AbstractFunc<InputElemType,OutputElemType>::getWindowTime();
65f64dfe   Benjamin Renard   Fix bugs with cor...
307
308
309
310
311
        bool res = AbstractFunc<InputElemType,OutputElemType>::setTarget(target);
        while (!_mem.empty() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(_mem.front().first)) {
            _mem.pop_front();
        }
        return res;
74a11471   Benjamin Renard   Add min, max, var...
312
313
314
	}

	virtual bool needToChangeTarget(double crtTime) {
06bc83c5   Benjamin Renard   Fix bug with slid...
315
		return !AbstractFunc<InputElemType,OutputElemType>::needInit() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(crtTime);
74a11471   Benjamin Renard   Add min, max, var...
316
317
318
319
320
321
322
323
324
	}

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

	virtual void pushData(double time, InputElemType& elem) {
		_mem.push_back(std::make_pair(time, elem));
	}
06bc83c5   Benjamin Renard   Fix bug with slid...
325
326
327
328
        
        virtual void resetFunc() {
            _mem.clear();
        }
74a11471   Benjamin Renard   Add min, max, var...
329
330

protected:
06bc83c5   Benjamin Renard   Fix bug with slid...
331
	std::list<std::pair<double,InputElemType> > _mem;
74a11471   Benjamin Renard   Add min, max, var...
332
333
334
335
336
337
338
339
340
341
342
343
};

template <typename InputElemType, typename OutputElemType>
class SmAbstractFunc : public AbstractFunc<InputElemType,OutputElemType> {
public:
	SmAbstractFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& paramInput, double windowtime)
	  : AbstractFunc<InputElemType,OutputElemType>(pProcess, pTimeIntervalList, paramInput, windowtime) {
	}

	virtual ~SmAbstractFunc() {
	}

06bc83c5   Benjamin Renard   Fix bug with slid...
344
345
346
347
348
349
	virtual void init() {
            if (!_targets.empty()) {
		AbstractFunc<InputElemType,OutputElemType>::setTarget(_targets.front());
                AbstractFunc<InputElemType,OutputElemType>::setNeedInit(false);
                _targets.pop_front();
            }
74a11471   Benjamin Renard   Add min, max, var...
350
351
352
353
354
355
	}

	virtual bool nextTarget() {
		if (!_targets.empty()) {
			bool res = AbstractFunc<InputElemType,OutputElemType>::setTarget(_targets.front());
			_targets.pop_front();
06bc83c5   Benjamin Renard   Fix bug with slid...
356
357
358
                        while (!_mem.empty() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(_mem.front().first)) {
                            _mem.pop_front();
                        }
74a11471   Benjamin Renard   Add min, max, var...
359
360
361
362
363
364
			return res;
		}
		return false;
	}

	virtual bool needToChangeTarget(double crtTime) {
06bc83c5   Benjamin Renard   Fix bug with slid...
365
		return !AbstractFunc<InputElemType,OutputElemType>::needInit() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(crtTime) && !_targets.empty() ;
74a11471   Benjamin Renard   Add min, max, var...
366
367
368
369
370
371
372
373
	}

	virtual double getSampling() {
		return AbstractFunc<InputElemType,OutputElemType>::getInputParamSampling();
	}

	virtual void pushData(double time, InputElemType& elem) {
		_mem.push_back(std::make_pair(time, elem));
c04e147f   Erdogan Furkan   #7318 - Done
374
375
		if (time >= AbstractFunc<InputElemType,OutputElemType>::getIntStartTime() && time <= AbstractFunc<InputElemType,OutputElemType>::getIntStopTime())
			_targets.push_back(time);
74a11471   Benjamin Renard   Add min, max, var...
376
	}
06bc83c5   Benjamin Renard   Fix bug with slid...
377
        
e336270a   Benjamin Renard   Fix correlation &...
378
379
380
381
382
383
384
385
386
387
388
389
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
    virtual void resetFunc() {
        _mem.clear();
        _targets.clear();
    }

protected:
	std::list<double> _targets;

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

template <typename InputElemType, typename OutputElemType>
class Sm2ParamsAbstractFunc : public Abstract2ParamsFunc<InputElemType, OutputElemType> {
public:
	Sm2ParamsAbstractFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& param1Input, ParamDataSpec<InputElemType>& param2Input, double windowtime)
	  : Abstract2ParamsFunc<InputElemType,OutputElemType>(pProcess, pTimeIntervalList, param1Input, param2Input, windowtime) {
	}

	virtual ~Sm2ParamsAbstractFunc() {
	}

	virtual void init() {
            if (!_targets.empty()) {
				Abstract2ParamsFunc<InputElemType,OutputElemType>::setTarget(_targets.front());
                Abstract2ParamsFunc<InputElemType,OutputElemType>::setNeedInit(false);
                _targets.pop_front();
            }
	}

	virtual bool nextTarget() {
		if (!_targets.empty()) {
			bool res = Abstract2ParamsFunc<InputElemType,OutputElemType>::setTarget(_targets.front());
			_targets.pop_front();
            while (!_mem.empty() && !Abstract2ParamsFunc<InputElemType,OutputElemType>::inWindow(_mem.front().first)) {
                _mem.pop_front();
            }
			return res;
		}
		return false;
	}

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

	virtual double getSampling() {
		return Abstract2ParamsFunc<InputElemType,OutputElemType>::getInputParamSampling();
	}

	virtual void pushData(double time, InputElemType& elem1, InputElemType& elem2) {
		_mem.push_back(std::make_pair(time, std::make_pair(elem1,elem2)));
		_targets.push_back(time);
	}
        
    virtual void resetFunc() {
        _mem.clear();
        _targets.clear();
    }
74a11471   Benjamin Renard   Add min, max, var...
437
438
439
440

protected:
	std::list<double> _targets;

e336270a   Benjamin Renard   Fix correlation &...
441
	std::list<std::pair<double,std::pair<InputElemType,InputElemType > > > _mem;
74a11471   Benjamin Renard   Add min, max, var...
442
443
444
445
446
447
};

} /* namespace StatisticFunctions */
} /* namespace Parameters */
} /* namespace AMDA */
#endif /* ABSTRACTFUNC_HH_ */