From 0f4c6f37bfc082de761c42d048186e217fd2f572 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 20:34:22 +0100 Subject: [PATCH 1/2] Joint sweeps: the reference is a sweep the others agree with The reference of a joint multi-sweep merge was the best sweep on its own (resolved ISa, finest d_min, then ISa). On a set of three non-merohedrally twinned crystals the first look made the reference a sweep that had integrated the crystal in a wrong C2 setting: it reached 1.65 A and resolved an ISa, but its CC1/2 was 0.02 and no other sweep correlated with it (CC -0.02..0.04). Every other sweep then had "no axes" and was processed again given its cell, and the final merge was one sweep at 37.5% completeness - while two of the others correlated with each other at 0.92 and 0.58. Every sweep is now put on the axes of every other (ChooseReferenceSetting, all ordered pairs, which also serves the star relation that follows), and among the sweeps with a resolved ISa those on whose axes the most others reach MIN_REFERENCE_CC are the candidates; the existing rule (finest d_min within 1%, then ISa) picks among them. Where every sweep agrees with every other - the passing multi-sweep sets measured - the reference is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- rugnux/RugnuxJointSweeps.cpp | 60 ++++++++++++++++++++++----------- rugnux/RugnuxJointSweeps.h | 13 +++++-- tests/RugnuxJointSweepsTest.cpp | 17 ++++++++++ 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/rugnux/RugnuxJointSweeps.cpp b/rugnux/RugnuxJointSweeps.cpp index 56057fb1c..9f5872124 100644 --- a/rugnux/RugnuxJointSweeps.cpp +++ b/rugnux/RugnuxJointSweeps.cpp @@ -388,29 +388,53 @@ JointSweepRelation RelateJointSweeps(const std::vector &sweeps out.sweeps.resize(sweeps.size()); 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). 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. + // Every sweep put on the axes of every other (ChooseReferenceSetting): on[k][j] puts sweep k on sweep j's. + Logger logger("Rugnux"); + TimingMark axes_mark(logger, "joint sweeps: axes"); + const size_t n = sweeps.size(); + std::vector> on(n, std::vector(n)); + std::vector> rows; + for (size_t k = 0; k < n; ++k) + rows.push_back(std::async(std::launch::async, [&sweeps, &on, n, k] { + for (size_t j = 0; j < n; ++j) + if (j != k && !sweeps[k].merged.empty() && !sweeps[j].merged.empty()) + on[k][j] = ChooseReferenceSetting(sweeps[k].merged, sweeps[k].cell, sweeps[k].space_group, + sweeps[j].merged, sweeps[j].cell, sweeps[j].space_group); + })); + for (auto &row : rows) + row.get(); + + // The reference (RelateJointSweeps in the header): of the sweeps with a resolved ISa, those that the most + // others correlate with on their axes; of these the finest, within 1%, then the higher ISa. + std::vector partners(n, 0); + for (size_t k = 0; k < n; ++k) + for (size_t j = 0; j < n; ++j) + if (on[k][j].cc >= MIN_REFERENCE_CC) // false on NaN + partners[j]++; 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 auto isa = [&](size_t k) { return std::isfinite(sweeps[k].isa) ? sweeps[k].isa : 0.0; }; 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) + std::vector candidates; + for (size_t k = 0; k < n; ++k) if (accuracy_known(k) == any_known) - finest = std::min(finest, d_min(k)); + candidates.push_back(k); + const int most = partners[*std::max_element(candidates.begin(), candidates.end(), + [&](size_t a, size_t b) { return partners[a] < partners[b]; })]; + std::erase_if(candidates, [&](size_t k) { return partners[k] < most; }); + double finest = INFINITY; + for (const size_t k : candidates) + 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); 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); + return isa(k) > isa(than); }; - out.reference = 0; - for (size_t k = 1; k < sweeps.size(); ++k) + out.reference = static_cast(candidates.front()); + for (const size_t k : candidates) if (better(k, out.reference)) out.reference = static_cast(k); const JointMemberSweep &ref = sweeps[out.reference]; @@ -418,11 +442,9 @@ JointSweepRelation RelateJointSweeps(const std::vector &sweeps out.sweeps[out.reference].cc = out.sweeps[out.reference].identity_cc = 1.0; out.sweeps[out.reference].decision = "reference"; - // First the axes: each sweep on the reference's (ChooseReferenceSetting), then the twin laws of the - // lattice settled over all the pairs at once (ConsensusTwinLaws). - Logger logger("Rugnux"); - TimingMark axes_mark(logger, "joint sweeps: axes"); - for (size_t k = 0; k < sweeps.size(); ++k) { + // First the axes: each sweep on the reference's, then the twin laws of the lattice settled over all + // the pairs at once (ConsensusTwinLaws). + for (size_t k = 0; k < n; ++k) { if (static_cast(k) == out.reference) continue; JointSweepRelation::Sweep &s = out.sweeps[k]; @@ -431,9 +453,7 @@ JointSweepRelation RelateJointSweeps(const std::vector &sweeps s.why_left_out = "it merged nothing"; continue; } - const ReferenceSettingChoice choice = ChooseReferenceSetting(sweeps[k].merged, sweeps[k].cell, - sweeps[k].space_group, ref.merged, ref.cell, - ref.space_group); + const ReferenceSettingChoice &choice = on[k][out.reference]; s.cob = choice.cob; s.cc = choice.cc; s.identity_cc = choice.identity_cc; diff --git a/rugnux/RugnuxJointSweeps.h b/rugnux/RugnuxJointSweeps.h index 966a82d97..45164d653 100644 --- a/rugnux/RugnuxJointSweeps.h +++ b/rugnux/RugnuxJointSweeps.h @@ -84,14 +84,21 @@ std::vector MeasureHeldOutGains(const std::vector sweeps; + sweeps.push_back(MakeSweep(Truth(99), gemmi::Op::identity(), 100, 2, 10.0)); + sweeps.push_back(MakeSweep(CoarserThan(truth, 3.0), gemmi::Op::identity(), 100, 3, 10.0)); + sweeps.push_back(MakeSweep(CoarserThan(truth, 3.0), gemmi::Op::identity(), 100, 4, 20.0)); + REQUIRE(sweeps[0].d_min < 0.99 * sweeps[1].d_min); + + const auto relation = RelateJointSweeps(sweeps); + CHECK(relation.reference == 1); + CHECK(relation.sweeps[2].included); + CHECK_FALSE(relation.sweeps[0].included); +} + 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. From 98bee31bdd88adaf4c5017c9b9bd6e089d6cfd66 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 21:53:07 +0100 Subject: [PATCH 2/2] Joint sweeps: log how many other sweeps can be put on each sweep's axes Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- rugnux/RugnuxJointSweeps.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rugnux/RugnuxJointSweeps.cpp b/rugnux/RugnuxJointSweeps.cpp index 9f5872124..2bdba3f38 100644 --- a/rugnux/RugnuxJointSweeps.cpp +++ b/rugnux/RugnuxJointSweeps.cpp @@ -411,6 +411,8 @@ JointSweepRelation RelateJointSweeps(const std::vector &sweeps for (size_t j = 0; j < n; ++j) if (on[k][j].cc >= MIN_REFERENCE_CC) // false on NaN partners[j]++; + for (size_t k = 0; k < n; ++k) + logger.Info("Sweep {}: {} of the other sweeps can be put on its axes", k + 1, partners[k]); 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 auto isa = [&](size_t k) { return std::isfinite(sweeps[k].isa) ? sweeps[k].isa : 0.0; };