From c6f97460dda8214ea63dae40cc0aa7c6a079bfe0 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 19 Sep 2026 11:37:55 +0200 Subject: [PATCH] PostRefine: report the realised held-out residual, its noise and the starting cell Each post-refinement now records, beside what it commits, what the integration it was handed realises at the geometry it started from: the held-out residual at nominal (the same value the log prints on the left of "held-out ... ->"), its standard error over the held-out residual values, and the cell the pass's own indexing refined. Nothing here changes a decision. Two helpers read them: HeldOutResidualFell (a pass at a new geometry realises a lower residual than another by more than the standard error of the difference) and ReindexPushesCellBack (re-indexing at a committed geometry returns, on the length the fit moved most, a cell on the side the fit moved away from). The geometry walk in RunAllPasses uses both in the next commit. Co-Authored-By: Claude Opus 5 (1M context) --- image_analysis/geom_refinement/PostRefine.cpp | 57 +++++++++++++++++-- image_analysis/geom_refinement/PostRefine.h | 23 ++++++++ tests/CMakeLists.txt | 1 + tests/PostRefineTest.cpp | 53 +++++++++++++++++ 4 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 tests/PostRefineTest.cpp diff --git a/image_analysis/geom_refinement/PostRefine.cpp b/image_analysis/geom_refinement/PostRefine.cpp index 82a8d4027..65a6e905c 100644 --- a/image_analysis/geom_refinement/PostRefine.cpp +++ b/image_analysis/geom_refinement/PostRefine.cpp @@ -497,10 +497,12 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, // Cost of a whole geometry over one subset, the two residual families kept apart so the log // can say which of them moved. Each is a mean per residual VALUE, and the number they are // summed over below is the same weighting the solver itself applies. + // se_out, where asked for: the standard error of the returned mean over the residual values + // it averages. auto joint_cost = [&](Subset s, const double bm[2], const double ds[1], const double rv[3], const double q0[3], const double q1[3], const double q2[3], - double &pos_out, double &exc_out) { - double cp = 0.0, ce = 0.0; + double &pos_out, double &exc_out, double *se_out = nullptr) { + double cp = 0.0, ce = 0.0, c4 = 0.0; size_t np = 0, ne = 0; for (size_t oi = 0; oi < n_obs; ++oi) { const Partial *pp = obs[oi]; @@ -509,7 +511,10 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, angle_rad(pp->img), pp->h, pp->k, pp->l, sys, orientation); double res[3] = {0, 0, 0}; r(bm, ds, det_rot, rv, q0, q1, q2, res); - cp += res[0] * res[0] + res[1] * res[1] + res[2] * res[2]; + for (int j = 0; j < 3; ++j) { + cp += res[j] * res[j]; + c4 += res[j] * res[j] * res[j] * res[j]; + } np += 3; } for (const int32_t i : selected) { @@ -520,11 +525,17 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, double res = 0.0; r(rv, q0, q1, q2, &res); ce += res * res; + c4 += res * res * res * res; ++ne; } pos_out = np ? cp / np : 0.0; exc_out = ne ? ce / ne : 0.0; - return (np + ne) ? (cp + ce) / static_cast(np + ne) : 0.0; + const double n = static_cast(np + ne); + const double mean = (np + ne) ? (cp + ce) / n : 0.0; + if (se_out) + *se_out = (np + ne) > 1 ? std::sqrt(std::max(0.0, c4 / n - mean * mean) / (n - 1.0)) + : std::numeric_limits::quiet_NaN(); + return mean; }; // One problem, both residual families, every block seeded at the geometry it is handed - @@ -596,7 +607,11 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, bool commit = false; if (n_obs >= static_cast(settings.min_events)) { double pos_nom = 0.0, exc_nom = 0.0, pos_ref = 0.0, exc_ref = 0.0; - const double cv_nom = joint_cost(VAL, beam, dist, axv, p0, p1, p2, pos_nom, exc_nom); + double cv_nom_se = 0.0; + const double cv_nom = joint_cost(VAL, beam, dist, axv, p0, p1, p2, pos_nom, exc_nom, + &cv_nom_se); + result.held_out_before = cv_nom; + result.held_out_before_se = cv_nom_se; // How far a geometry has moved from nominal, in units of one step. const auto beyond_a_step = [&](const double ds[1], const double q1c[3]) { @@ -747,6 +762,11 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, double len_nom[3], ang_nom[3], len_fit[3], ang_fit[3]; EffectiveCellFromParams(sys, p1_0, p2_0, len_nom, ang_nom); EffectiveCellFromParams(sys, q1_log, q2_log, len_fit, ang_fit); + result.cell_before = UnitCell{static_cast(len_nom[0]), static_cast(len_nom[1]), + static_cast(len_nom[2]), + static_cast(ang_nom[0] * 180.0 / PI), + static_cast(ang_nom[1] * 180.0 / PI), + static_cast(ang_nom[2] * 180.0 / PI)}; logger.Info("Post-refine GEOM (joint crystal + detector): dist {:.3f} -> {:.3f} mm, beam " "({:.2f},{:.2f}) -> ({:.2f},{:.2f}), cell {:.3f} {:.3f} {:.3f} {:.2f} {:.2f} " "{:.2f} -> {:.3f} {:.3f} {:.3f} {:.2f} {:.2f} {:.2f}, held-out positional " @@ -1016,3 +1036,30 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, } } + +bool HeldOutResidualFell(const PostRefineResult &before, const PostRefineResult &after) { + const double noise = std::hypot(before.held_out_before_se, after.held_out_before_se); + return std::isfinite(noise) && after.held_out_before < before.held_out_before - noise; +} + +int ReindexPushesCellBack(const PostRefineResult &fit, const PostRefineResult &next) { + if (!fit.cell_refined) + return 0; + const double from[3] = {fit.cell_before.a, fit.cell_before.b, fit.cell_before.c}; + const double to[3] = {fit.cell.a, fit.cell.b, fit.cell.c}; + const double again[3] = {next.cell_before.a, next.cell_before.b, next.cell_before.c}; + int axis = -1; + double largest = 0.0; + for (int j = 0; j < 3; ++j) + if (from[j] > 0.0 && std::fabs(to[j] - from[j]) / from[j] > largest) { + largest = std::fabs(to[j] - from[j]) / from[j]; + axis = j; + } + if (axis < 0 || !(again[axis] > 0.0)) + return 0; + const double move = to[axis] - from[axis]; + const double back = again[axis] - to[axis]; + if (!(move * back < 0.0)) + return 0; + return move > 0.0 ? axis + 1 : -(axis + 1); +} diff --git a/image_analysis/geom_refinement/PostRefine.h b/image_analysis/geom_refinement/PostRefine.h index e1f3b7101..b6f20833c 100644 --- a/image_analysis/geom_refinement/PostRefine.h +++ b/image_analysis/geom_refinement/PostRefine.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -48,6 +49,17 @@ struct PostRefineResult { // from. Such a move is not refused for its size - it is RATIFIED by re-integrating and re-indexing // at it, which a second lattice does not survive and a wrong header does (see RunAllPasses). bool large_move = false; + // The held-out residual at the geometry the fit STARTED from - what the integration this fit was + // handed actually realises there, before the fit moves anything - and its standard error over the + // held-out values. Two passes at two geometries are compared on this, not on the fit's own + // prediction of where it is going (see the geometry walk in RunAllPasses). NaN where the fit had + // too few observations to measure it. + double held_out_before = std::numeric_limits::quiet_NaN(); + double held_out_before_se = std::numeric_limits::quiet_NaN(); + // The cell the fit started from: the one this pass's own indexing refined. Beside `cell` (what the + // fit committed), and against the next pass's cell_before, it says whether re-indexing at the + // committed geometry ratifies the committed cell. + UnitCell cell_before{}; // Which test refused the fit and what it wanted, where it was refused; empty where it committed. // Nothing else says it - the geometry that comes out of a refusal is the nominal one whatever // refused it, so a silent refusal cannot be told from a fit that never wanted to move. @@ -119,3 +131,14 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations obs, const CrystalLattice &reference_latt, const PostRefineSettings &settings, Logger &logger); + +// Whether the held-out residual `after` realises at its geometry is below the one `before` realises at +// its own by more than the standard error of the difference - the cross-validation noise of the two +// measurements. False where either was not measured. +bool HeldOutResidualFell(const PostRefineResult &before, const PostRefineResult &after); + +// Whether re-indexing at the geometry `fit` committed pushed the committed cell back: on the cell length +// the fit moved most, the cell `next` (the fit of the pass run at that geometry) started from lies on the +// side the fit moved away from. Returns +-(axis + 1), signed as the fit's move, where it did; 0 where the +// re-indexing followed the fit, or the fit committed nothing. +int ReindexPushesCellBack(const PostRefineResult &fit, const PostRefineResult &next); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 45510abb0..1b9ef6040 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,6 +54,7 @@ ADD_EXECUTABLE(jfjoch_test ParallelForTest.cpp RugnuxTest.cpp ResultReportTest.cpp + PostRefineTest.cpp RugnuxLargeTest.cpp TestData.h MovingAverageTest.cpp diff --git a/tests/PostRefineTest.cpp b/tests/PostRefineTest.cpp new file mode 100644 index 000000000..eec9cb593 --- /dev/null +++ b/tests/PostRefineTest.cpp @@ -0,0 +1,53 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#include + +#include "../image_analysis/geom_refinement/PostRefine.h" + +namespace { + PostRefineResult Measured(double residual, double se) { + PostRefineResult r; + r.held_out_before = residual; + r.held_out_before_se = se; + return r; + } + + PostRefineResult Fit(UnitCell before, UnitCell after) { + PostRefineResult r; + r.cell_refined = true; + r.cell_before = before; + r.cell = after; + return r; + } + + UnitCell Cell(float a, float b, float c) { + return UnitCell{.a = a, .b = b, .c = c, .alpha = 90.0f, .beta = 90.0f, .gamma = 90.0f}; + } +} + +TEST_CASE("PostRefine_HeldOutResidualFell", "[PostRefine]") { + // Down by more than the standard error of the difference, sqrt(3^2 + 4^2) = 5. + CHECK(HeldOutResidualFell(Measured(100.0, 3.0), Measured(94.0, 4.0))); + // Down, but within that noise. + CHECK_FALSE(HeldOutResidualFell(Measured(100.0, 3.0), Measured(96.0, 4.0))); + // Up. + CHECK_FALSE(HeldOutResidualFell(Measured(100.0, 3.0), Measured(110.0, 4.0))); + // Not measured on either side. + CHECK_FALSE(HeldOutResidualFell(PostRefineResult{}, Measured(10.0, 1.0))); + CHECK_FALSE(HeldOutResidualFell(Measured(100.0, 3.0), PostRefineResult{})); +} + +TEST_CASE("PostRefine_ReindexPushesCellBack", "[PostRefine]") { + // The fit shortens b the most; re-indexing at its geometry returns a longer b: pushed back. + const auto fit = Fit(Cell(96.9f, 107.9f, 112.9f), Cell(96.8f, 106.6f, 112.9f)); + CHECK(ReindexPushesCellBack(fit, Fit(Cell(96.7f, 107.6f, 112.6f), Cell(96.6f, 106.4f, 112.8f))) == -2); + // A walk that re-indexing follows (and overshoots a little) is not pushed back. + const auto walk = Fit(Cell(60.48f, 60.48f, 196.7f), Cell(60.05f, 60.05f, 195.2f)); + CHECK(ReindexPushesCellBack(walk, Fit(Cell(60.00f, 60.00f, 195.1f), Cell(59.8f, 59.8f, 194.2f))) == 0); + // Nothing committed, or no cell measured by the next pass: no evidence either way. + PostRefineResult refused = fit; + refused.cell_refined = false; + CHECK(ReindexPushesCellBack(refused, Fit(Cell(96.7f, 107.6f, 112.6f), Cell(96.6f, 106.4f, 112.8f))) == 0); + CHECK(ReindexPushesCellBack(fit, PostRefineResult{}) == 0); +}