|
| 1 | +/// \file HfAeToMseXicToPKPi.h |
| 2 | +/// \brief Class to compute the mse for the Autoencoder for Xic+ → p K- π+ analysis selections |
| 3 | +/// \author Maria Teresa Camerlingo |
| 4 | + |
| 5 | +#ifndef PWGHF_CORE_HFAETOMSEXICTOPKPI_H_ |
| 6 | +#define PWGHF_CORE_HFAETOMSEXICTOPKPI_H_ |
| 7 | + |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#include "PWGHF/Core/HfMlResponse.h" |
| 11 | +namespace o2::analysis |
| 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 | + } |
| 61 | + |
| 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 | + |
| 101 | + |
| 102 | +} // namespace o2::analysis |
| 103 | +#endif // PWGHF_CORE_HFAETOMSEXICTOPKPI_H_ |
0 commit comments