diff --git a/include/aare/CalculateEta.hpp b/include/aare/CalculateEta.hpp index db17dad..2b28971 100644 --- a/include/aare/CalculateEta.hpp +++ b/include/aare/CalculateEta.hpp @@ -28,7 +28,7 @@ enum class pixel : int { template struct Eta2 { double x; double y; - int c; + int c{0}; T sum; }; @@ -70,6 +70,8 @@ calculate_eta2(const Cluster &cl) { size_t index_bottom_left_max_2x2_subcluster = (int(c / (ClusterSizeX - 1))) * ClusterSizeX + c % (ClusterSizeX - 1); + // calculate direction of gradient + // check that cluster center is in max subcluster if (cluster_center_index != index_bottom_left_max_2x2_subcluster && cluster_center_index != index_bottom_left_max_2x2_subcluster + 1 && @@ -128,12 +130,15 @@ Eta2 calculate_eta2(const Cluster &cl) { Eta2 eta{}; if ((cl.data[0] + cl.data[1]) != 0) - eta.x = static_cast(cl.data[1]) / (cl.data[0] + cl.data[1]); + eta.x = static_cast(cl.data[1]) / + (cl.data[0] + cl.data[1]); // between (0,1) the closer to zero + // left value probably larger if ((cl.data[0] + cl.data[2]) != 0) - eta.y = static_cast(cl.data[2]) / (cl.data[0] + cl.data[2]); + eta.y = static_cast(cl.data[2]) / + (cl.data[0] + cl.data[2]); // between (0,1) the closer to zero + // bottom value probably larger eta.sum = cl.sum(); - eta.c = static_cast(corner::cBottomLeft); // TODO! This is not correct, - // but need to put something + return eta; } @@ -150,13 +155,11 @@ template Eta2 calculate_eta3(const Cluster &cl) { eta.sum = sum; - eta.c = corner::cBottomLeft; - if ((cl.data[3] + cl.data[4] + cl.data[5]) != 0) eta.x = static_cast(-cl.data[3] + cl.data[3 + 2]) / - (cl.data[3] + cl.data[4] + cl.data[5]); + (cl.data[3] + cl.data[4] + cl.data[5]); // (-1,1) if ((cl.data[1] + cl.data[4] + cl.data[7]) != 0) diff --git a/python/aare/__init__.py b/python/aare/__init__.py index c48da49..7a17bd9 100644 --- a/python/aare/__init__.py +++ b/python/aare/__init__.py @@ -17,7 +17,7 @@ from .ClusterVector import ClusterVector from ._aare import fit_gaus, fit_pol1, fit_scurve, fit_scurve2 from ._aare import Interpolator from ._aare import calculate_eta2 - +from ._aare import reduce_to_2x2, reduce_to_3x3 from ._aare import apply_custom_weights diff --git a/python/src/bind_Cluster.hpp b/python/src/bind_Cluster.hpp index 690d0e8..e7da481 100644 --- a/python/src/bind_Cluster.hpp +++ b/python/src/bind_Cluster.hpp @@ -34,31 +34,54 @@ void define_Cluster(py::module &m, const std::string &typestr) { cluster.data[i] = r(i); } return cluster; - })); + })) - /* - //TODO! Review if to keep or not - .def_property( - "data", - [](ClusterType &c) -> py::array { - return py::array(py::buffer_info( - c.data, sizeof(Type), - py::format_descriptor::format(), // Type - // format - 1, // Number of dimensions - {static_cast(ClusterSizeX * - ClusterSizeY)}, // Shape (flattened) - {sizeof(Type)} // Stride (step size between elements) - )); + // TODO! Review if to keep or not + .def_property_readonly( + "data", + [](Cluster &c) + -> py::array { + return py::array(py::buffer_info( + c.data.data(), sizeof(Type), + py::format_descriptor::format(), // Type + // format + 2, // Number of dimensions + {static_cast(ClusterSizeX), + static_cast(ClusterSizeY)}, // Shape (flattened) + {sizeof(Type) * ClusterSizeY, sizeof(Type)} + // Stride (step size between elements) + )); + }) + + .def_readonly("x", + &Cluster::x) + + .def_readonly("y", + &Cluster::y); +} + +template +void reduce_to_3x3(py::module &m) { + + m.def( + "reduce_to_3x3", + [](const Cluster &cl) { + return reduce_to_3x3(cl); }, - [](ClusterType &c, py::array_t arr) { - py::buffer_info buf_info = arr.request(); - Type *ptr = static_cast(buf_info.ptr); - std::copy(ptr, ptr + ClusterSizeX * ClusterSizeY, - c.data); // TODO dont iterate over centers!!! + py::return_value_policy::move); +} - }); - */ +template +void reduce_to_2x2(py::module &m) { + + m.def( + "reduce_to_2x2", + [](const Cluster &cl) { + return reduce_to_2x2(cl); + }, + py::return_value_policy::move); } #pragma GCC diagnostic pop \ No newline at end of file diff --git a/python/src/module.cpp b/python/src/module.cpp index feafbd6..cd802e8 100644 --- a/python/src/module.cpp +++ b/python/src/module.cpp @@ -48,7 +48,8 @@ double, 'f' for float) define_ClusterCollector(m, "Cluster" #N "x" #M #TYPE_CODE); \ define_Cluster(m, #N "x" #M #TYPE_CODE); \ register_calculate_eta(m); \ - define_2x2_reduction(m); + define_2x2_reduction(m); \ + reduce_to_2x2(m); PYBIND11_MODULE(_aare, m) { define_file_io_bindings(m); @@ -86,16 +87,29 @@ PYBIND11_MODULE(_aare, m) { DEFINE_CLUSTER_BINDINGS(double, 9, 9, uint16_t, d); DEFINE_CLUSTER_BINDINGS(float, 9, 9, uint16_t, f); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); - define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + define_3x3_reduction(m); + + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); + reduce_to_3x3(m); } diff --git a/python/tests/test_Cluster.py b/python/tests/test_Cluster.py index ddaa6f3..506770b 100644 --- a/python/tests/test_Cluster.py +++ b/python/tests/test_Cluster.py @@ -101,6 +101,27 @@ def test_cluster_finder(): assert clusters.size == 0 +def test_2x2_reduction(): + """Test 2x2 Reduction""" + cluster = _aare.Cluster3x3i(5,5,np.array([1, 1, 1, 2, 3, 1, 2, 2, 1], dtype=np.int32)) + + reduced_cluster = _aare.reduce_to_2x2(cluster) + + assert reduced_cluster.x == 4 + assert reduced_cluster.y == 5 + assert (reduced_cluster.data == np.array([[2, 3], [2, 2]], dtype=np.int32)).all() + + +def test_3x3_reduction(): + """Test 3x3 Reduction""" + cluster = _aare.Cluster5x5d(5,5,np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 2.0, 2.0, 3.0, + 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=np.double)) + + reduced_cluster = _aare.reduce_to_3x3(cluster) + + assert reduced_cluster.x == 4 + assert reduced_cluster.y == 5 + assert (reduced_cluster.data == np.array([[1.0, 2.0, 1.0], [2.0, 2.0, 3.0], [1.0, 2.0, 1.0]], dtype=np.double)).all() diff --git a/python/tests/test_ClusterVector.py b/python/tests/test_ClusterVector.py index b64aeef..fa98e3f 100644 --- a/python/tests/test_ClusterVector.py +++ b/python/tests/test_ClusterVector.py @@ -5,7 +5,7 @@ import time from pathlib import Path import pickle -from aare import ClusterFile +from aare import ClusterFile, ClusterVector from aare import _aare from conftest import test_data_path @@ -51,4 +51,36 @@ def test_make_a_hitmap_from_cluster_vector(): # print(img) # print(ref) assert (img == ref).all() - \ No newline at end of file + + +def test_2x2_reduction(): + cv = ClusterVector((3,3)) + + cv.push_back(_aare.Cluster3x3i(5, 5, np.array([1, 1, 1, 2, 3, 1, 2, 2, 1], dtype=np.int32))) + cv.push_back(_aare.Cluster3x3i(5, 5, np.array([2, 2, 1, 2, 3, 1, 1, 1, 1], dtype=np.int32))) + + reduced_cv = np.array(_aare.reduce_to_2x2(cv), copy=False) + + assert reduced_cv.size == 2 + assert reduced_cv[0]["x"] == 4 + assert reduced_cv[0]["y"] == 5 + assert (reduced_cv[0]["data"] == np.array([[2, 3], [2, 2]], dtype=np.int32)).all() + assert reduced_cv[1]["x"] == 4 + assert reduced_cv[1]["y"] == 6 + assert (reduced_cv[1]["data"] == np.array([[2, 2], [2, 3]], dtype=np.int32)).all() + + +def test_3x3_reduction(): + cv = _aare.ClusterVector_Cluster5x5d() + + cv.push_back(_aare.Cluster5x5d(5,5,np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 2.0, 2.0, 3.0, + 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=np.double))) + cv.push_back(_aare.Cluster5x5d(5,5,np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 2.0, 2.0, 3.0, + 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=np.double))) + + reduced_cv = np.array(_aare.reduce_to_3x3(cv), copy=False) + + assert reduced_cv.size == 2 + assert reduced_cv[0]["x"] == 4 + assert reduced_cv[0]["y"] == 5 + assert (reduced_cv[0]["data"] == np.array([[1.0, 2.0, 1.0], [2.0, 2.0, 3.0], [1.0, 2.0, 1.0]], dtype=np.double)).all() \ No newline at end of file diff --git a/src/Cluster.test.cpp b/src/Cluster.test.cpp index 2d54363..c1e424a 100644 --- a/src/Cluster.test.cpp +++ b/src/Cluster.test.cpp @@ -65,7 +65,7 @@ TEST_CASE("Test reduce to 2x2 Cluster", "[.cluster]") { expected_reduced_cluster.data.begin())); } -TEST_CASE("Test reduce to 3x3 Clsuter", "[.cluster]") { +TEST_CASE("Test reduce to 3x3 Cluster", "[.cluster]") { auto [cluster, expected_reduced_cluster] = GENERATE( std::make_tuple(ClusterTypesLargerThan2x2{Cluster{ 5, 5, {1, 1, 1, 1, 3, 1, 1, 1, 1}}},