From 8ecaba13416498c09a6b20df34cd0134067aadc9 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sun, 20 Sep 2026 11:18:37 +0200 Subject: [PATCH] Rotation scaling: a collapsed frame stays out of the smoothing window Smoothing the per-frame scale inside the loop runs before the guard that drops frames whose scale collapsed, and the window is a geometric mean - so a frame the crystal did not diffract on was both dragging its whole window down by its logarithm and being lifted toward its neighbours, which is how it escaped the guard meant to drop it. Measured: on sweeps with a genuinely dead stretch the guard went from eight fires at up to a million times below the median to five at six hundred times, and on a long clean sweep with a dead block from six fires to none. A frame already below the guard's floor is now left out of every window and keeps its own fitted scale, so what reaches the guard is the scale the fit produced. The floor is the guard's own, read the same way - no new criterion and no new constant. The guard's fires come back where the fit still collapses those frames (on the two dead-stretch sets to seven and six, at up to three and seven million times below the median; on the long clean sweep to six, the count it had before), and with them the error-model asymptote: ISa 7.9 back to 9.2 on one dead-stretch set, 36.7 to 38.0 on the clean one. Where the restraint stops the fit from driving a frame below the floor at all there is nothing to exclude and nothing changes. No result moves the wrong way: the fine-sliced, small-cell and powder sets keep the merges the restraint recovered - two of them improve, one by CC1/2 0.916 to 0.943 - and every space group is unchanged, including the short-sweep tetragonal pair and the de-novo orthorhombic set the loop was fixed for. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_013nW6FNRP1bBJJ8pfHiByAT --- .../scale_merge/RotationScaleMerge.cpp | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/image_analysis/scale_merge/RotationScaleMerge.cpp b/image_analysis/scale_merge/RotationScaleMerge.cpp index 1a8fa8039..c2445a590 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.cpp +++ b/image_analysis/scale_merge/RotationScaleMerge.cpp @@ -3221,19 +3221,36 @@ void RotationScaleMerge::ComputeSmoothGWindow(const std::vector &g, int const int half = window / 2; g_smooth.assign(n, NAN); - // No collapsed-scale handling here, and the caller is the scaling loop, which runs BEFORE - // DropCollapsedScales - so a frame the crystal did not diffract on is in the window with the rest. - // The window average is a GEOMETRIC mean, so such a frame drags its whole window down by its - // logarithm, and is itself lifted toward its neighbours. That is the point on a fine-sliced sweep - // (a frame with two reflections cannot be told from a dead one by its own scale) and it is the - // price on a sweep with a genuinely dead stretch: the stretch is no longer far enough below the - // median for DropCollapsedScales to see it, and what removes it is the sweep ledger's - // delta-CC1/2, which asks what the frames cost rather than how small their scale came out. + // The caller is the scaling loop, which runs BEFORE DropCollapsedScales, so a frame the crystal + // did not diffract on is still here. The window average is a GEOMETRIC mean, so left in it such a + // frame would drag its whole window down by its logarithm AND be lifted toward its neighbours - + // which is how it would escape the guard that is meant to drop it. A frame already below the + // guard's floor is therefore left out of every window and keeps its own fitted scale (g_smooth + // stays NAN, which the caller reads as "leave this frame alone"), so what reaches + // DropCollapsedScales is the scale the fit actually produced. + // + // The floor is the guard's, read the same way: MIN_CREDIBLE_SCALE_RATIO of the MEDIAN fitted + // scale, not of the precision-weighted typical frame - see DropCollapsedScales for why the median. + std::vector fitted; + fitted.reserve(n); + for (int f = 0; f < n; ++f) + if (frame_scaled_scratch[f] && std::isfinite(g[f]) && g[f] > 0.0) + fitted.push_back(g[f]); + if (fitted.empty()) + return; + const size_t mid = fitted.size() / 2; + std::nth_element(fitted.begin(), fitted.begin() + mid, fitted.end()); + const double g_floor = fitted[mid] * MIN_CREDIBLE_SCALE_RATIO; + + auto credible = [&](int f) { + return frame_scaled_scratch[f] && std::isfinite(g[f]) && g[f] >= g_floor; + }; for (int o = 0; o < n; ++o) { + if (!credible(o)) continue; double sum_log = 0.0; int count = 0; for (int j = std::max(0, o - half); j <= std::min(n - 1, o + half); ++j) { - if (frame_scaled_scratch[j] && std::isfinite(g[j]) && g[j] > 0.0) { + if (credible(j)) { sum_log += std::log(g[j]); ++count; }