From 042240707125718b7c64c71027b8de22d07cddb8 Mon Sep 17 00:00:00 2001 From: froejdh_e Date: Mon, 2 Jun 2025 17:35:10 +0200 Subject: [PATCH] split up and renamed python bindings for cluster related functions and classes --- python/src/bind_Cluster.hpp | 64 ++++++ python/src/bind_ClusterCollector.hpp | 46 ++++ ...{cluster_file.hpp => bind_ClusterFile.hpp} | 2 +- python/src/bind_ClusterFileSink.hpp | 44 ++++ python/src/bind_ClusterFinder.hpp | 77 +++++++ python/src/bind_ClusterFinderMT.hpp | 81 +++++++ python/src/bind_ClusterVector.hpp | 4 +- python/src/cluster.hpp | 211 ------------------ python/src/module.cpp | 80 +++---- 9 files changed, 358 insertions(+), 251 deletions(-) create mode 100644 python/src/bind_Cluster.hpp create mode 100644 python/src/bind_ClusterCollector.hpp rename python/src/{cluster_file.hpp => bind_ClusterFile.hpp} (98%) create mode 100644 python/src/bind_ClusterFileSink.hpp create mode 100644 python/src/bind_ClusterFinder.hpp create mode 100644 python/src/bind_ClusterFinderMT.hpp delete mode 100644 python/src/cluster.hpp diff --git a/python/src/bind_Cluster.hpp b/python/src/bind_Cluster.hpp new file mode 100644 index 0000000..daf0946 --- /dev/null +++ b/python/src/bind_Cluster.hpp @@ -0,0 +1,64 @@ +#include "aare/Cluster.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace py = pybind11; +using pd_type = double; + +using namespace aare; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" + +template +void define_Cluster(py::module &m, const std::string &typestr) { + auto class_name = fmt::format("Cluster{}", typestr); + + py::class_>( + m, class_name.c_str(), py::buffer_protocol()) + + .def(py::init([](uint8_t x, uint8_t y, py::array_t data) { + py::buffer_info buf_info = data.request(); + Cluster cluster; + cluster.x = x; + cluster.y = y; + auto r = data.template unchecked<1>(); // no bounds checks + for (py::ssize_t i = 0; i < data.size(); ++i) { + cluster.data[i] = r(i); + } + return cluster; + })); + + /* + //TODO! Review if to keep or not + .def_property( + "data", + [](ClusterType &c) -> py::array { + return py::array(py::buffer_info( + c.data, sizeof(Type), + py::format_descriptor::format(), // Type + // format + 1, // Number of dimensions + {static_cast(ClusterSizeX * + ClusterSizeY)}, // Shape (flattened) + {sizeof(Type)} // Stride (step size between elements) + )); + }, + [](ClusterType &c, py::array_t arr) { + py::buffer_info buf_info = arr.request(); + Type *ptr = static_cast(buf_info.ptr); + std::copy(ptr, ptr + ClusterSizeX * ClusterSizeY, + c.data); // TODO dont iterate over centers!!! + + }); + */ +} + +#pragma GCC diagnostic pop \ No newline at end of file diff --git a/python/src/bind_ClusterCollector.hpp b/python/src/bind_ClusterCollector.hpp new file mode 100644 index 0000000..4836e6e --- /dev/null +++ b/python/src/bind_ClusterCollector.hpp @@ -0,0 +1,46 @@ +#include "aare/ClusterCollector.hpp" +#include "aare/ClusterFileSink.hpp" +#include "aare/ClusterFinder.hpp" +#include "aare/ClusterFinderMT.hpp" +#include "aare/ClusterVector.hpp" +#include "aare/NDView.hpp" +#include "aare/Pedestal.hpp" +#include "np_helper.hpp" + +#include +#include +#include +#include +#include + +namespace py = pybind11; +using pd_type = double; + +using namespace aare; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" + + +template +void define_ClusterCollector(py::module &m, + const std::string &typestr) { + auto class_name = fmt::format("ClusterCollector_{}", typestr); + + using ClusterType = Cluster; + + py::class_>(m, class_name.c_str()) + .def(py::init *>()) + .def("stop", &ClusterCollector::stop) + .def( + "steal_clusters", + [](ClusterCollector &self) { + auto v = new std::vector>( + self.steal_clusters()); + return v; // TODO change!!! + }, + py::return_value_policy::take_ownership); +} + +#pragma GCC diagnostic pop \ No newline at end of file diff --git a/python/src/cluster_file.hpp b/python/src/bind_ClusterFile.hpp similarity index 98% rename from python/src/cluster_file.hpp rename to python/src/bind_ClusterFile.hpp index ac384b2..8ce5360 100644 --- a/python/src/cluster_file.hpp +++ b/python/src/bind_ClusterFile.hpp @@ -21,7 +21,7 @@ using namespace ::aare; template -void define_cluster_file_io_bindings(py::module &m, +void define_ClusterFile(py::module &m, const std::string &typestr) { using ClusterType = Cluster; diff --git a/python/src/bind_ClusterFileSink.hpp b/python/src/bind_ClusterFileSink.hpp new file mode 100644 index 0000000..9b3a74d --- /dev/null +++ b/python/src/bind_ClusterFileSink.hpp @@ -0,0 +1,44 @@ +#include "aare/ClusterCollector.hpp" +#include "aare/ClusterFileSink.hpp" +#include "aare/ClusterFinder.hpp" +#include "aare/ClusterFinderMT.hpp" +#include "aare/ClusterVector.hpp" +#include "aare/NDView.hpp" +#include "aare/Pedestal.hpp" +#include "np_helper.hpp" + +#include +#include +#include +#include +#include + +namespace py = pybind11; +using pd_type = double; + +using namespace aare; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" + + + + + + +template +void define_ClusterFileSink(py::module &m, + const std::string &typestr) { + auto class_name = fmt::format("ClusterFileSink_{}", typestr); + + using ClusterType = Cluster; + + py::class_>(m, class_name.c_str()) + .def(py::init *, + const std::filesystem::path &>()) + .def("stop", &ClusterFileSink::stop); +} + + +#pragma GCC diagnostic pop diff --git a/python/src/bind_ClusterFinder.hpp b/python/src/bind_ClusterFinder.hpp new file mode 100644 index 0000000..5f0fe8d --- /dev/null +++ b/python/src/bind_ClusterFinder.hpp @@ -0,0 +1,77 @@ +#include "aare/ClusterCollector.hpp" +#include "aare/ClusterFileSink.hpp" +#include "aare/ClusterFinder.hpp" +#include "aare/ClusterFinderMT.hpp" +#include "aare/ClusterVector.hpp" +#include "aare/NDView.hpp" +#include "aare/Pedestal.hpp" +#include "np_helper.hpp" + +#include +#include +#include +#include +#include + +namespace py = pybind11; +using pd_type = double; + +using namespace aare; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" + +template +void define_ClusterFinder(py::module &m, const std::string &typestr) { + auto class_name = fmt::format("ClusterFinder_{}", typestr); + + using ClusterType = Cluster; + + py::class_>( + m, class_name.c_str()) + .def(py::init, pd_type, size_t>(), py::arg("image_size"), + py::arg("n_sigma") = 5.0, py::arg("capacity") = 1'000'000) + .def("push_pedestal_frame", + [](ClusterFinder &self, + py::array_t frame) { + auto view = make_view_2d(frame); + self.push_pedestal_frame(view); + }) + .def("clear_pedestal", + &ClusterFinder::clear_pedestal) + .def_property_readonly( + "pedestal", + [](ClusterFinder &self) { + auto pd = new NDArray{}; + *pd = self.pedestal(); + return return_image_data(pd); + }) + .def_property_readonly( + "noise", + [](ClusterFinder &self) { + auto arr = new NDArray{}; + *arr = self.noise(); + return return_image_data(arr); + }) + .def( + "steal_clusters", + [](ClusterFinder &self, + bool realloc_same_capacity) { + ClusterVector clusters = + self.steal_clusters(realloc_same_capacity); + return clusters; + }, + py::arg("realloc_same_capacity") = false) + .def( + "find_clusters", + [](ClusterFinder &self, + py::array_t frame, uint64_t frame_number) { + auto view = make_view_2d(frame); + self.find_clusters(view, frame_number); + return; + }, + py::arg(), py::arg("frame_number") = 0); +} + +#pragma GCC diagnostic pop \ No newline at end of file diff --git a/python/src/bind_ClusterFinderMT.hpp b/python/src/bind_ClusterFinderMT.hpp new file mode 100644 index 0000000..d1769db --- /dev/null +++ b/python/src/bind_ClusterFinderMT.hpp @@ -0,0 +1,81 @@ +#include "aare/ClusterCollector.hpp" +#include "aare/ClusterFileSink.hpp" +#include "aare/ClusterFinder.hpp" +#include "aare/ClusterFinderMT.hpp" +#include "aare/ClusterVector.hpp" +#include "aare/NDView.hpp" +#include "aare/Pedestal.hpp" +#include "np_helper.hpp" + +#include +#include +#include +#include +#include + +namespace py = pybind11; +using pd_type = double; + +using namespace aare; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" + +template +void define_ClusterFinderMT(py::module &m, + const std::string &typestr) { + auto class_name = fmt::format("ClusterFinderMT_{}", typestr); + + using ClusterType = Cluster; + + py::class_>( + m, class_name.c_str()) + .def(py::init, pd_type, size_t, size_t>(), + py::arg("image_size"), py::arg("n_sigma") = 5.0, + py::arg("capacity") = 2048, py::arg("n_threads") = 3) + .def("push_pedestal_frame", + [](ClusterFinderMT &self, + py::array_t frame) { + auto view = make_view_2d(frame); + self.push_pedestal_frame(view); + }) + .def( + "find_clusters", + [](ClusterFinderMT &self, + py::array_t frame, uint64_t frame_number) { + auto view = make_view_2d(frame); + self.find_clusters(view, frame_number); + return; + }, + py::arg(), py::arg("frame_number") = 0) + .def_property_readonly("cluster_size", [](ClusterFinderMT &self){ + return py::make_tuple(ClusterSizeX, ClusterSizeY); + }) + .def("clear_pedestal", + &ClusterFinderMT::clear_pedestal) + .def("sync", &ClusterFinderMT::sync) + .def("stop", &ClusterFinderMT::stop) + .def("start", &ClusterFinderMT::start) + .def( + "pedestal", + [](ClusterFinderMT &self, + size_t thread_index) { + auto pd = new NDArray{}; + *pd = self.pedestal(thread_index); + return return_image_data(pd); + }, + py::arg("thread_index") = 0) + .def( + "noise", + [](ClusterFinderMT &self, + size_t thread_index) { + auto arr = new NDArray{}; + *arr = self.noise(thread_index); + return return_image_data(arr); + }, + py::arg("thread_index") = 0); +} + + +#pragma GCC diagnostic pop \ No newline at end of file diff --git a/python/src/bind_ClusterVector.hpp b/python/src/bind_ClusterVector.hpp index db8c8a3..550db9a 100644 --- a/python/src/bind_ClusterVector.hpp +++ b/python/src/bind_ClusterVector.hpp @@ -101,4 +101,6 @@ void define_ClusterVector(py::module &m, const std::string &typestr) { return hitmap; }); -} \ No newline at end of file +} + +#pragma GCC diagnostic pop \ No newline at end of file diff --git a/python/src/cluster.hpp b/python/src/cluster.hpp deleted file mode 100644 index 58f137c..0000000 --- a/python/src/cluster.hpp +++ /dev/null @@ -1,211 +0,0 @@ -#include "aare/ClusterCollector.hpp" -#include "aare/ClusterFileSink.hpp" -#include "aare/ClusterFinder.hpp" -#include "aare/ClusterFinderMT.hpp" -#include "aare/ClusterVector.hpp" -#include "aare/NDView.hpp" -#include "aare/Pedestal.hpp" -#include "np_helper.hpp" - -#include -#include -#include -#include -#include - -namespace py = pybind11; -using pd_type = double; - -using namespace aare; - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-parameter" - -template -void define_cluster(py::module &m, const std::string &typestr) { - auto class_name = fmt::format("Cluster{}", typestr); - - py::class_>( - m, class_name.c_str(), py::buffer_protocol()) - - .def(py::init([](uint8_t x, uint8_t y, py::array_t data) { - py::buffer_info buf_info = data.request(); - Cluster cluster; - cluster.x = x; - cluster.y = y; - auto r = data.template unchecked<1>(); // no bounds checks - for (py::ssize_t i = 0; i < data.size(); ++i) { - cluster.data[i] = r(i); - } - return cluster; - })); - - /* - .def_property( - "data", - [](ClusterType &c) -> py::array { - return py::array(py::buffer_info( - c.data, sizeof(Type), - py::format_descriptor::format(), // Type - // format - 1, // Number of dimensions - {static_cast(ClusterSizeX * - ClusterSizeY)}, // Shape (flattened) - {sizeof(Type)} // Stride (step size between elements) - )); - }, - [](ClusterType &c, py::array_t arr) { - py::buffer_info buf_info = arr.request(); - Type *ptr = static_cast(buf_info.ptr); - std::copy(ptr, ptr + ClusterSizeX * ClusterSizeY, - c.data); // TODO dont iterate over centers!!! - - }); - */ -} - -template -void define_cluster_finder_mt_bindings(py::module &m, - const std::string &typestr) { - auto class_name = fmt::format("ClusterFinderMT_{}", typestr); - - using ClusterType = Cluster; - - py::class_>( - m, class_name.c_str()) - .def(py::init, pd_type, size_t, size_t>(), - py::arg("image_size"), py::arg("n_sigma") = 5.0, - py::arg("capacity") = 2048, py::arg("n_threads") = 3) - .def("push_pedestal_frame", - [](ClusterFinderMT &self, - py::array_t frame) { - auto view = make_view_2d(frame); - self.push_pedestal_frame(view); - }) - .def( - "find_clusters", - [](ClusterFinderMT &self, - py::array_t frame, uint64_t frame_number) { - auto view = make_view_2d(frame); - self.find_clusters(view, frame_number); - return; - }, - py::arg(), py::arg("frame_number") = 0) - .def_property_readonly("cluster_size", [](ClusterFinderMT &self){ - return py::make_tuple(ClusterSizeX, ClusterSizeY); - }) - .def("clear_pedestal", - &ClusterFinderMT::clear_pedestal) - .def("sync", &ClusterFinderMT::sync) - .def("stop", &ClusterFinderMT::stop) - .def("start", &ClusterFinderMT::start) - .def( - "pedestal", - [](ClusterFinderMT &self, - size_t thread_index) { - auto pd = new NDArray{}; - *pd = self.pedestal(thread_index); - return return_image_data(pd); - }, - py::arg("thread_index") = 0) - .def( - "noise", - [](ClusterFinderMT &self, - size_t thread_index) { - auto arr = new NDArray{}; - *arr = self.noise(thread_index); - return return_image_data(arr); - }, - py::arg("thread_index") = 0); -} - -template -void define_cluster_collector_bindings(py::module &m, - const std::string &typestr) { - auto class_name = fmt::format("ClusterCollector_{}", typestr); - - using ClusterType = Cluster; - - py::class_>(m, class_name.c_str()) - .def(py::init *>()) - .def("stop", &ClusterCollector::stop) - .def( - "steal_clusters", - [](ClusterCollector &self) { - auto v = new std::vector>( - self.steal_clusters()); - return v; // TODO change!!! - }, - py::return_value_policy::take_ownership); -} - -template -void define_cluster_file_sink_bindings(py::module &m, - const std::string &typestr) { - auto class_name = fmt::format("ClusterFileSink_{}", typestr); - - using ClusterType = Cluster; - - py::class_>(m, class_name.c_str()) - .def(py::init *, - const std::filesystem::path &>()) - .def("stop", &ClusterFileSink::stop); -} - -template -void define_cluster_finder_bindings(py::module &m, const std::string &typestr) { - auto class_name = fmt::format("ClusterFinder_{}", typestr); - - using ClusterType = Cluster; - - py::class_>( - m, class_name.c_str()) - .def(py::init, pd_type, size_t>(), py::arg("image_size"), - py::arg("n_sigma") = 5.0, py::arg("capacity") = 1'000'000) - .def("push_pedestal_frame", - [](ClusterFinder &self, - py::array_t frame) { - auto view = make_view_2d(frame); - self.push_pedestal_frame(view); - }) - .def("clear_pedestal", - &ClusterFinder::clear_pedestal) - .def_property_readonly( - "pedestal", - [](ClusterFinder &self) { - auto pd = new NDArray{}; - *pd = self.pedestal(); - return return_image_data(pd); - }) - .def_property_readonly( - "noise", - [](ClusterFinder &self) { - auto arr = new NDArray{}; - *arr = self.noise(); - return return_image_data(arr); - }) - .def( - "steal_clusters", - [](ClusterFinder &self, - bool realloc_same_capacity) { - ClusterVector clusters = - self.steal_clusters(realloc_same_capacity); - return clusters; - }, - py::arg("realloc_same_capacity") = false) - .def( - "find_clusters", - [](ClusterFinder &self, - py::array_t frame, uint64_t frame_number) { - auto view = make_view_2d(frame); - self.find_clusters(view, frame_number); - return; - }, - py::arg(), py::arg("frame_number") = 0); -} -#pragma GCC diagnostic pop diff --git a/python/src/module.cpp b/python/src/module.cpp index 946a41b..5945afb 100644 --- a/python/src/module.cpp +++ b/python/src/module.cpp @@ -1,11 +1,15 @@ // Files with bindings to the different classes //New style file naming +#include "bind_Cluster.hpp" +#include "bind_ClusterCollector.hpp" +#include "bind_ClusterFinder.hpp" +#include "bind_ClusterFinderMT.hpp" +#include "bind_ClusterFile.hpp" +#include "bind_ClusterFileSink.hpp" #include "bind_ClusterVector.hpp" //TODO! migrate the other names -#include "cluster.hpp" -#include "cluster_file.hpp" #include "ctb_raw_file.hpp" #include "file.hpp" #include "fit.hpp" @@ -38,12 +42,12 @@ PYBIND11_MODULE(_aare, m) { define_interpolation_bindings(m); define_jungfrau_data_file_io_bindings(m); - define_cluster_file_io_bindings(m, "Cluster3x3i"); - define_cluster_file_io_bindings(m, "Cluster3x3d"); - define_cluster_file_io_bindings(m, "Cluster3x3f"); - define_cluster_file_io_bindings(m, "Cluster2x2i"); - define_cluster_file_io_bindings(m, "Cluster2x2f"); - define_cluster_file_io_bindings(m, "Cluster2x2d"); + define_ClusterFile(m, "Cluster3x3i"); + define_ClusterFile(m, "Cluster3x3d"); + define_ClusterFile(m, "Cluster3x3f"); + define_ClusterFile(m, "Cluster2x2i"); + define_ClusterFile(m, "Cluster2x2f"); + define_ClusterFile(m, "Cluster2x2d"); define_ClusterVector(m, "Cluster3x3i"); define_ClusterVector(m, "Cluster3x3d"); @@ -52,40 +56,40 @@ PYBIND11_MODULE(_aare, m) { define_ClusterVector(m, "Cluster2x2d"); define_ClusterVector(m, "Cluster2x2f"); - define_cluster_finder_bindings(m, "Cluster3x3i"); - define_cluster_finder_bindings(m, "Cluster3x3d"); - define_cluster_finder_bindings(m, "Cluster3x3f"); - define_cluster_finder_bindings(m, "Cluster2x2i"); - define_cluster_finder_bindings(m, "Cluster2x2d"); - define_cluster_finder_bindings(m, "Cluster2x2f"); + define_ClusterFinder(m, "Cluster3x3i"); + define_ClusterFinder(m, "Cluster3x3d"); + define_ClusterFinder(m, "Cluster3x3f"); + define_ClusterFinder(m, "Cluster2x2i"); + define_ClusterFinder(m, "Cluster2x2d"); + define_ClusterFinder(m, "Cluster2x2f"); - define_cluster_finder_mt_bindings(m, "Cluster3x3i"); - define_cluster_finder_mt_bindings(m, "Cluster3x3d"); - define_cluster_finder_mt_bindings(m, "Cluster3x3f"); - define_cluster_finder_mt_bindings(m, "Cluster2x2i"); - define_cluster_finder_mt_bindings(m, "Cluster2x2d"); - define_cluster_finder_mt_bindings(m, "Cluster2x2f"); + define_ClusterFinderMT(m, "Cluster3x3i"); + define_ClusterFinderMT(m, "Cluster3x3d"); + define_ClusterFinderMT(m, "Cluster3x3f"); + define_ClusterFinderMT(m, "Cluster2x2i"); + define_ClusterFinderMT(m, "Cluster2x2d"); + define_ClusterFinderMT(m, "Cluster2x2f"); - define_cluster_file_sink_bindings(m, "Cluster3x3i"); - define_cluster_file_sink_bindings(m, "Cluster3x3d"); - define_cluster_file_sink_bindings(m, "Cluster3x3f"); - define_cluster_file_sink_bindings(m, "Cluster2x2i"); - define_cluster_file_sink_bindings(m, "Cluster2x2d"); - define_cluster_file_sink_bindings(m, "Cluster2x2f"); + define_ClusterFileSink(m, "Cluster3x3i"); + define_ClusterFileSink(m, "Cluster3x3d"); + define_ClusterFileSink(m, "Cluster3x3f"); + define_ClusterFileSink(m, "Cluster2x2i"); + define_ClusterFileSink(m, "Cluster2x2d"); + define_ClusterFileSink(m, "Cluster2x2f"); - define_cluster_collector_bindings(m, "Cluster3x3i"); - define_cluster_collector_bindings(m, "Cluster3x3f"); - define_cluster_collector_bindings(m, "Cluster3x3d"); - define_cluster_collector_bindings(m, "Cluster2x2i"); - define_cluster_collector_bindings(m, "Cluster2x2f"); - define_cluster_collector_bindings(m, "Cluster2x2d"); + define_ClusterCollector(m, "Cluster3x3i"); + define_ClusterCollector(m, "Cluster3x3d"); + define_ClusterCollector(m, "Cluster3x3f"); + define_ClusterCollector(m, "Cluster2x2i"); + define_ClusterCollector(m, "Cluster2x2d"); + define_ClusterCollector(m, "Cluster2x2f"); - define_cluster(m, "3x3i"); - define_cluster(m, "3x3f"); - define_cluster(m, "3x3d"); - define_cluster(m, "2x2i"); - define_cluster(m, "2x2f"); - define_cluster(m, "2x2d"); + define_Cluster(m, "3x3i"); + define_Cluster(m, "3x3f"); + define_Cluster(m, "3x3d"); + define_Cluster(m, "2x2i"); + define_Cluster(m, "2x2f"); + define_Cluster(m, "2x2d"); register_calculate_eta(m); register_calculate_eta(m);