Commit e8db9c264c1682cb41cdadc543f1cf3ad83b2c05

Authored by Hacene SI HADJ MOHAND
1 parent 8dd4b7f3

structure ok

src/ExternLib/StatisticCorrelation/AMDAPlugin.cc deleted
... ... @@ -1,35 +0,0 @@
1   -// Standard libraries include files
2   -//-----------------------------------------------------------------------------
3   -#include <iostream>
4   -
5   -// Include oriented definitions
6   -//-----------------------------------------------------------------------------
7   -
8   -// Module Kernel include files
9   -//-----------------------------------------------------------------------------
10   -
11   -#include "CorrelationProcess.hh"
12   -#include "ServicesServer.hh"
13   -#include "PluginManager.hh"
14   -
15   -using namespace AMDA::Parameters;
16   -
17   -// Other modules include files
18   -//-----------------------------------------------------------------------------
19   -
20   -/**
21   - Retrieve the Plugin version we're going to expect
22   - */
23   -extern "C" const char* getPluginVersion() {
24   - return "(Version)";
25   -}
26   -
27   -/**
28   - Tells us to register our functionality to an engine kernel
29   - */
30   -extern "C" void registerPlugin(AMDA::Plugins::PluginManager & pm) {
31   - std::string pluginPath=pm.getCurrentPluginPath();
32   - ProcessFactory factProcessCorrelation = boost::factory<CorrelationProcess*>();
33   - ServicesServer::getInstance()->addProcessFactory("statCorrelation", factProcessCorrelation);
34   - ServicesServer::getInstance()->linkProcessWithPlugin("statCorrelation", pluginPath);
35   - }
src/ExternLib/StatisticCorrelation/CMakeLists.txt deleted
... ... @@ -1,36 +0,0 @@
1   -
2   -PROJECT(statisticCorrelation)
3   -
4   -set(LIBRARY_OUTPUT_PATH ${PLUGIN_OUTPUT_PATH})
5   -
6   -include_directories(
7   - ${CMAKE_HOME_DIRECTORY}/src/InternLib/
8   - ${CMAKE_HOME_DIRECTORY}/src/Common/
9   - ${CMAKE_HOME_DIRECTORY}/src/Parameters/
10   - ${CMAKE_HOME_DIRECTORY}/src/Plugins/
11   - ${CMAKE_HOME_DIRECTORY}/src/helpers/
12   - ${CMAKE_HOME_DIRECTORY}/src/TimeTableCatalog/
13   - ${CMAKE_HOME_DIRECTORY}/src/Info/
14   - ${LOG4CXX_INCLUDE_DIR}
15   - ${Boost_INCLUDE_DIR}
16   -)
17   -
18   -#Library configuration
19   -file(
20   - GLOB_RECURSE
21   - source_files
22   - ./*
23   -)
24   -
25   -ADD_LIBRARY( statisticCorrelation SHARED ${source_files} )
26   -
27   -
28   -target_link_libraries(
29   - statisticCorrelation
30   - ${LOG4CXX_LIBRARIES}
31   - Parameters
32   - InternLib
33   - TimeTableCatalog
34   - TimeUtil
35   - Info
36   -)
src/ExternLib/StatisticFunctions/AMDAPlugin.cc
... ... @@ -9,6 +9,7 @@
9 9 //-----------------------------------------------------------------------------
10 10  
11 11 #include "ProcessStatisticFunctions.hh"
  12 +#include "CorrelationProcess.hh"
12 13 #include "ServicesServer.hh"
13 14 #include "PluginManager.hh"
14 15  
... ... @@ -28,7 +29,7 @@ extern &quot;C&quot; const char* getPluginVersion() {
28 29 Tells us to register our functionality to an engine kernel
29 30 */
30 31 extern "C" void registerPlugin(AMDA::Plugins::PluginManager & pm) {
31   - std::string pluginPath=pm.getCurrentPluginPath();
  32 + std::string pluginPath = pm.getCurrentPluginPath();
32 33 ProcessFactory factProcessMinFunc = boost::factory<ProcessMinFunc*>();
33 34 ServicesServer::getInstance()->addProcessFactory("min", factProcessMinFunc);
34 35 ServicesServer::getInstance()->linkProcessWithPlugin("min", pluginPath);
... ... @@ -72,4 +73,9 @@ extern &quot;C&quot; void registerPlugin(AMDA::Plugins::PluginManager &amp; pm) {
72 73 ProcessFactory factProcessMedianFunc = boost::factory<ProcessMedianFunc*>();
73 74 ServicesServer::getInstance()->addProcessFactory("median", factProcessMedianFunc);
74 75 ServicesServer::getInstance()->linkProcessWithPlugin("median", pluginPath);
  76 +
  77 + std::string pluginPath = pm.getCurrentPluginPath();
  78 + ProcessFactory factProcessCorrelation = boost::factory<CorrelationProcess*>();
  79 + ServicesServer::getInstance()->addProcessFactory("statCorrelation", factProcessCorrelation);
  80 + ServicesServer::getInstance()->linkProcessWithPlugin("statCorrelation", pluginPath);
75 81 }
... ...
src/ExternLib/StatisticFunctions/CorrelationFunctions.hh 0 โ†’ 100644
... ... @@ -0,0 +1,316 @@
  1 +/*
  2 + * To change this license header, choose License Headers in Project Properties.
  3 + * To change this template file, choose Tools | Templates
  4 + * and open the template in the editor.
  5 + */
  6 +
  7 +/*
  8 + * File: CorrelationFunctions.hh
  9 + * Author: hacene
  10 + *
  11 + * Created on September 27, 2021, 3:35 PM
  12 + */
  13 +
  14 +#ifndef CORRELATIONFUNCTIONS_HH
  15 +#define CORRELATIONFUNCTIONS_HH
  16 +
  17 +#include "Parameter.hh"
  18 +#include "ParamData.hh"
  19 +#include "DataTypeMath.hh"
  20 +#include "Operation.hh"
  21 +
  22 +#include <iterator>
  23 +#include <c++/4.8.2/bits/stl_vector.h>
  24 +#include <c++/4.8.2/bits/stl_pair.h>
  25 +#include "AbstractCorrelationFunc.hh"
  26 +
  27 +namespace AMDA {
  28 + namespace Parameters {
  29 + namespace StatisticFunctions {
  30 +
  31 +#define AVERAGE_TIME 1200 // (seconds)
  32 +#define MAX_GAP_SIZE 3600 // (seconds)
  33 +
  34 + class CorrelationBase{
  35 + public:
  36 + void pushSecondParamData(ParamDataIndexInfo &pParamDataIndexInfo) = 0;
  37 + };
  38 +
  39 + template <typename InputElemType, typename OutputElemType>
  40 + class AbstractCorrelationFunc : public AbstractCorrelationFuncBase, public CorrelationBase{
  41 + public:
  42 +
  43 + /**
  44 + * @brief Constructor.
  45 + * @details Create the ParamData type of the input ParamData.
  46 + */
  47 + AbstractCorrelationFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& firstParamInput, ParamDataSpec<InputElemType>& secondParamInput, double windowtime, std::string type)
  48 + : AbstractCorrelationFuncBase(pProcess, pTimeIntervalList, windowtime),
  49 + _firstParamInput(firstParamInput),
  50 + _secondParamInput(secondParamInput),
  51 + _type(type),
  52 + _paramOutput(new ParamDataSpec<OutputElemType>) {
  53 + _paramDataOutput = _paramOutput;
  54 + }
  55 +
  56 + virtual ~AbstractCorrelationFunc() {
  57 + }
  58 +
  59 + virtual void pushData(double time, std::pair<InputElemType, InputElemType>& elem) = 0;
  60 +
  61 + virtual OutputElemType compute() = 0;
  62 +
  63 + void pushSecondParamData(ParamDataIndexInfo &pParamDataIndexInfo) {
  64 + for (unsigned int _index = pParamDataIndexInfo._startIndex;
  65 + _index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
  66 + ++_index) {
  67 + double time = _paramImfInput.getTime(_index);
  68 + InputElemType val_ = _secondParamInput.get(_index);
  69 + _secondParamInputData.push_back(std::pair<double, InputElemType> val_);
  70 +
  71 + }
  72 + }
  73 +
  74 + virtual InputElemType getValue(std::vector<std::pair<double, InputElemType> >& input, double time) = 0;
  75 +
  76 + /**
  77 + * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
  78 + */
  79 +
  80 + void write(ParamDataIndexInfo &pParamDataIndexInfo) {
  81 + if ((pParamDataIndexInfo._nbDataToProcess > 0)) {
  82 + if (pParamDataIndexInfo._startIndex == 0) {
  83 + _nanVal = _firstParamInput.get(0);
  84 + _nanVal << NotANumber();
  85 + }
  86 + for (unsigned int _index = pParamDataIndexInfo._startIndex;
  87 + _index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
  88 + ++_index) {
  89 + double crtTime = _firstParamInput.getTime(_index);
  90 + InputElemType firstVal = _firstParamInput.get(_index);
  91 + // get the second element
  92 + InputElemType secondVal = getValue(_secondParamInputData, crtTime);
  93 + std::pair<InputElemType, InputElemType> crtVal(firstVal, secondVal);
  94 +
  95 + if (needToChangeTarget(crtTime)) {
  96 + _paramOutput->pushTime(getTarget());
  97 + _paramOutput->push(compute());
  98 + pushData(crtTime, crtVal);
  99 + nextTarget();
  100 + bool skip = false;
  101 + while (!skip && needToChangeTarget(crtTime)) {
  102 + _paramOutput->pushTime(getTarget());
  103 + _paramOutput->push(compute());
  104 + skip = nextTarget();
  105 + }
  106 + } else {
  107 + pushData(crtTime, crtVal);
  108 + if (needInit()) {
  109 + init();
  110 + }
  111 + }
  112 + }
  113 + }
  114 + if (pParamDataIndexInfo._timeIntToProcessChanged || pParamDataIndexInfo._noMoreTimeInt) {
  115 + if (!needInit()) {
  116 + do {
  117 + if (inInt(getTarget())) {
  118 + _paramOutput->pushTime(getTarget());
  119 + _paramOutput->push(compute());
  120 + }
  121 + } while (nextTarget());
  122 + }
  123 + }
  124 + }
  125 +
  126 + double getInputParamSampling() {
  127 + return _firstParamInput.getMinSampling();
  128 + }
  129 +
  130 + private:
  131 + ParamDataSpec<InputElemType>& _firstParamInput;
  132 +
  133 + ParamDataSpec<InputElemType>& _secondParamInput;
  134 +
  135 + ParamDataSpec<OutputElemType>* _paramOutput;
  136 +
  137 + std::vector<std::pair<double, InputElemType> > _secondParamInputData;
  138 +
  139 + protected:
  140 + OutputElemType _nanVal;
  141 + std::string _type;
  142 + std::list<std::pair<double, std::pair<InputElemType, InputElemType>> > _mem;
  143 + };
  144 +
  145 + /**
  146 + *
  147 + * @param pProcess
  148 + * @param pTimeIntervalList
  149 + * @param firstParamInput
  150 + * @param secondParamInput
  151 + * @param windowtime
  152 + * @param type
  153 + */
  154 + template <typename InputElemType, typename OutputElemType>
  155 + class Correlation1D : public AbstractCorrelationFunc<ParamDataSpec<InputElemType>, OutputElemType> {
  156 + public:
  157 +
  158 + Correlation1D(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>
  159 + & firstParamInput, ParamDataSpec<InputElemType>& secondParamInput, double windowtime, std::string type) :
  160 + AbstractCorrelationFunc(pProcess, pTimeIntervalList, firstParamInput, secondParamInput, windowtime, type) {
  161 +
  162 + }
  163 +
  164 + virtual ~Correlation1D() {
  165 + }
  166 +
  167 + virtual void init() {
  168 + AbstractCorrelationFunc<InputElemType, OutputElemType>::setTarget(AbstractCorrelationFunc<InputElemType, OutputElemType>::getIntStartTime());
  169 + AbstractCorrelationFunc<InputElemType, OutputElemType>::setNeedInit(false);
  170 + }
  171 +
  172 + virtual bool nextTarget() {
  173 + double target = AbstractCorrelationFunc<InputElemType, OutputElemType>::getTarget() + AbstractCorrelationFunc<InputElemType, OutputElemType>::getWindowTime();
  174 + bool res = AbstractCorrelationFunc<InputElemType, OutputElemType>::setTarget(target);
  175 + while (!_mem.empty() && !AbstractCorrelationFunc<InputElemType, OutputElemType>::inWindow(_mem.front().first)) {
  176 + _mem.pop_front();
  177 + }
  178 + return res;
  179 + }
  180 +
  181 + virtual bool needToChangeTarget(double crtTime) {
  182 + return !AbstractCorrelationFunc<InputElemType, OutputElemType>::needInit() && !AbstractCorrelationFunc<InputElemType, OutputElemType>::inWindow(crtTime);
  183 + }
  184 +
  185 + virtual double getSampling() {
  186 + return AbstractCorrelationFunc<InputElemType, OutputElemType>::getWindowTime();
  187 + }
  188 +
  189 + virtual void pushData(double time, std::pair<InputElemType, InputElemType>& elem) {
  190 + _mem.push_back(std::make_pair(time, elem));
  191 + }
  192 +
  193 + virtual void resetFunc() {
  194 + _mem.clear();
  195 + }
  196 +
  197 + InputElemType getValue(std::vector<std::pair<double, InputElemType> >& input, double time) {
  198 + double min_t = time - AVERAGE_TIME / 2.;
  199 + double max_t = time + AVERAGE_TIME / 2.;
  200 + std::vector<std::pair<double, InputElemType> > values_for_mean;
  201 + InputElemType nanVal = input[0]->second;
  202 + nanVal << NotANumber();
  203 + std::pair<double, InputElemType> prev_value(NAN, nanVal);
  204 + std::pair<double, InputElemType> next_value(NAN, nanVal);
  205 + for (std::vector<std::pair<double, InputElemType> >::iterator it = input.begin(); it != input.end(); ++it) {
  206 + if (isNAN(it->second))
  207 + continue;
  208 + else if (it->first > max_t) {
  209 + next_value = *it;
  210 + break;
  211 + } else if (it->first < min_t) {
  212 + prev_value = *it;
  213 + } else {
  214 + values_for_mean.push_back(*it);
  215 + }
  216 + }
  217 +
  218 + InputElemType value = nanVal;
  219 + if (!values_for_mean.empty()) {
  220 + //Compute mean
  221 + InputElemType sum = 0;
  222 + for (std::vector<std::pair<double, InputElemType> >::iterator it = values_for_mean.begin(); it != values_for_mean.end(); ++it) {
  223 + sum += it->second;
  224 + }
  225 + value = sum / (InputElemType) values_for_mean.size();
  226 + } else {
  227 + if (!isNAN(prev_value.first) && !isNAN(next_value.first) && (next_value.first - prev_value.first <= MAX_GAP_SIZE)) {
  228 + //Compute interpolated value
  229 + value = prev_value.second + (time - prev_value.first) / (next_value.first - prev_value.first) * (next_value.second - prev_value.second);
  230 + }
  231 + }
  232 +
  233 + return value;
  234 + }
  235 +
  236 + OutputElemType compute() {
  237 + return computeCorrelation(ClassicAbstractFunc<InputElemType, OutputElemType>::_mem, ClassicAbstractFunc<InputElemType, OutputElemType>::_nanVal, _type);
  238 + }
  239 + };
  240 +
  241 + template <typename Type>
  242 + std::pair<Type, Type> getMean(std::list<std::pair<Type, Type>> &list) {
  243 + std::pair<Type, Type> result(0, 0);
  244 + std::pair<int, int> counter(0,0);
  245 + for (int i = 0; i < 2; i++) {
  246 + for (auto elt : list) {
  247 + if (!isNan(std::get<i>(elt))) {
  248 + std::get<i>(result) += std::get<i>(elt);
  249 + std::get<i>(counter) += 1;
  250 + }
  251 + }
  252 + if(std::get<i>(counter) != 0)
  253 + std::get<i>(result) /= std::get<i>(counter);
  254 + return result;
  255 + }
  256 + }
  257 +
  258 + template <typename Type>
  259 + std::pair<Type, Type> getStd(std::list<std::pair<Type, Type>> &list) {
  260 + std::pair<Type, Type> mean = getMean(list);
  261 + std::pair<Type, Type> result(0, 0);
  262 + int counter1 = 0;
  263 + int counter2 = 0;
  264 + for (auto elt : list) {
  265 + if (!isNan(elt->first)) {
  266 + result.first += (elt->first - mean.first)*(elt->first - mean.first);
  267 + counter1 += 1;
  268 + }
  269 + if (!isNan(elt->second)) {
  270 + result.second += (elt->second - mean.second)*(elt->second - mean.second);
  271 + counter2 += 1;
  272 + }
  273 + }
  274 + if (counter1 != 0) {
  275 + result.first /= counter1;
  276 + } else {
  277 + result.first << NotANumber();
  278 + }
  279 + if (counter2 != 0) {
  280 + result.second /= counter2;
  281 + } else {
  282 + result.second << NotANumber();
  283 + }
  284 + return result;
  285 + }
  286 +
  287 + template <typename Type>
  288 + bool getCovariance(std::list<std::pair<Type, Type>> list, std::pair<Type, Type> &result) {
  289 + if (list.empty()) {
  290 + return false;
  291 + }
  292 + std::pair<Type, Type> mean = getMean(list);
  293 +
  294 + return true;
  295 + }
  296 +
  297 + template <typename InputElemType, typename OutputElemType>
  298 + OutputElemType computeCorrelation(std::list<std::pair<double, std::pair<InputElemType, InputElemType>>>&mem, OutputElemType& nanVal, std::string type) {
  299 + OutputElemType result = nanVal;
  300 + if (mem.empty()) {
  301 + return result;
  302 + }
  303 + std::list<std::pair<InputElemType, InputElemType>> list;
  304 + for (typename std::list<std::pair<double, std::pair < InputElemType, InputElemType>>>::iterator it = mem.begin(); it != mem.end(); ++it) {
  305 + list.push_back(it->second);
  306 + }
  307 + getCovariance(list, result);
  308 + return result;
  309 + }
  310 +
  311 + }
  312 + }
  313 +}
  314 +
  315 +#endif /* CORRELATIONFUNCTIONS_HH */
  316 +
... ...
src/ExternLib/StatisticCorrelation/CorrelationProcess.cc renamed to src/ExternLib/StatisticFunctions/CorrelationProcess.cc
... ... @@ -23,6 +23,7 @@
23 23 #include "ParamMgr.hh"
24 24 #include "ParameterCreatorFromExpression.hh"
25 25 #include "CorrelationProcess.hh"
  26 +#include "CorrelationFunctions.hh"
26 27 using namespace std;
27 28 using namespace boost;
28 29 using namespace log4cxx;
... ... @@ -77,19 +78,19 @@ namespace AMDA {
77 78 }
78 79  
79 80 void CorrelationProcess::getSecondParamData() {
80   - /**
  81 +
81 82 try {
82 83 ParamDataIndexInfo lParamDataIndexInfo;
83 84 lParamDataIndexInfo = _secondInputParam->getAsync(this).get();
84 85 while ((!lParamDataIndexInfo._noMoreTimeInt && !lParamDataIndexInfo._timeIntToProcessChanged) || (lParamDataIndexInfo._nbDataToProcess > 0)) {
85   - reinterpret_cast<XXX_className*> (_operation)->pushSecondParamData(lParamDataIndexInfo);
  86 + reinterpret_cast<StatisticFunctions::CorrelationBase*> (_operation)->pushSecondParamData(lParamDataIndexInfo);
86 87 if (lParamDataIndexInfo._timeIntToProcessChanged || lParamDataIndexInfo._noMoreTimeInt)
87 88 break;
88 89 lParamDataIndexInfo = _secondInputParam->getAsync(this).get();
89 90 }
90 91 } catch (...) {
91 92 throw;
92   - }*/
  93 + }
93 94 }
94 95 }
95 96 }
... ...
src/ExternLib/StatisticCorrelation/CorrelationProcess.hh renamed to src/ExternLib/StatisticFunctions/CorrelationProcess.hh
... ... @@ -45,7 +45,7 @@ namespace AMDA {
45 45 protected:
46 46  
47 47 void getSecondParamData();
48   -
  48 +
49 49 std::string _type;
50 50  
51 51 double _windowtime;
... ...
src/ExternLib/StatisticCorrelation/StatisticCorrelationCreator.hh renamed to src/ExternLib/StatisticFunctions/StatisticCorrelationCreator.hh
... ... @@ -12,6 +12,7 @@
12 12 #include "VisitorOfParamData.hh"
13 13 #include "SumIntoTableIndexes.hh"
14 14 #include "AMDA_exception.hh"
  15 +#include "CorrelationFunctions.hh"
15 16  
16 17 namespace AMDA {
17 18 namespace Parameters {
... ... @@ -36,35 +37,36 @@ namespace AMDA {
36 37 * @overload VisitorOfParamData::visit(ParamDataScalaireShort *)
37 38 */
38 39 void visit(ParamDataScalaireShort *) {
39   - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("Tsyganenko96Creator operation not supported"));
  40 + createOperation<short>();
40 41 }
41 42  
42 43 /**
43 44 * @overload VisitorOfParamData::visit(ParamDataScalaireFloat *)
44 45 */
45 46 void visit(ParamDataScalaireFloat *) {
46   - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("Tsyganenko96Creator operation not supported"));
  47 + createOperation<float>();
47 48 }
48 49  
49 50 /**
50 51 * @overload VisitorOfParamData::visit(ParamDataScalaireDouble *)
51 52 */
52 53 void visit(ParamDataScalaireDouble *) {
53   - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("Tsyganenko96Creator operation not supported"));
  54 + createOperation<double>();
  55 +
54 56 }
55 57  
56 58 /**
57 59 * @overload VisitorOfParamData::visit(ParamDataScalaireLongDouble *)
58 60 */
59 61 void visit(ParamDataScalaireLongDouble *) {
60   - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("Tsyganenko96Creator operation not supported"));
  62 + createOperation<long double>();
61 63 }
62 64  
63 65 /**
64 66 * @overload VisitorOfParamData::visit(ParamDataScalaireInt *)
65 67 */
66 68 void visit(ParamDataScalaireInt *) {
67   - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("Tsyganenko96Creator operation not supported"));
  69 + createOperation<int>()
68 70 }
69 71  
70 72 /**
... ... @@ -169,9 +171,15 @@ namespace AMDA {
169 171 template <typename Type>
170 172 void createOperation1D() {
171 173 }
  174 +
  175 + template <typename Type>
  176 + void createOperation1D() {
  177 + _operation = new StatisticFunctions::Correlation1D<Type,Type>(_process, _timeIntervalList, dynamic_cast<ParamDataSpec<Type>&>(_firstParamInput),
  178 + dynamic_cast<ParamDataSpec<Type>&>(_secondParamInput),_windowtime, _type);
  179 + }
172 180  
173 181 Process &_process;
174   - ParamData &_paramData;
  182 + ParamData & _firstParamInput;
175 183 ParamData &_secondParamInput;
176 184 double _windowtime;
177 185 std::string _type;
... ...