Merge branch 'integ-crowded-6jgj' into rc173 (integration: a ring starved only by neighbours is taken whole)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C

# Conflicts:
#	image_analysis/bragg_integration/BraggIntegrationEngineCPU.cpp
This commit is contained in:
2026-09-28 04:57:10 +02:00
4 changed files with 67 additions and 16 deletions
@@ -155,6 +155,7 @@ std::vector<Reflection> BraggIntegrationEngineCPU::RunImpl(const Sampler &img,
std::vector<Rough> rough(npredicted);
double inv_d2_min = std::numeric_limits<double>::max(), inv_d2_max = 0.0;
std::vector<int32_t> bkg_vals; // reused per reflection for the trimmed-mean background (idea 1)
std::vector<int32_t> bkg_vals_neighbour; // the ring pixels a neighbour's region holds, likewise
// Radial background curve, accumulated from the annulus pixels this pass already reads. A pixel's
// radius is the reflection's radius plus the pixel's projection on the beam->reflection direction,
@@ -211,7 +212,12 @@ std::vector<Reflection> BraggIntegrationEngineCPU::RunImpl(const Sampler &img,
// someone else's - never this reflection's own core. That makes this an exact count of what
// the pattern's density took, separable from what the detector took.
int n_bkg_neighbour = 0;
// The readable pixels among those, kept apart so a ring the neighbours starved can fall back
// on them (below).
double bkg_sum_neighbour = 0.0;
int n_bkg_neighbour_valid = 0;
bkg_vals.clear();
bkg_vals_neighbour.clear();
for (int y = y0; y <= y1; ++y)
for (int x = x0; x <= x1; ++x) {
const auto d = BraggStencilDistances(st, x - r.predicted_x, y - r.predicted_y);
@@ -237,7 +243,15 @@ std::vector<Reflection> BraggIntegrationEngineCPU::RunImpl(const Sampler &img,
y_sum += y;
++n_inner_valid;
} else if (d.inner >= r2_sq && d.outer < r3_sq) {
if (refl_mask.Get(x, y)) { ++n_bkg_neighbour; continue; }
if (refl_mask.Get(x, y)) {
++n_bkg_neighbour;
if (valid(px)) {
bkg_sum_neighbour += static_cast<double>(px);
++n_bkg_neighbour_valid;
if (bkg_trim_frac > 0.0) bkg_vals_neighbour.push_back(px);
}
continue;
}
if (!valid(px)) continue;
bkg_sum += static_cast<double>(px);
if (bkg_trim_frac > 0.0) bkg_vals.push_back(px);
@@ -254,9 +268,9 @@ std::vector<Reflection> BraggIntegrationEngineCPU::RunImpl(const Sampler &img,
// survived to constrain the amplitude (XDS's MINPK, dials' valid_foreground_threshold). A box
// sum has no profile to renormalise with, so there it stays all or nothing.
const bool full = n_inner_valid == n_inner;
// A ring left with five or fewer clean pixels cannot estimate a background, so the reflection
// is dropped whole - the one thing the stencil geometry does to the DATA rather than to a
// measurement. Counted here because it is the only direct evidence of a radius that has
// A ring left with five or fewer readable pixels cannot estimate a background, so the
// reflection is dropped whole - the one thing the stencil geometry does to the DATA rather
// than to a measurement. Counted here because it is the only direct evidence of a radius that has
// outgrown the pattern it is integrating (BraggIntegrationCounts).
const bool keep_partial = full || mode != IntegratorMode::BoxSum;
if (keep_partial && n_bkg <= 5) {
@@ -265,6 +279,18 @@ std::vector<Reflection> BraggIntegrationEngineCPU::RunImpl(const Sampler &img,
// detector, that took it.
if (n_bkg + n_bkg_neighbour > 5) ++counts.bkg_starved_by_neighbour;
}
// A ring the NEIGHBOURS starved still holds readable pixels - only the mask set them aside.
// Most of those neighbours are predictions in the tail of their rocking curve, which put a
// few percent of their flux on this frame: on a finely sliced, dense pattern they fill the
// whole ring while the frame shows no spot there. Dropping the reflection loses a measurement
// to protect a background from flux that is mostly not there, so the ring is taken whole
// instead, and the high-side clip below removes the neighbour cores that are.
const bool ring_unmasked = keep_partial && n_bkg <= 5 && n_bkg + n_bkg_neighbour_valid > 5;
if (ring_unmasked) {
bkg_sum += bkg_sum_neighbour;
n_bkg += n_bkg_neighbour_valid;
bkg_vals.insert(bkg_vals.end(), bkg_vals_neighbour.begin(), bkg_vals_neighbour.end());
}
if (keep_partial && n_bkg > 5) {
out.bkg = bkg_sum / n_bkg;
if (bkg_trim_frac > 0.0 && bkg_vals.size() > 5
@@ -291,7 +317,7 @@ std::vector<Reflection> BraggIntegrationEngineCPU::RunImpl(const Sampler &img,
for (int x = x0; x <= x1; ++x) {
const auto d = BraggStencilDistances(st, x - r.predicted_x, y - r.predicted_y);
if (!(d.inner >= r2_sq && d.outer < r3_sq)) continue;
if (refl_mask.Get(x, y)) continue;
if (refl_mask.Get(x, y) && !ring_unmasked) continue;
const int32_t px = img[y * W + x];
if (!valid(px)) continue;
if (static_cast<double>(px) <= thr) {