Merge branch 'main' into dev/license

This commit is contained in:
2025-11-21 14:52:54 +01:00
committed by GitHub
49 changed files with 3253 additions and 1171 deletions

File diff suppressed because one or more lines are too long

View File

@@ -3,6 +3,7 @@ import pytest
import numpy as np
from aare import _aare #import the C++ module
from aare import corner
from conftest import test_data_path
@@ -40,52 +41,49 @@ def test_Interpolator():
xbins = np.linspace(0, 5, 30, dtype=np.float64)
ybins = np.linspace(0, 5, 30, dtype=np.float64)
etacube = np.zeros(shape=[30, 30, 20], dtype=np.float64)
etacube = np.zeros(shape=[29, 29, 19], dtype=np.float64)
interpolator = _aare.Interpolator(etacube, xbins, ybins, ebins)
assert interpolator.get_ietax().shape == (30,30,20)
assert interpolator.get_ietay().shape == (30,30,20)
clustervector = _aare.ClusterVector_Cluster3x3i()
cluster = _aare.Cluster3x3i(0,0, np.ones(9, dtype=np.int32))
cluster = _aare.Cluster3x3i(1,1, np.ones(9, dtype=np.int32))
clustervector.push_back(cluster)
interpolated_photons = interpolator.interpolate(clustervector)
assert interpolated_photons.size == 1
assert interpolated_photons[0]["x"] == 0
assert interpolated_photons[0]["y"] == 0
assert interpolated_photons[0]["x"] == 0.5
assert interpolated_photons[0]["y"] == 0.5
assert interpolated_photons[0]["energy"] == 4 #eta_sum = 4, dx, dy = -1,-1 m_ietax = 0, m_ietay = 0
clustervector = _aare.ClusterVector_Cluster2x2i()
cluster = _aare.Cluster2x2i(0,0, np.ones(4, dtype=np.int32))
cluster = _aare.Cluster2x2i(1,1, np.ones(4, dtype=np.int32))
clustervector.push_back(cluster)
interpolated_photons = interpolator.interpolate(clustervector)
assert interpolated_photons.size == 1
assert interpolated_photons[0]["x"] == 0
assert interpolated_photons[0]["y"] == 0
assert interpolated_photons[0]["x"] == 0.5
assert interpolated_photons[0]["y"] == 0.5
assert interpolated_photons[0]["energy"] == 4
def test_calculate_eta():
"""Calculate Eta"""
clusters = _aare.ClusterVector_Cluster3x3i()
clusters.push_back(_aare.Cluster3x3i(0,0, np.ones(9, dtype=np.int32)))
clusters.push_back(_aare.Cluster3x3i(0,0, np.array([1,1,1,2,2,2,3,3,3])))
cluster = _aare.Cluster3x3i(0,0, np.ones(9, dtype=np.int32))
eta2 = _aare.calculate_eta2(clusters)
eta2 = _aare.calculate_eta2(cluster)
assert eta2.shape == (2,2)
assert eta2[0,0] == 0.5
assert eta2[0,1] == 0.5
assert eta2[1,0] == 0.5
assert eta2[1,1] == 0.4 #2/5
assert eta2.x == 0.5
assert eta2.y == 0.5
assert eta2.c == corner.cTopLeft
assert eta2.sum == 4
def test_max_sum():
@@ -119,7 +117,7 @@ def test_2x2_reduction():
reduced_cluster = _aare.reduce_to_2x2(cluster)
assert reduced_cluster.x == 4
assert reduced_cluster.x == 5
assert reduced_cluster.y == 5
assert (reduced_cluster.data == np.array([[2, 3], [2, 2]], dtype=np.int32)).all()
@@ -131,9 +129,9 @@ def test_3x3_reduction():
reduced_cluster = _aare.reduce_to_3x3(cluster)
assert reduced_cluster.x == 4
assert reduced_cluster.x == 5
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()
assert (reduced_cluster.data == np.array([[2.0, 1.0, 1.0], [2.0, 3.0, 1.0], [2.0, 1.0, 1.0]], dtype=np.double)).all()

View File

