|
5 | 5 | #ifndef PWGHF_CORE_HFAETOMSEXICTOPKPI_H_ |
6 | 6 | #define PWGHF_CORE_HFAETOMSEXICTOPKPI_H_ |
7 | 7 |
|
8 | | -#include <vector> |
9 | | - |
10 | 8 | #include "PWGHF/Core/HfMlResponse.h" |
| 9 | + |
| 10 | +#include <vector> |
11 | 11 | namespace o2::analysis |
12 | 12 | { |
13 | | - template <typename TypeOutputScore = float> |
14 | | - class HfAeToMseXicToPKPi : public HfMlResponse<TypeOutputScore> |
15 | | - { |
16 | | - public: |
17 | | - /// Default constructor |
18 | | - HfAeToMseXicToPKPi() = default; |
19 | | - /// Default destructor |
20 | | - virtual ~HfAeToMseXicToPKPi() = default; |
21 | | - |
22 | | - std::vector<float> yScaled, yOutRescaled; |
23 | | - //private : |
24 | | - void setMinMaxScaling(std::vector<float>& yOut, std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax) |
25 | | - { yOut.clear();//initial clear to avoid multiple filling if setMinMax o setScaling are called more than once |
26 | | - for (size_t j = 0; j < yIn.size(); ++j) |
27 | | - { //for over the features |
28 | | - //MinMax scaling of the input features |
29 | | - LOG(debug)<<"--------------> MinMax scaling Debug \t"<<scaleMin.at(j)<<"\t"<<scaleMax.at(j); |
30 | | - yOut.push_back((yIn.at(j) - scaleMin.at(j))/(scaleMax.at(j)- scaleMin.at(j))); |
31 | | - LOG(debug)<<"Feature = "<<j<<" ----> input = "<<yIn.at(j)<<" scaled feature = "<< yOut.at(j); |
32 | | - } |
33 | | - } |
34 | | - //---- External preprocessing scaling |
35 | | - void setScaling(bool scaleFlag, int scaleType, /*input features of a candidate*/ std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax){ //it takes the bool flag and scaling parameters configurables in taskXic |
36 | | - yScaled.clear(); |
37 | | - if( scaleFlag == false){ LOG(debug)<<"No external preprocessing transformation will be applied"; |
38 | | - yScaled.assign(yIn.begin(), yIn.end()); |
39 | | - } else{ |
40 | | - if(scaleType == 1){ |
41 | | - LOG(debug)<<"MinMax scaling will be applied"; |
42 | | - setMinMaxScaling(yScaled, yIn, scaleMin, scaleMax); |
43 | | - }//... with scaleType > 1 we could add other preprocessing trasformations |
44 | | - } |
45 | | - } |
46 | | - std::vector<float> getPreprocessedFeatures(){ |
47 | | - for (size_t j = 0; j < yScaled.size(); ++j) LOG(debug)<<"Global scaled feature = "<< yScaled.at(j); |
48 | | - return yScaled; |
49 | | - } |
50 | | - //Reverse preprocessing - output postprocessing |
51 | | - void unsetMinMaxScaling(std::vector<float>& yOut, std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax) |
52 | | - { yOut.clear();//initial clear to avoid multiple filling if setMinMax o setScaling are called more than once |
53 | | - for (size_t j = 0; j < yIn.size(); ++j) |
54 | | - { //for over the features |
55 | | - //MinMax scaling of the input features |
56 | | - LOG(debug)<<"--------------> MinMax unscaling Debug \t"<<scaleMin.at(j)<<"\t"<<scaleMax.at(j); |
57 | | - yOut.push_back(yIn.at(j)*(scaleMax.at(j)- scaleMin.at(j))+ scaleMin.at(j)); |
58 | | - LOG(debug)<<"Unscaling output = "<<j<<" ----> input = "<<yIn.at(j)<<" rescaled output = "<< yOut.at(j); |
59 | | - } |
60 | | - } |
| 13 | +template <typename TypeOutputScore = float> |
| 14 | +class HfAeToMseXicToPKPi : public HfMlResponse<TypeOutputScore> |
| 15 | +{ |
| 16 | + public: |
| 17 | + /// Default constructor |
| 18 | + HfAeToMseXicToPKPi() = default; |
| 19 | + /// Default destructor |
| 20 | + virtual ~HfAeToMseXicToPKPi() = default; |
| 21 | + |
| 22 | + std::vector<float> yScaled, yOutRescaled; |
| 23 | + // private : |
| 24 | + void setMinMaxScaling(std::vector<float>& yOut, std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax) |
| 25 | + { |
| 26 | + yOut.clear(); // initial clear to avoid multiple filling if setMinMax o setScaling are called more than once |
| 27 | + for (size_t j = 0; j < yIn.size(); ++j) { // for over the features |
| 28 | + // MinMax scaling of the input features |
| 29 | + LOG(debug) << "--------------> MinMax scaling Debug \t" << scaleMin.at(j) << "\t" << scaleMax.at(j); |
| 30 | + yOut.push_back((yIn.at(j) - scaleMin.at(j)) / (scaleMax.at(j) - scaleMin.at(j))); |
| 31 | + LOG(debug) << "Feature = " << j << " ----> input = " << yIn.at(j) << " scaled feature = " << yOut.at(j); |
| 32 | + } |
| 33 | + } |
| 34 | + //---- External preprocessing scaling |
| 35 | + void setScaling(bool scaleFlag, int scaleType, /*input features of a candidate*/ std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax) |
| 36 | + { // it takes the bool flag and scaling parameters configurables in taskXic |
| 37 | + yScaled.clear(); |
| 38 | + if (scaleFlag == false) { |
| 39 | + LOG(debug) << "No external preprocessing transformation will be applied"; |
| 40 | + yScaled.assign(yIn.begin(), yIn.end()); |
| 41 | + } else { |
| 42 | + if (scaleType == 1) { |
| 43 | + LOG(debug) << "MinMax scaling will be applied"; |
| 44 | + setMinMaxScaling(yScaled, yIn, scaleMin, scaleMax); |
| 45 | + } //... with scaleType > 1 we could add other preprocessing trasformations |
| 46 | + } |
| 47 | + } |
| 48 | + std::vector<float> getPreprocessedFeatures() |
| 49 | + { |
| 50 | + for (size_t j = 0; j < yScaled.size(); ++j) |
| 51 | + LOG(debug) << "Global scaled feature = " << yScaled.at(j); |
| 52 | + return yScaled; |
| 53 | + } |
| 54 | + // Reverse preprocessing - output postprocessing |
| 55 | + void unsetMinMaxScaling(std::vector<float>& yOut, std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax) |
| 56 | + { |
| 57 | + yOut.clear(); // initial clear to avoid multiple filling if setMinMax o setScaling are called more than once |
| 58 | + for (size_t j = 0; j < yIn.size(); ++j) { // for over the features |
| 59 | + // MinMax scaling of the input features |
| 60 | + LOG(debug) << "--------------> MinMax unscaling Debug \t" << scaleMin.at(j) << "\t" << scaleMax.at(j); |
| 61 | + yOut.push_back(yIn.at(j) * (scaleMax.at(j) - scaleMin.at(j)) + scaleMin.at(j)); |
| 62 | + LOG(debug) << "Unscaling output = " << j << " ----> input = " << yIn.at(j) << " rescaled output = " << yOut.at(j); |
| 63 | + } |
| 64 | + } |
61 | 65 |
|
62 | | - void unsetScaling(bool scaleFlag, int scaleType, /*AE output*/ std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax){ //it takes the bool flag and scaling parameters configurables in taskXic |
63 | | - yOutRescaled.clear(); |
64 | | - if( scaleFlag == false){ LOG(debug)<<"No external preprocessing transformation will be applied"; |
65 | | - yOutRescaled.assign(yIn.begin(), yIn.end()); |
66 | | - } else{ |
67 | | - if(scaleType == 1){ |
68 | | - LOG(debug)<<"MinMax unscaling will be applied"; |
69 | | - unsetMinMaxScaling(yOutRescaled, yIn, scaleMin, scaleMax); |
70 | | - }//... with scaleType > 1 we could add other preprocessing trasformations |
71 | | - } |
72 | | - } |
73 | | - std::vector<float> getPostprocessedOutput(){ |
74 | | - for (size_t j = 0; j < yOutRescaled.size(); ++j) LOG(debug)<<"Global rescaled AE output = "<< yOutRescaled.at(j); |
75 | | - return yOutRescaled; |
76 | | - } |
77 | | - //---- MSE function |
78 | | - float getMse(std::vector<float> yTrue, std::vector<float> yPred){ |
79 | | - LOG(debug)<<"Inside getMse sizes "<<yTrue.size()<<"\t"<<yPred.size(); |
80 | | - float mse= 0.0f; |
81 | | - float sum = 0.0f; |
82 | | - for (size_t j = 0; j < yTrue.size(); ++j) LOG(debug)<<"Local Feature = "<<j<<" ----> input = "<<yTrue.at(j)<<" scaled feature = "<< yPred.at(j); |
83 | | - std::vector<float> yTrueScaled = getPreprocessedFeatures(); |
84 | | - if( yTrue.size() != yPred.size()){ |
85 | | - LOG(debug)<< "size of input vector ="<<yTrue.size(); |
86 | | - LOG(debug)<< "size of AE output vector ="<< yPred.size(); |
87 | | - LOG(fatal) << "vectors of input and predictions don't have the same size"; |
88 | | - } |
89 | | - else{//MSE |
90 | | - for (size_t j = 0; j < yPred.size(); ++j) { //for over the features |
91 | | - sum += pow(((yTrueScaled).at(j) - (yPred).at(j)), 2); //has dimensions |
92 | | - LOG(debug)<<"getMse Local feature = "<<j<<" ----> input = "<<yTrueScaled.at(j)<<" AE prediction = "<< yPred.at(j); |
93 | | - } |
94 | | - mse = sum/yPred.size(); //MSE of a candidate |
95 | | - LOG(debug)<<"Local mse "<<mse; |
96 | | - } |
97 | | - return mse; |
98 | | - } |
99 | | - }; //end of the class |
100 | | - |
| 66 | + void unsetScaling(bool scaleFlag, int scaleType, /*AE output*/ std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax) |
| 67 | + { // it takes the bool flag and scaling parameters configurables in taskXic |
| 68 | + yOutRescaled.clear(); |
| 69 | + if (scaleFlag == false) { |
| 70 | + LOG(debug) << "No external preprocessing transformation will be applied"; |
| 71 | + yOutRescaled.assign(yIn.begin(), yIn.end()); |
| 72 | + } else { |
| 73 | + if (scaleType == 1) { |
| 74 | + LOG(debug) << "MinMax unscaling will be applied"; |
| 75 | + unsetMinMaxScaling(yOutRescaled, yIn, scaleMin, scaleMax); |
| 76 | + } //... with scaleType > 1 we could add other preprocessing trasformations |
| 77 | + } |
| 78 | + } |
| 79 | + std::vector<float> getPostprocessedOutput() |
| 80 | + { |
| 81 | + for (size_t j = 0; j < yOutRescaled.size(); ++j) |
| 82 | + LOG(debug) << "Global rescaled AE output = " << yOutRescaled.at(j); |
| 83 | + return yOutRescaled; |
| 84 | + } |
| 85 | + //---- MSE function |
| 86 | + float getMse(std::vector<float> yTrue, std::vector<float> yPred) |
| 87 | + { |
| 88 | + LOG(debug) << "Inside getMse sizes " << yTrue.size() << "\t" << yPred.size(); |
| 89 | + float mse = 0.0f; |
| 90 | + float sum = 0.0f; |
| 91 | + for (size_t j = 0; j < yTrue.size(); ++j) |
| 92 | + LOG(debug) << "Local Feature = " << j << " ----> input = " << yTrue.at(j) << " scaled feature = " << yPred.at(j); |
| 93 | + std::vector<float> yTrueScaled = getPreprocessedFeatures(); |
| 94 | + if (yTrue.size() != yPred.size()) { |
| 95 | + LOG(debug) << "size of input vector =" << yTrue.size(); |
| 96 | + LOG(debug) << "size of AE output vector =" << yPred.size(); |
| 97 | + LOG(fatal) << "vectors of input and predictions don't have the same size"; |
| 98 | + } else { // MSE |
| 99 | + for (size_t j = 0; j < yPred.size(); ++j) { // for over the features |
| 100 | + sum += pow(((yTrueScaled).at(j) - (yPred).at(j)), 2); // has dimensions |
| 101 | + LOG(debug) << "getMse Local feature = " << j << " ----> input = " << yTrueScaled.at(j) << " AE prediction = " << yPred.at(j); |
| 102 | + } |
| 103 | + mse = sum / yPred.size(); // MSE of a candidate |
| 104 | + LOG(debug) << "Local mse " << mse; |
| 105 | + } |
| 106 | + return mse; |
| 107 | + } |
| 108 | +}; // end of the class |
101 | 109 |
|
102 | 110 | } // namespace o2::analysis |
103 | 111 | #endif // PWGHF_CORE_HFAETOMSEXICTOPKPI_H_ |
0 commit comments