From d2d208d603231f3178be6b0774da1df3044c7869 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sun, 22 Nov 2026 17:30:28 +0100 Subject: [PATCH] Distance check: pre-passes at pass 1's tilt, on pass 1's lattice, decided on the residual The distance check's pre-passes set the beam and the distance from the post-refinement but not the tilt. The fit's beam is a PONI at the tilt pass 1's indexing settled on; without it the direct beam of a pre-pass lands off by the distance times the tilt (15 px for half a degree at 160 mm). The pre-passes then indexed another lattice, on a few of the validation frames, and their asks and residuals were that lattice's: a line through them put a fixed point where no well-indexed pass asked to go, and a P21 crystal was merged as P21212. - Every pre-pass now starts at pass 1's refined tilt, whether or not pass 1 moved the distance. - A pre-pass that did not index pass 1's lattice, orientation included (each primitive basis whole-numbered in the other's, to the 0.1 the reindexing uses), is not used and ends the search; a line in the log says so. Cell edges alone cannot tell: the wrong lattice had edges within 2 % of the right one's. - The candidates are decided on the realised held-out residual alone. The share of validation spots on the lattice moves with the validation scheme a pass indexes on, and pre-passes do not always index on pass 1's: a sparse cubic crystal read 29 % against 42 % at every distance and adopted a 3 % move its residual refused. On the affected sets against the combined branch: the P21 crystal passes again (R-free 0.256 -> 0.212, ISa 5.3 -> 9.2), a four-sweep 2theta-arm CCD set merges in I222 again instead of C2 (cell deviation 34 % -> 0.4 %), a small-molecule set on a tilted detector improves (ISa 15 -> 24); the small-molecule sets and the strongly tilted protein sets are otherwise unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQpv3w9JdZswRqKUW2H --- rugnux/RugnuxPasses.cpp | 84 ++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 22 deletions(-) diff --git a/rugnux/RugnuxPasses.cpp b/rugnux/RugnuxPasses.cpp index 42093d935..1af9658bc 100644 --- a/rugnux/RugnuxPasses.cpp +++ b/rugnux/RugnuxPasses.cpp @@ -133,6 +133,30 @@ namespace { const auto same_length = [](double a, double b) { return std::abs(a - b) <= 0.05 * (a + b) / 2; }; return same_length(c1.a, c2.a) && same_length(c1.b, c2.b) && same_length(c1.c, c2.c); } + + // Whether two passes indexed one lattice, orientation included: each one's primitive basis vectors + // are whole-number combinations of the other's, to the 0.1 ScaleMergeAndSymmetry reindexes to. The + // cell edges alone do not say it: a pre-pass at a beam 15 px off was measured settling on a cell of + // edges within 2 % of the right one's, in another orientation, with 8 of 60 validation frames. + bool IndexesSameLattice(const ProcessResult &a, const ProcessResult &b) { + const auto primitive = [](const ProcessResult &r) { + const auto &type = r.end_message.rotation_lattice_type; + return r.end_message.rotation_lattice->ToPrimitive(type ? type->centering : 'P'); + }; + const auto integral = [](const CrystalLattice &x, const CrystalLattice &y) { + const Coord v[3] = {x.Vec0(), x.Vec1(), x.Vec2()}; + const Coord rs[3] = {y.Astar(), y.Bstar(), y.Cstar()}; + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + if (!(std::fabs(v[i] * rs[j] - std::round(v[i] * rs[j])) < 0.1)) + return false; + return true; + }; + if (!a.end_message.rotation_lattice || !b.end_message.rotation_lattice) + return false; + const CrystalLattice pa = primitive(a), pb = primitive(b); + return integral(pa, pb) && integral(pb, pa); + } } // Pass 2 is normally the better answer, which is why it is the canonical output - but it is not @@ -793,9 +817,12 @@ std::optional> Rugnux::FixedPointDistance(RugnuxObserver *o } const double direct_beam[2] = {held.beam_x_free_px + per_mm[0] * held.distance_free_mm, held.beam_y_free_px + per_mm[1] * held.distance_free_mm}; - // The tilt pass 1's indexing settled on, where pass 1 committed a geometry: it travels with it. - const float tilt[2] = {moved ? (*prepass_detector_geometry_)[3] : experiment_.GetPoniRot1_rad(), - moved ? (*prepass_detector_geometry_)[4] : experiment_.GetPoniRot2_rad()}; + // The tilt pass 1's indexing settled on: the fit's beam is a PONI at that tilt, so it travels with it. + // Without it the direct beam of every pre-pass is off by the distance times the tilt (measured: 15 px + // for half a degree at 160 mm), and the pre-passes index another lattice. + const auto &end = pass1.end_message; + const float tilt[2] = {end.refined_poni_rot1.value_or(experiment_.GetPoniRot1_rad()), + end.refined_poni_rot2.value_or(experiment_.GetPoniRot2_rad())}; const auto geometry_at = [&](double d) { return std::array{static_cast(direct_beam[0] - per_mm[0] * d), static_cast(direct_beam[1] - per_mm[1] * d), static_cast(d), @@ -819,7 +846,8 @@ std::optional> Rugnux::FixedPointDistance(RugnuxObserver *o if (bragg_adaptive_) probe.experiment_.ImportBraggIntegrationSettings(*bragg_adaptive_); const auto g = geometry_at(d); - probe.experiment_.BeamX_pxl(g[0]).BeamY_pxl(g[1]).DetectorDistance_mm(g[2]); + probe.experiment_.BeamX_pxl(g[0]).BeamY_pxl(g[1]).DetectorDistance_mm(g[2]).PoniRot1_rad(g[3]) + .PoniRot2_rad(g[4]); probe.prepass_mosaicity_.clear(); probe.prepass_detector_geometry_.reset(); probe.prepass_result_.reset(); @@ -841,25 +869,30 @@ std::optional> Rugnux::FixedPointDistance(RugnuxObserver *o : r.post_refine->distance_after_mm; }; - // What a distance integrated realises: its held-out residual and its validation evidence. + // What a distance integrated realises: its held-out residual. struct Realised { double distance; std::optional fit; - ValidationSpotEvidence evidence; }; - std::vector realised{{d0, held, pass1.validation_evidence}}; + std::vector realised{{d0, held}}; - // Distances along the free fit's move, one move apart, until the move asked for changes sign. + // Distances along the free fit's move, one move apart, until the move asked for changes sign. A pre-pass + // that did not index pass 1's lattice is not used, and ends the search: its ask and its residual are + // another lattice's, and no residual of it can be compared with pass 1's. std::vector> points{{d0, held.distance_free_mm}}; std::string text = fmt::format("{:.2f} -> {:.2f}", d0, held.distance_free_mm); for (int k = 1; k <= 4 && !cancelled_; ++k) { const double d = d0 + k * move; const ProcessResult r = prepass_at(d); const double a = asked(r); - text += fmt::format(", {:.2f} -> {:.2f}", d, a); - if (!std::isfinite(a)) + const bool same_lattice = IndexesSameLattice(pass1, r); + text += fmt::format(", {:.2f} -> {:.2f}{}", d, a, same_lattice ? "" : " (another lattice)"); + if (!same_lattice) + logger.Info("Distance check: the pre-pass at {:.3f} mm indexed another lattice than pass 1's - not used", + d); + if (!std::isfinite(a) || !same_lattice) break; - realised.push_back({d, r.post_refine, r.validation_evidence}); + realised.push_back({d, r.post_refine}); points.emplace_back(d, a); if ((a - d) * move <= 0.0) break; @@ -892,26 +925,33 @@ std::optional> Rugnux::FixedPointDistance(RugnuxObserver *o // tried), that pass is what it realises. const auto tried = std::find_if(realised.begin(), realised.end(), [&](const Realised &r) { return r.distance == d_star; }); - const Realised star = tried != realised.end() ? *tried : [&] { - const ProcessResult r = prepass_at(d_star); - return Realised{d_star, r.post_refine, r.validation_evidence}; - }(); + std::optional star; + if (tried != realised.end()) { + star = *tried; + } else if (const ProcessResult r = prepass_at(d_star); IndexesSameLattice(pass1, r)) { + star = Realised{d_star, r.post_refine}; + } else { + logger.Info("Distance check: the pre-pass at {:.3f} mm indexed another lattice than pass 1's - not used", + d_star); + } // The fixed point is where the ask vanishes, but the ask follows the distance it was integrated at (the // slope above), so a small bias in it lands in the fixed point several times over: on a detector on a // 2theta arm the ask still wanted 0.14 mm at the distance a reference program refines, and the fixed // point overshot it by three times that. What the data realise is the direct evidence. So the run takes // the shortest move the realised residual pays for: every distance integrated, the fixed point among // them, is a candidate in order of its distance from the header, each kept only where it realises better - // than the best nearer the header - by the test a round of the geometry walk has to pass. Against the + // than the best nearer the header, by more than the noise of the two (HeldOutResidualFell). Against the // header alone, every distance past the first step won, and the run took the far end of a plateau the - // residual is flat on. + // residual is flat on. The share of validation spots on the lattice is not asked: a pre-pass can index + // on another validation scheme than pass 1 did, and the share then moves with the scheme - measured, a + // cubic crystal of a few reflections read 29 % on pass 1's and 42 % on the pre-passes' at every + // distance, which adopted a move its residual refused. const auto beats = [](const Realised &best, const Realised &c) { - return (best.fit && c.fit && HeldOutResidualFell(*best.fit, *c.fit)) - || ValidationEvidencePrefers(best.evidence, c.evidence); + return best.fit && c.fit && HeldOutResidualFell(*best.fit, *c.fit); }; std::vector candidates = realised; - if (tried == realised.end()) - candidates.push_back(star); + if (star && tried == realised.end()) + candidates.push_back(*star); std::stable_sort(candidates.begin(), candidates.end(), [&](const Realised &a, const Realised &b) { return std::fabs(a.distance - d0) < std::fabs(b.distance - d0); }); @@ -925,7 +965,7 @@ std::optional> Rugnux::FixedPointDistance(RugnuxObserver *o residuals += fmt::format("{:.3f} mm {:.3e}, ", r.distance, r.fit ? r.fit->held_out_before : NAN); logger.Info("Distance check: integrated at -> asked for (mm): {}; fixed point {:.3f} mm (asked minus integrated " "falls {:.2f} mm per mm); realised held-out residual by distance: {}{:.3f} mm {:.3e} - {}", text, - d_star, -slope, residuals, d_star, star.fit ? star.fit->held_out_before : NAN, + d_star, -slope, residuals, d_star, star && star->fit ? star->fit->held_out_before : NAN, adopt ? fmt::format("{:.3f} mm adopted, the shortest move the realised residual pays for", best.distance) : std::string("none realises better than the header, the header stands"));