From 37e7ccb1a5f664e73a7acd5836388ce0d6fedc8e Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 18:00:37 +0100 Subject: [PATCH 1/2] Joint sweeps: sweeps within 1% of the finest d_min tie on resolution, the higher ISa decides The reference was the finest per-sweep d_min, ties broken by ISa - but sweeps of one crystal mostly stop at the same detector edge, with d_min apart in the third decimal by which reflections happen to be measured there, so the tie-break almost never fired and the reference was picked on noise. On a twinned small-molecule multi-sweep set this flipped when unrelated upstream changes moved the long, best sweep's edge d_min by a few thousandths of an A: a short sweep with half its ISa and a different beta became the reference, was joined, and SHELXL R1 went from 0.050 to 0.135. Within 1% of the finest d_min the sweeps now count as reaching the same resolution and the ISa decides. The per-sweep log line now also prints each sweep's d_min and ISa. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- rugnux/RugnuxJointSweeps.cpp | 19 ++++++++++++++----- rugnux/rugnux_cli.cpp | 5 +++-- tests/RugnuxJointSweepsTest.cpp | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/rugnux/RugnuxJointSweeps.cpp b/rugnux/RugnuxJointSweeps.cpp index db0a59943..0dff9f2ba 100644 --- a/rugnux/RugnuxJointSweeps.cpp +++ b/rugnux/RugnuxJointSweeps.cpp @@ -389,15 +389,24 @@ JointSweepRelation RelateJointSweeps(const std::vector &sweeps if (sweeps.empty()) return out; // The best sweep on its own: of the sweeps whose error model resolved an ISa, the one reaching the - // finest resolution, ties broken by the higher ISa (a sweep that merged nothing comes last). + // finest resolution, ties broken by the higher ISa (a sweep that merged nothing comes last). Sweeps of + // one crystal mostly stop at the same detector edge, their d_min apart in the third decimal by which + // reflections happen to be measured there: within 1% of the finest they reach the same resolution. const auto accuracy_known = [&](size_t k) { return sweeps[k].isa > 0.0; }; // false on NaN + const auto d_min = [&](size_t k) { return std::isfinite(sweeps[k].d_min) ? sweeps[k].d_min : INFINITY; }; + const bool any_known = std::any_of(sweeps.begin(), sweeps.end(), [](const JointMemberSweep &s) { return s.isa > 0.0; }); + double finest = INFINITY; + for (size_t k = 0; k < sweeps.size(); ++k) + if (accuracy_known(k) == any_known) + finest = std::min(finest, d_min(k)); + const auto reaches_finest = [&](size_t k) { return d_min(k) <= 1.01 * finest; }; const auto better = [&](size_t k, size_t than) { if (accuracy_known(k) != accuracy_known(than)) return accuracy_known(k); - const double dk = std::isfinite(sweeps[k].d_min) ? sweeps[k].d_min : INFINITY; - const double dt = std::isfinite(sweeps[than].d_min) ? sweeps[than].d_min : INFINITY; - if (dk != dt) - return dk < dt; + if (reaches_finest(k) != reaches_finest(than)) + return reaches_finest(k); + if (!reaches_finest(k) && d_min(k) != d_min(than)) + return d_min(k) < d_min(than); return (std::isfinite(sweeps[k].isa) ? sweeps[k].isa : 0.0) > (std::isfinite(sweeps[than].isa) ? sweeps[than].isa : 0.0); }; out.reference = 0; diff --git a/rugnux/rugnux_cli.cpp b/rugnux/rugnux_cli.cpp index 5d1cb6a2a..dc87a6760 100644 --- a/rugnux/rugnux_cli.cpp +++ b/rugnux/rugnux_cli.cpp @@ -4167,8 +4167,9 @@ int RugnuxCli::RunJointSweeps(const RugnuxSweepPlan &plan) { m.distance_mm = sweep.member_result->used_distance_mm; m.cell_esd = sweep.member_result->cell_esd; logger.Info("Sweep {}: {} frames, {} in {:.3f} {:.3f} {:.3f} {:.2f} {:.2f} {:.2f} at {:.2f} mm, {} fulls, " - "{} merged", k + 1, m.n_frames, m.space_group.xhm(), m.cell.a, m.cell.b, m.cell.c, - m.cell.alpha, m.cell.beta, m.cell.gamma, m.distance_mm, m.fulls.size(), m.merged.size()); + "{} merged to {:.3f} A, ISa {:.1f}", k + 1, m.n_frames, m.space_group.xhm(), m.cell.a, m.cell.b, + m.cell.c, m.cell.alpha, m.cell.beta, m.cell.gamma, m.distance_mm, m.fulls.size(), m.merged.size(), + m.d_min, m.isa); } else logger.Warning("Sweep {} ({}) gave no merge", k + 1, plan.sweeps[k].label); return m; diff --git a/tests/RugnuxJointSweepsTest.cpp b/tests/RugnuxJointSweepsTest.cpp index 6fd660ee7..dfb4af3c0 100644 --- a/tests/RugnuxJointSweepsTest.cpp +++ b/tests/RugnuxJointSweepsTest.cpp @@ -182,6 +182,23 @@ TEST_CASE("RelateJointSweeps_ReferenceIsTheBestMeasuredSweepNotTheFinestResoluti CHECK(relation.sweeps[1].decision == "no axes"); } +TEST_CASE("RelateJointSweeps_SweepsAtTheSameEdgeAreTiedOnResolution", "[Rugnux]") { + // Both sweeps stop at the same detector edge; the second's d_min is finer in the third decimal only, by + // the reflections it happened to measure there. That is a tie, and the higher ISa (the first) decides it. + const auto truth = Truth(1); + std::vector sweeps; + sweeps.push_back(MakeSweep(truth, gemmi::Op::identity(), 100, 2, 10.0)); + sweeps.push_back(MakeSweep(truth, gemmi::Op::identity(), 100, 3, 10.0)); + sweeps[0].isa = 40.0; + sweeps[1].isa = 20.0; + sweeps[1].d_min = sweeps[0].d_min * 0.995; + + CHECK(RelateJointSweeps(sweeps).reference == 0); + // ... but a resolution finer beyond that is a better sweep. + sweeps[1].d_min = sweeps[0].d_min * 0.95; + CHECK(RelateJointSweeps(sweeps).reference == 1); +} + TEST_CASE("RelateJointSweeps_ASweepTheMergeCannotMeasureJoins", "[Rugnux]") { // A cell of a few dozen unique reflections: too few for the isomorphism ratio, and the held-out // residual of the merge moves by less than its own noise. Nothing measured refuses the sweep, so it From 5be740a646bc3418f1accfe1e189295884f7c512 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 19:10:52 +0100 Subject: [PATCH 2/2] Joint sweeps: a sweep that kept the twin overlap the reference left out is not joined Where a crystal is a twin composite, the first pass of a sweep adopts the stronger domain's cell and leaves the reflections the other domain overlaps out of integration (RotationIndexerResult:: TwinOverlap). A sweep of the same crystal that indexed the domain's cell directly, without the composite, integrates those reflections with both domains' intensity in them. They are reflections the reference does not have, so neither the isomorphism ratio nor the held-out residual sees them - they count as "unique added" - and joining such a sweep put the overlapped class into the merge: on a twinned small-molecule multi-sweep set weak reflections came out several times too strong and SHELXL R1 rose from 0.05 to 0.13, whichever sweep was the reference. The sweep's run now reports its twin_overlap_index to the joint relation, and against a reference that left the overlap out such a sweep is left out ("twin overlap"). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- rugnux/RugnuxJointSweeps.cpp | 6 ++++++ rugnux/RugnuxJointSweeps.h | 1 + rugnux/rugnux_cli.cpp | 1 + tests/RugnuxJointSweepsTest.cpp | 17 +++++++++++++++++ 4 files changed, 25 insertions(+) diff --git a/rugnux/RugnuxJointSweeps.cpp b/rugnux/RugnuxJointSweeps.cpp index 0dff9f2ba..56057fb1c 100644 --- a/rugnux/RugnuxJointSweeps.cpp +++ b/rugnux/RugnuxJointSweeps.cpp @@ -440,6 +440,12 @@ JointSweepRelation RelateJointSweeps(const std::vector &sweeps if (!std::isfinite(choice.cc)) { s.decision = "no axes"; s.why_left_out = "it shares too few reflections with the reference sweep to be put on its axes"; + } else if (ref.twin_overlap_index > 0 && sweeps[k].twin_overlap_index == 0) { + // The reference's integration left out the reflections the other twin domain overlaps; this + // sweep's kept them, and there each holds both domains' intensity. + s.decision = "twin overlap"; + s.why_left_out = "its integration kept the reflections the other twin domain overlaps, which the " + "reference sweep's left out"; } } axes_mark.Stop(); diff --git a/rugnux/RugnuxJointSweeps.h b/rugnux/RugnuxJointSweeps.h index dbc0afd8c..966a82d97 100644 --- a/rugnux/RugnuxJointSweeps.h +++ b/rugnux/RugnuxJointSweeps.h @@ -35,6 +35,7 @@ struct JointMemberSweep { double isa = NAN; // ... and its ISa, NaN where b was not resolved double distance_mm = NAN; // the detector distance its run ended at std::optional cell_esd; // its cell's e.s.d.s, where its run measured them + int twin_overlap_index = 0; // its run's ProcessResult::twin_overlap_index double reprocessed_from_mm = NAN; // where processed again (SharedCellTarget): its first run's }; diff --git a/rugnux/rugnux_cli.cpp b/rugnux/rugnux_cli.cpp index dc87a6760..487b4acf1 100644 --- a/rugnux/rugnux_cli.cpp +++ b/rugnux/rugnux_cli.cpp @@ -4165,6 +4165,7 @@ int RugnuxCli::RunJointSweeps(const RugnuxSweepPlan &plan) { m.d_min = merged.statistics.overall.d_min; m.isa = sweep.member_result->error_model_isa_resolved ? sweep.member_result->error_model_isa : NAN; m.distance_mm = sweep.member_result->used_distance_mm; + m.twin_overlap_index = sweep.member_result->twin_overlap_index; m.cell_esd = sweep.member_result->cell_esd; logger.Info("Sweep {}: {} frames, {} in {:.3f} {:.3f} {:.3f} {:.2f} {:.2f} {:.2f} at {:.2f} mm, {} fulls, " "{} merged to {:.3f} A, ISa {:.1f}", k + 1, m.n_frames, m.space_group.xhm(), m.cell.a, m.cell.b, diff --git a/tests/RugnuxJointSweepsTest.cpp b/tests/RugnuxJointSweepsTest.cpp index dfb4af3c0..cced8cb75 100644 --- a/tests/RugnuxJointSweepsTest.cpp +++ b/tests/RugnuxJointSweepsTest.cpp @@ -199,6 +199,23 @@ TEST_CASE("RelateJointSweeps_SweepsAtTheSameEdgeAreTiedOnResolution", "[Rugnux]" CHECK(RelateJointSweeps(sweeps).reference == 1); } +TEST_CASE("RelateJointSweeps_ASweepThatKeptTheTwinOverlapIsLeftOut", "[Rugnux]") { + // The reference's integration left out the reflections the other twin domain overlaps (index 2); the + // third sweep's kept them. Its intensities agree elsewhere, so only the integration tells them apart. + const auto truth = Truth(1); + std::vector sweeps; + for (uint32_t seed = 2; seed <= 4; ++seed) + sweeps.push_back(MakeSweep(truth, gemmi::Op::identity(), 100, seed, 10.0)); + sweeps[0].isa = 200.0; + sweeps[0].twin_overlap_index = sweeps[1].twin_overlap_index = 2; + + const auto relation = RelateJointSweeps(sweeps); + REQUIRE(relation.reference == 0); + CHECK(relation.sweeps[1].included); + CHECK_FALSE(relation.sweeps[2].included); + CHECK(relation.sweeps[2].decision == "twin overlap"); +} + TEST_CASE("RelateJointSweeps_ASweepTheMergeCannotMeasureJoins", "[Rugnux]") { // A cell of a few dozen unique reflections: too few for the isomorphism ratio, and the held-out // residual of the merge moves by less than its own noise. Nothing measured refuses the sweep, so it