This commit is contained in:
froejdh_e
2024-12-10 17:21:05 +01:00
parent b43003966f
commit 6a150e8d98
7 changed files with 268 additions and 98 deletions

View File

@ -1,15 +1,53 @@
import sys
sys.path.append('/home/l_msdetect/erik/aare/build')
#Our normal python imports
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
from pathlib import Path
from aare import ClusterFile
import boost_histogram as bh
import time
base = Path('~/data/aare_test_data/clusters').expanduser()
from aare import File, ClusterFinder, VarClusterFinder
f = ClusterFile(base / 'beam_En700eV_-40deg_300V_10us_d0_f0_100.clust')
# f = ClusterFile(base / 'single_frame_97_clustrers.clust')
base = Path('/mnt/sls_det_storage/matterhorn_data/aare_test_data/')
f = File(base/'Moench03new/cu_half_speed_master_4.json')
cf = ClusterFinder((400,400), (3,3))
for i in range(1000):
cf.push_pedestal_frame(f.read_frame())
fig, ax = plt.subplots()
im = ax.imshow(cf.pedestal())
cf.pedestal()
cf.noise()
N = 200
t0 = time.perf_counter()
hist1 = bh.Histogram(bh.axis.Regular(40, -2, 4000))
f.seek(0)
t0 = time.perf_counter()
data = f.read_n(N)
t_elapsed = time.perf_counter()-t0
print(f'Reading {N} frames took {t_elapsed:.3f}s {N/t_elapsed:.0f} FPS')
clusters = []
for frame in data:
clusters += [cf.find_clusters_without_threshold(frame)]
for i in range(10):
fn, cl = f.read_frame()
print(fn, cl.size)
t_elapsed = time.perf_counter()-t0
print(f'Clustering {N} frames took {t_elapsed:.2f}s {N/t_elapsed:.0f} FPS')
t0 = time.perf_counter()
total_clusters = 0
for cl in clusters:
arr = np.array(cl, copy = False)
hist1.fill(arr['data'].sum(axis = 1).sum(axis = 1))
total_clusters += cl.size
# t_elapsed = time.perf_counter()-t0
print(f'Filling histogram with {total_clusters} clusters took: {t_elapsed:.3f}s')
print(f'Cluster per frame {total_clusters/N:.3f}')

View File

@ -1,4 +1,5 @@
#include "aare/ClusterFinder.hpp"
#include "aare/ClusterVector.hpp"
#include "aare/NDView.hpp"
#include "aare/Pedestal.hpp"
#include "np_helper.hpp"
@ -9,30 +10,98 @@
#include <pybind11/stl.h>
namespace py = pybind11;
using pd_type = double;
template <typename T>
void define_cluster_vector(py::module &m, const std::string &typestr) {
auto class_name = fmt::format("ClusterVector_{}", typestr);
py::class_<ClusterVector<T>>(m, class_name.c_str(), py::buffer_protocol())
.def(py::init<int, int>())
.def_property_readonly("size", &ClusterVector<T>::size)
.def("element_offset",
py::overload_cast<>(&ClusterVector<T>::element_offset, py::const_))
.def_property_readonly("fmt",
[typestr](ClusterVector<T> &v) {
return fmt::format(
"i:x:\ni:y:\n({},{}){}:data:", v.cluster_size_x(),
v.cluster_size_y(), typestr);
})
.def("sum", &ClusterVector<T>::sum)
.def_buffer([typestr](ClusterVector<T> &v) -> py::buffer_info {
return py::buffer_info(
v.data(), /* Pointer to buffer */
v.element_offset(), /* Size of one scalar */
fmt::format("i:x:\ni:y:\n({},{}){}:data:", v.cluster_size_x(),
v.cluster_size_y(),
typestr), /* Format descriptor */
1, /* Number of dimensions */
{v.size()}, /* Buffer dimensions */
{v.element_offset()} /* Strides (in bytes) for each index */
);
});
}
void define_cluster_finder_bindings(py::module &m) {
py::class_<ClusterFinder<uint16_t, double>>(m, "ClusterFinder")
py::class_<ClusterFinder<uint16_t, pd_type>>(m, "ClusterFinder")
.def(py::init<Shape<2>, Shape<2>>())
.def("push_pedestal_frame",
[](ClusterFinder<uint16_t, double> &self,
[](ClusterFinder<uint16_t, pd_type> &self,
py::array_t<uint16_t> frame) {
auto view = make_view_2d(frame);
self.push_pedestal_frame(view);
})
.def("pedestal",
[](ClusterFinder<uint16_t, double> &self) {
auto pd = new NDArray<double, 2>{};
[](ClusterFinder<uint16_t, pd_type> &self) {
auto pd = new NDArray<pd_type, 2>{};
*pd = self.pedestal();
return return_image_data(pd);
})
.def("noise",
[](ClusterFinder<uint16_t, pd_type> &self) {
auto arr = new NDArray<pd_type, 2>{};
*arr = self.noise();
return return_image_data(arr);
})
.def("find_clusters_without_threshold",
[](ClusterFinder<uint16_t, double> &self,
[](ClusterFinder<uint16_t, pd_type> &self,
py::array_t<uint16_t> frame) {
auto view = make_view_2d(frame);
auto clusters = self.find_clusters_without_threshold(view);
return clusters;
auto *vec = new ClusterVector<pd_type>(
self.find_clusters_without_threshold(view));
return vec;
});
m.def("hello", []() {
fmt::print("Hello from C++\n");
auto v = new ClusterVector<int>(3, 3);
int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
v->push_back(5, 30, reinterpret_cast<std::byte *>(data));
v->push_back(5, 55, reinterpret_cast<std::byte *>(data));
v->push_back(5, 20, reinterpret_cast<std::byte *>(data));
v->push_back(5, 30, reinterpret_cast<std::byte *>(data));
return v;
});
define_cluster_vector<int>(m, "i");
define_cluster_vector<double>(m, "d");
// py::class_<ClusterVector<int>>(m, "ClusterVector", py::buffer_protocol())
// .def(py::init<int, int>())
// .def("size", &ClusterVector<int>::size)
// .def("element_offset",
// py::overload_cast<>(&ClusterVector<int>::element_offset, py::const_))
// .def_buffer([](ClusterVector<int> &v) -> py::buffer_info {
// return py::buffer_info(
// v.data(), /* Pointer to buffer */
// v.element_offset(), /* Size of one scalar */
// fmt::format("h:x:\nh:y:\n({},{})i:data:", v.cluster_size_x(),
// v.cluster_size_y()), /* Format descriptor */ 1, /* Number of
// dimensions */ {v.size()}, /* Buffer dimensions */
// {v.element_offset()} /* Strides (in bytes) for each index */
// );
// });
py::class_<DynamicCluster>(m, "DynamicCluster", py::buffer_protocol())
.def(py::init<int, int, Dtype>())
.def("size", &DynamicCluster::size)