python function accepts any shape
All checks were successful
Build on RHEL9 / build (push) Successful in 2m1s
Build on RHEL8 / build (push) Successful in 2m12s

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

View File

@ -67,56 +67,54 @@ 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);
auto weights_view = make_view_1d(weights); // Use NDViews to call into the C++ library
auto weights_view = make_view_1d(weights);
NDView<uint16_t, 1> input_view(input.mutable_data(), {input.size()});
NDView<double, 1> output_view(output.mutable_data(), {output.size()});
// Create a view of the input and output arrays apply_custom_weights(input_view, output_view, weights_view);
NDView<uint16_t, 1> input_view(input.mutable_data(), {input.shape(0)}); return output;
NDView<double, 1> output_view(output.mutable_data(), {output.shape(0)}); });
apply_custom_weights(input_view, output_view, weights_view); py::class_<CtbRawFile>(m, "CtbRawFile")
.def(py::init<const std::filesystem::path &>())
.def("read_frame",
[](CtbRawFile &self) {
size_t image_size = self.image_size_in_bytes();
py::array image;
std::vector<ssize_t> shape;
shape.reserve(2);
shape.push_back(1);
shape.push_back(image_size);
return output; py::array_t<DetectorHeader> header(1);
});
py::class_<CtbRawFile>(m, "CtbRawFile") // always read bytes
.def(py::init<const std::filesystem::path &>()) image = py::array_t<uint8_t>(shape);
.def("read_frame",
[](CtbRawFile &self) {
size_t image_size = self.image_size_in_bytes();
py::array image;
std::vector<ssize_t> shape;
shape.reserve(2);
shape.push_back(1);
shape.push_back(image_size);
py::array_t<DetectorHeader> header(1); self.read_into(reinterpret_cast<std::byte *>(image.mutable_data()),
header.mutable_data());
// always read bytes return py::make_tuple(header, image);
image = py::array_t<uint8_t>(shape); })
.def("seek", &CtbRawFile::seek)
.def("tell", &CtbRawFile::tell)
.def("master", &CtbRawFile::master)
self.read_into( .def_property_readonly("image_size_in_bytes",
reinterpret_cast<std::byte *>(image.mutable_data()), &CtbRawFile::image_size_in_bytes)
header.mutable_data());
return py::make_tuple(header, image); .def_property_readonly("frames_in_file", &CtbRawFile::frames_in_file);
})
.def("seek", &CtbRawFile::seek)
.def("tell", &CtbRawFile::tell)
.def("master", &CtbRawFile::master)
.def_property_readonly("image_size_in_bytes",
&CtbRawFile::image_size_in_bytes)
.def_property_readonly("frames_in_file", &CtbRawFile::frames_in_file);
} }