mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-04-29 09:50:02 +02:00
precalculate weights for arrays
This commit is contained in:
parent
3760fd5ed0
commit
86311bcfc0
@ -68,7 +68,7 @@ m.def("adc_sar_04_decode64to16", [](py::array_t<uint8_t> input) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
m.def("apply_custom_weights", [](py::array_t<uint16_t> input, py::array_t<double> weights) {
|
m.def("apply_custom_weights", [](py::array_t<uint16_t>& input, py::array_t<double>& weights) {
|
||||||
if (input.ndim() != 2) {
|
if (input.ndim() != 2) {
|
||||||
throw std::runtime_error("Only 2D arrays are supported at this moment");
|
throw std::runtime_error("Only 2D arrays are supported at this moment");
|
||||||
}
|
}
|
||||||
|
@ -81,9 +81,22 @@ void apply_custom_weights(NDView<uint16_t, 2> input, NDView<double, 2> output, c
|
|||||||
if(input.shape() != output.shape()){
|
if(input.shape() != output.shape()){
|
||||||
throw std::invalid_argument(LOCATION + " input and output shapes must match");
|
throw std::invalid_argument(LOCATION + " input and output shapes must match");
|
||||||
}
|
}
|
||||||
for (int64_t i = 0; i < input.shape(0); i++) {
|
|
||||||
for (int64_t j = 0; j < input.shape(1); j++) {
|
//Calculate weights to avoid repeatedly calling std::pow
|
||||||
output(i, j) = apply_custom_weights(input(i, j), weights);
|
std::vector<double> weights_powers(weights.size());
|
||||||
|
for (ssize_t i = 0; i < weights.size(); ++i) {
|
||||||
|
weights_powers[i] = std::pow(weights[i], i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply custom weights to each element in the input array
|
||||||
|
for (ssize_t i = 0; i < input.shape(0); i++) {
|
||||||
|
for (ssize_t j = 0; j < input.shape(1); j++) {
|
||||||
|
|
||||||
|
double result = 0.0;
|
||||||
|
for (ssize_t bit_index = 0; bit_index < weights_powers.size(); ++bit_index) {
|
||||||
|
result += ((input(i,j) >> bit_index) & 1) * weights_powers[bit_index];
|
||||||
|
}
|
||||||
|
output(i,j) = result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user