Blame view

src/ExternLib/StatisticProcesses/MinMaxMeanStatistic.hh 12.8 KB
fbe3c2bb   Benjamin Renard   First commit
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
98
99
100
101
102
103
104
105
106
107
108
109
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
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
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
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
358
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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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
/**
 * 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"

#include "TimeInterval.hh"

namespace AMDA {
namespace Statistic {
namespace MinMaxMean {

using namespace AMDA::Parameters;

typedef enum
{
	FT_MIN,
	FT_MAX,
	FT_MEAN
} FUNC_TYPE;


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

	MinMaxMeanStatisticOperation(StatisticProcess& process,
			TimeIntervalListSPtr pTimeIntervalList, TParamData &param, FUNC_TYPE funcType) :
				StatisticOperation<TResultData>(process),
				_paramInput(param), _timeIntervalList(pTimeIntervalList),
				_currentTimeInterval(_timeIntervalList->begin()),
				_funcType(funcType), _dimDef("unknown")
	{
		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);
			switch (_funcType)
			{
			case FT_MIN :
				computeMin(_val);
				break;
			case FT_MAX :
				computeMax(_val);
				break;
			case FT_MEAN :
				addForMean(_val);
				break;
			}
		}
	}

	virtual void finalizeCompute(void)
	{
		if (_funcType == FT_MEAN)
			finalizeMeanResult(StatisticOperation<TResultData>::_resultData);
	}

	virtual void reset() {
		StatisticOperation<TResultData>::reset();
		resetData(StatisticOperation<TResultData>::_resultData);
	}

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

private:
	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)
	{
		_dimDef.str("1");

		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)
	{
		_dimDef.str("1");

		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)
	{
		_dimDef.str("1");

		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>
	void finalizeMeanResult(Type& a)
	{
		if (a._nbDataProcessed > 0)
			if (!isNAN(a._result))
				a._result /= a._nbDataProcessed;
	}

	template<typename Type>
	void finalizeMeanResult(std::vector<Type>& a)
	{
		for (unsigned int i = 0; i < a.size(); ++i)
			finalizeMeanResult(a[i]);
	}

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

	TimeIntervalListSPtr _timeIntervalList;

	TimeIntervalList::iterator _currentTimeInterval;

	ElementType _val;

	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 */
} /* namespace AMDA */

#endif /* MINMAXMEANSTATISTIC_HH_ */