Project
Loading...
Searching...
No Matches
Processors.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
16
17#include "Processors.h"
18
19#include <cmath>
20
21using namespace o2::zdc::fastsim::processors;
22
23std::optional<std::vector<float>> StandardScaler::scale(const std::vector<float>& data) const
24{
25 if (data.size() != mMeans.size()) {
26 return std::nullopt;
27 }
28
29 std::vector<float> scaledData(mMeans.size(), 0);
30 for (size_t i = 0; i < mMeans.size(); ++i) {
31 scaledData.at(i) = (data[i] - mMeans[i]) / mScales[i];
32 }
33
34 return scaledData;
35}
36
37std::optional<std::vector<std::vector<float>>> StandardScaler::scale_batch(
38 const std::vector<std::vector<float>>& data) const
39{
40
41 std::vector<std::vector<float>> scaledData;
42 for (auto& particleData : data) {
43 if (particleData.size() != mMeans.size()) {
44 return std::nullopt;
45 }
46 std::vector<float> scaledParticleData(mMeans.size(), 0);
47 for (size_t i = 0; i < mMeans.size(); ++i) {
48 scaledParticleData.at(i) = (particleData[i] - mMeans[i]) / mScales[i];
49 }
50 scaledData.emplace_back(std::move(scaledParticleData));
51 }
52 return scaledData;
53}
54
55bool StandardScaler::setScales(const std::vector<float>& means, const std::vector<float>& scales)
56{
57 if (means.size() != scales.size()) {
58 return false;
59 }
60 mMeans = means;
61 mScales = scales;
62 return true;
63}
64
65std::vector<int> o2::zdc::fastsim::processors::readClassifier(const Ort::Value& value, const size_t batchSize)
66{
67 std::vector<int> results;
68 auto predictedClasses = value.GetTensorData<float>();
69 for (size_t i = 0; i < batchSize; ++i) {
70 results.emplace_back(std::round(*(i + predictedClasses)));
71 }
72 return std::move(results);
73}
74
75std::vector<std::array<long, 5>> o2::zdc::fastsim::processors::calculateChannels(const Ort::Value& value,
76 const size_t batchSize)
77{
78 std::vector<std::array<long, 5>> results; // results vector
79 std::array<float, 5> channels = {0}; // 5 photon channels
80 auto flattedImageVector = value.GetTensorData<float>(); // Converts Ort::Value to flat const float*
81
82 for (size_t batch = 0; batch < batchSize; ++batch) {
83 // Model output needs to be converted with exp(x)-1 function to be valid
84 for (int i = 0; i < 44; i++) {
85 for (int j = 0; j < 44; j++) {
86 if (i % 2 == j % 2) {
87 if (i < 22 && j < 22) {
88 channels[0] += std::expm1(flattedImageVector[j + i * 44 + (batch * 44 * 44)]);
89 } else if (i < 22 && j >= 22) {
90 channels[1] += std::expm1(flattedImageVector[j + i * 44 + (batch * 44 * 44)]);
91 } else if (i >= 22 && j < 22) {
92 channels[2] += std::expm1(flattedImageVector[j + i * 44 + (batch * 44 * 44)]);
93 } else if (i >= 22 && j >= 22) {
94 channels[3] += std::expm1(flattedImageVector[j + i * 44 + (batch * 44 * 44)]);
95 }
96 } else {
97 channels[4] += std::expm1(flattedImageVector[j + i * 44 + (batch * 44 * 44)]);
98 }
99 }
100 }
101 results.emplace_back(std::array<long, 5>{0});
102 for (int ch = 0; ch < 5; ++ch) {
103 results.back()[ch] = std::lround(channels[ch]);
104 channels[ch] = 0;
105 }
106 }
107 return std::move(results);
108}
int32_t i
uint32_t j
Definition RawData.h:0
bool setScales(const std::vector< float > &means, const std::vector< float > &scales)
Sets scales for standard scaler. Checks if sizes of scales are equal.
std::optional< std::vector< float > > scale(const std::vector< float > &data) const
Scales data with standard scale algorithm.
std::optional< std::vector< std::vector< float > > > scale_batch(const std::vector< std::vector< float > > &data) const
Scales batch of data with standard scale algorithm.
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLboolean * data
Definition glcorearb.h:298
std::vector< int > readClassifier(const Ort::Value &value, size_t batchSize)
Reads predicted class as int.
std::vector< std::array< long, 5 > > calculateChannels(const Ort::Value &value, size_t batchSize)
Calculate 5 channels values from 44x44 float array (for every batch)
std::vector< ChannelData > channels