This commit is contained in:
froejdh_e
2024-12-10 17:21:05 +01:00
parent b43003966f
commit 6a150e8d98
7 changed files with 268 additions and 98 deletions

View File

@ -1,4 +1,5 @@
#include "aare/ClusterFinder.hpp"
#include "aare/ClusterVector.hpp"
#include "aare/NDView.hpp"
#include "aare/Pedestal.hpp"
#include "np_helper.hpp"
@ -9,30 +10,98 @@
#include <pybind11/stl.h>
namespace py = pybind11;
using pd_type = double;
template <typename T>
void define_cluster_vector(py::module &m, const std::string &typestr) {
auto class_name = fmt::format("ClusterVector_{}", typestr);
py::class_<ClusterVector<T>>(m, class_name.c_str(), py::buffer_protocol())
.def(py::init<int, int>())
.def_property_readonly("size", &ClusterVector<T>::size)
.def("element_offset",
py::overload_cast<>(&ClusterVector<T>::element_offset, py::const_))
.def_property_readonly("fmt",
[typestr](ClusterVector<T> &v) {
return fmt::format(
"i:x:\ni:y:\n({},{}){}:data:", v.cluster_size_x(),
v.cluster_size_y(), typestr);
})
.def("sum", &ClusterVector<T>::sum)
.def_buffer([typestr](ClusterVector<T> &v) -> py::buffer_info {
return py::buffer_info(
v.data(), /* Pointer to buffer */
v.element_offset(), /* Size of one scalar */
fmt::format("i:x:\ni:y:\n({},{}){}:data:", v.cluster_size_x(),
v.cluster_size_y(),
typestr), /* Format descriptor */
1, /* Number of dimensions */
{v.size()}, /* Buffer dimensions */
{v.element_offset()} /* Strides (in bytes) for each index */
);
});
}
void define_cluster_finder_bindings(py::module &m) {
py::class_<ClusterFinder<uint16_t, double>>(m, "ClusterFinder")
py::class_<ClusterFinder<uint16_t, pd_type>>(m, "ClusterFinder")
.def(py::init<Shape<2>, Shape<2>>())
.def("push_pedestal_frame",
[](ClusterFinder<uint16_t, double> &self,
[](ClusterFinder<uint16_t, pd_type> &self,
py::array_t<uint16_t> frame) {
auto view = make_view_2d(frame);
self.push_pedestal_frame(view);
})
.def("pedestal",
[](ClusterFinder<uint16_t, double> &self) {
auto pd = new NDArray<double, 2>{};
[](ClusterFinder<uint16_t, pd_type> &self) {
auto pd = new NDArray<pd_type, 2>{};
*pd = self.pedestal();
return return_image_data(pd);
})
.def("noise",
[](ClusterFinder<uint16_t, pd_type> &self) {
auto arr = new NDArray<pd_type, 2>{};
*arr = self.noise();
return return_image_data(arr);
})
.def("find_clusters_without_threshold",
[](ClusterFinder<uint16_t, double> &self,
[](ClusterFinder<uint16_t, pd_type> &self,
py::array_t<uint16_t> frame) {
auto view = make_view_2d(frame);
auto clusters = self.find_clusters_without_threshold(view);
return clusters;
auto *vec = new ClusterVector<pd_type>(
self.find_clusters_without_threshold(view));
return vec;
});
m.def("hello", []() {
fmt::print("Hello from C++\n");
auto v = new ClusterVector<int>(3, 3);
int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
v->push_back(5, 30, reinterpret_cast<std::byte *>(data));
v->push_back(5, 55, reinterpret_cast<std::byte *>(data));
v->push_back(5, 20, reinterpret_cast<std::byte *>(data));
v->push_back(5, 30, reinterpret_cast<std::byte *>(data));
return v;
});
define_cluster_vector<int>(m, "i");
define_cluster_vector<double>(m, "d");
// py::class_<ClusterVector<int>>(m, "ClusterVector", py::buffer_protocol())
// .def(py::init<int, int>())
// .def("size", &ClusterVector<int>::size)
// .def("element_offset",
// py::overload_cast<>(&ClusterVector<int>::element_offset, py::const_))
// .def_buffer([](ClusterVector<int> &v) -> py::buffer_info {
// return py::buffer_info(
// v.data(), /* Pointer to buffer */
// v.element_offset(), /* Size of one scalar */
// fmt::format("h:x:\nh:y:\n({},{})i:data:", v.cluster_size_x(),
// v.cluster_size_y()), /* Format descriptor */ 1, /* Number of
// dimensions */ {v.size()}, /* Buffer dimensions */
// {v.element_offset()} /* Strides (in bytes) for each index */
// );
// });
py::class_<DynamicCluster>(m, "DynamicCluster", py::buffer_protocol())
.def(py::init<int, int, Dtype>())
.def("size", &DynamicCluster::size)