diff --git a/image_analysis/scale_merge/SearchSpaceGroup.cpp b/image_analysis/scale_merge/SearchSpaceGroup.cpp index 12d5872c0..889e8f635 100644 --- a/image_analysis/scale_merge/SearchSpaceGroup.cpp +++ b/image_analysis/scale_merge/SearchSpaceGroup.cpp @@ -2980,10 +2980,22 @@ std::string FinalistLedgerToText(const SearchSpaceGroupResult& result) { return os.str(); } -std::optional OperatorCorrelation(const std::vector& merged, - const gemmi::Mat33& hkl_matrix, - const SearchSpaceGroupOptions& opt) { - if (merged.empty()) +struct OperatorScorer::Impl { + SearchSpaceGroupOptions opt; + std::optional nm; // none for an empty merge +}; + +OperatorScorer::OperatorScorer(const std::vector& merged, const SearchSpaceGroupOptions& opt) + : impl(std::make_unique()) { + impl->opt = opt; + if (!merged.empty()) + impl->nm = PrepareMerge(merged, opt); +} + +OperatorScorer::~OperatorScorer() = default; + +std::optional 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 OperatorCorrelation(const std::vector(std::lround(hkl_matrix[j][i])) * gemmi::Op::DEN; - const NormalisedMerge nm = PrepareMerge(merged, opt); + const NormalisedMerge &nm = *impl->nm; std::vector 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 OperatorCorrelation(const std::vector& 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; diff --git a/image_analysis/scale_merge/SearchSpaceGroup.h b/image_analysis/scale_merge/SearchSpaceGroup.h index 477a865e2..b503cff73 100644 --- a/image_analysis/scale_merge/SearchSpaceGroup.h +++ b/image_analysis/scale_merge/SearchSpaceGroup.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -906,6 +907,18 @@ std::optional OperatorCorrelation(const std::vector& merged, const SearchSpaceGroupOptions& opt = {}); + ~OperatorScorer(); + std::optional operator()(const gemmi::Mat33& hkl_matrix) const; +private: + struct Impl; + std::unique_ptr impl; +}; + std::string SearchSpaceGroupResultToText( const SearchSpaceGroupResult& result, size_t max_candidates_to_print = 20); diff --git a/rugnux/RugnuxScaleMerge.cpp b/rugnux/RugnuxScaleMerge.cpp index 504e20cd0..3d0c3dc7b 100644 --- a/rugnux/RugnuxScaleMerge.cpp +++ b/rugnux/RugnuxScaleMerge.cpp @@ -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 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(op.rot[i][j]) / gemmi::Op::DEN; SearchSpaceGroupOptions law_opts; law_opts.nthreads = static_cast(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));