mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2026-02-20 04:18:40 +01:00
make max_sum_2x2 properly accessible from python
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "defs.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -45,7 +46,7 @@ struct Cluster {
|
|||||||
* @return photon energy of subcluster, 2x2 subcluster index relative to
|
* @return photon energy of subcluster, 2x2 subcluster index relative to
|
||||||
* cluster center
|
* cluster center
|
||||||
*/
|
*/
|
||||||
std::pair<T, int> max_sum_2x2() const {
|
Sum_index_pair<T, int> max_sum_2x2() const {
|
||||||
|
|
||||||
if constexpr (cluster_size_x == 3 && cluster_size_y == 3) {
|
if constexpr (cluster_size_x == 3 && cluster_size_y == 3) {
|
||||||
std::array<T, 4> sum_2x2_subclusters;
|
std::array<T, 4> sum_2x2_subclusters;
|
||||||
@@ -56,9 +57,10 @@ struct Cluster {
|
|||||||
int index = std::max_element(sum_2x2_subclusters.begin(),
|
int index = std::max_element(sum_2x2_subclusters.begin(),
|
||||||
sum_2x2_subclusters.end()) -
|
sum_2x2_subclusters.end()) -
|
||||||
sum_2x2_subclusters.begin();
|
sum_2x2_subclusters.begin();
|
||||||
return std::make_pair(sum_2x2_subclusters[index], index);
|
return Sum_index_pair<T, int>{sum_2x2_subclusters[index], index};
|
||||||
} else if constexpr (cluster_size_x == 2 && cluster_size_y == 2) {
|
} else if constexpr (cluster_size_x == 2 && cluster_size_y == 2) {
|
||||||
return std::make_pair(data[0] + data[1] + data[2] + data[3], 0);
|
return Sum_index_pair<T, int>{data[0] + data[1] + data[2] + data[3],
|
||||||
|
0};
|
||||||
} else {
|
} else {
|
||||||
constexpr size_t cluster_center_index =
|
constexpr size_t cluster_center_index =
|
||||||
(ClusterSizeX / 2) + (ClusterSizeY / 2) * ClusterSizeX;
|
(ClusterSizeX / 2) + (ClusterSizeY / 2) * ClusterSizeX;
|
||||||
@@ -97,7 +99,7 @@ struct Cluster {
|
|||||||
int index = std::max_element(sum_2x2_subcluster.begin(),
|
int index = std::max_element(sum_2x2_subcluster.begin(),
|
||||||
sum_2x2_subcluster.end()) -
|
sum_2x2_subcluster.end()) -
|
||||||
sum_2x2_subcluster.begin();
|
sum_2x2_subcluster.begin();
|
||||||
return std::make_pair(sum_2x2_subcluster[index], index);
|
return Sum_index_pair<T, int>{sum_2x2_subcluster[index], index};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -86,15 +86,14 @@ class ClusterVector<Cluster<T, ClusterSizeX, ClusterSizeY, CoordType>> {
|
|||||||
/**
|
/**
|
||||||
* @brief Sum the pixels in the 2x2 subcluster with the biggest pixel sum in
|
* @brief Sum the pixels in the 2x2 subcluster with the biggest pixel sum in
|
||||||
* each cluster
|
* each cluster
|
||||||
* @return std::vector<T> vector of sums for each cluster
|
* @return vector of sums index pairs for each cluster
|
||||||
*/
|
*/
|
||||||
std::vector<T> sum_2x2() {
|
std::vector<Sum_index_pair<T, int>> sum_2x2() {
|
||||||
std::vector<T> sums_2x2(m_data.size());
|
std::vector<Sum_index_pair<T, int>> sums_2x2(m_data.size());
|
||||||
|
|
||||||
std::transform(m_data.begin(), m_data.end(), sums_2x2.begin(),
|
std::transform(
|
||||||
[](const ClusterType &cluster) {
|
m_data.begin(), m_data.end(), sums_2x2.begin(),
|
||||||
return cluster.max_sum_2x2().first;
|
[](const ClusterType &cluster) { return cluster.max_sum_2x2(); });
|
||||||
});
|
|
||||||
|
|
||||||
return sums_2x2;
|
return sums_2x2;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -331,6 +331,12 @@ enum DACIndex {
|
|||||||
SLOW_ADC_TEMP
|
SLOW_ADC_TEMP
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// helper pair class to easily expose in python
|
||||||
|
template <typename T1, typename T2> struct Sum_index_pair {
|
||||||
|
T1 sum;
|
||||||
|
T2 index;
|
||||||
|
};
|
||||||
|
|
||||||
enum class TimingMode { Auto, Trigger };
|
enum class TimingMode { Auto, Trigger };
|
||||||
enum class FrameDiscardPolicy { NoDiscard, Discard, DiscardPartial };
|
enum class FrameDiscardPolicy { NoDiscard, Discard, DiscardPartial };
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,11 @@ void define_Cluster(py::module &m, const std::string &typestr) {
|
|||||||
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::x)
|
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::x)
|
||||||
|
|
||||||
.def_readonly("y",
|
.def_readonly("y",
|
||||||
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::y);
|
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::y)
|
||||||
|
|
||||||
|
.def(
|
||||||
|
"max_sum_2x2",
|
||||||
|
&Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>::max_sum_2x2);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, uint8_t ClusterSizeX, uint8_t ClusterSizeY,
|
template <typename T, uint8_t ClusterSizeX, uint8_t ClusterSizeY,
|
||||||
|
|||||||
@@ -6,12 +6,15 @@
|
|||||||
#include "aare/NDView.hpp"
|
#include "aare/NDView.hpp"
|
||||||
#include "aare/Pedestal.hpp"
|
#include "aare/Pedestal.hpp"
|
||||||
#include "np_helper.hpp"
|
#include "np_helper.hpp"
|
||||||
|
#include "utils/bind_Vector.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#define PYBIND11_DETAILED_ERROR_MESSAGES
|
||||||
|
#define PYBIND11_DISABLE_STL_CONTAINERS
|
||||||
#include <pybind11/pybind11.h>
|
#include <pybind11/pybind11.h>
|
||||||
#include <pybind11/stl.h>
|
// #include <pybind11/stl.h>
|
||||||
#include <pybind11/stl_bind.h>
|
// #include <pybind11/stl_bind.h>
|
||||||
|
|
||||||
namespace py = pybind11;
|
namespace py = pybind11;
|
||||||
using pd_type = double;
|
using pd_type = double;
|
||||||
@@ -46,8 +49,31 @@ void define_ClusterVector(py::module &m, const std::string &typestr) {
|
|||||||
})
|
})
|
||||||
.def("sum_2x2",
|
.def("sum_2x2",
|
||||||
[](ClusterVector<ClusterType> &self) {
|
[](ClusterVector<ClusterType> &self) {
|
||||||
auto *vec = new std::vector<Type>(self.sum_2x2());
|
// auto *vec =
|
||||||
return return_vector(vec);
|
// new std::vector<Sum_index_pair<Type, int>>(self.sum_2x2());
|
||||||
|
|
||||||
|
/*
|
||||||
|
py::capsule free_when_done(vec, [](void *f) {
|
||||||
|
std::vector<std::pair<Type, int>> *foo =
|
||||||
|
reinterpret_cast<std::vector<std::pair<Type, int>> *>(
|
||||||
|
f);
|
||||||
|
delete foo;
|
||||||
|
});
|
||||||
|
|
||||||
|
py::buffer result(
|
||||||
|
vec->data(),
|
||||||
|
static_cast<py::ssize_t>(sizeof(std::pair<Type, int>)),
|
||||||
|
fmt::format("T{{{}:sum:i:index}}",
|
||||||
|
py::format_descriptor<Type>::format()),
|
||||||
|
static_cast<py::ssize_t>(1),
|
||||||
|
std::vector<py::ssize_t>{
|
||||||
|
static_cast<py::ssize_t>(vec->size())},
|
||||||
|
std::vector<py::ssize_t>{static_cast<py::ssize_t>(
|
||||||
|
sizeof(std::pair<Type, int>))});
|
||||||
|
//,static_cast<py::object>(free_when_done));
|
||||||
|
*/
|
||||||
|
|
||||||
|
return self.sum_2x2(); // return_vector(vec);
|
||||||
})
|
})
|
||||||
.def_property_readonly("size", &ClusterVector<ClusterType>::size)
|
.def_property_readonly("size", &ClusterVector<ClusterType>::size)
|
||||||
.def("item_size", &ClusterVector<ClusterType>::item_size)
|
.def("item_size", &ClusterVector<ClusterType>::item_size)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "bind_ClusterFinderMT.hpp"
|
#include "bind_ClusterFinderMT.hpp"
|
||||||
#include "bind_ClusterVector.hpp"
|
#include "bind_ClusterVector.hpp"
|
||||||
#include "bind_calibration.hpp"
|
#include "bind_calibration.hpp"
|
||||||
|
#include "utils/bind_Vector.hpp"
|
||||||
|
|
||||||
// TODO! migrate the other names
|
// TODO! migrate the other names
|
||||||
#include "ctb_raw_file.hpp"
|
#include "ctb_raw_file.hpp"
|
||||||
@@ -114,4 +115,19 @@ PYBIND11_MODULE(_aare, m) {
|
|||||||
reduce_to_3x3<int, 9, 9, uint16_t>(m);
|
reduce_to_3x3<int, 9, 9, uint16_t>(m);
|
||||||
reduce_to_3x3<double, 9, 9, uint16_t>(m);
|
reduce_to_3x3<double, 9, 9, uint16_t>(m);
|
||||||
reduce_to_3x3<float, 9, 9, uint16_t>(m);
|
reduce_to_3x3<float, 9, 9, uint16_t>(m);
|
||||||
|
|
||||||
|
define_Vector<Sum_index_pair<double, int>>(
|
||||||
|
m, "Sum_index_pair_d",
|
||||||
|
fmt::format("T{{{}:sum:i:index}}",
|
||||||
|
py::format_descriptor<double>::format()));
|
||||||
|
|
||||||
|
define_Vector<Sum_index_pair<float, int>>(
|
||||||
|
m, "Sum_index_pair_f",
|
||||||
|
fmt::format("T{{{}:sum:i:index}}",
|
||||||
|
py::format_descriptor<double>::format()));
|
||||||
|
|
||||||
|
define_Vector<Sum_index_pair<int, int>>(
|
||||||
|
m, "Sum_index_pair_i",
|
||||||
|
fmt::format("T{{{}:sum:i:index}}",
|
||||||
|
py::format_descriptor<double>::format()));
|
||||||
}
|
}
|
||||||
|
|||||||
30
python/src/utils/bind_Vector.hpp
Normal file
30
python/src/utils/bind_Vector.hpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <pybind11/pybind11.h>
|
||||||
|
// #include <pybind11/stl.h>
|
||||||
|
// #include <pybind11/stl_bind.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace py = pybind11;
|
||||||
|
|
||||||
|
// TODO add index, itemize
|
||||||
|
template <typename Type>
|
||||||
|
void define_Vector(py::module &m, const std::string &type_str,
|
||||||
|
const std::string &format_string) {
|
||||||
|
auto class_name = "Vector_" + type_str;
|
||||||
|
|
||||||
|
py::class_<std::vector<Type>>(m, class_name.c_str(), py::buffer_protocol())
|
||||||
|
|
||||||
|
.def(py::init())
|
||||||
|
|
||||||
|
.def_buffer([&format_string](
|
||||||
|
std::vector<Type> &self) -> py::buffer_info {
|
||||||
|
return py::buffer_info(
|
||||||
|
self.data(), static_cast<py::ssize_t>(sizeof(Type)),
|
||||||
|
format_string, static_cast<py::ssize_t>(1),
|
||||||
|
std::vector<py::ssize_t>{static_cast<py::ssize_t>(self.size())},
|
||||||
|
std::vector<py::ssize_t>{
|
||||||
|
static_cast<py::ssize_t>(sizeof(Type))});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// fmt::format("T{{{}:sum:i:index}}",py::format_descriptor<Type>::format())
|
||||||
@@ -32,6 +32,17 @@ def test_push_back_on_cluster_vector():
|
|||||||
assert arr[0]['y'] == 22
|
assert arr[0]['y'] == 22
|
||||||
|
|
||||||
|
|
||||||
|
def test_max_2x2_sum():
|
||||||
|
"""max_2x2_sum"""
|
||||||
|
cv = _aare.ClusterVector_Cluster3x3i()
|
||||||
|
cv.push_back(_aare.Cluster3x3i(19, 22, np.array([0,1,0,2,3,0,2,1,0], dtype=np.int32)))
|
||||||
|
cv.push_back(_aare.Cluster3x3i(19, 22, np.ones(9, dtype=np.int32)))
|
||||||
|
assert cv.size == 2
|
||||||
|
max_2x2 = cv.sum_2x2()
|
||||||
|
assert max_2x2.size == 2
|
||||||
|
print(max_2x2[0])
|
||||||
|
|
||||||
|
|
||||||
def test_make_a_hitmap_from_cluster_vector():
|
def test_make_a_hitmap_from_cluster_vector():
|
||||||
cv = _aare.ClusterVector_Cluster3x3i()
|
cv = _aare.ClusterVector_Cluster3x3i()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user