make max_sum_2x2 properly accessible from python

This commit is contained in:
2025-10-23 15:00:52 +02:00
parent 45f506b473
commit 790dd63ba3
8 changed files with 110 additions and 16 deletions

View File

@@ -58,7 +58,11 @@ void define_Cluster(py::module &m, const std::string &typestr) {
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::x)
.def_readonly("y",
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::y);
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::y)
.def(
"max_sum_2x2",
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::max_sum_2x2);
}
template <typename T, uint8_t ClusterSizeX, uint8_t ClusterSizeY,

View File

@@ -6,12 +6,15 @@
#include "aare/NDView.hpp"
#include "aare/Pedestal.hpp"
#include "np_helper.hpp"
#include "utils/bind_Vector.hpp"
#include <cstdint>
#include <filesystem>
#define PYBIND11_DETAILED_ERROR_MESSAGES
#define PYBIND11_DISABLE_STL_CONTAINERS
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
// #include <pybind11/stl.h>
// #include <pybind11/stl_bind.h>
namespace py = pybind11;
using pd_type = double;
@@ -46,8 +49,31 @@ void define_ClusterVector(py::module &m, const std::string &typestr) {
})
.def("sum_2x2",
[](ClusterVector<ClusterType> &self) {
auto *vec = new std::vector<Type>(self.sum_2x2());
return return_vector(vec);
// auto *vec =
// new std::vector<Sum_index_pair<Type, int>>(self.sum_2x2());
/*
py::capsule free_when_done(vec, [](void *f) {
std::vector<std::pair<Type, int>> *foo =
reinterpret_cast<std::vector<std::pair<Type, int>> *>(
f);
delete foo;
});
py::buffer result(
vec->data(),
static_cast<py::ssize_t>(sizeof(std::pair<Type, int>)),
fmt::format("T{{{}:sum:i:index}}",
py::format_descriptor<Type>::format()),
static_cast<py::ssize_t>(1),
std::vector<py::ssize_t>{
static_cast<py::ssize_t>(vec->size())},
std::vector<py::ssize_t>{static_cast<py::ssize_t>(
sizeof(std::pair<Type, int>))});
//,static_cast<py::object>(free_when_done));
*/
return self.sum_2x2(); // return_vector(vec);
})
.def_property_readonly("size", &ClusterVector<ClusterType>::size)
.def("item_size", &ClusterVector<ClusterType>::item_size)

View File

@@ -9,6 +9,7 @@
#include "bind_ClusterFinderMT.hpp"
#include "bind_ClusterVector.hpp"
#include "bind_calibration.hpp"
#include "utils/bind_Vector.hpp"
// TODO! migrate the other names
#include "ctb_raw_file.hpp"
@@ -114,4 +115,19 @@ PYBIND11_MODULE(_aare, m) {
reduce_to_3x3<int, 9, 9, uint16_t>(m);
reduce_to_3x3<double, 9, 9, uint16_t>(m);
reduce_to_3x3<float, 9, 9, uint16_t>(m);
define_Vector<Sum_index_pair<double, int>>(
m, "Sum_index_pair_d",
fmt::format("T{{{}:sum:i:index}}",
py::format_descriptor<double>::format()));
define_Vector<Sum_index_pair<float, int>>(
m, "Sum_index_pair_f",
fmt::format("T{{{}:sum:i:index}}",
py::format_descriptor<double>::format()));
define_Vector<Sum_index_pair<int, int>>(
m, "Sum_index_pair_i",
fmt::format("T{{{}:sum:i:index}}",
py::format_descriptor<double>::format()));
}

View File

@@ -0,0 +1,30 @@
#pragma once
#include <pybind11/pybind11.h>
// #include <pybind11/stl.h>
// #include <pybind11/stl_bind.h>
#include <vector>
namespace py = pybind11;
// TODO add index, itemize
template <typename Type>
void define_Vector(py::module &m, const std::string &type_str,
const std::string &format_string) {
auto class_name = "Vector_" + type_str;
py::class_<std::vector<Type>>(m, class_name.c_str(), py::buffer_protocol())
.def(py::init())
.def_buffer([&format_string](
std::vector<Type> &self) -> py::buffer_info {
return py::buffer_info(
self.data(), static_cast<py::ssize_t>(sizeof(Type)),
format_string, static_cast<py::ssize_t>(1),
std::vector<py::ssize_t>{static_cast<py::ssize_t>(self.size())},
std::vector<py::ssize_t>{
static_cast<py::ssize_t>(sizeof(Type))});
});
}
// fmt::format("T{{{}:sum:i:index}}",py::format_descriptor<Type>::format())