25
0
mirror of https://github.com/slsdetectorgroup/aare.git synced 2025-05-06 05:00:03 +02:00

python function accepts any shape

This commit is contained in:
froejdh_e 2025-04-22 10:00:42 +02:00
parent d75a870e37
commit c2593c5c15

@ -67,24 +67,23 @@ m.def("adc_sar_04_decode64to16", [](py::array_t<uint8_t> input) {
return output; return output;
}); });
m.def(
"apply_custom_weights",
[](py::array_t<uint16_t, py::array::c_style | py::array::forcecast> &input,
py::array_t<double, py::array::c_style | py::array::forcecast>
&weights) {
m.def("apply_custom_weights", [](py::array_t<uint16_t>& input, py::array_t<double>& weights) {
if (input.ndim() != 1) {
throw std::runtime_error("Only 1D arrays are supported at this moment");
}
// Create a 1D output array with the same shape as the input // Create new array with same shape as the input array (uninitialized values)
std::vector<ssize_t> shape{input.shape(0)}; py::buffer_info buf = input.request();
py::array_t<double> output(shape); py::array_t<double> output(buf.shape);
// Use NDViews to call into the C++ library
auto weights_view = make_view_1d(weights); auto weights_view = make_view_1d(weights);
NDView<uint16_t, 1> input_view(input.mutable_data(), {input.size()});
// Create a view of the input and output arrays NDView<double, 1> output_view(output.mutable_data(), {output.size()});
NDView<uint16_t, 1> input_view(input.mutable_data(), {input.shape(0)});
NDView<double, 1> output_view(output.mutable_data(), {output.shape(0)});
apply_custom_weights(input_view, output_view, weights_view); apply_custom_weights(input_view, output_view, weights_view);
return output; return output;
}); });
@ -104,8 +103,7 @@ m.def("apply_custom_weights", [](py::array_t<uint16_t>& input, py::array_t<doubl
// always read bytes // always read bytes
image = py::array_t<uint8_t>(shape); image = py::array_t<uint8_t>(shape);
self.read_into( self.read_into(reinterpret_cast<std::byte *>(image.mutable_data()),
reinterpret_cast<std::byte *>(image.mutable_data()),
header.mutable_data()); header.mutable_data());
return py::make_tuple(header, image); return py::make_tuple(header, image);