From 6cfebfd2d079c151cbab4931ebaa1d928e9e4beb Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Mon, 23 Nov 2026 18:49:50 +0100 Subject: [PATCH] Stills merge: a reflection's resolution is the reference cell's, not the image's Stills each carry their own cell (and an XFEL pulse its own wavelength), merged while it is within the indexing tolerance of the reference cell (5 % by default). The merge cut each observation at the high-resolution limit by the d of the image that measured it, and gave the merged reflection the d of its first observation, while the completeness counted the possible reflections in the reference cell. A reflection just past the limit was then merged from whichever image's cell put it inside, and the outermost shell counted more reflections than it could hold: measured, 204 % and 147 % in the last shell of a four-run --mode scale merge at multiplicities in the thousands, with a CC1/2 in those shells that meant nothing. The inner shells read 99-101 % from the same shuffling across shell boundaries. The merge now takes a reflection's d in the reference cell, where there is one, for the resolution cut, the d it reports and the shell it is counted in - the cell the possible reflections are counted in - so a reflection has one d whichever image measured it and no shell can exceed complete. Without a reference cell nothing changes. New test: images on cells 2 % either side of the reference, every reflection to past the limit; it fails before (reflections past the limit, shells over 100 %) and passes now. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQrPqZ52Tp6rzzZGxf3 (cherry picked from commit 5172955cb4504e7d374010b57d8810842d7f038a) --- image_analysis/scale_merge/Merge.cpp | 34 +++++++++++++--- image_analysis/scale_merge/Merge.h | 10 +++++ tests/MergeScaleTest.cpp | 61 ++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 6 deletions(-) diff --git a/image_analysis/scale_merge/Merge.cpp b/image_analysis/scale_merge/Merge.cpp index cf69a1229..40e37e45c 100644 --- a/image_analysis/scale_merge/Merge.cpp +++ b/image_analysis/scale_merge/Merge.cpp @@ -83,9 +83,31 @@ MergeOnTheFly::MergeOnTheFly(const DiffractionExperiment &x) MergeOnTheFly &MergeOnTheFly::ReferenceCell(const std::optional &cell) { reference_cell = cell; + reference_reciprocal.reset(); + if (cell) { + const CrystalLattice lattice(*cell); + reference_reciprocal = {lattice.Astar(), lattice.Bstar(), lattice.Cstar()}; + } return *this; } +float MergeOnTheFly::Resolution(const Reflection &r) const { + if (!reference_reciprocal) + return r.d; + const auto &[astar, bstar, cstar] = *reference_reciprocal; + const float q = (static_cast(r.h) * astar + static_cast(r.k) * bstar + + static_cast(r.l) * cstar).Length(); + return q > 0.0f ? 1.0f / q : NAN; +} + +bool MergeOnTheFly::Accept(const Reflection &r, std::optional d_min_limit, + std::optional d_max_limit) const { + if (!AcceptReflection(r, std::nullopt, std::nullopt)) + return false; + const float d = Resolution(r); + return std::isfinite(d) && !(d_min_limit && d < *d_min_limit) && !(d_max_limit && d > *d_max_limit); +} + size_t MergeOnTheFly::Contributions(const IntegrationOutcome &outcome, std::vector &out) const { size_t rejected = 0; for (const auto &r: outcome.reflections) { @@ -94,7 +116,7 @@ size_t MergeOnTheFly::Contributions(const IntegrationOutcome &outcome, std::vect if (r.image_scale_corr <= 0.0 || !std::isfinite(r.image_scale_corr)) continue; - if (!AcceptReflection(r, high_resolution_limit, std::nullopt)) + if (!Accept(r, high_resolution_limit, std::nullopt)) continue; if (exclude_ice_rings && r.on_ice_ring) continue; @@ -130,7 +152,7 @@ size_t MergeOnTheFly::Contributions(const IntegrationOutcome &outcome, std::vect .l = hkl.plus ? hkl.l : -hkl.l, .w = w, .wI = w * I_corr, - .d = r.d, + .d = Resolution(r), .on_ice_ring = r.on_ice_ring, .exists = r.partiality >= existence_min_partiality, .far_tail = r.partiality < FAR_TAIL_PARTIALITY @@ -151,7 +173,7 @@ std::vector> MergeOnTheFly::Verdicts(const std:: const bool scaled = r.image_scale_corr > 0.0 && std::isfinite(r.image_scale_corr); if (!scaled) v.flags |= OBS_NO_SCALE; - if (!AcceptReflection(r, high_resolution_limit, std::nullopt)) + if (!Accept(r, high_resolution_limit, std::nullopt)) v.flags |= OBS_RESOLUTION; if (exclude_ice_rings && r.on_ice_ring) v.flags |= OBS_ICE_EXCLUDED; @@ -328,7 +350,7 @@ void MergeOnTheFly::RefineErrorModel(const std::vector &outc continue; if (r.image_scale_corr <= 0.0 || !std::isfinite(r.image_scale_corr)) continue; - if (!AcceptReflection(r, high_resolution_limit, std::nullopt)) + if (!Accept(r, high_resolution_limit, std::nullopt)) continue; if (exclude_ice_rings && r.on_ice_ring) continue; @@ -899,7 +921,7 @@ MergeStatistics MergeOnTheFly::MergeStats(const std::vector &m continue; if (r.image_scale_corr <= 0.0 || !std::isfinite(r.image_scale_corr)) continue; - if (!AcceptReflection(r, d_min_limit_A, d_max_limit_A)) + if (!Accept(r, d_min_limit_A, d_max_limit_A)) continue; if (BelowMergeFloor(r.partiality)) continue; @@ -908,7 +930,7 @@ MergeStatistics MergeOnTheFly::MergeStats(const std::vector &m if (w == 0.0) continue; - const auto shell = shells.GetShell(r.d); + const auto shell = shells.GetShell(Resolution(r)); if (!shell.has_value()) continue; const int s = *shell; diff --git a/image_analysis/scale_merge/Merge.h b/image_analysis/scale_merge/Merge.h index d81ad5e2d..aa9a7cdf0 100644 --- a/image_analysis/scale_merge/Merge.h +++ b/image_analysis/scale_merge/Merge.h @@ -348,6 +348,7 @@ class MergeOnTheFly { IndexingSettings indexing_settings; std::optional reference_cell; + std::optional> reference_reciprocal; // a*, b*, c* of reference_cell std::optional high_resolution_limit; std::optional image_cc_limit; // Apply image_cc_limit in Mask(). One flag for the whole engine, not a per-call argument, so the @@ -367,6 +368,15 @@ class MergeOnTheFly { static constexpr double EXISTENCE_MIN_PARTIALITY = 0.2; double min_partiality = 0.02; double existence_min_partiality = 0.02; + // A reflection's resolution in the merge: in the reference cell where there is one, not in the cell of the + // image that measured it. Stills each carry their own cell (and an XFEL pulse its own wavelength), so the + // same reflection reads a slightly different d on every image; cut and binned by those, a reflection just + // past the limit is merged from whichever image puts it inside, and the outermost shell counts more of + // them than the reference cell, which the completeness counts the possible reflections in, allows. + [[nodiscard]] float Resolution(const Reflection &r) const; + // AcceptReflection, with the limits applied to Resolution(r). + [[nodiscard]] bool Accept(const Reflection &r, std::optional d_min_limit, + std::optional d_max_limit) const; [[nodiscard]] bool BelowMergeFloor(float partiality) const { return error_model_active ? partiality <= 0.0f : partiality < min_partiality; } diff --git a/tests/MergeScaleTest.cpp b/tests/MergeScaleTest.cpp index 77a05dd2d..0d3696446 100644 --- a/tests/MergeScaleTest.cpp +++ b/tests/MergeScaleTest.cpp @@ -1679,3 +1679,64 @@ TEST_CASE("MergeOnTheFly_StillsAnomalousStatistics") { CHECK(stats.overall.abs_diff_over_sigma_anomalous > 5.0); CHECK(stats.overall.anomalous_measurability > 0.9); } + +// Stills each carry their own cell, merged while it is within the tolerance of the reference cell. Cut and +// binned by those cells, a reflection just past the limit is merged from the image whose cell puts it inside, +// and the outermost shell counted more reflections than the reference cell - the one the possible +// reflections are counted in - allows (measured: 204 % in the last shell of a four-run merge). Cut and +// binned in the reference cell, every shell is at most complete and every reflection has one d. +TEST_CASE("MergeStats_StillsOnCellsAroundTheReferenceStayAtMostComplete") { + const auto &sg = TestSpaceGroup(); + const UnitCell reference{40.0f, 45.0f, 50.0f, 90.0f, 100.0f, 90.0f}; + constexpr double LIMIT = 2.0; + + DiffractionExperiment x; + x.SetSpaceGroup(sg); + ScalingSettings s = x.GetScalingSettings(); + x.ImportScalingSettings(s.HighResolutionLimit_A(LIMIT)); + + // Every reflection to 1.9 A on its image's own cell, on images 2 % larger and 2 % smaller than the + // reference. + const auto image = [&sg](float scale) { + const UnitCell own{40.0f * scale, 45.0f * scale, 50.0f * scale, 90.0f, 100.0f, 90.0f}; + const gemmi::UnitCell gemmi_cell = own; + IntegrationOutcome o; + o.latt = CrystalLattice(own); + for (const auto &hkl: gemmi::make_miller_vector(gemmi_cell, &sg, 1.9, 50.0, true)) { + Reflection r{}; + r.h = hkl[0]; + r.k = hkl[1]; + r.l = hkl[2]; + r.d = static_cast(gemmi_cell.calculate_d(hkl)); + r.I = 100.0f; + r.sigma = 10.0f; + r.prescaling_corr = 1.0f; + r.partiality = 1.0f; + r.image_scale_corr = 1.0f; + o.reflections.push_back(r); + } + return o; + }; + std::vector outcomes; + for (int i = 0; i < 4; ++i) + outcomes.push_back(image(i % 2 ? 1.02f : 0.98f)); + + MergeOnTheFly merge(x); + merge.ReferenceCell(reference); + merge.AddImages(outcomes); + const auto merged = merge.ExportIntensities(); + REQUIRE(!merged.empty()); + + const gemmi::UnitCell gemmi_reference = reference; + for (const auto &m: merged) { + const double d = gemmi_reference.calculate_d({m.h, m.k, m.l}); + CHECK(m.d == Catch::Approx(d).epsilon(1e-4)); + CHECK(m.d >= LIMIT); + } + + const auto stats = merge.MergeStats(merged, outcomes); + CHECK(Completeness(stats.overall) <= 100.0); + CHECK(Completeness(stats.overall) > 99.0); + for (const auto &sh: stats.shells) + CHECK(Completeness(sh) <= 100.0); +}