From 4936f9b408c042371131651ed4eaefedbf8820db Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Wed, 14 Oct 2026 11:32:51 +0200 Subject: [PATCH] RESULT-CHANGING (owner decision): stills scaling reference and tilt refinement only over the crystals the merge keeps The final stills merge drops every crystal whose cell is not close to the consensus cell (MergeOnTheFly::Mask), but the scaling reference (MergeAll) and the partiality post-refinement were made over all of them. On two 25k-frame XFEL runs of a tetragonal crystal those were 26-30% of the indexed frames and 60% of the integrated reflections: weak frames (median 8 indexed spots) indexed in a 2x/3x/4x supercell or an unrelated lattice, whose supercell-only reflections are pure noise and whose indices do not name the true reflections. They went into the reference every merged crystal was scaled against, and through the tilt refinement. MergeAll and StillsPartialityRefine::Run now take the reference cell and use only the crystals it accepts - the same test, with the same tolerances, as the merge. Measured (both runs, mx path): CC1/2 0.9599 -> 0.9598 and 0.9645 -> 0.9644, I/sigma +0.05 and +0.02, R_pim unchanged, space group and resolution unchanged; --mode scale against a deposited tetragonal lysozyme model CC 0.9452 -> 0.9477 and 0.9461 -> 0.9473. Scale+merge 2.1x faster on --mode scale (14.6 -> 6.9 s, loaded box). Co-Authored-By: Claude Opus 5.5 (1M context) --- image_analysis/scale_merge/Merge.cpp | 5 +++-- image_analysis/scale_merge/Merge.h | 5 ++++- .../scale_merge/StillsPartialityRefine.cpp | 13 ++++++++++--- image_analysis/scale_merge/StillsPartialityRefine.h | 5 ++++- rugnux/RugnuxScaleMerge.cpp | 5 +++-- rugnux/rugnux_cli.cpp | 4 ++-- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/image_analysis/scale_merge/Merge.cpp b/image_analysis/scale_merge/Merge.cpp index 2c3bf37f1..4f72271c7 100644 --- a/image_analysis/scale_merge/Merge.cpp +++ b/image_analysis/scale_merge/Merge.cpp @@ -469,9 +469,10 @@ std::vector MergeOnTheFly::ExportReflections() { std::vector MergeAll(const DiffractionExperiment &x, const std::vector &integration_outcome, - size_t nthreads) { + size_t nthreads, + const std::optional &reference_cell) { MergeOnTheFly merge(x); - merge.Threads(nthreads).AddImages(integration_outcome); + merge.Threads(nthreads).ReferenceCell(reference_cell).AddImages(integration_outcome); return merge.ExportIntensities(); } diff --git a/image_analysis/scale_merge/Merge.h b/image_analysis/scale_merge/Merge.h index 7cca243a3..cfb5cd615 100644 --- a/image_analysis/scale_merge/Merge.h +++ b/image_analysis/scale_merge/Merge.h @@ -378,9 +378,12 @@ public: std::vector ExportIntensities(); }; +// The merged intensities of the images whose cell is close to reference_cell (all of them without one): +// the reference a per-image scale is fitted against. std::vector MergeAll(const DiffractionExperiment &x, const std::vector &reflections, - size_t nthreads); + size_t nthreads, + const std::optional &reference_cell); // Pearson CC between one image's corrected intensities (I * image_scale_corr) and a reference set of // full intensities, over the reflections that would enter the merge (non-ice, within the resolution diff --git a/image_analysis/scale_merge/StillsPartialityRefine.cpp b/image_analysis/scale_merge/StillsPartialityRefine.cpp index 12265a510..60ee3f7dd 100644 --- a/image_analysis/scale_merge/StillsPartialityRefine.cpp +++ b/image_analysis/scale_merge/StillsPartialityRefine.cpp @@ -349,7 +349,14 @@ double StillsPartialityRefine::RefineOne(IntegrationOutcome &outcome, return tilt_deg; } -double StillsPartialityRefine::Run(std::vector &outcomes, size_t nthreads) const { +double StillsPartialityRefine::Run(std::vector &outcomes, size_t nthreads, + const std::optional &reference_cell) const { + const auto &indexing = experiment_.GetIndexingSettings(); + auto merged_crystal = [&](const IntegrationOutcome &o) { + return !reference_cell || o.latt.GetUnitCell().is_close(*reference_cell, + indexing.GetUnitCellDistTolerance(), + indexing.GetUnitCellAngleTolerance_deg()); + }; if (nthreads == 0) nthreads = AvailableCpus(); nthreads = std::max(1, nthreads); @@ -358,7 +365,7 @@ double StillsPartialityRefine::Run(std::vector &outcomes, si for (int outer = 0; outer < settings_.outer_iterations; ++outer) { // Reference full intensities from the current corrections. - const std::vector merged = MergeAll(experiment_, outcomes, nthreads); + const std::vector merged = MergeAll(experiment_, outcomes, nthreads, reference_cell); std::map reference; for (const auto &m: merged) reference[hkl_key_generator_(m)] = m.I; @@ -370,7 +377,7 @@ double StillsPartialityRefine::Run(std::vector &outcomes, si auto worker = [&]() { size_t i = next.fetch_add(1); while (i < outcomes.size()) { - const double t = RefineOne(outcomes[i], reference); + const double t = merged_crystal(outcomes[i]) ? RefineOne(outcomes[i], reference) : 0.0; if (t > 0.0) { double prev = tilt_sum.load(); while (!tilt_sum.compare_exchange_weak(prev, prev + t)) {} diff --git a/image_analysis/scale_merge/StillsPartialityRefine.h b/image_analysis/scale_merge/StillsPartialityRefine.h index fbce8c1dd..f74568b65 100644 --- a/image_analysis/scale_merge/StillsPartialityRefine.h +++ b/image_analysis/scale_merge/StillsPartialityRefine.h @@ -43,7 +43,10 @@ public: explicit StillsPartialityRefine(const DiffractionExperiment &x); // Refine all crystals in place. Returns the mean |dpsi| applied (degrees), for diagnostics. - double Run(std::vector &outcomes, size_t nthreads = 0) const; + // Only the crystals whose cell is close to reference_cell (all without one) are refined and make the + // reference: the others are not merged. + double Run(std::vector &outcomes, size_t nthreads, + const std::optional &reference_cell) const; private: const DiffractionExperiment experiment_; diff --git a/rugnux/RugnuxScaleMerge.cpp b/rugnux/RugnuxScaleMerge.cpp index e42731bfa..f390ebc3f 100644 --- a/rugnux/RugnuxScaleMerge.cpp +++ b/rugnux/RugnuxScaleMerge.cpp @@ -1251,7 +1251,8 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { { phase("Scaling images (" + label + ")"); TimingMark scale_mark(logger, "stills scaling images " + label); - auto merge_result = MergeAll(experiment_, indexer->GetIntegrationOutcome(), config_.nthreads); + auto merge_result = MergeAll(experiment_, indexer->GetIntegrationOutcome(), config_.nthreads, + result.consensus_cell); indexer->ScaleAllImages(merge_result); } // Physical partiality post-refinement (default on; --simple-stills disables): refine a per-crystal @@ -1261,7 +1262,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { phase("Partiality post-refine (" + label + ")"); TimingMark refine_mark(logger, "stills partiality post-refine " + label); StillsPartialityRefine refiner(experiment_); - const double mean_tilt = refiner.Run(indexer->GetIntegrationOutcome(), config_.nthreads); + const double mean_tilt = refiner.Run(indexer->GetIntegrationOutcome(), config_.nthreads, result.consensus_cell); logger.Info("Stills partiality post-refine: mean |dpsi| = {:.3f} deg", mean_tilt); // The refine fits its OWN per-image scale, with no floor, and adopts it unconditionally // when the image has no prior CC - which is exactly the state the guard above leaves. So diff --git a/rugnux/rugnux_cli.cpp b/rugnux/rugnux_cli.cpp index 5527130e9..6b29580ca 100644 --- a/rugnux/rugnux_cli.cpp +++ b/rugnux/rugnux_cli.cpp @@ -2103,13 +2103,13 @@ static int RunRugnux(int argc, char **argv) { // against a foreign dataset injects cross-dataset systematics and is a worse reference than // the data's own merge. The per-image scale G is the exact one-pass solution, so one pass // (iterating a self-rebuilt reference only re-fits the freshly-scaled noise on weak stills). - ScaleOnTheFly(experiment, MergeAll(experiment, reflections, nthreads)).Scale(reflections, nthreads); + ScaleOnTheFly(experiment, MergeAll(experiment, reflections, nthreads, experiment.GetUnitCell())).Scale(reflections, nthreads); // Physical partiality post-refinement (default on; --simple-stills disables): refine a per-crystal // orientation tilt against the merge and recompute each reflection's partiality + scale correction // (no re-integration), then merge with the improved corrections. if (experiment.GetScalingSettings().GetStillsPartialityRefine()) { StillsPartialityRefine refiner(experiment); - const double mean_tilt = refiner.Run(reflections, nthreads); + const double mean_tilt = refiner.Run(reflections, nthreads, experiment.GetUnitCell()); logger.Info("Stills partiality post-refine: mean |dpsi| = {:.3f} deg", mean_tilt); } MergeOnTheFly merge_engine(experiment);