Blame view

src/ExternLib/StatisticFunctions/MedianFunc.hh 3.48 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
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
/*
 * MedianFunc.hh
 *
 *  Created on: Jun 23, 2018
 *      Author: benjamin
 */

#ifndef MEDIANFUNC_HH_
#define MEDIANFUNC_HH_

#include "AbstractFunc.hh"
#include "Toolbox.hh"

namespace AMDA {
namespace Parameters {
namespace StatisticFunctions {

template <typename Type>
bool getMedian(std::list<Type>& list, Type& result) {
	result << NotANumber();

	if (list.empty()) {
		return false;
	}

	std::vector<Type> sorted_list;
	for (typename std::list<Type>::iterator it = list.begin(); it != list.end(); ++it) {
		if (isNAN(*it)) {
			continue;
		}
		sorted_list.push_back(*it);
	}
	std::sort(sorted_list.begin(), sorted_list.end());

	if (sorted_list.empty()) {
		return false;
	}

	if (sorted_list.size() % 2 == 0) {
		//even
		int index = (sorted_list.size() / 2);
		result = sorted_list[index];
	}
	else {
		//odd
		int index = (sorted_list.size() / 2);
		result = (sorted_list[index] + sorted_list[index+1]) / 2.;
	}
	return true;
}

template <typename Type>
bool getMedian(std::list<std::vector<Type>>& list, std::vector<Type>& result) {
	if (list.empty()) {
		return false;
	}
	result = list.front();
	result << NotANumber();
	int eltSize = result.size();
	for (int i = 0; i < eltSize; ++i) {
		std::list<Type> compList;
		for (typename std::list<std::vector<Type>>::iterator it = list.begin(); it != list.end(); ++it) {
			if ((int)it->size() != eltSize) {
				continue;
			}
			compList.push_back((*it)[i]);
		}
		Type compRes;
		getMedian(compList, compRes);
		result[i] = compRes;
	}
	return true;
}

template <typename Type>
bool getMedian(std::list<Tab2DData<Type>>& list, Tab2DData<Type>& result) {
	if (list.empty()) {
		return false;
	}
	Tab2DData<Type> nanVal(list.front());
	nanVal << NotANumber();
	result = nanVal;
	int eltDim1Size = result.getDim1Size();
	int eltDim2Size = result.getDim2Size();
	for (int i = 0; i < eltDim1Size; ++i) {
		for (int j = 0; j < eltDim2Size; ++j) {
			std::list<Type> compList;
			for (typename std::list<Tab2DData<Type>>::iterator it = list.begin(); it != list.end(); ++it) {
				if (it->getDim1Size() != eltDim1Size || it->getDim2Size() != eltDim2Size) {
					continue;
				}
				compList.push_back((*it)[i][j]);
			}
			Type compRes;
			getMedian(compList, compRes);
			result[i][j] = compRes;
		}
	}
	return true;
}

template <typename InputElemType, typename OutputElemType>
OutputElemType computeMedian(std::list<std::pair<double,InputElemType>>& mem, OutputElemType& nanVal) {
	OutputElemType result = nanVal;
	if (mem.empty()) {
		return result;
	}
	std::list<InputElemType> list;
	for (typename std::list<std::pair<double,InputElemType>>::iterator it = mem.begin(); it != mem.end(); ++it) {
		list.push_back(it->second);
	}
	getMedian(list, result);
	return result;
}

/**
 * @class MedianFunc
 * @brief 
 * @details This class implements AbstractFunc.
 */
template <typename InputElemType, typename OutputElemType> 
class MedianFunc : public ClassicAbstractFunc<InputElemType,OutputElemType> {
public:
	/**
	 * @brief Constructor.
	 */
	MedianFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& paramInput, double windowtime)
	: ClassicAbstractFunc<InputElemType,OutputElemType>(pProcess, pTimeIntervalList, paramInput, windowtime) {
	}

	virtual ~MedianFunc() {
	}

	OutputElemType compute() {
		return computeMedian(ClassicAbstractFunc<InputElemType,OutputElemType>::_mem, ClassicAbstractFunc<InputElemType,OutputElemType>::_nanVal);
	}
};  

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