@@ -6,7 +6,7 @@ import time
from pathlib import Path
import pickle
from aare import ClusterFile, ClusterVector
from aare import ClusterFile, ClusterVector, calculate_eta2
from aare import _aare
from conftest import test_data_path
@@ -45,6 +45,19 @@ def test_max_2x2_sum():
assert max_2x2[0]["index"] == 2
def test_eta2():
"""calculate eta2"""
cv = _aare.ClusterVector_Cluster3x3i()
cv.push_back(_aare.Cluster3x3i(19, 22, np.ones(9, dtype=np.int32)))
assert cv.size == 1
eta2 = calculate_eta2(cv)
assert eta2.size == 1
assert eta2[0]["x"] == 0.5
assert eta2[0]["y"] == 0.5
assert eta2[0]["c"] == 0
assert eta2[0]["sum"] == 4
def test_make_a_hitmap_from_cluster_vector():
cv = _aare.ClusterVector_Cluster3x3i()
@@ -75,11 +88,11 @@ def test_2x2_reduction():
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]["x"] == 5
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]["x"] == 5
assert reduced_cv[1]["y"] == 5
assert (reduced_cv[1]["data"] == np.array([[2, 2], [2, 3]], dtype=np.int32)).all()
@@ -94,6 +107,6 @@ def test_3x3_reduction():
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]["x"] == 5
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()
assert (reduced_cv[0]["data"] == np.array([[2.0, 1.0, 1.0], [2.0, 3.0, 1.0], [2.0, 1.0, 1.0]], dtype=np.double)).all()

View File

