From 697bc1143842ee1f1c27c95237edea185e72dbf6 Mon Sep 17 00:00:00 2001 From: Alice Date: Thu, 8 Oct 2026 10:26:11 +0200 Subject: [PATCH] WIP: read_current_source_mean --- benchmarks/DataSetAggregation_benchmark.cpp | 17 ++- include/aare/DataSetAggregation.hpp | 131 ++++++++++++++++++++ 2 files changed, 147 insertions(+), 1 deletion(-) diff --git a/benchmarks/DataSetAggregation_benchmark.cpp b/benchmarks/DataSetAggregation_benchmark.cpp index bce3ec21..563000fd 100644 --- a/benchmarks/DataSetAggregation_benchmark.cpp +++ b/benchmarks/DataSetAggregation_benchmark.cpp @@ -21,7 +21,7 @@ class JungfrauTestDataSet : public benchmark::Fixture { static void DatasetAggregation_MeanOverDataset(benchmark::State &st) { const std::filesystem::path file_path = "/home/mazzol_a/Documents/temp_jungfrau/" - "CS_M749_2026-05-19_HGOG1G2_000000.dat"; + "BP_M749_2026-05-19_HGOG1G2_000000.dat"; for (auto _ : st) { aare::NDArray res = @@ -31,4 +31,19 @@ static void DatasetAggregation_MeanOverDataset(benchmark::State &st) { } } +static void DatasetAggregation_MeanCurrentSource(benchmark::State &st) { + const std::filesystem::path file_path = + "/home/mazzol_a/Documents/temp_jungfrau/" + "CS_M749_2026-05-19_HGOG1G2_000000.dat"; + + for (auto _ : st) { + aare::NDArray res = aare::mean_current_source( + file_path, 10, 97, 16, + 10 * 3 * 64); // 10 frames * 3 samples * 64 columns + + benchmark::DoNotOptimize(res); + } +} + BENCHMARK(DatasetAggregation_MeanOverDataset); +BENCHMARK(DatasetAggregation_MeanCurrentSource); \ No newline at end of file diff --git a/include/aare/DataSetAggregation.hpp b/include/aare/DataSetAggregation.hpp index 52a66530..6ebcb3ca 100644 --- a/include/aare/DataSetAggregation.hpp +++ b/include/aare/DataSetAggregation.hpp @@ -108,4 +108,135 @@ NDArray mean_over_dataset(const std::filesystem::path &path, return mean_per_sample; } +/* + * @brief Compute the mean current source data from a Jungfrau detector file + * @param path The path to the Jungfrau detector file. + * @param mean_over_num_frames The number of frames to mean over for each + * column. + * @param num_samples The number of samples in the dataset e.g. exposure times + * @param num_threads The number of threads to use for parallel computation. + * @param file_offset The offset in the file to start reading from. + * @return An NDArray containing the mean current source data for each sample. + */ +NDArray mean_current_source(const std::filesystem::path &path, + const size_t mean_over_num_frames, + const size_t num_samples, + const int num_threads = 16, + size_t file_offset = 0) { + + File meta_file(path); + + size_t total_num_frames = meta_file.total_frames(); + + const size_t num_rows = meta_file.rows(); + const size_t num_cols = meta_file.cols(); + const size_t half_rows = num_rows / 2; + + constexpr size_t columns_per_subcolumn = 64; + + constexpr size_t num_subcolumns = 16; + + if (total_num_frames != + mean_over_num_frames * num_samples * columns_per_subcolumn + + file_offset) { + throw std::runtime_error(fmt::format( + "Frames mismatch: Expected {} frames got {}", + mean_over_num_frames * num_samples * columns_per_subcolumn + + file_offset, + total_num_frames)); + } + + const double inv_mean_over_num_frames = + 1.0 / static_cast(mean_over_num_frames); + + NDArray cs_data({static_cast(num_samples), + static_cast(num_rows), + static_cast(num_cols)}, + 0.0); + + auto mean_over_samples = [&](size_t start_sample, size_t end_sample) { + File file(path); + + file.seek(start_sample * mean_over_num_frames * columns_per_subcolumn + + file_offset); + + NDArray frame( + {static_cast(num_rows), static_cast(num_cols)}); + + std::vector sum(num_rows * num_cols, + 0.0); // need to be double to avoid + // overflow for uint16_t + + for (size_t i = start_sample; i < end_sample; ++i) { + + std::fill(sum.begin(), sum.end(), 0.0); + + for (size_t acq_idx = 0; acq_idx < mean_over_num_frames; + ++acq_idx) { + for (size_t col_idx = 0; col_idx < columns_per_subcolumn; + ++col_idx) { + file.read_into(reinterpret_cast(frame.data())); + + const uint16_t *frame_data_ptr = frame.data(); + + for (size_t r = 0; r < half_rows; ++r) { + const size_t row_offset = r * num_cols; + for (size_t subcol_idx = 0; subcol_idx < num_subcolumns; + ++subcol_idx) { + + const size_t absolute_col_idx = + subcol_idx * columns_per_subcolumn + + col_idx; // TODO: maybe precompute and use a + // lookup table for this + sum[row_offset + absolute_col_idx] += + frame_data_ptr[row_offset + absolute_col_idx]; + } + } + + for (size_t r = half_rows; r < num_rows; ++r) { + const size_t row_offset = r * num_cols; + for (size_t subcol_idx = 0; subcol_idx < num_subcolumns; + ++subcol_idx) { + + const size_t absolute_col_idx = + (subcol_idx + 1) * columns_per_subcolumn - + col_idx; + sum[row_offset + absolute_col_idx] += + frame_data_ptr[row_offset + absolute_col_idx]; + } + } + } + } + + uint16_t *cs_data_ptr = cs_data.data() + i * num_rows * num_cols; + for (size_t idx = 0; idx < num_rows * num_cols; ++idx) { + cs_data_ptr[idx] = + static_cast(sum[idx] * inv_mean_over_num_frames); + } + } + }; + + std::vector> futures(num_threads); + + const size_t chunk_size = num_samples / num_threads; + + for (int i = 0; i < num_threads - 1; ++i) { + size_t end_sample = std::min((i + 1) * chunk_size, num_samples); + + futures[i] = std::async(std::launch::async, mean_over_samples, + i * chunk_size, end_sample); + } + + int end_sample = num_samples; + futures[num_threads - 1] = + std::async(std::launch::async, mean_over_samples, + (num_threads - 1) * chunk_size, end_sample); + + for (auto &f : futures) { + f.get(); + } + + return cs_data; +} + } // namespace aare \ No newline at end of file