Commit f5c74402b54a5981c4d342636b6fcd0373319abf
1 parent
e6a241e7
Exists in
master
and in
63 other branches
compilation ok
Showing
6 changed files
with
109 additions
and
62 deletions
Show diff stats
src/ExternLib/StatisticFunctions/CorrelationFunctions.hh
... | ... | @@ -83,6 +83,7 @@ namespace AMDA { |
83 | 83 | */ |
84 | 84 | |
85 | 85 | void write(ParamDataIndexInfo &pParamDataIndexInfo) { |
86 | + | |
86 | 87 | if ((pParamDataIndexInfo._nbDataToProcess > 0)) { |
87 | 88 | if (pParamDataIndexInfo._startIndex == 0) { |
88 | 89 | _nanVal = _firstParamInput.get(0); |
... | ... | @@ -126,6 +127,7 @@ namespace AMDA { |
126 | 127 | } while (nextTarget()); |
127 | 128 | } |
128 | 129 | } |
130 | + | |
129 | 131 | } |
130 | 132 | |
131 | 133 | double getInputParamSampling() { |
... | ... | @@ -158,9 +160,9 @@ namespace AMDA { |
158 | 160 | class Correlation : public AbstractCorrelationFunc<InputElemType, OutputElemType> { |
159 | 161 | public: |
160 | 162 | |
161 | - Correlation(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType> | |
162 | - & firstParamInput, ParamDataSpec<InputElemType>& secondParamInput, double windowtime, std::string correlationType), _correlationType(correlationType) : | |
163 | - AbstractCorrelationFunc<InputElemType, OutputElemType> (pProcess, pTimeIntervalList, firstParamInput, secondParamInput, windowtime) { | |
163 | + Correlation(Process & pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& firstParamInput, | |
164 | + ParamDataSpec<InputElemType>& secondParamInput, double windowtime, std::string correlationType) : | |
165 | + AbstractCorrelationFunc<InputElemType, OutputElemType> (pProcess, pTimeIntervalList, firstParamInput, secondParamInput, windowtime), _correlationType(correlationType) { | |
164 | 166 | |
165 | 167 | } |
166 | 168 | |
... | ... | @@ -201,11 +203,11 @@ namespace AMDA { |
201 | 203 | double min_t = time - AVERAGE_TIME / 2.; |
202 | 204 | double max_t = time + AVERAGE_TIME / 2.; |
203 | 205 | std::vector<std::pair<double, InputElemType> > values_for_mean; |
204 | - InputElemType nanVal = input[0]->second; | |
206 | + InputElemType nanVal; | |
205 | 207 | nanVal << NotANumber(); |
206 | 208 | std::pair<double, InputElemType> prev_value(NAN, nanVal); |
207 | 209 | std::pair<double, InputElemType> next_value(NAN, nanVal); |
208 | - for (std::vector<std::pair<double, InputElemType> >::iterator it = input.begin(); it != input.end(); ++it) { | |
210 | + for (auto it = input.begin(); it != input.end(); ++it) { | |
209 | 211 | if (isNAN(it->second)) |
210 | 212 | continue; |
211 | 213 | else if (it->first > max_t) { |
... | ... | @@ -222,7 +224,7 @@ namespace AMDA { |
222 | 224 | if (!values_for_mean.empty()) { |
223 | 225 | //Compute mean |
224 | 226 | InputElemType sum = 0; |
225 | - for (std::vector<std::pair<double, InputElemType> >::iterator it = values_for_mean.begin(); it != values_for_mean.end(); ++it) { | |
227 | + for (auto it = values_for_mean.begin(); it != values_for_mean.end(); ++it) { | |
226 | 228 | sum += it->second; |
227 | 229 | } |
228 | 230 | value = sum / (InputElemType) values_for_mean.size(); |
... | ... | @@ -237,39 +239,44 @@ namespace AMDA { |
237 | 239 | } |
238 | 240 | |
239 | 241 | OutputElemType compute() { |
240 | - return computeCorrelation(ClassicAbstractFunc<InputElemType, OutputElemType>::_mem, ClassicAbstractFunc<InputElemType, OutputElemType>::_nanVal, _correlationType); | |
242 | + return computeCorrelation(_mem, AbstractCorrelationFunc<InputElemType, OutputElemType>::_nanVal, _correlationType); | |
241 | 243 | } |
242 | 244 | |
243 | - /** | |
244 | - template <typename Type> | |
245 | - std::pair<Type, Type> getMean(std::list<std::pair<Type, Type>> &list) { | |
246 | - std::pair<Type, Type> result(0, 0); | |
247 | - std::pair<int, int> counter(0,0); | |
248 | - for (int i = 0; i < 2; i++) { | |
249 | - for (auto elt : list) { | |
250 | - if (!isNan(std::get<i>(elt))) { | |
251 | - std::get<i>(result) += std::get<i>(elt); | |
252 | - std::get<i>(counter) += 1; | |
253 | - } | |
245 | + | |
246 | + std::pair<OutputElemType,OutputElemType> getMean(std::list<std::pair<InputElemType, InputElemType>> &list) { | |
247 | + std::pair<OutputElemType,OutputElemType> result(0, 0); | |
248 | + std::pair<int, int> counter(0, 0); | |
249 | + | |
250 | + for (auto elt : list) { | |
251 | + if (!isNAN(elt.first)) { | |
252 | + result.first += elt.first; | |
253 | + counter.first += 1; | |
254 | + } | |
255 | + if (!isNAN(elt.second)) { | |
256 | + result.second += elt.second; | |
257 | + counter.second += 1; | |
254 | 258 | } |
255 | - if(std::get<i>(counter) != 0) | |
256 | - std::get<i>(result) /= std::get<i>(counter); | |
257 | - return result; | |
258 | 259 | } |
260 | + if (counter.first != 0) | |
261 | + result.first /= counter.first; | |
262 | + | |
263 | + if (counter.second != 0) | |
264 | + result.second /= counter.second; | |
265 | + | |
266 | + return result; | |
259 | 267 | } |
260 | 268 | |
261 | - template <typename Type> | |
262 | - std::pair<Type, Type> getStd(std::list<std::pair<Type, Type>> &list) { | |
263 | - std::pair<Type, Type> mean = getMean(list); | |
264 | - std::pair<Type, Type> result(0, 0); | |
269 | + std::pair<OutputElemType,OutputElemType> getStd(std::list<std::pair<InputElemType, InputElemType>> &list) { | |
270 | + std::pair<OutputElemType,OutputElemType> mean = getMean(list); | |
271 | + std::pair<OutputElemType,OutputElemType> result(0, 0); | |
265 | 272 | int counter1 = 0; |
266 | 273 | int counter2 = 0; |
267 | 274 | for (auto elt : list) { |
268 | - if (!isNan(elt->first)) { | |
275 | + if (!isNAN(elt->first)) { | |
269 | 276 | result.first += (elt->first - mean.first)*(elt->first - mean.first); |
270 | 277 | counter1 += 1; |
271 | 278 | } |
272 | - if (!isNan(elt->second)) { | |
279 | + if (!isNAN(elt->second)) { | |
273 | 280 | result.second += (elt->second - mean.second)*(elt->second - mean.second); |
274 | 281 | counter2 += 1; |
275 | 282 | } |
... | ... | @@ -284,20 +291,27 @@ namespace AMDA { |
284 | 291 | } else { |
285 | 292 | result.second << NotANumber(); |
286 | 293 | } |
287 | - return result; | |
294 | + return std::sqrt(result); | |
288 | 295 | } |
289 | - | |
290 | - template <typename Type> | |
291 | - bool getCovariance(std::list<std::pair<Type, Type>> list, std::pair<Type, Type> &result) { | |
296 | + | |
297 | + bool getCovariance(std::list<std::pair<InputElemType, InputElemType>> &list, OutputElemType &result) { | |
292 | 298 | if (list.empty()) { |
293 | 299 | return false; |
294 | 300 | } |
295 | - std::pair<Type, Type> mean = getMean(list); | |
296 | - | |
301 | + std::pair<OutputElemType,OutputElemType> mean = getMean(list); | |
302 | + result = 0; | |
303 | + int counter =0; | |
304 | + for (auto elt : list) { | |
305 | + if(!isNAN(elt.first) && !isNAN(elt.second)){ | |
306 | + result += (elt.first-mean.first)*(elt.second-mean.second); | |
307 | + counter += 1; | |
308 | + } | |
309 | + } | |
310 | + if(counter != 0) | |
311 | + result /= counter; | |
297 | 312 | return true; |
298 | 313 | } |
299 | - */ | |
300 | - | |
314 | + | |
301 | 315 | OutputElemType computeCorrelation(std::list<std::pair<double, std::pair<InputElemType, InputElemType>>>&mem, OutputElemType& nanVal, std::string type) { |
302 | 316 | OutputElemType result = nanVal; |
303 | 317 | if (mem.empty()) { |
... | ... | @@ -307,16 +321,14 @@ namespace AMDA { |
307 | 321 | for (typename std::list<std::pair<double, std::pair < InputElemType, InputElemType>>>::iterator it = mem.begin(); it != mem.end(); ++it) { |
308 | 322 | list.push_back(it->second); |
309 | 323 | } |
310 | - // getCovariance(list, result); | |
324 | + getCovariance(list, result); | |
311 | 325 | return result; |
312 | 326 | } |
313 | - | |
327 | + | |
314 | 328 | protected: |
315 | 329 | std::string _correlationType; |
316 | 330 | std::list<std::pair<double, std::pair<InputElemType, InputElemType>> > _mem; |
317 | - | |
318 | 331 | }; |
319 | - | |
320 | 332 | } |
321 | 333 | } |
322 | 334 | } | ... | ... |
src/ExternLib/StatisticFunctions/CorrelationProcess.cc
... | ... | @@ -24,6 +24,7 @@ |
24 | 24 | #include "ParameterCreatorFromExpression.hh" |
25 | 25 | #include "CorrelationProcess.hh" |
26 | 26 | #include "StatisticCorrelationCreator.hh" |
27 | +#include "CorrelationFunctions.hh" | |
27 | 28 | using namespace std; |
28 | 29 | using namespace boost; |
29 | 30 | using namespace log4cxx; |
... | ... | @@ -77,17 +78,17 @@ namespace AMDA { |
77 | 78 | } |
78 | 79 | |
79 | 80 | unsigned int CorrelationProcess::write() { |
80 | - //getSecondParamData(); | |
81 | + getSecondParamData(); | |
81 | 82 | return SingleParamProcess::write(); |
82 | 83 | } |
83 | -/* | |
84 | + | |
84 | 85 | void CorrelationProcess::getSecondParamData() { |
85 | 86 | |
86 | 87 | try { |
87 | 88 | ParamDataIndexInfo lParamDataIndexInfo; |
88 | 89 | lParamDataIndexInfo = _secondInputParam->getAsync(this).get(); |
89 | 90 | while ((!lParamDataIndexInfo._noMoreTimeInt && !lParamDataIndexInfo._timeIntToProcessChanged) || (lParamDataIndexInfo._nbDataToProcess > 0)) { |
90 | - //reinterpret_cast<StatisticFunctions::CorrelationBase*> (_operation)->pushSecondParamData(lParamDataIndexInfo); | |
91 | + reinterpret_cast<StatisticFunctions::CorrelationBase*> (_operation)->pushSecondParamData(lParamDataIndexInfo); | |
91 | 92 | if (lParamDataIndexInfo._timeIntToProcessChanged || lParamDataIndexInfo._noMoreTimeInt) |
92 | 93 | break; |
93 | 94 | lParamDataIndexInfo = _secondInputParam->getAsync(this).get(); |
... | ... | @@ -96,6 +97,5 @@ namespace AMDA { |
96 | 97 | throw; |
97 | 98 | } |
98 | 99 | } |
99 | - * **/ | |
100 | 100 | } |
101 | 101 | } | ... | ... |
src/ExternLib/StatisticFunctions/CorrelationProcess.hh
... | ... | @@ -24,7 +24,7 @@ namespace AMDA { |
24 | 24 | public: |
25 | 25 | CorrelationProcess(Parameter ¶meter); |
26 | 26 | CorrelationProcess(const CorrelationProcess& pProcess, Parameter ¶meter); |
27 | - | |
27 | + | |
28 | 28 | virtual ~CorrelationProcess(); |
29 | 29 | /** |
30 | 30 | * @overload Process::establishConnection() |
... | ... | @@ -35,29 +35,31 @@ namespace AMDA { |
35 | 35 | * @overload Process::init() |
36 | 36 | */ |
37 | 37 | TimeStamp init(); |
38 | - | |
38 | + | |
39 | 39 | /* |
40 | 40 | |
41 | 41 | * Write data in dataParam. |
42 | 42 | */ |
43 | - unsigned int write(); | |
43 | + unsigned int write(); | |
44 | + | |
45 | + void getSecondParamData(); | |
44 | 46 | |
45 | 47 | protected: |
46 | - | |
47 | - // void getSecondParamData(); | |
48 | - | |
48 | + | |
49 | + // void getSecondParamData(); | |
50 | + | |
49 | 51 | std::string _type; |
50 | - | |
52 | + | |
51 | 53 | double _windowtime; |
52 | 54 | |
53 | - | |
54 | - private: | |
55 | - /** | |
56 | - * @brief list of param name intput | |
57 | - * @detail this list must be ordered | |
58 | - */ | |
59 | - ParameterSPtr _secondInputParam; | |
60 | - | |
55 | + | |
56 | + private: | |
57 | + /** | |
58 | + * @brief list of param name intput | |
59 | + * @detail this list must be ordered | |
60 | + */ | |
61 | + ParameterSPtr _secondInputParam; | |
62 | + | |
61 | 63 | |
62 | 64 | }; |
63 | 65 | ... | ... |
src/ExternLib/StatisticFunctions/StatisticCorrelationCreator.hh
... | ... | @@ -11,7 +11,7 @@ |
11 | 11 | #include "ParamData.hh" |
12 | 12 | #include "VisitorOfParamData.hh" |
13 | 13 | #include "AMDA_exception.hh" |
14 | -//#include "CorrelationFunctions.hh" | |
14 | +#include "CorrelationFunctions.hh" | |
15 | 15 | |
16 | 16 | namespace AMDA { |
17 | 17 | namespace Parameters { |
... | ... | @@ -169,8 +169,8 @@ namespace AMDA { |
169 | 169 | |
170 | 170 | template <typename Type> |
171 | 171 | void createOperation() { |
172 | - // _operation = new StatisticFunctions::Correlation1D<Type,Type>(_process, _timeIntervalList, dynamic_cast<ParamDataSpec<Type>&>(_firstParamInput), | |
173 | - //dynamic_cast<ParamDataSpec<Type>&>(_secondParamInput),_windowtime, _type); | |
172 | + _operation = new StatisticFunctions::Correlation<Type,Type>(_process, _timeIntervalList, dynamic_cast<ParamDataSpec<Type>&>(_paramData), | |
173 | + dynamic_cast<ParamDataSpec<Type>&>(_secondParamInput),_windowtime, _type); | |
174 | 174 | } |
175 | 175 | |
176 | 176 | template <typename Type> | ... | ... |
src/ExternLib/StatisticFunctions/StatisticFunctionsCreator.hh
src/Parameters/DataTypeMath.hh
... | ... | @@ -1499,6 +1499,38 @@ bool isValid(const std::string& num) { |
1499 | 1499 | flag = false; |
1500 | 1500 | } |
1501 | 1501 | return flag; |
1502 | -} | |
1502 | +} | |
1503 | + | |
1504 | +template <typename Type> | |
1505 | +std::pair<Type, Type> operator-(std::pair<Type, Type> a, std::pair<Type, Type> b) { | |
1506 | + std::pair<Type, Type> c; | |
1507 | + c.first = a.first - b.firest; | |
1508 | + c.second = a.second - b.second; | |
1509 | + return c; | |
1510 | +} | |
1511 | + | |
1512 | +template <typename Type> | |
1513 | +std::pair<Type, Type> operator+(std::pair<Type, Type> a, std::pair<Type, Type> b) { | |
1514 | + std::pair<Type, Type> c; | |
1515 | + c.first = a.first + b.firest; | |
1516 | + c.second = a.second + b.second; | |
1517 | + return c; | |
1518 | +} | |
1519 | + | |
1520 | +template <typename Type> | |
1521 | +std::pair<Type, Type> operator*(std::pair<Type, Type> a, std::pair<Type, Type> b) { | |
1522 | + std::pair<Type, Type> c; | |
1523 | + c.first = a.first* b.firest; | |
1524 | + c.second = a.second* b.second; | |
1525 | + return c; | |
1526 | +} | |
1527 | + | |
1528 | +template <typename Type, typename Type2> | |
1529 | +std::pair<Type, Type> operator/(std::pair<Type, Type> a, Type2 b) { | |
1530 | + std::pair<Type, Type> c; | |
1531 | + c.first = a.first / b; | |
1532 | + c.second = a.second / b; | |
1533 | + return c; | |
1534 | +} | |
1503 | 1535 | |
1504 | 1536 | #endif /* LOGICALDATATYPE_HH_ */ | ... | ... |