mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-11 06:47:14 +02:00
@ -7,11 +7,12 @@ from ._aare import Pedestal_d, Pedestal_f, ClusterFinder, VarClusterFinder
|
||||
from ._aare import DetectorType
|
||||
from ._aare import ClusterFile
|
||||
from ._aare import hitmap
|
||||
from ._aare import ROI
|
||||
|
||||
from ._aare import ClusterFinderMT, ClusterCollector, ClusterFileSink, ClusterVector_i
|
||||
|
||||
from ._aare import fit_gaus, fit_pol1
|
||||
|
||||
from ._aare import Interpolator
|
||||
from .CtbRawFile import CtbRawFile
|
||||
from .RawFile import RawFile
|
||||
from .ScanParameters import ScanParameters
|
||||
|
@ -1,50 +1,77 @@
|
||||
import sys
|
||||
sys.path.append('/home/l_msdetect/erik/aare/build')
|
||||
|
||||
#Our normal python imports
|
||||
from pathlib import Path
|
||||
import matplotlib.pyplot as plt
|
||||
from aare._aare import ClusterVector_i, Interpolator
|
||||
|
||||
import pickle
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import boost_histogram as bh
|
||||
import torch
|
||||
import math
|
||||
import time
|
||||
|
||||
|
||||
import aare
|
||||
def gaussian_2d(mx, my, sigma = 1, res=100, grid_size = 2):
|
||||
"""
|
||||
Generate a 2D gaussian as position mx, my, with sigma=sigma.
|
||||
The gaussian is placed on a 2x2 pixel matrix with resolution
|
||||
res in one dimesion.
|
||||
"""
|
||||
x = torch.linspace(0, pixel_size*grid_size, res)
|
||||
x,y = torch.meshgrid(x,x, indexing="ij")
|
||||
return 1 / (2*math.pi*sigma**2) * \
|
||||
torch.exp(-((x - my)**2 / (2*sigma**2) + (y - mx)**2 / (2*sigma**2)))
|
||||
|
||||
data = np.random.normal(10, 1, 1000)
|
||||
scale = 1000 #Scale factor when converting to integer
|
||||
pixel_size = 25 #um
|
||||
grid = 2
|
||||
resolution = 100
|
||||
sigma_um = 10
|
||||
xa = np.linspace(0,grid*pixel_size,resolution)
|
||||
ticks = [0, 25, 50]
|
||||
|
||||
hist = bh.Histogram(bh.axis.Regular(10, 0, 20))
|
||||
hist.fill(data)
|
||||
hit = np.array((20,20))
|
||||
etahist_fname = "/home/l_msdetect/erik/tmp/test_hist.pkl"
|
||||
|
||||
local_resolution = 99
|
||||
grid_size = 3
|
||||
xaxis = np.linspace(0,grid_size*pixel_size, local_resolution)
|
||||
t = gaussian_2d(hit[0],hit[1], grid_size = grid_size, sigma = 10, res = local_resolution)
|
||||
pixels = t.reshape(grid_size, t.shape[0] // grid_size, grid_size, t.shape[1] // grid_size).sum(axis = 3).sum(axis = 1)
|
||||
pixels = pixels.numpy()
|
||||
pixels = (pixels*scale).astype(np.int32)
|
||||
v = ClusterVector_i(3,3)
|
||||
v.push_back(1,1, pixels)
|
||||
|
||||
with open(etahist_fname, "rb") as f:
|
||||
hist = pickle.load(f)
|
||||
eta = hist.view().copy()
|
||||
etabinsx = np.array(hist.axes.edges.T[0].flat)
|
||||
etabinsy = np.array(hist.axes.edges.T[1].flat)
|
||||
ebins = np.array(hist.axes.edges.T[2].flat)
|
||||
p = Interpolator(eta, etabinsx[0:-1], etabinsy[0:-1], ebins[0:-1])
|
||||
|
||||
|
||||
x = hist.axes[0].centers
|
||||
y = hist.values()
|
||||
y_err = np.sqrt(y)+1
|
||||
res = aare.fit_gaus(x, y, y_err, chi2 = True)
|
||||
|
||||
#Generate the hit
|
||||
|
||||
|
||||
|
||||
t_elapsed = time.perf_counter()-t0
|
||||
print(f'Histogram filling took: {t_elapsed:.3f}s {total_clusters/t_elapsed/1e6:.3f}M clusters/s')
|
||||
|
||||
histogram_data = hist3d.counts()
|
||||
x = hist3d.axes[2].edges[:-1]
|
||||
|
||||
y = histogram_data[100,100,:]
|
||||
xx = np.linspace(x[0], x[-1])
|
||||
# fig, ax = plt.subplots()
|
||||
# ax.step(x, y, where = 'post')
|
||||
tmp = p.interpolate(v)
|
||||
print(f'tmp:{tmp}')
|
||||
pos = np.array((tmp['x'], tmp['y']))*25
|
||||
|
||||
y_err = np.sqrt(y)
|
||||
y_err = np.zeros(y.size)
|
||||
y_err += 1
|
||||
|
||||
# par = fit_gaus2(y,x, y_err)
|
||||
# ax.plot(xx, gaus(xx,par))
|
||||
# print(par)
|
||||
|
||||
res = fit_gaus(y,x)
|
||||
res2 = fit_gaus(y,x, y_err)
|
||||
print(res)
|
||||
print(res2)
|
||||
|
||||
print(pixels)
|
||||
fig, ax = plt.subplots(figsize = (7,7))
|
||||
ax.pcolormesh(xaxis, xaxis, t)
|
||||
ax.plot(*pos, 'o')
|
||||
ax.set_xticks([0,25,50,75])
|
||||
ax.set_yticks([0,25,50,75])
|
||||
ax.set_xlim(0,75)
|
||||
ax.set_ylim(0,75)
|
||||
ax.grid()
|
||||
print(f'{hit=}')
|
||||
print(f'{pos=}')
|
@ -20,7 +20,13 @@ 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(py::init<int, int>(),
|
||||
py::arg("cluster_size_x") = 3, py::arg("cluster_size_y") = 3)
|
||||
.def("push_back",
|
||||
[](ClusterVector<T> &self, int x, int y, py::array_t<T> data) {
|
||||
// auto view = make_view_2d(data);
|
||||
self.push_back(x, y, reinterpret_cast<const std::byte*>(data.data()));
|
||||
})
|
||||
.def_property_readonly("size", &ClusterVector<T>::size)
|
||||
.def("item_size", &ClusterVector<T>::item_size)
|
||||
.def_property_readonly("fmt",
|
||||
@ -38,6 +44,8 @@ void define_cluster_vector(py::module &m, const std::string &typestr) {
|
||||
auto *vec = new std::vector<T>(self.sum_2x2());
|
||||
return return_vector(vec);
|
||||
})
|
||||
.def_property_readonly("cluster_size_x", &ClusterVector<T>::cluster_size_x)
|
||||
.def_property_readonly("cluster_size_y", &ClusterVector<T>::cluster_size_y)
|
||||
.def_property_readonly("capacity", &ClusterVector<T>::capacity)
|
||||
.def_property("frame_number", &ClusterVector<T>::frame_number,
|
||||
&ClusterVector<T>::set_frame_number)
|
||||
|
@ -31,6 +31,11 @@ void define_cluster_file_io_bindings(py::module &m) {
|
||||
auto v = new ClusterVector<int32_t>(self.read_clusters(n_clusters));
|
||||
return v;
|
||||
},py::return_value_policy::take_ownership)
|
||||
.def("read_clusters",
|
||||
[](ClusterFile &self, size_t n_clusters, ROI roi) {
|
||||
auto v = new ClusterVector<int32_t>(self.read_clusters(n_clusters, roi));
|
||||
return v;
|
||||
},py::return_value_policy::take_ownership)
|
||||
.def("read_frame",
|
||||
[](ClusterFile &self) {
|
||||
auto v = new ClusterVector<int32_t>(self.read_frame());
|
||||
|
@ -195,6 +195,8 @@ void define_file_io_bindings(py::module &m) {
|
||||
|
||||
py::class_<ROI>(m, "ROI")
|
||||
.def(py::init<>())
|
||||
.def(py::init<int64_t, int64_t, int64_t, int64_t>(), py::arg("xmin"),
|
||||
py::arg("xmax"), py::arg("ymin"), py::arg("ymax"))
|
||||
.def_readwrite("xmin", &ROI::xmin)
|
||||
.def_readwrite("xmax", &ROI::xmax)
|
||||
.def_readwrite("ymin", &ROI::ymin)
|
||||
|
58
python/src/interpolation.hpp
Normal file
58
python/src/interpolation.hpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include "aare/Interpolator.hpp"
|
||||
#include "aare/NDArray.hpp"
|
||||
#include "aare/NDView.hpp"
|
||||
#include "np_helper.hpp"
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <pybind11/stl.h>
|
||||
|
||||
namespace py = pybind11;
|
||||
void define_interpolation_bindings(py::module &m) {
|
||||
|
||||
PYBIND11_NUMPY_DTYPE(aare::Photon, x,y,energy);
|
||||
|
||||
py::class_<aare::Interpolator>(m, "Interpolator")
|
||||
.def(py::init([](py::array_t<double, py::array::c_style | py::array::forcecast> etacube, py::array_t<double> xbins,
|
||||
py::array_t<double> ybins, py::array_t<double> ebins) {
|
||||
return Interpolator(make_view_3d(etacube), make_view_1d(xbins),
|
||||
make_view_1d(ybins), make_view_1d(ebins));
|
||||
}))
|
||||
.def("get_ietax", [](Interpolator& self){
|
||||
auto*ptr = new NDArray<double,3>{};
|
||||
*ptr = self.get_ietax();
|
||||
return return_image_data(ptr);
|
||||
})
|
||||
.def("get_ietay", [](Interpolator& self){
|
||||
auto*ptr = new NDArray<double,3>{};
|
||||
*ptr = self.get_ietay();
|
||||
return return_image_data(ptr);
|
||||
})
|
||||
.def("interpolate", [](Interpolator& self, const ClusterVector<int32_t>& clusters){
|
||||
auto photons = self.interpolate(clusters);
|
||||
auto* ptr = new std::vector<Photon>{photons};
|
||||
return return_vector(ptr);
|
||||
});
|
||||
|
||||
// TODO! Evaluate without converting to double
|
||||
m.def(
|
||||
"hej",
|
||||
[]() {
|
||||
// auto boost_histogram = py::module_::import("boost_histogram");
|
||||
// py::object axis =
|
||||
// boost_histogram.attr("axis").attr("Regular")(10, 0.0, 10.0);
|
||||
// py::object histogram = boost_histogram.attr("Histogram")(axis);
|
||||
// return histogram;
|
||||
// return h;
|
||||
},
|
||||
R"(
|
||||
Evaluate a 1D Gaussian function for all points in x using parameters par.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : array_like
|
||||
The points at which to evaluate the Gaussian function.
|
||||
par : array_like
|
||||
The parameters of the Gaussian function. The first element is the amplitude, the second element is the mean, and the third element is the standard deviation.
|
||||
)");
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
#include "cluster.hpp"
|
||||
#include "cluster_file.hpp"
|
||||
#include "fit.hpp"
|
||||
#include "interpolation.hpp"
|
||||
|
||||
//Pybind stuff
|
||||
#include <pybind11/pybind11.h>
|
||||
@ -31,5 +32,6 @@ PYBIND11_MODULE(_aare, m) {
|
||||
define_cluster_collector_bindings(m);
|
||||
define_cluster_file_sink_bindings(m);
|
||||
define_fit_bindings(m);
|
||||
define_interpolation_bindings(m);
|
||||
|
||||
}
|
Reference in New Issue
Block a user