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) <noreply@anthropic.com>
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-14 11:32:51 +02:00
1 parent 34d5bb5631
commit 4936f9b408
6 files changed
+26 -11

No files matched your search

+3 -2
View File
@@ -469,9 +469,10 @@ std::vector<MergedReflection> MergeOnTheFly::ExportReflections() {
std::vector<MergedReflection> MergeAll(const DiffractionExperiment &x,
const std::vector<IntegrationOutcome> &integration_outcome,
size_t nthreads) {
size_t nthreads,
const std::optional<UnitCell> &reference_cell) {
MergeOnTheFly merge(x);
merge.Threads(nthreads).AddImages(integration_outcome);
merge.Threads(nthreads).ReferenceCell(reference_cell).AddImages(integration_outcome);
return merge.ExportIntensities();
}
+4 -1
View File
@@ -378,9 +378,12 @@ public:
std::vector<MergedReflection> 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<MergedReflection> MergeAll(const DiffractionExperiment &x,
const std::vector<IntegrationOutcome> &reflections,
size_t nthreads);
size_t nthreads,
const std::optional<UnitCell> &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
@@ -349,7 +349,14 @@ double StillsPartialityRefine::RefineOne(IntegrationOutcome &outcome,
return tilt_deg;
}
double StillsPartialityRefine::Run(std::vector<IntegrationOutcome> &outcomes, size_t nthreads) const {
double StillsPartialityRefine::Run(std::vector<IntegrationOutcome> &outcomes, size_t nthreads,
const std::optional<UnitCell> &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<size_t>(1, nthreads);
@@ -358,7 +365,7 @@ double StillsPartialityRefine::Run(std::vector<IntegrationOutcome> &outcomes, si
for (int outer = 0; outer < settings_.outer_iterations; ++outer) {
// Reference full intensities from the current corrections.
const std::vector<MergedReflection> merged = MergeAll(experiment_, outcomes, nthreads);
const std::vector<MergedReflection> merged = MergeAll(experiment_, outcomes, nthreads, reference_cell);
std::map<HKLKey, double> reference;
for (const auto &m: merged)
reference[hkl_key_generator_(m)] = m.I;
@@ -370,7 +377,7 @@ double StillsPartialityRefine::Run(std::vector<IntegrationOutcome> &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)) {}
@@ -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<IntegrationOutcome> &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<IntegrationOutcome> &outcomes, size_t nthreads,
const std::optional<UnitCell> &reference_cell) const;
private:
const DiffractionExperiment experiment_;
+3 -2
View File
@@ -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
+2 -2
View File
@@ -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);