Blame view

src/ExternLib/StatisticFunctions/AbstractFunc.hh 8.68 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * 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.;
		if (_minWindow < _currentTimeInterval->_startTime) {
			_minWindow = _currentTimeInterval->_startTime;
		}
		_maxWindow = _target + _windowtime / 2.;
		if (_maxWindow > _currentTimeInterval->_stopTime) {
			_maxWindow = _currentTimeInterval->_stopTime;
		}
		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;
06bc83c5   Benjamin Renard   Fix bug with slid...
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
                                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();
                                    }
                                }
74a11471   Benjamin Renard   Add min, max, var...
182
183
184
			}
		}
		if (pParamDataIndexInfo._timeIntToProcessChanged || pParamDataIndexInfo._noMoreTimeInt) {
06bc83c5   Benjamin Renard   Fix bug with slid...
185
                    if (!needInit()) {
74a11471   Benjamin Renard   Add min, max, var...
186
			do {
06bc83c5   Benjamin Renard   Fix bug with slid...
187
                            if (inInt(getTarget())) {
74a11471   Benjamin Renard   Add min, max, var...
188
189
				_paramOutput->pushTime(getTarget());
				_paramOutput->push(compute());
06bc83c5   Benjamin Renard   Fix bug with slid...
190
191
192
                            }
			} while (nextTarget());
                    }
74a11471   Benjamin Renard   Add min, max, var...
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
		}
	}

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

private:
	ParamDataSpec<InputElemType>& _paramInput;

	ParamDataSpec<OutputElemType>* _paramOutput;

protected:
	OutputElemType _nanVal;
};  

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...
219
	virtual void init() {
74a11471   Benjamin Renard   Add min, max, var...
220
221
222
223
224
225
		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();
06bc83c5   Benjamin Renard   Fix bug with slid...
226
227
228
229
230
                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...
231
232
233
	}

	virtual bool needToChangeTarget(double crtTime) {
06bc83c5   Benjamin Renard   Fix bug with slid...
234
		return !AbstractFunc<InputElemType,OutputElemType>::needInit() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(crtTime);
74a11471   Benjamin Renard   Add min, max, var...
235
236
237
238
239
240
241
242
243
	}

	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...
244
245
246
247
        
        virtual void resetFunc() {
            _mem.clear();
        }
74a11471   Benjamin Renard   Add min, max, var...
248
249

protected:
06bc83c5   Benjamin Renard   Fix bug with slid...
250
	std::list<std::pair<double,InputElemType> > _mem;
74a11471   Benjamin Renard   Add min, max, var...
251
252
253
254
255
256
257
258
259
260
261
262
};

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...
263
264
265
266
267
268
	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...
269
270
271
272
273
274
	}

	virtual bool nextTarget() {
		if (!_targets.empty()) {
			bool res = AbstractFunc<InputElemType,OutputElemType>::setTarget(_targets.front());
			_targets.pop_front();
06bc83c5   Benjamin Renard   Fix bug with slid...
275
276
277
                        while (!_mem.empty() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(_mem.front().first)) {
                            _mem.pop_front();
                        }
74a11471   Benjamin Renard   Add min, max, var...
278
279
280
281
282
283
			return res;
		}
		return false;
	}

	virtual bool needToChangeTarget(double crtTime) {
06bc83c5   Benjamin Renard   Fix bug with slid...
284
		return !AbstractFunc<InputElemType,OutputElemType>::needInit() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(crtTime) && !_targets.empty() ;
74a11471   Benjamin Renard   Add min, max, var...
285
286
287
288
289
290
291
292
293
294
	}

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

	virtual void pushData(double time, InputElemType& elem) {
		_mem.push_back(std::make_pair(time, elem));
		_targets.push_back(time);
	}
06bc83c5   Benjamin Renard   Fix bug with slid...
295
296
297
298
299
        
        virtual void resetFunc() {
            _mem.clear();
            _targets.clear();
        }
74a11471   Benjamin Renard   Add min, max, var...
300
301
302
303

protected:
	std::list<double> _targets;

06bc83c5   Benjamin Renard   Fix bug with slid...
304
	std::list<std::pair<double,OutputElemType> > _mem;
74a11471   Benjamin Renard   Add min, max, var...
305
306
307
308
309
310
};

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