mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2026-02-19 10:08:40 +01:00
reduction tests for python
This commit is contained in:
@@ -28,7 +28,7 @@ enum class pixel : int {
|
|||||||
template <typename T> struct Eta2 {
|
template <typename T> struct Eta2 {
|
||||||
double x;
|
double x;
|
||||||
double y;
|
double y;
|
||||||
int c;
|
int c{0};
|
||||||
T sum;
|
T sum;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -70,6 +70,8 @@ calculate_eta2(const Cluster<T, ClusterSizeX, ClusterSizeY, CoordType> &cl) {
|
|||||||
size_t index_bottom_left_max_2x2_subcluster =
|
size_t index_bottom_left_max_2x2_subcluster =
|
||||||
(int(c / (ClusterSizeX - 1))) * ClusterSizeX + c % (ClusterSizeX - 1);
|
(int(c / (ClusterSizeX - 1))) * ClusterSizeX + c % (ClusterSizeX - 1);
|
||||||
|
|
||||||
|
// calculate direction of gradient
|
||||||
|
|
||||||
// check that cluster center is in max subcluster
|
// check that cluster center is in max subcluster
|
||||||
if (cluster_center_index != index_bottom_left_max_2x2_subcluster &&
|
if (cluster_center_index != index_bottom_left_max_2x2_subcluster &&
|
||||||
cluster_center_index != index_bottom_left_max_2x2_subcluster + 1 &&
|
cluster_center_index != index_bottom_left_max_2x2_subcluster + 1 &&
|
||||||
@@ -128,12 +130,15 @@ Eta2<T> calculate_eta2(const Cluster<T, 2, 2, int16_t> &cl) {
|
|||||||
Eta2<T> eta{};
|
Eta2<T> eta{};
|
||||||
|
|
||||||
if ((cl.data[0] + cl.data[1]) != 0)
|
if ((cl.data[0] + cl.data[1]) != 0)
|
||||||
eta.x = static_cast<double>(cl.data[1]) / (cl.data[0] + cl.data[1]);
|
eta.x = static_cast<double>(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)
|
if ((cl.data[0] + cl.data[2]) != 0)
|
||||||
eta.y = static_cast<double>(cl.data[2]) / (cl.data[0] + cl.data[2]);
|
eta.y = static_cast<double>(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.sum = cl.sum();
|
||||||
eta.c = static_cast<int>(corner::cBottomLeft); // TODO! This is not correct,
|
|
||||||
// but need to put something
|
|
||||||
return eta;
|
return eta;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,13 +155,11 @@ template <typename T> Eta2<T> calculate_eta3(const Cluster<T, 3, 3> &cl) {
|
|||||||
|
|
||||||
eta.sum = sum;
|
eta.sum = sum;
|
||||||
|
|
||||||
eta.c = corner::cBottomLeft;
|
|
||||||
|
|
||||||
if ((cl.data[3] + cl.data[4] + cl.data[5]) != 0)
|
if ((cl.data[3] + cl.data[4] + cl.data[5]) != 0)
|
||||||
|
|
||||||
eta.x = static_cast<double>(-cl.data[3] + cl.data[3 + 2]) /
|
eta.x = static_cast<double>(-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)
|
if ((cl.data[1] + cl.data[4] + cl.data[7]) != 0)
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ from .ClusterVector import ClusterVector
|
|||||||
from ._aare import fit_gaus, fit_pol1, fit_scurve, fit_scurve2
|
from ._aare import fit_gaus, fit_pol1, fit_scurve, fit_scurve2
|
||||||
from ._aare import Interpolator
|
from ._aare import Interpolator
|
||||||
from ._aare import calculate_eta2
|
from ._aare import calculate_eta2
|
||||||
|
from ._aare import reduce_to_2x2, reduce_to_3x3
|
||||||
|
|
||||||
from ._aare import apply_custom_weights
|
from ._aare import apply_custom_weights
|
||||||
|
|
||||||
|
|||||||
@@ -34,31 +34,54 @@ void define_Cluster(py::module &m, const std::string &typestr) {
|
|||||||
cluster.data[i] = r(i);
|
cluster.data[i] = r(i);
|
||||||
}
|
}
|
||||||
return cluster;
|
return cluster;
|
||||||
}));
|
}))
|
||||||
|
|
||||||
/*
|
// TODO! Review if to keep or not
|
||||||
//TODO! Review if to keep or not
|
.def_property_readonly(
|
||||||
.def_property(
|
"data",
|
||||||
"data",
|
[](Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType> &c)
|
||||||
[](ClusterType &c) -> py::array {
|
-> py::array {
|
||||||
return py::array(py::buffer_info(
|
return py::array(py::buffer_info(
|
||||||
c.data, sizeof(Type),
|
c.data.data(), sizeof(Type),
|
||||||
py::format_descriptor<Type>::format(), // Type
|
py::format_descriptor<Type>::format(), // Type
|
||||||
// format
|
// format
|
||||||
1, // Number of dimensions
|
2, // Number of dimensions
|
||||||
{static_cast<ssize_t>(ClusterSizeX *
|
{static_cast<ssize_t>(ClusterSizeX),
|
||||||
ClusterSizeY)}, // Shape (flattened)
|
static_cast<ssize_t>(ClusterSizeY)}, // Shape (flattened)
|
||||||
{sizeof(Type)} // Stride (step size between elements)
|
{sizeof(Type) * ClusterSizeY, sizeof(Type)}
|
||||||
));
|
// Stride (step size between elements)
|
||||||
|
));
|
||||||
|
})
|
||||||
|
|
||||||
|
.def_readonly("x",
|
||||||
|
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::x)
|
||||||
|
|
||||||
|
.def_readonly("y",
|
||||||
|
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, uint8_t ClusterSizeX, uint8_t ClusterSizeY,
|
||||||
|
typename CoordType = int16_t>
|
||||||
|
void reduce_to_3x3(py::module &m) {
|
||||||
|
|
||||||
|
m.def(
|
||||||
|
"reduce_to_3x3",
|
||||||
|
[](const Cluster<T, ClusterSizeX, ClusterSizeY, CoordType> &cl) {
|
||||||
|
return reduce_to_3x3(cl);
|
||||||
},
|
},
|
||||||
[](ClusterType &c, py::array_t<Type> arr) {
|
py::return_value_policy::move);
|
||||||
py::buffer_info buf_info = arr.request();
|
}
|
||||||
Type *ptr = static_cast<Type *>(buf_info.ptr);
|
|
||||||
std::copy(ptr, ptr + ClusterSizeX * ClusterSizeY,
|
|
||||||
c.data); // TODO dont iterate over centers!!!
|
|
||||||
|
|
||||||
});
|
template <typename T, uint8_t ClusterSizeX, uint8_t ClusterSizeY,
|
||||||
*/
|
typename CoordType = int16_t>
|
||||||
|
void reduce_to_2x2(py::module &m) {
|
||||||
|
|
||||||
|
m.def(
|
||||||
|
"reduce_to_2x2",
|
||||||
|
[](const Cluster<T, ClusterSizeX, ClusterSizeY, CoordType> &cl) {
|
||||||
|
return reduce_to_2x2(cl);
|
||||||
|
},
|
||||||
|
py::return_value_policy::move);
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
@@ -48,7 +48,8 @@ double, 'f' for float)
|
|||||||
define_ClusterCollector<T, N, M, U>(m, "Cluster" #N "x" #M #TYPE_CODE); \
|
define_ClusterCollector<T, N, M, U>(m, "Cluster" #N "x" #M #TYPE_CODE); \
|
||||||
define_Cluster<T, N, M, U>(m, #N "x" #M #TYPE_CODE); \
|
define_Cluster<T, N, M, U>(m, #N "x" #M #TYPE_CODE); \
|
||||||
register_calculate_eta<T, N, M, U>(m); \
|
register_calculate_eta<T, N, M, U>(m); \
|
||||||
define_2x2_reduction<T, N, M, U>(m);
|
define_2x2_reduction<T, N, M, U>(m); \
|
||||||
|
reduce_to_2x2<T, N, M, U>(m);
|
||||||
|
|
||||||
PYBIND11_MODULE(_aare, m) {
|
PYBIND11_MODULE(_aare, m) {
|
||||||
define_file_io_bindings(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(double, 9, 9, uint16_t, d);
|
||||||
DEFINE_CLUSTER_BINDINGS(float, 9, 9, uint16_t, f);
|
DEFINE_CLUSTER_BINDINGS(float, 9, 9, uint16_t, f);
|
||||||
|
|
||||||
define_3x3_reduction<int, 3, 3>(m);
|
define_3x3_reduction<int, 3, 3, uint16_t>(m);
|
||||||
define_3x3_reduction<double, 3, 3>(m);
|
define_3x3_reduction<double, 3, 3, uint16_t>(m);
|
||||||
define_3x3_reduction<float, 3, 3>(m);
|
define_3x3_reduction<float, 3, 3, uint16_t>(m);
|
||||||
define_3x3_reduction<int, 5, 5>(m);
|
define_3x3_reduction<int, 5, 5, uint16_t>(m);
|
||||||
define_3x3_reduction<double, 5, 5>(m);
|
define_3x3_reduction<double, 5, 5, uint16_t>(m);
|
||||||
define_3x3_reduction<float, 5, 5>(m);
|
define_3x3_reduction<float, 5, 5, uint16_t>(m);
|
||||||
define_3x3_reduction<int, 7, 7>(m);
|
define_3x3_reduction<int, 7, 7, uint16_t>(m);
|
||||||
define_3x3_reduction<double, 7, 7>(m);
|
define_3x3_reduction<double, 7, 7, uint16_t>(m);
|
||||||
define_3x3_reduction<float, 7, 7>(m);
|
define_3x3_reduction<float, 7, 7, uint16_t>(m);
|
||||||
define_3x3_reduction<int, 9, 9>(m);
|
define_3x3_reduction<int, 9, 9, uint16_t>(m);
|
||||||
define_3x3_reduction<double, 9, 9>(m);
|
define_3x3_reduction<double, 9, 9, uint16_t>(m);
|
||||||
define_3x3_reduction<float, 9, 9>(m);
|
define_3x3_reduction<float, 9, 9, uint16_t>(m);
|
||||||
|
|
||||||
|
reduce_to_3x3<int, 3, 3, uint16_t>(m);
|
||||||
|
reduce_to_3x3<double, 3, 3, uint16_t>(m);
|
||||||
|
reduce_to_3x3<float, 3, 3, uint16_t>(m);
|
||||||
|
reduce_to_3x3<int, 5, 5, uint16_t>(m);
|
||||||
|
reduce_to_3x3<double, 5, 5, uint16_t>(m);
|
||||||
|
reduce_to_3x3<float, 5, 5, uint16_t>(m);
|
||||||
|
reduce_to_3x3<int, 7, 7, uint16_t>(m);
|
||||||
|
reduce_to_3x3<double, 7, 7, uint16_t>(m);
|
||||||
|
reduce_to_3x3<float, 7, 7, uint16_t>(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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,6 +101,27 @@ def test_cluster_finder():
|
|||||||
assert clusters.size == 0
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import time
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
from aare import ClusterFile
|
from aare import ClusterFile, ClusterVector
|
||||||
from aare import _aare
|
from aare import _aare
|
||||||
from conftest import test_data_path
|
from conftest import test_data_path
|
||||||
|
|
||||||
@@ -51,4 +51,36 @@ def test_make_a_hitmap_from_cluster_vector():
|
|||||||
# print(img)
|
# print(img)
|
||||||
# print(ref)
|
# print(ref)
|
||||||
assert (img == ref).all()
|
assert (img == ref).all()
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
@@ -65,7 +65,7 @@ TEST_CASE("Test reduce to 2x2 Cluster", "[.cluster]") {
|
|||||||
expected_reduced_cluster.data.begin()));
|
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(
|
auto [cluster, expected_reduced_cluster] = GENERATE(
|
||||||
std::make_tuple(ClusterTypesLargerThan2x2{Cluster<int, 3, 3>{
|
std::make_tuple(ClusterTypesLargerThan2x2{Cluster<int, 3, 3>{
|
||||||
5, 5, {1, 1, 1, 1, 3, 1, 1, 1, 1}}},
|
5, 5, {1, 1, 1, 1, 3, 1, 1, 1, 1}}},
|
||||||
|
|||||||
Reference in New Issue
Block a user