@@ -25,10 +25,6 @@ def create_photon_hit_with_gaussian_distribution(mean, covariance_matrix, data_p
probability_values = gaussian.pdf(data_points)
return (probability_values.reshape(X.shape)).round() #python bindings only support frame types of uint16_t
def photon_hit_in_euclidean_space(cluster_center, pixels_per_superpixel, photon_hit):
scaled_photon_hit_x = cluster_center - (1 - photon_hit[0][0])*pixels_per_superpixel*pixel_width
scaled_photon_hit_y = cluster_center - (1 - photon_hit[0][1])*pixels_per_superpixel*pixel_width
return (scaled_photon_hit_x, scaled_photon_hit_y)
def create_2x2cluster_from_frame(frame, pixels_per_superpixel):
return Cluster2x2d(1, 1, np.array([frame[0:pixels_per_superpixel, 0:pixels_per_superpixel].sum(),
@@ -49,10 +45,10 @@ def create_3x3cluster_from_frame(frame, pixels_per_superpixel):
frame[2*pixels_per_superpixel:3*pixels_per_superpixel, 2*pixels_per_superpixel:3*pixels_per_superpixel].sum()], dtype=np.float64))
def calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator, cluster_2x2 = True):
def calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator, bin_edges_x = bh.axis.Regular(100, -0.2, 1.2), bin_edges_y = bh.axis.Regular(100, -0.2, 1.2), cluster_2x2 = True):
hist = bh.Histogram(
bh.axis.Regular(100, -0.2, 1.2),
bh.axis.Regular(100, -0.2, 1.2), bh.axis.Regular(1, 0, num_pixels*num_pixels*1/(variance*2*np.pi)))
bin_edges_x,
bin_edges_y, bh.axis.Regular(1, 0, num_pixels*num_pixels*1/(variance*2*np.pi)))
for _ in range(0, num_frames):
mean_x = random_number_generator.uniform(pixels_per_superpixel*pixel_width, 2*pixels_per_superpixel*pixel_width)
@@ -67,7 +63,7 @@ def calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_
cluster = create_3x3cluster_from_frame(frame, pixels_per_superpixel)
eta2 = calculate_eta2(cluster)
hist.fill(eta2[0], eta2[1], eta2[2])
hist.fill(eta2.x, eta2.y, eta2.sum)
return hist
@@ -86,9 +82,9 @@ def test_interpolation_of_2x2_cluster(test_data_path):
pixels_per_superpixel = int(num_pixels*0.5)
random_number_generator = np.random.default_rng(42)
eta_distribution = calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator)
eta_distribution = calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator, bin_edges_x = bh.axis.Regular(100, -0.1, 0.6), bin_edges_y = bh.axis.Regular(100, -0.1, 0.6))
interpolation = Interpolator(eta_distribution, eta_distribution.axes[0].edges[:-1], eta_distribution.axes[1].edges[:-1], eta_distribution.axes[2].edges[:-1])
interpolation = Interpolator(eta_distribution, eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges)
#actual photon hit
mean = 1.2*pixels_per_superpixel*pixel_width
@@ -105,7 +101,7 @@ def test_interpolation_of_2x2_cluster(test_data_path):
cluster_center = 1.5*pixels_per_superpixel*pixel_width
scaled_photon_hit = photon_hit_in_euclidean_space(cluster_center, pixels_per_superpixel, interpolated_photon)
scaled_photon_hit = (interpolated_photon[0][0]*pixels_per_superpixel*pixel_width, interpolated_photon[0][1]*pixels_per_superpixel*pixel_width)
assert (np.linalg.norm(scaled_photon_hit - mean) < np.linalg.norm(np.array([cluster_center, cluster_center] - mean)))
@@ -124,13 +120,14 @@ def test_interpolation_of_3x3_cluster(test_data_path):
num_frames = 1000
pixels_per_superpixel = int(num_pixels/3)
random_number_generator = np.random.default_rng(42)
eta_distribution = calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator, False)
eta_distribution = calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator, bin_edges_x = bh.axis.Regular(100, -0.1, 1.1), bin_edges_y = bh.axis.Regular(100, -0.1, 1.1), cluster_2x2 = False)
interpolation = Interpolator(eta_distribution, eta_distribution.axes[0].edges[:-1], eta_distribution.axes[1].edges[:-1], eta_distribution.axes[2].edges[:-1])
interpolation = Interpolator(eta_distribution, eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges)
#actual photon hit
mean = 1.2*pixels_per_superpixel*pixel_width
mean = np.array([mean, mean])
mean_x = (1 + 0.8)*pixels_per_superpixel*pixel_width
mean_y = (1 + 0.2)*pixels_per_superpixel*pixel_width
mean = np.array([mean_x, mean_y])
frame = create_photon_hit_with_gaussian_distribution(mean, covariance_matrix, data_points)
cluster = create_3x3cluster_from_frame(frame, pixels_per_superpixel)
@@ -143,7 +140,7 @@ def test_interpolation_of_3x3_cluster(test_data_path):
cluster_center = 1.5*pixels_per_superpixel*pixel_width
scaled_photon_hit = photon_hit_in_euclidean_space(cluster_center, pixels_per_superpixel, interpolated_photon)
scaled_photon_hit = (interpolated_photon[0][0]*pixels_per_superpixel*pixel_width, interpolated_photon[0][1]*pixels_per_superpixel*pixel_width)
assert (np.linalg.norm(scaled_photon_hit - mean) < np.linalg.norm(np.array([cluster_center, cluster_center] - mean)))

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,291 @@
#test script to test interpolation on simulated data
import pytest
import pytest_check as check
import numpy as np
import boost_histogram as bh
import pickle
from scipy.stats import multivariate_normal
from aare import Interpolator, calculate_eta2, calculate_cross_eta3, calculate_full_eta2, calculate_eta3
from aare import ClusterFile
from conftest import test_data_path
## TODO: is there something like a test fixture setup/teardown in pytest?
def calculate_eta_distribution(cv, calculate_eta, edges_x=[-0.5,0.5], edges_y=[-0.5,0.5], nbins = 101):
energy_bins = bh.axis.Regular(1, 0, 16) # max and min energy of simulated photons
eta_distribution = bh.Histogram(
bh.axis.Regular(nbins, edges_x[0], edges_x[1]),
bh.axis.Regular(nbins, edges_y[0], edges_y[1]), energy_bins)
eta = calculate_eta(cv)
eta_distribution.fill(eta['x'], eta['y'], eta['sum'])
return eta_distribution
@pytest.fixture
def load_data(test_data_path):
"""Load simulated cluster data and ground truth positions"""
f = ClusterFile(test_data_path / "clust" / "simulated_clusters.clust", dtype=np.float64, mode="r")
cv = f.read_frame()
ground_truths = np.load(test_data_path / "interpolation/ground_truth_simulated.npy")
return cv, ground_truths
@pytest.mark.withdata
def test_eta2_interpolation(load_data, check):
"""Test eta2 interpolation on simulated data"""
cv, ground_truths = load_data
num_bins = 201
eta_distribution = calculate_eta_distribution(cv, calculate_eta2, edges_x=[-0.1,1.1], edges_y=[-0.1,1.1], nbins=num_bins)
interpolator = Interpolator(eta_distribution, eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges)
assert interpolator.get_ietax().shape == (num_bins,num_bins,1)
assert interpolator.get_ietay().shape == (num_bins,num_bins,1)
interpolated_photons = interpolator.interpolate(cv)
assert interpolated_photons.size == cv.size
interpolated_photons["x"] += 1.0 #groud truth label uses 5x5 clusters
interpolated_photons["y"] += 1.0
residuals_interpolated_x = abs(ground_truths[:, 0] - interpolated_photons["x"])
residuals_interpolated_y = abs(ground_truths[:, 1] - interpolated_photons["y"])
"""
residuals_center_pixel_x = abs(ground_truths[:, 0] - 2.5)
residuals_center_pixel_y = abs(ground_truths[:, 1] - 2.5)
# interpolation needs to perform better than center pixel assignment - not true for photon close to the center
assert (residuals_interpolated_x < residuals_center_pixel_x).all()
assert (residuals_interpolated_y < residuals_center_pixel_y).all()
"""
# check within photon hit pixel for all
with check:
assert np.allclose(interpolated_photons["x"], ground_truths[:, 0], atol=5e-1)
with check:
assert np.allclose(interpolated_photons["y"], ground_truths[:, 1], atol=5e-1)
# check mean and std of residuals
with check:
assert residuals_interpolated_y.mean() <= 0.1
with check:
assert residuals_interpolated_x.mean() <= 0.1
with check:
assert residuals_interpolated_x.std() <= 0.05
with check:
assert residuals_interpolated_y.std() <= 0.05
@pytest.mark.withdata
def test_eta2_interpolation_rosenblatt(load_data, check):
"""Test eta2 interpolation on simulated data using Rosenblatt transform"""
cv, ground_truths = load_data
num_bins = 201
eta_distribution = calculate_eta_distribution(cv, calculate_eta2, edges_x=[-0.1,1.1], edges_y=[-0.1,1.1], nbins=num_bins)
interpolator = Interpolator(eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges)
interpolator.rosenblatttransform(eta_distribution)
assert interpolator.get_ietax().shape == (num_bins,num_bins,1)
assert interpolator.get_ietay().shape == (num_bins,num_bins,1)
interpolated_photons = interpolator.interpolate(cv)
assert interpolated_photons.size == cv.size
interpolated_photons["x"] += 1.0 #groud truth label uses 5x5 clusters
interpolated_photons["y"] += 1.0
residuals_interpolated_x = abs(ground_truths[:, 0] - interpolated_photons["x"])
residuals_interpolated_y = abs(ground_truths[:, 1] - interpolated_photons["y"])
"""
residuals_center_pixel_x = abs(ground_truths[:, 0] - 2.5)
residuals_center_pixel_y = abs(ground_truths[:, 1] - 2.5)
# interpolation needs to perform better than center pixel assignment - not true for photon close to the center
assert (residuals_interpolated_x < residuals_center_pixel_x).all()
assert (residuals_interpolated_y < residuals_center_pixel_y).all()
"""
# check within photon hit pixel for all
with check:
assert np.allclose(interpolated_photons["x"], ground_truths[:, 0], atol=5e-1)
with check:
assert np.allclose(interpolated_photons["y"], ground_truths[:, 1], atol=5e-1)
# check mean and std of residuals
with check:
assert residuals_interpolated_y.mean() <= 0.1
with check:
assert residuals_interpolated_x.mean() <= 0.1
with check:
assert residuals_interpolated_x.std() <= 0.055 #performs slightly worse
with check:
assert residuals_interpolated_y.std() <= 0.055 #performs slightly worse
@pytest.mark.withdata
def test_cross_eta_interpolation(load_data, check):
"""Test cross eta interpolation on simulated data"""
cv, ground_truths = load_data
num_bins = 201
eta_distribution = calculate_eta_distribution(cv, calculate_cross_eta3, edges_x=[-0.5,0.5], edges_y=[-0.5,0.5], nbins=num_bins)
interpolator = Interpolator(eta_distribution, eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges)
assert interpolator.get_ietax().shape == (num_bins,num_bins,1)
assert interpolator.get_ietay().shape == (num_bins,num_bins,1)
interpolated_photons = interpolator.interpolate_cross_eta3(cv)
assert interpolated_photons.size == cv.size
interpolated_photons["x"] += 1.0 #groud truth label uses 5x5 clusters
interpolated_photons["y"] += 1.0
residuals_interpolated_x = abs(ground_truths[:, 0] - interpolated_photons["x"])
residuals_interpolated_y = abs(ground_truths[:, 1] - interpolated_photons["y"])
"""
residuals_center_pixel_x = abs(ground_truths[:, 0] - 2.5)
residuals_center_pixel_y = abs(ground_truths[:, 1] - 2.5)
# interpolation needs to perform better than center pixel assignment - not true for photon close to the center
assert (residuals_interpolated_x < residuals_center_pixel_x).all()
assert (residuals_interpolated_y < residuals_center_pixel_y).all()
"""
# check within photon hit pixel for all
# TODO: fails as eta_x = 0, eta_y = 0 is not leading to offset (0.5,0.5)
with check:
assert np.allclose(interpolated_photons["x"], ground_truths[:, 0], atol=5e-1)
with check:
assert np.allclose(interpolated_photons["y"], ground_truths[:, 1], atol=5e-1)
# check mean and std of residuals
with check:
assert residuals_interpolated_y.mean() <= 0.1
with check:
assert residuals_interpolated_x.mean() <= 0.1
with check:
assert residuals_interpolated_x.std() <= 0.05
with check:
assert residuals_interpolated_y.std() <= 0.05
@pytest.mark.withdata
def test_eta3_interpolation(load_data, check):
"""Test eta3 interpolation on simulated data"""
cv, ground_truths = load_data
num_bins = 201
eta_distribution = calculate_eta_distribution(cv, calculate_eta3, edges_x=[-0.5,0.5], edges_y=[-0.5,0.5], nbins=num_bins)
interpolator = Interpolator(eta_distribution, eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges)
assert interpolator.get_ietax().shape == (num_bins,num_bins,1)
assert interpolator.get_ietay().shape == (num_bins,num_bins,1)
interpolated_photons = interpolator.interpolate_eta3(cv)
assert interpolated_photons.size == cv.size
interpolated_photons["x"] += 1.0 #groud truth label uses 5x5 clusters
interpolated_photons["y"] += 1.0
residuals_interpolated_x = abs(ground_truths[:, 0] - interpolated_photons["x"])
residuals_interpolated_y = abs(ground_truths[:, 1] - interpolated_photons["y"])
"""
residuals_center_pixel_x = abs(ground_truths[:, 0] - 2.5)
residuals_center_pixel_y = abs(ground_truths[:, 1] - 2.5)
# interpolation needs to perform better than center pixel assignment - not true for photon close to the center
assert (residuals_interpolated_x < residuals_center_pixel_x).all()
assert (residuals_interpolated_y < residuals_center_pixel_y).all()
"""
# check within photon hit pixel for all
# TODO: fails as eta_x = 0, eta_y = 0 is not leading to offset (0.5,0.5)
with check:
assert np.allclose(interpolated_photons["x"], ground_truths[:, 0], atol=5e-1)
with check:
assert np.allclose(interpolated_photons["y"], ground_truths[:, 1], atol=5e-1)
# check mean and std of residuals
with check:
assert residuals_interpolated_y.mean() <= 0.1
with check:
assert residuals_interpolated_x.mean() <= 0.1
with check:
assert residuals_interpolated_x.std() <= 0.05
with check:
assert residuals_interpolated_y.std() <= 0.05
@pytest.mark.withdata
def test_full_eta2_interpolation(load_data, check):
"""Test full eta2 interpolation on simulated data"""
cv, ground_truths = load_data
num_bins = 201
eta_distribution = calculate_eta_distribution(cv, calculate_full_eta2, edges_x=[-0.1,1.1], edges_y=[-0.1,1.1], nbins=num_bins)
interpolator = Interpolator(eta_distribution, eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges)
assert interpolator.get_ietax().shape == (num_bins,num_bins,1)
assert interpolator.get_ietay().shape == (num_bins,num_bins,1)
interpolated_photons = interpolator.interpolate_full_eta2(cv)
assert interpolated_photons.size == cv.size
interpolated_photons["x"] += 1.0 #groud truth label uses 5x5 clusters
interpolated_photons["y"] += 1.0
residuals_interpolated_x = abs(ground_truths[:, 0] - interpolated_photons["x"])
residuals_interpolated_y = abs(ground_truths[:, 1] - interpolated_photons["y"])
"""
residuals_center_pixel_x = abs(ground_truths[:, 0] - 2.5)
residuals_center_pixel_y = abs(ground_truths[:, 1] - 2.5)
# interpolation needs to perform better than center pixel assignment - not true for photon close to the center
assert (residuals_interpolated_x < residuals_center_pixel_x).all()
assert (residuals_interpolated_y < residuals_center_pixel_y).all()
"""
# check within photon hit pixel for all
with check:
assert np.allclose(interpolated_photons["x"], ground_truths[:, 0], atol=5e-1)
with check:
assert np.allclose(interpolated_photons["y"], ground_truths[:, 1], atol=5e-1)
# check mean and std of residuals
with check:
assert residuals_interpolated_y.mean() <= 0.1
with check:
assert residuals_interpolated_x.mean() <= 0.1
with check:
assert residuals_interpolated_x.std() <= 0.05
with check:
assert residuals_interpolated_y.std() <= 0.05