python repr for Eta, Cluster

This commit is contained in:
mazzol_a committed 2026-09-09 17:57:00 +02:00
1 parent 3431bbe117
commit c67e5f8ff1
7 files changed
+73 -6

No files matched your search

+1
View File
@@ -381,6 +381,7 @@ set(PUBLICHEADERS
include/aare/RawMasterFile.hpp
include/aare/RawSubFile.hpp
include/aare/VarClusterFinder.hpp
include/aare/to_string.hpp
include/aare/utils/task.hpp
include/aare/utils/ifstream_helpers.hpp
include/aare/utils/math_helpers.hpp)
@@ -87,4 +87,12 @@ template <> FrameDiscardPolicy string_to(const std::string &arg);
*/
template <> DACIndex string_to(const std::string &arg);
/**
* @brief Convert a corner enum to string
* @param arg corner enum value
* @return string representation of the corner enum
* @throw runtime_error if the corner value is invalid
*/
std::string to_string(const corner &arg);
} // namespace aare
+22
View File
@@ -9,6 +9,8 @@
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <iostream>
namespace py = pybind11;
using namespace aare;
@@ -60,6 +62,26 @@ void define_Cluster(py::module &m, const std::string &typestr) {
.def_readonly("y",
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::y)
.def("__repr__",
[class_name](const Cluster<Type, ClusterSizeX, ClusterSizeY,
CoordType> &self) {
fmt::memory_buffer data;
fmt::format_to(std::back_inserter(data), "[[");
for (size_t i = 0; i < self.data.size(); ++i) {
if (i != 0) {
fmt::format_to(std::back_inserter(data),
i % ClusterSizeX == 0 ? "], [" : ", ");
}
fmt::format_to(std::back_inserter(data), "{}",
self.data[i]);
}
fmt::format_to(std::back_inserter(data), "]]");
return fmt::format("{}(x={}, y={}, data={})", class_name,
self.x, self.y,
fmt::string_view(data.data(), data.size()));
})
.def(
"max_sum_2x2",
[](Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType> &self) {
+8 -2
View File
@@ -1,6 +1,8 @@
#include "aare/CalculateEta.hpp"
#include "aare/to_string.hpp"
#include <cstdint>
#include <fmt/format.h>
// #include <pybind11/native_enum.h> only for version 3
#include <pybind11/pybind11.h>
@@ -18,7 +20,11 @@ void define_eta(py::module &m, const std::string &typestr) {
.def_readwrite("c", &Eta2<T>::c,
"eta corner value cTopLeft, cTopRight, "
"cBottomLeft, cBottomRight")
.def_readwrite("sum", &Eta2<T>::sum, "photon energy of cluster");
.def_readwrite("sum", &Eta2<T>::sum, "photon energy of cluster")
.def("__repr__", [class_name](const Eta2<T> &self) {
return fmt::format("{}(x={}, y={}, c={}, sum={})", class_name,
self.x, self.y, to_string(self.c), self.sum);
});
}
void define_corner_enum(py::module &m) {
@@ -101,4 +107,4 @@ void register_calculate_3x3eta(py::module &m) {
},
R"(calculates eta3x3 taking into account cross pixels in cluster)",
py::arg("cluster"));
}
}
+17 -2
View File
@@ -3,7 +3,7 @@ import pytest
import numpy as np
from aare import _aare #import the C++ module
from aare import corner
from aare import ROI, corner
from conftest import test_data_path
@@ -91,6 +91,22 @@ def test_calculate_eta():
assert eta2.sum == 4
def test_cluster_representations():
"""Test Cluster representation"""
cluster = _aare.Cluster2x2i(5, 6, np.array([1, 2, 3, 4], dtype=np.int32))
assert repr(cluster) == "Cluster2x2i(x=5, y=6, data=[[1, 2], [3, 4]])"
def test_eta_representations():
"""Test Eta representation"""
eta = _aare.Etai()
eta.x = 0.25
eta.y = 0.75
eta.c = corner.cBottomRight
eta.sum = 42
assert repr(eta) == "Etai(x=0.25, y=0.75, c=BottomRight, sum=42)"
def test_max_sum():
"""Max 2x2 Sum"""
cluster = _aare.Cluster3x3i(5,5,np.array([1, 1, 1, 2, 3, 1, 2, 2, 1], dtype=np.int32))
@@ -146,4 +162,3 @@ def test_3x3_reduction():
+1 -1
View File
@@ -4,7 +4,7 @@
#include "aare/logger.hpp"
#include <sstream>
#include "to_string.hpp"
#include "aare/to_string.hpp"
namespace aare {
+16 -1
View File
@@ -1,4 +1,4 @@
#include "to_string.hpp"
#include "aare/to_string.hpp"
namespace aare {
@@ -254,6 +254,21 @@ template <> UDPPortPosition string_to(const std::string &arg) {
"\"");
}
std::string to_string(const corner &arg) {
switch (arg) {
case corner::cTopLeft:
return "TopLeft";
case corner::cTopRight:
return "TopRight";
case corner::cBottomLeft:
return "BottomLeft";
case corner::cBottomRight:
return "BottomRight";
default:
throw std::runtime_error("Invalid corner value");
}
}
std::string remove_unit(std::string &str) {
auto it = str.begin();
while (it != str.end()) {