Generalized calculate_eta2 function to work with general cluster types

This commit is contained in:
mazzol_a 2025-03-28 14:29:20 +01:00
parent 0876b6891a
commit f8f98b6ec3
3 changed files with 106 additions and 36 deletions

View File

@ -31,7 +31,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
# General options
option(AARE_PYTHON_BINDINGS "Build python bindings" ON)
option(AARE_PYTHON_BINDINGS "Build python bindings" OFF)
option(AARE_TESTS "Build tests" OFF)
option(AARE_BENCHMARKS "Build benchmarks" OFF)
option(AARE_EXAMPLES "Build examples" OFF)
@ -307,7 +307,8 @@ endif()
set(PUBLICHEADERS
include/aare/ArrayExpr.hpp
include/aare/ClusterFinder.hpp
include/aare/Cluster.hpp
#include/aare/ClusterFinder.hpp
include/aare/ClusterFile.hpp
include/aare/CtbRawFile.hpp
include/aare/ClusterVector.hpp
@ -328,7 +329,7 @@ set(PUBLICHEADERS
include/aare/RawFile.hpp
include/aare/RawMasterFile.hpp
include/aare/RawSubFile.hpp
include/aare/VarClusterFinder.hpp
#include/aare/VarClusterFinder.hpp
include/aare/utils/task.hpp
)
@ -336,7 +337,7 @@ set(PUBLICHEADERS
set(SourceFiles
${CMAKE_CURRENT_SOURCE_DIR}/src/CtbRawFile.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterFile.cpp
#${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterFile.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/defs.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Dtype.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/decode.cpp
@ -394,8 +395,9 @@ if(AARE_TESTS)
${CMAKE_CURRENT_SOURCE_DIR}/src/RawMasterFile.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/NDArray.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/NDView.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterFinder.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterVector.test.cpp
#${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterFinder.test.cpp
#${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterVector.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Cluster.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Pedestal.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/NumpyFile.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/NumpyHelpers.test.cpp

View File

@ -28,10 +28,11 @@ typedef enum {
pTopRight = 8
} pixel;
// TODO: maybe template this!!!!!! why int32_t????
struct Eta2 {
double x;
double y;
corner c;
int c;
int32_t sum;
};
@ -131,22 +132,22 @@ int analyze_cluster(Cluster<int32_t, 3, 3> &cl, int32_t *t2, int32_t *t3,
char *quad, double *eta2x, double *eta2y, double *eta3x,
double *eta3y);
template <typename ClusterType,
typename = std::enable_if_t<is_cluster_v<ClusterType>>>
NDArray<double, 2> calculate_eta2(ClusterVector<ClusterType> &clusters);
// template <typename ClusterType,
// typename = std::enable_if_t<is_cluster_v<ClusterType>>>
// NDArray<double, 2> calculate_eta2(ClusterVector<ClusterType> &clusters);
// TODO: do we need rquire clauses?
template <typename T> Eta2 calculate_eta2(const Cluster<T, 3, 3> &cl);
// template <typename T> Eta2 calculate_eta2(const Cluster<T, 3, 3> &cl);
template <typename T> Eta2 calculate_eta2(const Cluster<T, 2, 2> &cl);
// template <typename T> Eta2 calculate_eta2(const Cluster<T, 2, 2> &cl);
template <typename ClusterType, std::enable_if_t<is_cluster_v<ClusterType>>>
Eta2 calculate_eta2(const ClusterType &cl);
// template <typename ClusterType, std::enable_if_t<is_cluster_v<ClusterType>>>
// Eta2 calculate_eta2(const ClusterType &cl);
template <typename T, uint8_t ClusterSizeX, uint8_t ClusterSizeY,
typename CoordType>
Eta2 calculate_eta2(
const Cluster<T, ClusterSizeX, ClusterSizeY, CoordType> &cl);
// template <typename T, uint8_t ClusterSizeX, uint8_t ClusterSizeY,
// typename CoordType>
// Eta2 calculate_eta2(
// const Cluster<T, ClusterSizeX, ClusterSizeY, CoordType> &cl);
template <typename ClusterType, typename Enable>
ClusterFile<ClusterType, Enable>::ClusterFile(
@ -379,7 +380,7 @@ NDArray<double, 2> calculate_eta2(const ClusterVector<ClusterType> &clusters) {
NDArray<double, 2> eta2({static_cast<int64_t>(clusters.size()), 2});
for (size_t i = 0; i < clusters.size(); i++) {
auto e = calculate_eta2<ClusterType>(clusters.at(i));
auto e = calculate_eta2(clusters.at(i));
eta2(i, 0) = e.x;
eta2(i, 1) = e.y;
}
@ -417,25 +418,23 @@ Eta2 calculate_eta2(
eta.sum = sum_2x2_subcluster[c];
eta.x = static_cast<double>(cl.data[(c + 1) * ClusterSizeX + 1]) /
(cl.data[0] + cl.data[1]);
size_t index_bottom_left_max_2x2_subcluster =
(int(c / (ClusterSizeX - 1))) * ClusterSizeX + c % (ClusterSizeX - 1);
size_t index_top_left_2x2_subcluster =
(int(c / (ClusterSizeX - 1)) + 1) * ClusterSizeX +
c % (ClusterSizeX - 1) * 2 + 1;
if ((cl.data[index_top_left_2x2_subcluster] +
cl.data[index_top_left_2x2_subcluster - 1]) != 0)
eta.x =
static_cast<double>(cl.data[index_top_left_2x2_subcluster] /
(cl.data[index_top_left_2x2_subcluster] +
cl.data[index_top_left_2x2_subcluster - 1]));
if ((cl.data[index_bottom_left_max_2x2_subcluster] +
cl.data[index_bottom_left_max_2x2_subcluster + 1]) != 0)
eta.x = static_cast<double>(
cl.data[index_bottom_left_max_2x2_subcluster + 1]) /
(cl.data[index_bottom_left_max_2x2_subcluster] +
cl.data[index_bottom_left_max_2x2_subcluster + 1]);
if ((cl.data[index_top_left_2x2_subcluster] +
cl.data[index_top_left_2x2_subcluster - ClusterSizeX]) != 0)
eta.y = static_cast<double>(
cl.data[index_top_left_2x2_subcluster] /
(cl.data[index_top_left_2x2_subcluster] +
cl.data[index_top_left_2x2_subcluster - ClusterSizeX]));
if ((cl.data[index_bottom_left_max_2x2_subcluster] +
cl.data[index_bottom_left_max_2x2_subcluster + ClusterSizeX]) != 0)
eta.y =
static_cast<double>(
cl.data[index_bottom_left_max_2x2_subcluster + ClusterSizeX]) /
(cl.data[index_bottom_left_max_2x2_subcluster] +
cl.data[index_bottom_left_max_2x2_subcluster + ClusterSizeX]);
eta.c = c; // TODO only supported for 2x2 and 3x3 clusters -> at least no
// underyling enum class
@ -446,6 +445,7 @@ Eta2 calculate_eta2(
* @brief Calculate the eta2 values for a 3x3 cluster and return them in a Eta2
* struct containing etay, etax and the corner of the cluster.
*/
/*
template <typename T> Eta2 calculate_eta2(const Cluster<T, 3, 3> &cl) {
Eta2 eta{};
@ -489,7 +489,9 @@ template <typename T> Eta2 calculate_eta2(const Cluster<T, 3, 3> &cl) {
}
return eta;
}
*/
/*
template <typename T> Eta2 calculate_eta2(const Cluster<T, 2, 2> &cl) {
Eta2 eta{};
@ -499,6 +501,7 @@ template <typename T> Eta2 calculate_eta2(const Cluster<T, 2, 2> &cl) {
eta.c = cBottomLeft; // TODO! This is not correct, but need to put something
return eta;
}
*/
// TODO complicated API simplify?
int analyze_cluster(Cluster<int32_t, 3, 3> &cl, int32_t *t2, int32_t *t3,

65
src/Cluster.test.cpp Normal file
View File

@ -0,0 +1,65 @@
/************************************************
* @file test-Cluster.cpp
* @short test case for generic Cluster, ClusterVector, and calculate_eta2
***********************************************/
#include "aare/Cluster.hpp"
#include "aare/ClusterFile.hpp"
// #include "catch.hpp"
#include <array>
#include <catch2/catch_all.hpp>
#include <catch2/catch_test_macros.hpp>
using namespace aare;
/*
TEST_CASE("Correct Instantiation of Cluster and ClusterVector",
"[.cluster][.instantiation]") {
REQUIRE(not std::is_constructible_v<aare::Cluster<std::string, 4, 4>>);
// all 1,2 and 0,4 are not defined!!
std::make_tuple(Cluster<int, 1, 1>, ),
std::make_tuple(Cluster<int, 0, 0>, )
}
*/
using ClusterTypes =
std::variant<Cluster<int, 2, 2>, Cluster<int, 3, 3>, Cluster<int, 5, 5>,
Cluster<int, 4, 2>, Cluster<int, 2, 3>>;
TEST_CASE("calculate_eta2", "[.cluster][.instantiation]") {
// weird expect cluster_start to be in bottom_left corner -> row major ->
// check how its used should be an image!!
auto [cluster, expected_eta] = GENERATE(
std::make_tuple(ClusterTypes{Cluster<int, 2, 2>{0, 0, {1, 2, 3, 1}}},
Eta2{2. / 3, 3. / 4, corner::cBottomLeft, 7}),
std::make_tuple(
ClusterTypes{Cluster<int, 3, 3>{0, 0, {1, 2, 3, 4, 5, 6, 1, 2, 7}}},
Eta2{6. / 11, 2. / 7, corner::cTopRight, 20}),
std::make_tuple(ClusterTypes{Cluster<int, 5, 5>{
0, 0, {1, 6, 7, 6, 5, 4, 3, 2, 1, 8, 8, 9, 2,
1, 4, 5, 6, 7, 8, 4, 1, 1, 1, 1, 1}}},
Eta2{9. / 17, 5. / 13, 8, 28}),
std::make_tuple(
ClusterTypes{Cluster<int, 4, 2>{0, 0, {1, 4, 7, 2, 5, 6, 4, 3}}},
Eta2{7. / 11, 6. / 10, 1, 21}),
std::make_tuple(
ClusterTypes{Cluster<int, 2, 3>{0, 0, {1, 3, 2, 3, 4, 2}}},
Eta2{3. / 5, 4. / 6, 1, 11}));
Eta2 eta = std::visit(
[](const auto &clustertype) { return calculate_eta2(clustertype); },
cluster);
CHECK(eta.x == expected_eta.x);
CHECK(eta.y == expected_eta.y);
CHECK(eta.c == expected_eta.c);
CHECK(eta.sum == expected_eta.sum);
}