From 6f0bc64b0a8e308c1e215564369a3c5fb3cf24be Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Mon, 2 Nov 2026 08:43:07 +0100 Subject: [PATCH] Joint sweeps: a doubtful sweep's added coverage must be at least 1% of the merge's unique reflections; isomorphism rows tie-broken by index Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQqrcrEZy6XBUCMZvyT --- docs/RUGNUX_CLI.md | 4 ++-- docs/RUGNUX_REPORT.md | 4 ++-- rugnux/RugnuxJointSweeps.cpp | 13 +++++++++---- rugnux/RugnuxJointSweeps.h | 10 ++++++++-- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/docs/RUGNUX_CLI.md b/docs/RUGNUX_CLI.md index 56dab1a29..d52de7f74 100644 --- a/docs/RUGNUX_CLI.md +++ b/docs/RUGNUX_CLI.md @@ -112,8 +112,8 @@ of basis, an axis permutation or a twin law of the lattice, under which their me best; where the lattice allows more than one indexing, which one each sweep takes is settled over all pairs of sweeps at once - and each is judged by its isomorphism ratio with it (see [RUGNUX_REPORT](RUGNUX_REPORT.md)): clearly about 1 it joins, clearly below it (another crystal) it is left out, and in between the merge decides - it -joins if it helps - adding it lowers the held-out residual of the others' merge, or it adds unique -reflections while that residual is not worse beyond its noise (a sweep left out +joins if it helps - adding it lowers the held-out residual of the others' merge, or it adds at least 1% +to their unique reflections while that residual is not worse beyond its noise (a sweep left out is named in the log). The crystal has one cell: a joined sweep whose own cell is more than 0.3% off that of the joined sweep reaching the finest resolution (where the cell is measured rather than the distance) is processed a second time, from the distance that would make the two cells agree, and that run replaces the diff --git a/docs/RUGNUX_REPORT.md b/docs/RUGNUX_REPORT.md index cd26dee8f..020993710 100644 --- a/docs/RUGNUX_REPORT.md +++ b/docs/RUGNUX_REPORT.md @@ -349,8 +349,8 @@ A run of several sweeps merged as one (`--sweeps joint`, the default with severa residual of the merge of the joined sweeps - each half of their observations predicted from the other half, without and with this sweep's added, over the reflections it has, after a scale and B factor onto the reference - with its bootstrap error, and **`SWEEP_k_HELD_OUT_ADDED=`**, the unique reflections it adds to - that merge; the sweep joins where it helps: the change is negative, or it adds unique reflections and the - change is not above twice its error; **`SWEEP_k_DISTANCE=`**, the detector + that merge; the sweep joins where it helps: the change is negative, or it adds at least 1% to the merge's + unique reflections and the change is not above twice its error; **`SWEEP_k_DISTANCE=`**, the detector distance its run ended at, and **`SWEEP_k_REPROCESSED_FROM=`** where the sweep was processed a second time (see [RUGNUX_CLI](RUGNUX_CLI.md)): the distance its first run ended at; and, for a sweep in the joint merge, **`SWEEP_k_REINDEX_OP=`** (onto the reference's axes) with diff --git a/rugnux/RugnuxJointSweeps.cpp b/rugnux/RugnuxJointSweeps.cpp index 3fbe0decd..0138acc1d 100644 --- a/rugnux/RugnuxJointSweeps.cpp +++ b/rugnux/RugnuxJointSweeps.cpp @@ -42,7 +42,7 @@ SweepIsomorphism MeasureSweepIsomorphism(const std::vector &a, const }; const auto ma = merge(a, cob_a); const auto mb = merge(b, cob_b); - struct Row { double fa, a1, a2, fb, b1, b2; bool ha, hb; float d; }; + struct Row { double fa, a1, a2, fb, b1, b2; bool ha, hb; float d; uint64_t key; }; std::vector rows; for (const auto &[k, sa] : ma) { const auto it = mb.find(k); @@ -52,14 +52,16 @@ SweepIsomorphism MeasureSweepIsomorphism(const std::vector &a, const const auto full = [](const Sums &s) { return (s.wi[0] + s.wi[1]) / (s.w[0] + s.w[1]); }; const auto half = [](const Sums &s, int h) { return s.w[h] > 0 ? s.wi[h] / s.w[h] : 0.0; }; rows.push_back({full(sa), half(sa, 0), half(sa, 1), full(sb), half(sb, 0), half(sb, 1), - sa.w[0] > 0 && sa.w[1] > 0, sb.w[0] > 0 && sb.w[1] > 0, sa.d}); + sa.w[0] > 0 && sa.w[1] > 0, sb.w[0] > 0 && sb.w[1] > 0, sa.d, k}); } SweepIsomorphism out; constexpr size_t MIN_ROWS = 200; // 100 in the half the ratio is measured on if (rows.size() < MIN_ROWS) return out; // The coarser half of the common range, in a fixed order (the map's order is not). - std::sort(rows.begin(), rows.end(), [](const Row &x, const Row &y) { return x.d > y.d; }); + std::sort(rows.begin(), rows.end(), [](const Row &x, const Row &y) { + return x.d != y.d ? x.d > y.d : x.key < y.key; // ties by index, not by the map's hash order + }); rows.resize(rows.size() / 2); const auto ratio = [&](const std::vector &idx) { @@ -212,6 +214,7 @@ HeldOutGain MeasureHeldOutGain(const std::vector * rows.push_back({r.without / n_target, r.with / n_target}); } HeldOutGain out; + out.base_unique = static_cast(pool.size()); for (const auto &[k, obs] : added) if (!pool.contains(k)) ++out.added; @@ -455,7 +458,9 @@ JointSweepRelation RelateJointSweeps(const std::vector &sweeps s.held_out_n = g.n; s.held_out_added = g.added; s.included = std::isfinite(g.change) - && (g.change < 0.0 || (g.added > 0 && std::isfinite(g.se) && g.change <= 2.0 * g.se)); + && (g.change < 0.0 + || (static_cast(g.added) >= MIN_ADDED_FRACTION * static_cast(g.base_unique) + && g.added > 0 && std::isfinite(g.se) && g.change <= 2.0 * g.se)); s.decision = s.included ? "doubtful: kept" : "doubtful: left out"; if (s.included) continue; diff --git a/rugnux/RugnuxJointSweeps.h b/rugnux/RugnuxJointSweeps.h index 7356c7ee9..caeb8b323 100644 --- a/rugnux/RugnuxJointSweeps.h +++ b/rugnux/RugnuxJointSweeps.h @@ -65,7 +65,8 @@ SweepIsomorphism MeasureSweepIsomorphism(const std::vector &a, const struct HeldOutGain { double change = NAN, se = NAN; int64_t n = 0; - int64_t added = 0; // unique reflections `d` has that the merge of `base` lacks + int64_t added = 0; // unique reflections `d` has that the merge of `base` lacks + int64_t base_unique = 0; // ... and the unique reflections that merge has }; HeldOutGain MeasureHeldOutGain(const std::vector *> &base, const std::vector &base_cob, @@ -74,7 +75,7 @@ HeldOutGain MeasureHeldOutGain(const std::vector * // How each sweep is put on the axes of the reference and whether it joins it. The reference is the sweep // whose own merge reaches the finest resolution (ties: the higher ISa) - the best-diffracting, least -// damaged one, as KAMO picks the data set of the smallest Wilson B - not the largest: a long but poor +// damaged one, as KAMO (Yamashita et al. (2018) Acta Cryst. D74, 441-449) picks the data set of the smallest Wilson B - not the largest: a long but poor // sweep is a poor yardstick for the others, and one that stays at low resolution shares too few // reflections with the rest. Each other sweep is put on its axes (the change of basis, an // axis permutation or a twin law of the lattice, ChooseReferenceSetting, under which its merged @@ -94,6 +95,11 @@ HeldOutGain MeasureHeldOutGain(const std::vector * // 0.997 +- 0.002; two non-isomorphous tetragonal crystals 0.951 +- 0.003, two non-isomorphous trigonal // crystals 0.962 +- 0.002. constexpr double JOIN_RATIO = 0.99; +// What "adds unique reflections" means for a doubtful sweep: at least 1% more than the joined sweeps have. +// Below that its coverage changes the completeness by less than the last digit a report prints, so it +// buys nothing a user would see, and a handful of reflections at the edge of its range must not carry a +// doubtful sweep in. The small wedges this is for add 10-60% (a 50 deg wedge to two others: ~30%). +constexpr double MIN_ADDED_FRACTION = 0.01; constexpr double LEAVE_RATIO = 0.97; struct JointSweepRelation {