From 0acbfe19810aff4ea54d9c507e28ef697171ab1e Mon Sep 17 00:00:00 2001 From: Alice Date: Mon, 26 Oct 2026 16:26:20 +0100 Subject: [PATCH] added datasetaggregation test in python --- python/tests/conftest.py | 26 ++++++++++++ python/tests/test_DataSetAggregation.py | 55 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 python/tests/test_DataSetAggregation.py diff --git a/python/tests/conftest.py b/python/tests/conftest.py index cdd14f86..f4c66c57 100644 --- a/python/tests/conftest.py +++ b/python/tests/conftest.py @@ -2,6 +2,7 @@ import os from pathlib import Path import pytest +import numpy as np @@ -33,3 +34,28 @@ def test_data_path(): return Path(env_value) +@pytest.fixture +def create_temporary_jungfrau_files(tmp_path): + """Factory fixture: writes a Jungfrau raw file set into tmp_path and returns the path of the first file.""" + + JungfrauDataHeader = np.dtype([("framenum", np.uint64), ("bunchid", np.uint64)]) + + def _create(num_frames, frame_generator, num_files=1): + master_path = tmp_path / "aare-raw" + master_path.mkdir(exist_ok=True) + + frames_per_file = num_frames // num_files + + for file_index in range(num_files): + start_frame = file_index * frames_per_file + end_frame = num_frames if file_index == num_files - 1 else start_frame + frames_per_file + + with open(master_path / f"JungfrauTest_{file_index:06}.dat", "wb") as f: + for frame in range(start_frame, end_frame): + # header + f.write(np.array([(frame, 0)], dtype=JungfrauDataHeader).tobytes()) + f.write(np.ascontiguousarray(frame_generator(frame), dtype=np.uint16).tobytes()) + + return master_path / "JungfrauTest_000000.dat" + + return _create diff --git a/python/tests/test_DataSetAggregation.py b/python/tests/test_DataSetAggregation.py new file mode 100644 index 00000000..3a525da8 --- /dev/null +++ b/python/tests/test_DataSetAggregation.py @@ -0,0 +1,55 @@ +# SPDX-License-Identifier: MPL-2.0 +import numpy as np +import pytest + +from aare import block_mean, mean_current_source + +def test_block_mean(create_temporary_jungfrau_files): + """ test block_mean """ + rows, cols = 256, 256 + num_samples = 100 + num_frames_per_sample = 10 + num_threads = 16 + + file_system = create_temporary_jungfrau_files( + num_samples * num_frames_per_sample, + lambda frame: np.full((rows, cols), frame % num_frames_per_sample, dtype=np.uint16), + ) + + result = block_mean(file_system, num_samples, num_frames_per_sample, num_threads) + + assert result.shape == (rows, cols, num_samples) + + # mean of 0,1,...,num_frames_per_sample-1 + mean_value = (num_frames_per_sample - 1) / 2 + np.testing.assert_allclose(result, mean_value) + + +def test_mean_current_source(create_temporary_jungfrau_files): + """ test mean_current_source """ + + rows, cols = 256, 256 + columns_per_subcolumn = 64 + mean_over_num_frames = 3 + num_samples = 5 + file_offset = 2 * columns_per_subcolumn + frames_per_sample = mean_over_num_frames * columns_per_subcolumn + num_frames = num_samples * frames_per_sample + file_offset + + col_values = np.tile(np.arange(cols, dtype=np.uint16), (rows, 1)) + + def frame_generator(frame): + frame_idx = ((frame - file_offset) // columns_per_subcolumn) % mean_over_num_frames + return col_values + frame_idx + + # spread over several files to cross file boundaries mid sample + file_system = create_temporary_jungfrau_files(num_frames, frame_generator, num_files=3) + + result = mean_current_source(file_system, num_samples, mean_over_num_frames, 4, file_offset) + + assert result.shape == (rows, cols, num_samples) + + # mean of c + 0, c + 1, ..., c + mean_over_num_frames-1 + sum_mean_over_frames = mean_over_num_frames * (mean_over_num_frames - 1) / 2 + expected = (col_values * mean_over_num_frames + sum_mean_over_frames) / mean_over_num_frames + np.testing.assert_allclose(result, np.repeat(expected[:, :, np.newaxis], num_samples, axis=2))