SearchSpaceGroup: prepare a merge once for several single-operator correlations

OperatorCorrelation flattened, normalised (two serial sorts) and hash-indexed the whole merge for
every operator it was asked about. The metric sub-lattice ask scores each two-fold of the metric
against the same P1 merge, and the twin-law report each law against the same expanded merge, so
both now build an OperatorScorer once and ask it per operator; OperatorCorrelation itself is a
scorer used once. Same prepared merge, same pairing: the scores are the ones it gave.

On 8a1a (hexagonal metric over a 6-fold point group) the stretch between the all-observation
search and the adoption goes from 1.37 to 0.39 s. p.mtz and the twin-law lines byte-identical
(myob, thau, cytc, kdp, 8a1a). The two call sites in rugnux/RugnuxScaleMerge.cpp change by a line
each.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-07 21:31:29 +02:00
1 parent de9b2a7cd6
commit 661f812202
3 files changed
+44 -9

No files matched your search

@@ -2980,10 +2980,22 @@ std::string FinalistLedgerToText(const SearchSpaceGroupResult& result) {
return os.str();
}
std::optional<SpaceGroupOperatorScore> OperatorCorrelation(const std::vector<MergedReflection>& merged,
const gemmi::Mat33& hkl_matrix,
const SearchSpaceGroupOptions& opt) {
if (merged.empty())
struct OperatorScorer::Impl {
SearchSpaceGroupOptions opt;
std::optional<NormalisedMerge> nm; // none for an empty merge
};
OperatorScorer::OperatorScorer(const std::vector<MergedReflection>& merged, const SearchSpaceGroupOptions& opt)
: impl(std::make_unique<Impl>()) {
impl->opt = opt;
if (!merged.empty())
impl->nm = PrepareMerge(merged, opt);
}
OperatorScorer::~OperatorScorer() = default;
std::optional<SpaceGroupOperatorScore> OperatorScorer::operator()(const gemmi::Mat33& hkl_matrix) const {
if (!impl->nm)
return std::nullopt;
// gemmi applies an Op to Miller indices through the TRANSPOSE of its rot (Op::apply_to_hkl), and
@@ -2996,16 +3008,22 @@ std::optional<SpaceGroupOperatorScore> OperatorCorrelation(const std::vector<Mer
for (int j = 0; j < 3; ++j)
op.rot[i][j] = static_cast<int>(std::lround(hkl_matrix[j][i])) * gemmi::Op::DEN;
const NormalisedMerge nm = PrepareMerge(merged, opt);
const NormalisedMerge &nm = *impl->nm;
std::vector<uint32_t> visited(nm.n, 0);
uint32_t epoch = 0;
const SpaceGroupOperatorScore s = ScoreOperator(nm, opt, op, visited, epoch);
const SpaceGroupOperatorScore s = ScoreOperator(nm, impl->opt, op, visited, epoch);
// Too few pairs to judge - the same floor Stage A refuses to call an operator present on.
if (s.n_pairs < opt.min_pairs_per_operator || !std::isfinite(s.cc))
if (s.n_pairs < impl->opt.min_pairs_per_operator || !std::isfinite(s.cc))
return std::nullopt;
return s;
}
std::optional<SpaceGroupOperatorScore> OperatorCorrelation(const std::vector<MergedReflection>& merged,
const gemmi::Mat33& hkl_matrix,
const SearchSpaceGroupOptions& opt) {
return OperatorScorer(merged, opt)(hkl_matrix);
}
std::string SearchSpaceGroupResultToText(const SearchSpaceGroupResult& result,
size_t max_candidates_to_print) {
std::ostringstream os;
@@ -5,6 +5,7 @@
#include <array>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <vector>
@@ -906,6 +907,18 @@ std::optional<SpaceGroupOperatorScore> OperatorCorrelation(const std::vector<Mer
const gemmi::Mat33& hkl_matrix,
const SearchSpaceGroupOptions& opt = {});
// OperatorCorrelation for several operators of one merge: the merge is flattened, normalised and
// indexed once, at construction, instead of once per operator. Each call is OperatorCorrelation's.
class OperatorScorer {
public:
explicit OperatorScorer(const std::vector<MergedReflection>& merged, const SearchSpaceGroupOptions& opt = {});
~OperatorScorer();
std::optional<SpaceGroupOperatorScore> operator()(const gemmi::Mat33& hkl_matrix) const;
private:
struct Impl;
std::unique_ptr<Impl> impl;
};
std::string SearchSpaceGroupResultToText(
const SearchSpaceGroupResult& result,
size_t max_candidates_to_print = 20);
+6 -2
View File
@@ -1761,10 +1761,11 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) {
// random end leaves no scale to measure on (max_reference_over_random).
const bool contrast_known = std::isfinite(r_random) && std::isfinite(r_best)
&& r_best <= sg_opts.max_reference_over_random * r_random;
const OperatorScorer operator_correlation(sm.merged, sg_opts);
const auto keep_operator = [&](const gemmi::Mat33 &m_primitive) {
const gemmi::Mat33 m_indexed =
from_primitive.multiply(m_primitive).multiply(to_primitive);
const auto score = OperatorCorrelation(sm.merged, m_indexed, sg_opts);
const auto score = operator_correlation(m_indexed);
if (!score.has_value() || !score->present)
return false;
return !contrast_known
@@ -2853,6 +2854,7 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) {
}
return expanded;
};
std::optional<OperatorScorer> law_correlation;
std::string laws;
for (const auto &op : twin_ops) {
std::string t = op.triplet();
@@ -2870,7 +2872,9 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) {
m[j][i] = static_cast<double>(op.rot[i][j]) / gemmi::Op::DEN;
SearchSpaceGroupOptions law_opts;
law_opts.nthreads = static_cast<size_t>(std::max(1, config_.nthreads));
if (const auto s = OperatorCorrelation(expanded_merge(), m, law_opts); s.has_value())
if (!law_correlation)
law_correlation.emplace(expanded_merge(), law_opts);
if (const auto s = (*law_correlation)(m); s.has_value())
result.twin_law_evidence.push_back(fmt::format(
"{}: {} pairs, H {:.3f} (twin fraction {:.2f} or more), R {:.3f}, CC {:.3f}",
t, s->n_pairs, s->h_stat, std::max(0.0, 0.5 - s->h_stat), s->r_stat, s->cc));