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; }