started to do python refactoring
Some checks failed
Build the package using cmake then documentation / build (ubuntu-latest, 3.12) (push) Failing after 44s

This commit is contained in:
Mazzoleni Alice Francesca 2025-04-03 11:56:25 +02:00
parent d7ef9bb1d8
commit a24bbd9cf9
4 changed files with 198 additions and 146 deletions

View File

@ -16,40 +16,51 @@
namespace py = pybind11; namespace py = pybind11;
using pd_type = double; using pd_type = double;
template <typename T> using namespace aare;
template <typename ClusterType>
void define_cluster_vector(py::module &m, const std::string &typestr) { void define_cluster_vector(py::module &m, const std::string &typestr) {
using T = typename extract_template_arguments<ClusterType>::value_type;
auto class_name = fmt::format("ClusterVector_{}", typestr); auto class_name = fmt::format("ClusterVector_{}", typestr);
py::class_<ClusterVector<T>>(m, class_name.c_str(), py::buffer_protocol()) py::class_<ClusterVector<ClusterType>>(m, class_name.c_str(),
.def(py::init<int, int>(), py::buffer_protocol())
py::arg("cluster_size_x") = 3, py::arg("cluster_size_y") = 3) .def(py::init<int, int>(), py::arg("cluster_size_x") = 3,
py::arg("cluster_size_y") = 3) // TODO change!!!
.def("push_back", .def("push_back",
[](ClusterVector<T> &self, int x, int y, py::array_t<T> data) { [](ClusterVector<ClusterType> &self, ClusterType &cl) {
// auto view = make_view_2d(data); // auto view = make_view_2d(data);
self.push_back(x, y, reinterpret_cast<const std::byte*>(data.data())); self.push_back(cl);
}) })
.def_property_readonly("size", &ClusterVector<T>::size) .def_property_readonly("size", &ClusterVector<ClusterType>::size)
.def("item_size", &ClusterVector<T>::item_size) .def("item_size", &ClusterVector<ClusterType>::item_size)
.def_property_readonly("fmt", .def_property_readonly("fmt",
[typestr](ClusterVector<T> &self) { [typestr](ClusterVector<ClusterType> &self) {
return fmt::format( return fmt::format(
self.fmt_base(), self.cluster_size_x(), self.fmt_base(), self.cluster_size_x(),
self.cluster_size_y(), typestr); self.cluster_size_y(), typestr);
}) })
.def("sum", .def("sum",
[](ClusterVector<T> &self) { [](ClusterVector<ClusterType> &self) {
auto *vec = new std::vector<T>(self.sum()); auto *vec = new std::vector<T>(self.sum());
return return_vector(vec); return return_vector(vec);
}) })
.def("sum_2x2", [](ClusterVector<T> &self) { .def("sum_2x2",
[](ClusterVector<ClusterType> &self) {
auto *vec = new std::vector<T>(self.sum_2x2()); auto *vec = new std::vector<T>(self.sum_2x2());
return return_vector(vec); return return_vector(vec);
}) })
.def_property_readonly("cluster_size_x", &ClusterVector<T>::cluster_size_x) .def_property_readonly("cluster_size_x",
.def_property_readonly("cluster_size_y", &ClusterVector<T>::cluster_size_y) &ClusterVector<ClusterType>::cluster_size_x)
.def_property_readonly("capacity", &ClusterVector<T>::capacity) .def_property_readonly("cluster_size_y",
.def_property("frame_number", &ClusterVector<T>::frame_number, &ClusterVector<ClusterType>::cluster_size_y)
&ClusterVector<T>::set_frame_number) .def_property_readonly("capacity",
.def_buffer([typestr](ClusterVector<T> &self) -> py::buffer_info { &ClusterVector<ClusterType>::capacity)
.def_property("frame_number", &ClusterVector<ClusterType>::frame_number,
&ClusterVector<ClusterType>::set_frame_number)
.def_buffer(
[typestr](ClusterVector<ClusterType> &self) -> py::buffer_info {
return py::buffer_info( return py::buffer_info(
self.data(), /* Pointer to buffer */ self.data(), /* Pointer to buffer */
self.item_size(), /* Size of one scalar */ self.item_size(), /* Size of one scalar */
@ -63,102 +74,118 @@ void define_cluster_vector(py::module &m, const std::string &typestr) {
}); });
} }
template <typename ClusterType>
void define_cluster_finder_mt_bindings(py::module &m) { void define_cluster_finder_mt_bindings(py::module &m) {
py::class_<ClusterFinderMT<uint16_t, pd_type>>(m, "ClusterFinderMT") py::class_<ClusterFinderMT<ClusterType, uint16_t, pd_type>>(
m, "ClusterFinderMT")
.def(py::init<Shape<2>, Shape<2>, pd_type, size_t, size_t>(), .def(py::init<Shape<2>, Shape<2>, pd_type, size_t, size_t>(),
py::arg("image_size"), py::arg("cluster_size"), py::arg("image_size"), py::arg("cluster_size"),
py::arg("n_sigma") = 5.0, py::arg("capacity") = 2048, py::arg("n_sigma") = 5.0, py::arg("capacity") = 2048,
py::arg("n_threads") = 3) py::arg("n_threads") = 3)
.def("push_pedestal_frame", .def("push_pedestal_frame",
[](ClusterFinderMT<uint16_t, pd_type> &self, [](ClusterFinderMT<ClusterType, uint16_t, pd_type> &self,
py::array_t<uint16_t> frame) { py::array_t<uint16_t> frame) {
auto view = make_view_2d(frame); auto view = make_view_2d(frame);
self.push_pedestal_frame(view); self.push_pedestal_frame(view);
}) })
.def( .def(
"find_clusters", "find_clusters",
[](ClusterFinderMT<uint16_t, pd_type> &self, [](ClusterFinderMT<ClusterType, uint16_t, pd_type> &self,
py::array_t<uint16_t> frame, uint64_t frame_number) { py::array_t<uint16_t> frame, uint64_t frame_number) {
auto view = make_view_2d(frame); auto view = make_view_2d(frame);
self.find_clusters(view, frame_number); self.find_clusters(view, frame_number);
return; return;
}, },
py::arg(), py::arg("frame_number") = 0) py::arg(), py::arg("frame_number") = 0)
.def("clear_pedestal", &ClusterFinderMT<uint16_t, pd_type>::clear_pedestal) .def("clear_pedestal",
.def("sync", &ClusterFinderMT<uint16_t, pd_type>::sync) &ClusterFinderMT<ClusterType, uint16_t, pd_type>::clear_pedestal)
.def("stop", &ClusterFinderMT<uint16_t, pd_type>::stop) .def("sync", &ClusterFinderMT<ClusterType, uint16_t, pd_type>::sync)
.def("start", &ClusterFinderMT<uint16_t, pd_type>::start) .def("stop", &ClusterFinderMT<ClusterType, uint16_t, pd_type>::stop)
.def("pedestal", .def("start", &ClusterFinderMT<ClusterType, uint16_t, pd_type>::start)
[](ClusterFinderMT<uint16_t, pd_type> &self, size_t thread_index) { .def(
"pedestal",
[](ClusterFinderMT<ClusterType, uint16_t, pd_type> &self,
size_t thread_index) {
auto pd = new NDArray<pd_type, 2>{}; auto pd = new NDArray<pd_type, 2>{};
*pd = self.pedestal(thread_index); *pd = self.pedestal(thread_index);
return return_image_data(pd); return return_image_data(pd);
},py::arg("thread_index") = 0) },
.def("noise", py::arg("thread_index") = 0)
[](ClusterFinderMT<uint16_t, pd_type> &self, size_t thread_index) { .def(
"noise",
[](ClusterFinderMT<ClusterType, uint16_t, pd_type> &self,
size_t thread_index) {
auto arr = new NDArray<pd_type, 2>{}; auto arr = new NDArray<pd_type, 2>{};
*arr = self.noise(thread_index); *arr = self.noise(thread_index);
return return_image_data(arr); return return_image_data(arr);
},py::arg("thread_index") = 0); },
py::arg("thread_index") = 0);
} }
template <typename ClusterType>
void define_cluster_collector_bindings(py::module &m) { void define_cluster_collector_bindings(py::module &m) {
py::class_<ClusterCollector>(m, "ClusterCollector") py::class_<ClusterCollector<ClusterType>>(m, "ClusterCollector")
.def(py::init<ClusterFinderMT<uint16_t, double, int32_t> *>()) .def(py::init<ClusterFinderMT<ClusterType, uint16_t, double> *>())
.def("stop", &ClusterCollector::stop) .def("stop", &ClusterCollector<ClusterType>::stop)
.def( .def(
"steal_clusters", "steal_clusters",
[](ClusterCollector &self) { [](ClusterCollector<ClusterType> &self) {
auto v = auto v = new std::vector<ClusterVector<ClusterType>>(
new std::vector<ClusterVector<int>>(self.steal_clusters()); self.steal_clusters());
return v; return v;
}, },
py::return_value_policy::take_ownership); py::return_value_policy::take_ownership);
} }
template <typename ClusterType>
void define_cluster_file_sink_bindings(py::module &m) { void define_cluster_file_sink_bindings(py::module &m) {
py::class_<ClusterFileSink>(m, "ClusterFileSink") py::class_<ClusterFileSink<ClusterType>>(m, "ClusterFileSink")
.def(py::init<ClusterFinderMT<uint16_t, double, int32_t> *, .def(py::init<ClusterFinderMT<ClusterType, uint16_t, double> *,
const std::filesystem::path &>()) const std::filesystem::path &>())
.def("stop", &ClusterFileSink::stop); .def("stop", &ClusterFileSink<ClusterType>::stop);
} }
template <typename ClusterType>
void define_cluster_finder_bindings(py::module &m) { void define_cluster_finder_bindings(py::module &m) {
py::class_<ClusterFinder<uint16_t, pd_type>>(m, "ClusterFinder") py::class_<ClusterFinder<ClusterType, uint16_t, pd_type>>(m,
"ClusterFinder")
.def(py::init<Shape<2>, Shape<2>, pd_type, size_t>(), .def(py::init<Shape<2>, Shape<2>, pd_type, size_t>(),
py::arg("image_size"), py::arg("cluster_size"), py::arg("image_size"), py::arg("cluster_size"),
py::arg("n_sigma") = 5.0, py::arg("capacity") = 1'000'000) py::arg("n_sigma") = 5.0, py::arg("capacity") = 1'000'000)
.def("push_pedestal_frame", .def("push_pedestal_frame",
[](ClusterFinder<uint16_t, pd_type> &self, [](ClusterFinder<ClusterType, uint16_t, pd_type> &self,
py::array_t<uint16_t> frame) { py::array_t<uint16_t> frame) {
auto view = make_view_2d(frame); auto view = make_view_2d(frame);
self.push_pedestal_frame(view); self.push_pedestal_frame(view);
}) })
.def("clear_pedestal", &ClusterFinder<uint16_t, pd_type>::clear_pedestal) .def("clear_pedestal",
.def_property_readonly("pedestal", &ClusterFinder<ClusterType, uint16_t, pd_type>::clear_pedestal)
[](ClusterFinder<uint16_t, pd_type> &self) { .def_property_readonly(
"pedestal",
[](ClusterFinder<ClusterType, uint16_t, pd_type> &self) {
auto pd = new NDArray<pd_type, 2>{}; auto pd = new NDArray<pd_type, 2>{};
*pd = self.pedestal(); *pd = self.pedestal();
return return_image_data(pd); return return_image_data(pd);
}) })
.def_property_readonly("noise", .def_property_readonly(
[](ClusterFinder<uint16_t, pd_type> &self) { "noise",
[](ClusterFinder<ClusterType, uint16_t, pd_type> &self) {
auto arr = new NDArray<pd_type, 2>{}; auto arr = new NDArray<pd_type, 2>{};
*arr = self.noise(); *arr = self.noise();
return return_image_data(arr); return return_image_data(arr);
}) })
.def( .def(
"steal_clusters", "steal_clusters",
[](ClusterFinder<uint16_t, pd_type> &self, [](ClusterFinder<ClusterType, uint16_t, pd_type> &self,
bool realloc_same_capacity) { bool realloc_same_capacity) {
auto v = new ClusterVector<int>( auto v = new ClusterVector<ClusterType>(
self.steal_clusters(realloc_same_capacity)); self.steal_clusters(realloc_same_capacity));
return v; return v;
}, },
py::arg("realloc_same_capacity") = false) py::arg("realloc_same_capacity") = false)
.def( .def(
"find_clusters", "find_clusters",
[](ClusterFinder<uint16_t, pd_type> &self, [](ClusterFinder<ClusterType, uint16_t, pd_type> &self,
py::array_t<uint16_t> frame, uint64_t frame_number) { py::array_t<uint16_t> frame, uint64_t frame_number) {
auto view = make_view_2d(frame); auto view = make_view_2d(frame);
self.find_clusters(view, frame_number); self.find_clusters(view, frame_number);
@ -167,7 +194,7 @@ void define_cluster_finder_bindings(py::module &m) {
py::arg(), py::arg("frame_number") = 0); py::arg(), py::arg("frame_number") = 0);
m.def("hitmap", m.def("hitmap",
[](std::array<size_t, 2> image_size, ClusterVector<int32_t> &cv) { [](std::array<size_t, 2> image_size, ClusterVector<ClusterType> &cv) {
py::array_t<int32_t> hitmap(image_size); py::array_t<int32_t> hitmap(image_size);
auto r = hitmap.mutable_unchecked<2>(); auto r = hitmap.mutable_unchecked<2>();

View File

@ -15,64 +15,81 @@
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wunused-parameter"
namespace py = pybind11; namespace py = pybind11;
using namespace ::aare; using namespace ::aare;
template <typename ClusterType>
void define_cluster_file_io_bindings(py::module &m) { void define_cluster_file_io_bindings(py::module &m) {
PYBIND11_NUMPY_DTYPE(Cluster3x3, x, y, data); PYBIND11_NUMPY_DTYPE(Cluster3x3, x, y, data);
py::class_<ClusterFile>(m, "ClusterFile") py::class_<ClusterFile<ClusterType>>(m, "ClusterFile")
.def(py::init<const std::filesystem::path &, size_t, .def(py::init<const std::filesystem::path &, size_t,
const std::string &>(), const std::string &>(),
py::arg(), py::arg("chunk_size") = 1000, py::arg("mode") = "r") py::arg(), py::arg("chunk_size") = 1000, py::arg("mode") = "r")
.def("read_clusters", .def(
[](ClusterFile &self, size_t n_clusters) { "read_clusters",
auto v = new ClusterVector<int32_t>(self.read_clusters(n_clusters)); [](ClusterFile<ClusterType> &self, size_t n_clusters) {
auto v = new ClusterVector<ClusterType>(
self.read_clusters(n_clusters));
return v; return v;
},py::return_value_policy::take_ownership) },
.def("read_clusters", py::return_value_policy::take_ownership)
[](ClusterFile &self, size_t n_clusters, ROI roi) { .def(
auto v = new ClusterVector<int32_t>(self.read_clusters(n_clusters, roi)); "read_clusters",
[](ClusterFile<ClusterType> &self, size_t n_clusters, ROI roi) {
auto v = new ClusterVector<ClusterType>(
self.read_clusters(n_clusters, roi));
return v; return v;
},py::return_value_policy::take_ownership) },
py::return_value_policy::take_ownership)
.def("read_frame", .def("read_frame",
[](ClusterFile &self) { [](ClusterFile<ClusterType> &self) {
auto v = new ClusterVector<int32_t>(self.read_frame()); auto v = new ClusterVector<ClusterType>(self.read_frame());
return v; return v;
}) })
.def("set_roi", &ClusterFile::set_roi) .def("set_roi", &ClusterFile<ClusterType>::set_roi)
.def("set_noise_map", [](ClusterFile &self, py::array_t<int32_t> noise_map) { .def(
"set_noise_map",
[](ClusterFile<ClusterType> &self, py::array_t<int32_t> noise_map) {
auto view = make_view_2d(noise_map); auto view = make_view_2d(noise_map);
self.set_noise_map(view); self.set_noise_map(view);
}) })
.def("set_gain_map", [](ClusterFile &self, py::array_t<double> gain_map) {
.def("set_gain_map",
[](ClusterFile<ClusterType> &self, py::array_t<double> gain_map) {
auto view = make_view_2d(gain_map); auto view = make_view_2d(gain_map);
self.set_gain_map(view); self.set_gain_map(view);
}) })
.def("close", &ClusterFile::close)
.def("write_frame", &ClusterFile::write_frame) // void set_gain_map(const GainMap &gain_map); //TODO do i need a
.def("__enter__", [](ClusterFile &self) { return &self; }) // gainmap constructor?
.def("close", &ClusterFile<ClusterType>::close)
.def("write_frame", &ClusterFile<ClusterType>::write_frame)
.def("__enter__", [](ClusterFile<ClusterType> &self) { return &self; })
.def("__exit__", .def("__exit__",
[](ClusterFile &self, [](ClusterFile<ClusterType> &self,
const std::optional<pybind11::type> &exc_type, const std::optional<pybind11::type> &exc_type,
const std::optional<pybind11::object> &exc_value, const std::optional<pybind11::object> &exc_value,
const std::optional<pybind11::object> &traceback) { const std::optional<pybind11::object> &traceback) {
self.close(); self.close();
}) })
.def("__iter__", [](ClusterFile &self) { return &self; }) .def("__iter__", [](ClusterFile<ClusterType> &self) { return &self; })
.def("__next__", [](ClusterFile &self) { .def("__next__", [](ClusterFile<ClusterType> &self) {
auto v = new ClusterVector<int32_t>(self.read_clusters(self.chunk_size())); auto v = new ClusterVector<ClusterType>(
self.read_clusters(self.chunk_size()));
if (v->size() == 0) { if (v->size() == 0) {
throw py::stop_iteration(); throw py::stop_iteration();
} }
return v; return v;
}); });
m.def("calculate_eta2", []( aare::ClusterVector<int32_t> &clusters) { /*
m.def("calculate_eta2", []( aare::ClusterVector<ClusterType> &clusters) {
auto eta2 = new NDArray<double, 2>(calculate_eta2(clusters)); auto eta2 = new NDArray<double, 2>(calculate_eta2(clusters));
return return_image_data(eta2); return return_image_data(eta2);
}); });
*/ //add in different file
} }
#pragma GCC diagnostic pop #pragma GCC diagnostic pop

View File

@ -8,28 +8,37 @@
#include <pybind11/stl.h> #include <pybind11/stl.h>
namespace py = pybind11; namespace py = pybind11;
void define_interpolation_bindings(py::module &m) { void define_interpolation_bindings(py::module &m) {
PYBIND11_NUMPY_DTYPE(aare::Photon, x, y, energy); PYBIND11_NUMPY_DTYPE(aare::Photon, x, y, energy);
py::class_<aare::Interpolator>(m, "Interpolator") py::class_<aare::Interpolator>(m, "Interpolator")
.def(py::init([](py::array_t<double, py::array::c_style | py::array::forcecast> etacube, py::array_t<double> xbins, .def(py::init(
py::array_t<double> ybins, py::array_t<double> ebins) { [](py::array_t<double, py::array::c_style | py::array::forcecast>
etacube,
py::array_t<double> xbins, py::array_t<double> ybins,
py::array_t<double> ebins) {
return Interpolator(make_view_3d(etacube), make_view_1d(xbins), return Interpolator(make_view_3d(etacube), make_view_1d(xbins),
make_view_1d(ybins), make_view_1d(ebins)); make_view_1d(ybins), make_view_1d(ebins));
})) }))
.def("get_ietax", [](Interpolator& self){ .def("get_ietax",
[](Interpolator &self) {
auto *ptr = new NDArray<double, 3>{}; auto *ptr = new NDArray<double, 3>{};
*ptr = self.get_ietax(); *ptr = self.get_ietax();
return return_image_data(ptr); return return_image_data(ptr);
}) })
.def("get_ietay", [](Interpolator& self){ .def("get_ietay",
[](Interpolator &self) {
auto *ptr = new NDArray<double, 3>{}; auto *ptr = new NDArray<double, 3>{};
*ptr = self.get_ietay(); *ptr = self.get_ietay();
return return_image_data(ptr); return return_image_data(ptr);
}) })
.def("interpolate", [](Interpolator& self, const ClusterVector<int32_t>& clusters){
auto photons = self.interpolate(clusters); // TODO take care of clustertype template
.def("interpolate",
[](Interpolator &self, const ClusterVector<auto> &clusters) {
auto photons = self.interpolate<ClusterType>(clusters);
auto *ptr = new std::vector<Photon>{photons}; auto *ptr = new std::vector<Photon>{photons};
return return_vector(ptr); return return_vector(ptr);
}); });

View File

@ -1,15 +1,15 @@
// Files with bindings to the different classes // Files with bindings to the different classes
#include "file.hpp"
#include "raw_file.hpp"
#include "ctb_raw_file.hpp"
#include "raw_master_file.hpp"
#include "var_cluster.hpp"
#include "pixel_map.hpp"
#include "pedestal.hpp"
#include "cluster.hpp" #include "cluster.hpp"
#include "cluster_file.hpp" #include "cluster_file.hpp"
#include "ctb_raw_file.hpp"
#include "file.hpp"
#include "fit.hpp" #include "fit.hpp"
#include "interpolation.hpp" #include "interpolation.hpp"
#include "pedestal.hpp"
#include "pixel_map.hpp"
#include "raw_file.hpp"
#include "raw_master_file.hpp"
#include "var_cluster.hpp"
// Pybind stuff // Pybind stuff
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
@ -33,5 +33,4 @@ PYBIND11_MODULE(_aare, m) {
define_cluster_file_sink_bindings(m); define_cluster_file_sink_bindings(m);
define_fit_bindings(m); define_fit_bindings(m);
define_interpolation_bindings(m); define_interpolation_bindings(m);
} }