From 7b5e32a824af294ca6b35d763dd296a23f6b2c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Fri, 25 Apr 2025 10:31:16 +0200 Subject: [PATCH] Api extra (#166) Changes to be able to run the example notebooks: - Invert gain map on setting (multiplication is faster but user supplies ADU/energy) - Cast after applying gain map not to loose precision (Important for int32 clusters) - "factor" for ClusterFileSink - Cluster size available to be able to create the right file sink --- include/aare/GainMap.hpp | 12 ++++++++++-- python/aare/ClusterFinder.py | 17 +++++++++++++++++ python/aare/__init__.py | 2 +- python/src/bind_ClusterVector.hpp | 9 ++++----- python/src/cluster.hpp | 3 +++ python/src/module.cpp | 11 +++++------ src/ClusterFile.test.cpp | 3 ++- 7 files changed, 42 insertions(+), 15 deletions(-) diff --git a/include/aare/GainMap.hpp b/include/aare/GainMap.hpp index 5311916..621cc9f 100644 --- a/include/aare/GainMap.hpp +++ b/include/aare/GainMap.hpp @@ -16,10 +16,18 @@ class GainMap { public: explicit GainMap(const NDArray &gain_map) - : m_gain_map(gain_map) {}; + : m_gain_map(gain_map) { + for (auto &item : m_gain_map) { + item = 1.0 / item; + } + + }; explicit GainMap(const NDView gain_map) { m_gain_map = NDArray(gain_map); + for (auto &item : m_gain_map) { + item = 1.0 / item; + } } template (m_gain_map(y, x)); + cl.data[j] = static_cast(cl.data[j] * m_gain_map(y, x)); //cast after conversion to keep precision } } else { // clear edge clusters diff --git a/python/aare/ClusterFinder.py b/python/aare/ClusterFinder.py index f678dd1..6e7c352 100644 --- a/python/aare/ClusterFinder.py +++ b/python/aare/ClusterFinder.py @@ -1,5 +1,8 @@ from ._aare import ClusterFinder_Cluster3x3i, ClusterFinder_Cluster2x2i, ClusterFinderMT_Cluster3x3i, ClusterFinderMT_Cluster2x2i, ClusterCollector_Cluster3x3i, ClusterCollector_Cluster2x2i + + +from ._aare import ClusterFileSink_Cluster3x3i, ClusterFileSink_Cluster2x2i import numpy as np def ClusterFinder(image_size, cluster_size, n_sigma=5, dtype = np.int32, capacity = 1024): @@ -48,3 +51,17 @@ def ClusterCollector(clusterfindermt, cluster_size = (3,3), dtype=np.int32): #TODO! add the other formats raise ValueError(f"Unsupported dtype: {dtype}. Only np.int32 is supported.") +def ClusterFileSink(clusterfindermt, cluster_file, dtype=np.int32): + """ + Factory function to create a ClusterCollector object. Provides a cleaner syntax for + the templated ClusterCollector in C++. + """ + + if dtype == np.int32 and clusterfindermt.cluster_size == (3,3): + return ClusterFileSink_Cluster3x3i(clusterfindermt, cluster_file) + elif dtype == np.int32 and clusterfindermt.cluster_size == (2,2): + return ClusterFileSink_Cluster2x2i(clusterfindermt, cluster_file) + + else: + #TODO! add the other formats + raise ValueError(f"Unsupported dtype: {dtype}. Only np.int32 is supported.") \ No newline at end of file diff --git a/python/aare/__init__.py b/python/aare/__init__.py index 81a9b86..e1e5757 100644 --- a/python/aare/__init__.py +++ b/python/aare/__init__.py @@ -11,7 +11,7 @@ from ._aare import ROI # from ._aare import ClusterFinderMT, ClusterCollector, ClusterFileSink, ClusterVector_i -from .ClusterFinder import ClusterFinder, ClusterCollector, ClusterFinderMT +from .ClusterFinder import ClusterFinder, ClusterCollector, ClusterFinderMT, ClusterFileSink from .ClusterVector import ClusterVector diff --git a/python/src/bind_ClusterVector.hpp b/python/src/bind_ClusterVector.hpp index ecd7a77..db8c8a3 100644 --- a/python/src/bind_ClusterVector.hpp +++ b/python/src/bind_ClusterVector.hpp @@ -44,11 +44,10 @@ void define_ClusterVector(py::module &m, const std::string &typestr) { auto *vec = new std::vector(self.sum()); return return_vector(vec); }) - .def("sum_2x2", - [](ClusterVector &self) { - auto *vec = new std::vector(self.sum_2x2()); - return return_vector(vec); - }) + .def("sum_2x2", [](ClusterVector &self){ + auto *vec = new std::vector(self.sum_2x2()); + return return_vector(vec); + }) .def_property_readonly("size", &ClusterVector::size) .def("item_size", &ClusterVector::item_size) .def_property_readonly("fmt", diff --git a/python/src/cluster.hpp b/python/src/cluster.hpp index 6dd05ad..58f137c 100644 --- a/python/src/cluster.hpp +++ b/python/src/cluster.hpp @@ -93,6 +93,9 @@ void define_cluster_finder_mt_bindings(py::module &m, 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) diff --git a/python/src/module.cpp b/python/src/module.cpp index cac97dd..946a41b 100644 --- a/python/src/module.cpp +++ b/python/src/module.cpp @@ -10,13 +10,12 @@ #include "file.hpp" #include "fit.hpp" #include "interpolation.hpp" -#include "pedestal.hpp" -#include "pixel_map.hpp" -#include "raw_file.hpp" -#include "raw_master_file.hpp" -#include "var_cluster.hpp" #include "raw_sub_file.hpp" - +#include "raw_master_file.hpp" +#include "raw_file.hpp" +#include "pixel_map.hpp" +#include "var_cluster.hpp" +#include "pedestal.hpp" #include "jungfrau_data_file.hpp" // Pybind stuff diff --git a/src/ClusterFile.test.cpp b/src/ClusterFile.test.cpp index 39c45d4..f688c3d 100644 --- a/src/ClusterFile.test.cpp +++ b/src/ClusterFile.test.cpp @@ -10,8 +10,9 @@ using aare::Cluster; using aare::ClusterFile; using aare::ClusterVector; + TEST_CASE("Read one frame from a cluster file", "[.files]") { - // We know that the frame has 97 clusters + //We know that the frame has 97 clusters auto fpath = test_data_path() / "clust" / "single_frame_97_clustrers.clust"; REQUIRE(std::filesystem::exists(fpath));