From ca98ca8397c406cc1e9c62b1d50c50cdf8ce631d Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 26 Sep 2026 23:02:14 +0200 Subject: [PATCH] HotPixelFinder: keep the level and valid-frame sums per ring-sector, not per pixel Every valid pixel of every sampled frame read and wrote its valid count and level sum, 20 of the 44 bytes the accumulation moved per pixel, on a pass that is bound by memory bandwidth. Both depend on the pixel only through its ring-sector and its own error frames: they are now summed once per frame per ring-sector, and a pixel keeps only what its error frames take out of them. Integers, so the same sums. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C --- rugnux/HotPixels.cpp | 31 ++++++++++++++++++++----------- rugnux/HotPixels.h | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/rugnux/HotPixels.cpp b/rugnux/HotPixels.cpp index d8eb8b67d..65804f8ac 100644 --- a/rugnux/HotPixels.cpp +++ b/rugnux/HotPixels.cpp @@ -39,8 +39,8 @@ HotPixelFinder::HotPixelFinder(const DiffractionExperiment &experiment, const Pi axis(experiment.GetGoniometer() ? experiment.GetGoniometer()->GetAxis().Normalize() : Coord(0, 0, 0)), width(experiment.GetXPixelsNumConv()), height(experiment.GetYPixelsNumConv()), key(width * height, -1), - n_valid(width * height, 0), n_lit(width * height, 0), n_error(width * height, 0), - sum_value(width * height, 0), sum_level(width * height, 0) { + n_lit(width * height, 0), n_error(width * height, 0), sum_value(width * height, 0), + n_error_ring_ok(width * height, 0), error_level_sum(width * height, 0) { const auto &m = mask.GetMask(); // Rings of equal 2theta, RING_WIDTH_PX wide where the beam meets the detector. const float ring_rad = RING_WIDTH_PX * geometry.GetPixelSize_mm() / geometry.GetDetectorDistance_mm(); @@ -62,6 +62,8 @@ HotPixelFinder::HotPixelFinder(const DiffractionExperiment &experiment, const Pi unmasked++; } + key_frames.assign(static_cast(nrings) * SECTORS, 0); + key_level_sum.assign(static_cast(nrings) * SECTORS, 0); key_begin.assign(static_cast(nrings) * SECTORS + 1, 0); for (const int32_t k : key) if (k >= 0) key_begin[k + 1]++; @@ -121,6 +123,11 @@ void HotPixelFinder::AddImage(const int32_t *image, std::vector &scratc { std::lock_guard lock(m); frames++; + for (size_t k = 0; k < nkeys; k++) + if (ring_ok[k / SECTORS]) { + key_frames[k]++; + key_level_sum[k] += level[k]; + } } const size_t rows_per_band = (height + BANDS - 1) / BANDS; const size_t first = next_band.fetch_add(1); @@ -134,12 +141,14 @@ void HotPixelFinder::AddImage(const int32_t *image, std::vector &scratc if (k < 0) continue; if (v == INT32_MIN) { n_error[i]++; + if (ring_ok[k / SECTORS]) { // counted in its ring-sector's frames, but not valid here + n_error_ring_ok[i]++; + error_level_sum[i] += level[k]; + } continue; } if (!ring_ok[k / SECTORS]) continue; - n_valid[i]++; sum_value[i] += v; - sum_level[i] += level[k]; if (v == INT32_MAX || static_cast(v) > threshold[k]) n_lit[i]++; } @@ -158,9 +167,9 @@ HotPixelFinder::Result HotPixelFinder::GetMask(double oscillation_deg, double sp // lights those - reflections, zingers, noise above the bound - lights a defect-free pixel too. std::vector lit(nrings, 0.0), seen(nrings, 0.0); for (size_t i = 0; i < key.size(); i++) - if (key[i] >= 0 && n_valid[i] > 0 && 2 * n_lit[i] <= n_valid[i]) { + if (key[i] >= 0 && n_valid(i) > 0 && 2 * n_lit[i] <= n_valid(i)) { lit[key[i] / SECTORS] += n_lit[i]; - seen[key[i] / SECTORS] += n_valid[i]; + seen[key[i] / SECTORS] += n_valid(i); } std::vector k_chance(nrings, n + 1); for (int r = 0; r < nrings; r++) @@ -175,7 +184,7 @@ HotPixelFinder::Result HotPixelFinder::GetMask(double oscillation_deg, double sp for (size_t y = y0; y < static_cast(y1); y++) for (size_t x = 0; x < width; x++) { const size_t i = y * width + x; - if (key[i] < 0 || n_valid[i] < min_valid || n_lit[i] == 0 || !(spacing_deg > 0.0)) continue; + if (key[i] < 0 || n_valid(i) < min_valid || n_lit[i] == 0 || !(spacing_deg > 0.0)) continue; // One reflection's stay on this pixel, in sampled frames: 1 / |zeta| turns the rocking // width into rotation, and zeta = |axis . (s1 x s0)| with s0 along z. const Coord s1 = geometry.LabCoord(static_cast(x), static_cast(y)).Normalize(); @@ -183,7 +192,7 @@ HotPixelFinder::Result HotPixelFinder::GetMask(double oscillation_deg, double sp const double stay = (oscillation_deg + ROCKING_WIDTH_DEG) / zeta; const double k_bragg = 1.0 + std::ceil(stay / spacing_deg); const double k = std::min(std::max(k_bragg, k_chance[key[i] / SECTORS]), - n_valid[i]); + n_valid(i)); persistent[i] = n_lit[i] >= k; } }); @@ -215,10 +224,10 @@ HotPixelFinder::Result HotPixelFinder::GetMask(double oscillation_deg, double sp for (size_t xx = (x > 0 ? x - 1 : 0); xx <= std::min(width - 1, x + 1); xx++) if ((xx != x || yy != y) && persistent[yy * width + xx]) isolated = persistent_neighbours(xx, yy) == 1; - const double mean_level = static_cast(sum_level[i]) / n_valid[i]; - const double excess = static_cast(sum_value[i] - sum_level[i]) / n_valid[i]; + const double mean_level = static_cast(sum_level(i)) / n_valid(i); + const double excess = static_cast(sum_value[i] - sum_level(i)) / n_valid(i); if (isolated - && static_cast(sum_value[i]) >= STRONG_RATIO * static_cast(sum_level[i]) + && static_cast(sum_value[i]) >= STRONG_RATIO * static_cast(sum_level(i)) && excess >= LIT_NSIGMA * std::sqrt(std::max(mean_level, 0.0)) + LIT_OFFSET) ret.mask[i] = 1; } diff --git a/rugnux/HotPixels.h b/rugnux/HotPixels.h index 03dfc64e0..bd183ad0d 100644 --- a/rugnux/HotPixels.h +++ b/rugnux/HotPixels.h @@ -106,6 +106,16 @@ private: std::atomic next_band{0}; mutable std::mutex m; uint32_t frames = 0; - std::vector n_valid, n_lit, n_error; - std::vector sum_value, sum_level; + std::vector n_lit, n_error; + std::vector sum_value; + // A pixel's valid frames and the sum of its ring-sector level over them, kept per ring-sector - the + // frames its ring had a level on, and the sum of the level over those - less what the pixel's own + // error frames among them took out. Integers, so exactly the per-pixel sums, at a fraction of the + // memory traffic per frame. + std::vector key_frames; + std::vector key_level_sum; + std::vector n_error_ring_ok; + std::vector error_level_sum; + [[nodiscard]] int n_valid(size_t i) const { return key_frames[key[i]] - n_error_ring_ok[i]; } + [[nodiscard]] int64_t sum_level(size_t i) const { return key_level_sum[key[i]] - error_level_sum[i]; } };