mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-12 23:37:13 +02:00
For 2025.5.22 release (#181)
Co-authored-by: Patrick <patrick.sieberer@psi.ch> Co-authored-by: JulianHeymes <julian.heymes@psi.ch> Co-authored-by: Dhanya Thattil <dhanya.thattil@psi.ch> Co-authored-by: Xiangyu Xie <45243914+xiangyuxie@users.noreply.github.com> Co-authored-by: xiangyu.xie <xiangyu.xie@psi.ch> Co-authored-by: AliceMazzoleni99 <alice.mazzoleni@psi.ch> Co-authored-by: Mazzoleni Alice Francesca <mazzol_a@pc17378.psi.ch> Co-authored-by: siebsi <sieb.patr@gmail.com>
This commit is contained in:
@ -25,5 +25,10 @@ def pytest_collection_modifyitems(config, items):
|
||||
|
||||
@pytest.fixture
|
||||
def test_data_path():
|
||||
return Path(os.environ["AARE_TEST_DATA"])
|
||||
env_value = os.environ.get("AARE_TEST_DATA")
|
||||
if not env_value:
|
||||
raise RuntimeError("Environment variable AARE_TEST_DATA is not set or is empty")
|
||||
|
||||
return Path(env_value)
|
||||
|
||||
|
||||
|
110
python/tests/test_Cluster.py
Normal file
110
python/tests/test_Cluster.py
Normal file
@ -0,0 +1,110 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
|
||||
from aare import _aare #import the C++ module
|
||||
from conftest import test_data_path
|
||||
|
||||
|
||||
def test_cluster_vector_can_be_converted_to_numpy():
|
||||
cv = _aare.ClusterVector_Cluster3x3i()
|
||||
arr = np.array(cv, copy=False)
|
||||
assert arr.shape == (0,) # 4 for x, y, size, energy and 9 for the cluster data
|
||||
|
||||
|
||||
def test_ClusterVector():
|
||||
"""Test ClusterVector"""
|
||||
|
||||
clustervector = _aare.ClusterVector_Cluster3x3i()
|
||||
assert clustervector.cluster_size_x == 3
|
||||
assert clustervector.cluster_size_y == 3
|
||||
assert clustervector.item_size() == 4+9*4
|
||||
assert clustervector.frame_number == 0
|
||||
assert clustervector.size == 0
|
||||
|
||||
cluster = _aare.Cluster3x3i(0,0,np.ones(9, dtype=np.int32))
|
||||
|
||||
clustervector.push_back(cluster)
|
||||
assert clustervector.size == 1
|
||||
|
||||
with pytest.raises(TypeError): # Or use the appropriate exception type
|
||||
clustervector.push_back(_aare.Cluster2x2i(0,0,np.ones(4, dtype=np.int32)))
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
clustervector.push_back(_aare.Cluster3x3f(0,0,np.ones(9, dtype=np.float32)))
|
||||
|
||||
def test_Interpolator():
|
||||
"""Test Interpolator"""
|
||||
|
||||
ebins = np.linspace(0,10, 20, dtype=np.float64)
|
||||
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)
|
||||
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))
|
||||
clustervector.push_back(cluster)
|
||||
|
||||
interpolated_photons = interpolator.interpolate(clustervector)
|
||||
|
||||
assert interpolated_photons.size == 1
|
||||
|
||||
assert interpolated_photons[0]["x"] == -1
|
||||
assert interpolated_photons[0]["y"] == -1
|
||||
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))
|
||||
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]["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])))
|
||||
|
||||
eta2 = _aare.calculate_eta2(clusters)
|
||||
|
||||
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.6 #1/5
|
||||
|
||||
def test_cluster_finder():
|
||||
"""Test ClusterFinder"""
|
||||
|
||||
clusterfinder = _aare.ClusterFinder_Cluster3x3i([100,100])
|
||||
|
||||
#frame = np.random.rand(100,100)
|
||||
frame = np.zeros(shape=[100,100])
|
||||
|
||||
clusterfinder.find_clusters(frame)
|
||||
|
||||
clusters = clusterfinder.steal_clusters(False) #conversion does not work
|
||||
|
||||
assert clusters.size == 0
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
64
python/tests/test_ClusterFile.py
Normal file
64
python/tests/test_ClusterFile.py
Normal file
@ -0,0 +1,64 @@
|
||||
|
||||
import pytest
|
||||
import numpy as np
|
||||
import boost_histogram as bh
|
||||
import time
|
||||
from pathlib import Path
|
||||
import pickle
|
||||
|
||||
from aare import ClusterFile
|
||||
from conftest import test_data_path
|
||||
|
||||
@pytest.mark.files
|
||||
def test_cluster_file(test_data_path):
|
||||
"""Test ClusterFile"""
|
||||
f = ClusterFile(test_data_path / "clust/single_frame_97_clustrers.clust")
|
||||
cv = f.read_clusters(10) #conversion does not work
|
||||
|
||||
|
||||
assert cv.frame_number == 135
|
||||
assert cv.size == 10
|
||||
|
||||
#Known data
|
||||
#frame_number, num_clusters [135] 97
|
||||
#[ 1 200] [0 1 2 3 4 5 6 7 8]
|
||||
#[ 2 201] [ 9 10 11 12 13 14 15 16 17]
|
||||
#[ 3 202] [18 19 20 21 22 23 24 25 26]
|
||||
#[ 4 203] [27 28 29 30 31 32 33 34 35]
|
||||
#[ 5 204] [36 37 38 39 40 41 42 43 44]
|
||||
#[ 6 205] [45 46 47 48 49 50 51 52 53]
|
||||
#[ 7 206] [54 55 56 57 58 59 60 61 62]
|
||||
#[ 8 207] [63 64 65 66 67 68 69 70 71]
|
||||
#[ 9 208] [72 73 74 75 76 77 78 79 80]
|
||||
#[ 10 209] [81 82 83 84 85 86 87 88 89]
|
||||
|
||||
#conversion to numpy array
|
||||
arr = np.array(cv, copy = False)
|
||||
|
||||
assert arr.size == 10
|
||||
for i in range(10):
|
||||
assert arr[i]['x'] == i+1
|
||||
|
||||
@pytest.mark.files
|
||||
def test_read_clusters_and_fill_histogram(test_data_path):
|
||||
# Create the histogram
|
||||
n_bins = 100
|
||||
xmin = -100
|
||||
xmax = 1e4
|
||||
hist_aare = bh.Histogram(bh.axis.Regular(n_bins, xmin, xmax))
|
||||
|
||||
fname = test_data_path / "clust/beam_En700eV_-40deg_300V_10us_d0_f0_100.clust"
|
||||
|
||||
#Read clusters and fill the histogram with pixel values
|
||||
with ClusterFile(fname, chunk_size = 10000) as f:
|
||||
for clusters in f:
|
||||
arr = np.array(clusters, copy = False)
|
||||
hist_aare.fill(arr['data'].flat)
|
||||
|
||||
|
||||
#Load the histogram from the pickle file
|
||||
with open(fname.with_suffix('.pkl'), 'rb') as f:
|
||||
hist_py = pickle.load(f)
|
||||
|
||||
#Compare the two histograms
|
||||
assert hist_aare == hist_py
|
54
python/tests/test_ClusterVector.py
Normal file
54
python/tests/test_ClusterVector.py
Normal file
@ -0,0 +1,54 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
import boost_histogram as bh
|
||||
import time
|
||||
from pathlib import Path
|
||||
import pickle
|
||||
|
||||
from aare import ClusterFile
|
||||
from aare import _aare
|
||||
from conftest import test_data_path
|
||||
|
||||
|
||||
def test_create_cluster_vector():
|
||||
cv = _aare.ClusterVector_Cluster3x3i()
|
||||
assert cv.cluster_size_x == 3
|
||||
assert cv.cluster_size_y == 3
|
||||
assert cv.size == 0
|
||||
|
||||
|
||||
def test_push_back_on_cluster_vector():
|
||||
cv = _aare.ClusterVector_Cluster2x2i()
|
||||
assert cv.cluster_size_x == 2
|
||||
assert cv.cluster_size_y == 2
|
||||
assert cv.size == 0
|
||||
|
||||
cluster = _aare.Cluster2x2i(19, 22, np.ones(4, dtype=np.int32))
|
||||
cv.push_back(cluster)
|
||||
assert cv.size == 1
|
||||
|
||||
arr = np.array(cv, copy=False)
|
||||
assert arr[0]['x'] == 19
|
||||
assert arr[0]['y'] == 22
|
||||
|
||||
|
||||
def test_make_a_hitmap_from_cluster_vector():
|
||||
cv = _aare.ClusterVector_Cluster3x3i()
|
||||
|
||||
# Push back 4 clusters with different positions
|
||||
cv.push_back(_aare.Cluster3x3i(0, 0, np.ones(9, dtype=np.int32)))
|
||||
cv.push_back(_aare.Cluster3x3i(1, 1, np.ones(9, dtype=np.int32)))
|
||||
cv.push_back(_aare.Cluster3x3i(1, 1, np.ones(9, dtype=np.int32)))
|
||||
cv.push_back(_aare.Cluster3x3i(2, 2, np.ones(9, dtype=np.int32)))
|
||||
|
||||
ref = np.zeros((5, 5), dtype=np.int32)
|
||||
ref[0,0] = 1
|
||||
ref[1,1] = 2
|
||||
ref[2,2] = 1
|
||||
|
||||
|
||||
img = _aare.hitmap((5,5), cv)
|
||||
# print(img)
|
||||
# print(ref)
|
||||
assert (img == ref).all()
|
||||
|
@ -5,32 +5,35 @@ from aare import RawSubFile, DetectorType
|
||||
|
||||
@pytest.mark.files
|
||||
def test_read_a_jungfrau_RawSubFile(test_data_path):
|
||||
|
||||
# Starting with f1 there is now 7 frames left in the series of files
|
||||
with RawSubFile(test_data_path / "raw/jungfrau/jungfrau_single_d0_f1_0.raw", DetectorType.Jungfrau, 512, 1024, 16) as f:
|
||||
assert f.frames_in_file == 3
|
||||
assert f.frames_in_file == 7
|
||||
|
||||
headers, frames = f.read()
|
||||
|
||||
assert headers.size == 3
|
||||
assert frames.shape == (3, 512, 1024)
|
||||
assert headers.size == 7
|
||||
assert frames.shape == (7, 512, 1024)
|
||||
|
||||
# Frame numbers in this file should be 4, 5, 6
|
||||
for i,h in zip(range(4,7,1), headers):
|
||||
|
||||
for i,h in zip(range(4,11,1), headers):
|
||||
assert h["frameNumber"] == i
|
||||
|
||||
# Compare to canned data using numpy
|
||||
data = np.load(test_data_path / "raw/jungfrau/jungfrau_single_0.npy")
|
||||
assert np.all(data[3:6] == frames)
|
||||
assert np.all(data[3:] == frames)
|
||||
|
||||
@pytest.mark.files
|
||||
def test_iterate_over_a_jungfrau_RawSubFile(test_data_path):
|
||||
|
||||
data = np.load(test_data_path / "raw/jungfrau/jungfrau_single_0.npy")
|
||||
|
||||
# Given the first subfile in a series we can read all frames from f0, f1, f2...fN
|
||||
with RawSubFile(test_data_path / "raw/jungfrau/jungfrau_single_d0_f0_0.raw", DetectorType.Jungfrau, 512, 1024, 16) as f:
|
||||
i = 0
|
||||
for header, frame in f:
|
||||
assert header["frameNumber"] == i+1
|
||||
assert np.all(frame == data[i])
|
||||
i += 1
|
||||
assert i == 3
|
||||
assert header["frameNumber"] == 3
|
||||
assert i == 10
|
||||
assert header["frameNumber"] == 10
|
||||
|
Reference in New Issue
Block a user