diff --git a/image_analysis/scale_merge/SearchSpaceGroup.cpp b/image_analysis/scale_merge/SearchSpaceGroup.cpp index 24d4c67c4..a724c0b5e 100644 --- a/image_analysis/scale_merge/SearchSpaceGroup.cpp +++ b/image_analysis/scale_merge/SearchSpaceGroup.cpp @@ -798,6 +798,12 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array key_to_index; + // The weak parity class (SearchSpaceGroupResult::weak_parity_class): membership, empty when the + // merge has none, and E^2 normalised over that class alone, for the operator correlation. + std::array weak_parity{}; + double weak_ratio = std::numeric_limits::quiet_NaN(); + std::vector weak; + std::vector Eweak; }; NormalisedMerge PrepareMerge(const std::vector& merged, @@ -928,6 +934,56 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array Ecc = shell_normalised(pass_cc); + // The weak parity class. A half-integer pseudo-translation t makes the two halves of the cell + // nearly alike, and the reflections with 2t.h odd then carry only what differs between them - + // weak, and the one part of the data in which a symmetry of the average of the two halves is not + // obeyed (measured on a monoclinic crystal indexed on a doubled pseudo-orthorhombic cell: its + // true two-fold reads CC 0.18 on that class, the two-folds only the average obeys 0.02 and 0.03, + // ~29 standard errors apart, while all three read 0.68-0.80 on the strong class, which holds 79% + // of the intensity and so hides the difference in every pooled statistic). The class is the + // parity with the lowest mean E^2 against the rest, taken when that is under half + // (PSEUDO_TRANSLATION_MAX_RATIO, the bound the axial-row correction uses). A class extinct by a + // centring the cell does not name is found the same way, and refuses nothing: every operator reads + // noise on it, the best one too. The reflections the cell's own centring extinguishes are left out: + // a merge can hold them (a doubled cell is integrated on its primitive lattice), and they are no + // class of the crystal. + gemmi::GroupOps lattice; + lattice.sym_ops = {gemmi::Op::identity()}; + lattice.cen_ops = gemmi::centring_vectors(opt.lattice_centring); + std::vector in_lattice(n, 0); + for (size_t i = 0; i < n; ++i) + in_lattice[i] = pass_absence[i] && !CenteringAbsent(lattice, {{H[i], K[i], L[i]}}); + std::array weak_parity{}; + double weak_ratio = std::numeric_limits::quiet_NaN(); + std::vector weak; + std::vector Eweak; + for (int c = 1; c < 8; ++c) { + const std::array p{c & 1, (c >> 1) & 1, (c >> 2) & 1}; + double sum_odd = 0.0, sum_even = 0.0; + size_t n_odd = 0, n_even = 0; + for (size_t i = 0; i < n; ++i) { + if (!in_lattice[i]) + continue; + const double e = std::max(0.0, Esq[i]); + if (std::abs(H[i] * p[0] + K[i] * p[1] + L[i] * p[2]) % 2) { sum_odd += e; ++n_odd; } + else { sum_even += e; ++n_even; } + } + if (n_odd == 0 || n_even == 0 || !(sum_even > 0.0)) + continue; + const double ratio = (sum_odd / n_odd) / (sum_even / n_even); + if (ratio < PSEUDO_TRANSLATION_MAX_RATIO && !(ratio >= weak_ratio)) { + weak_ratio = ratio; + weak_parity = p; + } + } + if (std::isfinite(weak_ratio)) { + weak.assign(n, 0); + for (size_t i = 0; i < n; ++i) + weak[i] = in_lattice[i] + && std::abs(H[i] * weak_parity[0] + K[i] * weak_parity[1] + L[i] * weak_parity[2]) % 2; + Eweak = shell_normalised(weak); + } + std::unordered_map key_to_index; key_to_index.reserve(n * 2); for (size_t i = 0; i < n; ++i) @@ -940,7 +996,9 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array& visited, uint32_t& epoch) { + // The reflection op maps i onto, or -1 where that is no other reflection of the merge. + const auto mate = [&](size_t i) { + // A pseudo-merohedral law of a centred lattice has half-integer entries in the lattice's own + // basis (OperatorScorer), so it maps an index outside the centring onto non-integers; such an + // image is no reflection, and apply_to_hkl would silently truncate it onto one. + const auto scaled = op.apply_to_hkl_without_division(gemmi::Op::Miller{{m.H[i], m.K[i], m.L[i]}}); + if (scaled[0] % gemmi::Op::DEN != 0 || scaled[1] % gemmi::Op::DEN != 0 || scaled[2] % gemmi::Op::DEN != 0) + return -1; + const auto m2 = gemmi::Op::divide_hkl_by_DEN(scaled); + const HKLKey k2 = Canonicalize(m2[0], m2[1], m2[2], opt.merge_friedel); + if (k2 == m.key[i]) + return -1; // reflection lies on this rotation axis + const auto it = m.key_to_index.find(k2); + return it == m.key_to_index.end() ? -1 : it->second; + }; ++epoch; std::vector x, y; // raw merged I of each pair, for the H statistic std::vector ex, ey; // the same pairs as E^2, for the correlation @@ -957,21 +1030,8 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::arraysecond; - if (!m.pass_cc[j]) + const int j = mate(i); + if (j < 0 || !m.pass_cc[j]) continue; x.push_back(m.I[i]); y.push_back(m.I[j]); @@ -983,10 +1043,29 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array wx, wy; + ++epoch; + for (size_t i = 0; i < m.n && !m.weak.empty(); ++i) { + if (!m.weak[i] || visited[i] == epoch) + continue; + const int j = mate(i); + if (j < 0 || !m.weak[j]) + continue; + wx.push_back(m.Eweak[i]); + wy.push_back(m.Eweak[j]); + visited[i] = epoch; + visited[j] = epoch; + } SpaceGroupOperatorScore s; s.op_triplet_hkl = op.as_hkl().triplet('h'); s.n_pairs = static_cast(x.size()); s.cc = PearsonCC(ex, ey); + if (!m.weak.empty()) { + s.weak_pairs = static_cast(wx.size()); + s.weak_cc = PearsonCC(wx, wy); + } for (int sh = 0; sh < SpaceGroupOperatorScore::OPERATOR_SHELLS; ++sh) { s.shell_pairs[sh] = static_cast(shell_ex[sh].size()); s.shell_cc[sh] = s.shell_pairs[sh] >= opt.min_pairs_per_operator @@ -1499,6 +1578,7 @@ SearchSpaceGroupResult SearchSpaceGroup( std::string why; }; int refused_order = 0; bool refused_twin_gate = false; + bool refused_weak_class = false; double refused_fraction = std::numeric_limits::quiet_NaN(); std::string refused_pg_hm, refused_why; const gemmi::SpaceGroup *refused_pg_rep = nullptr; @@ -1608,6 +1688,16 @@ SearchSpaceGroupResult SearchSpaceGroup( global_best_r = std::min(global_best_r, s.r_stat); ++n_confirmed_ops; } + // The same reference on the weak parity class (see NormalisedMerge::weak): the confirmed operator + // that agrees best there. A genuine operator relates equal intensities in that class as in any other, + // so it is what every operator of a candidate is held to on it (the weak-class gate below). + const SpaceGroupOperatorScore* weak_best = nullptr; + for (const auto& [rk, s] : op_cache) + if (s.present && s.weak_pairs >= opt.min_pairs_for_h && std::isfinite(s.weak_cc) + && (!weak_best || s.weak_cc > weak_best->weak_cc)) + weak_best = &s; + result.weak_parity_class = nm.weak_parity; + result.weak_class_ratio = nm.weak_ratio; // Choose the largest point group that is both operator-confirmed AND self-consistent (its merge // chi^2 is not inflated past the miscalibration-widened bound below; ties -> the smaller worst H). @@ -2101,18 +2191,66 @@ SearchSpaceGroupResult SearchSpaceGroup( if (zone_refused) consistent = false; + // Weak-class test, where the merge has a weak parity class (a half-integer pseudo-translation, see + // NormalisedMerge::weak). Every gate above reads statistics pooled over all reflections, and on such + // a crystal the strong class carries most of the intensity: there the two halves of the cell are + // averaged, and a symmetry of that average - non-crystallographic, obeyed by both halves' content + // but not by how they differ - agrees nearly as well as a real operator. The weak class is how they + // differ, so a real operator agrees there too and a symmetry of the average does not. Each of the + // candidate's operators is held, on that class alone, to the confirmed operator that agrees best + // there, and refused when it reaches less than half that CC and the gap is beyond noise - three + // standard errors of the two CCs, (1 - cc^2) / sqrt(n - 3) each. Measured on a monoclinic crystal + // indexed on its doubled pseudo-orthorhombic cell: the two-folds of its average read 0.03-0.04 + // against 0.20 for its own on the all-observation search merge, 4.4-4.7 standard errors inside the + // search's resolution range (~29 over the whole merge); on the Lorentz-filtered one 0.02 against + // 0.19, only 2.7, as that merge keeps fewer of the class's reflections - which is why Rugnux lets + // the all-observation merge's refusal here stand against the filtered merge's confirmation. + // The reference must itself show agreement on the class: two and a half times the highest CC a + // false operator reads on normalised intensities (0.06, see min_operator_cc). A class that weak is + // noise - the added class of a doubled cell the crystal does not have reads 0.02-0.03 of the rest + // - and over tens of thousands of pairs a gap of a few hundredths in it is significant without + // meaning anything (measured: references of 0.045, 0.08 and 0.13 on three such merges, 0.20 on the + // crystal above). And every CC compared needs the pairs the H test asks for (min_pairs_for_h): on a + // few dozen pairs a genuine cubic operator of a small-molecule crystal read 0.05 against 0.67. + constexpr double WEAK_CLASS_MIN_REFERENCE_CC = 0.15; + constexpr double WEAK_CLASS_MAX_CC_FRACTION = 0.5; + constexpr double WEAK_CLASS_MIN_GAP_SE = 3.0; + bool weak_refused = false; + const SpaceGroupOperatorScore* weak_worst = nullptr; + if (weak_best && weak_best->weak_cc >= WEAK_CLASS_MIN_REFERENCE_CC) { + const auto se = [](const SpaceGroupOperatorScore& os) { + return (1.0 - os.weak_cc * os.weak_cc) / std::sqrt(std::max(1, os.weak_pairs - 3)); + }; + for (const auto& rot : c.pg->rotations) { + const auto& os = operator_score(rot); + if (rot.rot == gemmi::Op::identity().rot || os.weak_pairs < opt.min_pairs_for_h + || !std::isfinite(os.weak_cc)) + continue; + const double gap = weak_best->weak_cc - os.weak_cc; + if (os.weak_cc < WEAK_CLASS_MAX_CC_FRACTION * weak_best->weak_cc + && gap > WEAK_CLASS_MIN_GAP_SE * std::hypot(se(os), se(*weak_best)) + && (!weak_worst || os.weak_cc < weak_worst->weak_cc)) { + weak_refused = true; + weak_worst = &os; + } + } + } + if (weak_refused) + consistent = false; + if (!consistent) { // Same three sentences the highest refusal gets below, but kept per candidate for the // ledger: short, because the long form is written once for the group the user is told about. c.why = h_refused ? "H ratio" : r_refused ? "added-operator R" : spread_refused ? "operator spread" : l_refused ? "L-test under merge" - : zone_refused ? "twin-immune zone" : "merge chi^2"; + : zone_refused ? "twin-immune zone" : weak_refused ? "weak parity class" : "merge chi^2"; // Record the highest-order refusal so the caller can say WHY it is processing lower. if (c.order > refused_order && c.pg->representative) { refused_order = c.order; refused_pg_hm = c.pg->representative->point_group_hm(); refused_pg_rep = c.pg->representative; refused_twin_gate = h_refused || r_refused || spread_refused || l_refused || zone_refused; + refused_weak_class = weak_refused; refused_fraction = h_refused ? std::max(0.0, 0.5 - h_added) : std::numeric_limits::quiet_NaN(); if (h_refused) @@ -2163,6 +2301,15 @@ SearchSpaceGroupResult SearchSpaceGroup( + " leave as their own twin mates read acentric (" + FormatDouble(zone_evidence, 1) + " nats against centric, bound " + FormatDouble(ZONE_DECISIVE_NATS, 0) + ") - so they are a twin law of the subgroup, not a symmetry"; + else if (weak_refused) + refused_why = "on the weak parity class " + std::to_string(nm.weak_parity[0]) + " " + + std::to_string(nm.weak_parity[1]) + " " + std::to_string(nm.weak_parity[2]) + + " (mean E^2 " + FormatDouble(nm.weak_ratio, 2) + " of the rest, a half-integer " + "pseudo-translation) its operator " + weak_worst->op_triplet_hkl + " reads CC " + + FormatDouble(weak_worst->weak_cc, 3) + " over " + std::to_string(weak_worst->weak_pairs) + + " pairs against " + FormatDouble(weak_best->weak_cc, 3) + " for " + + weak_best->op_triplet_hkl + " - it is obeyed only by the average of the two " + "halves of the cell the pseudo-translation relates, not by the crystal"; else if (std::isfinite(c.chi2) && std::isfinite(chi2_ref)) refused_why = "merge chi^2 is " + FormatDouble(c.chi2 / chi2_ref, 2) + "x the subgroup's (bound " + FormatDouble(opt.max_merge_chi2_ratio, 2) + ")"; @@ -2337,6 +2484,7 @@ SearchSpaceGroupResult SearchSpaceGroup( result.refused_reason = refused_why; result.refused_point_group_representative = *refused_pg_rep; result.refused_by_twin_gate = refused_twin_gate; + result.refused_by_weak_class = refused_weak_class; result.refused_twin_fraction = refused_fraction; } @@ -3372,6 +3520,16 @@ std::string SearchSpaceGroupResultToText(const SearchSpaceGroupResult& result, << std::setw(9) << std::fixed << std::setprecision(3) << s.h_stat << std::setw(9) << std::fixed << std::setprecision(3) << s.r_stat << "\n"; } + if (std::isfinite(result.weak_class_ratio)) { + const auto& p = result.weak_parity_class; + os << " Weak parity class " << p[0] << " " << p[1] << " " << p[2] << " (mean E^2 " + << FormatDouble(result.weak_class_ratio, 3) << " of the rest) - each operator's CC on it alone:\n"; + for (const auto& s : result.operator_scores) + if (s.weak_pairs > 0) + os << " " << std::setw(14) << std::left << s.op_triplet_hkl << std::right + << std::setw(9) << std::fixed << std::setprecision(3) << s.weak_cc + << std::setw(10) << s.weak_pairs << "\n"; + } os << " H = median |I1-I2|/(I1+I2) over the operator's pairs - the disagreement it implies, with\n" " no sigma in it. The promotion gate is the RATIO of the mean H over the operators a\n" " promotion adds to the mean over the parent group's own, which is what separates a real\n" diff --git a/image_analysis/scale_merge/SearchSpaceGroup.h b/image_analysis/scale_merge/SearchSpaceGroup.h index 18a63b6a3..9b9578841 100644 --- a/image_analysis/scale_merge/SearchSpaceGroup.h +++ b/image_analysis/scale_merge/SearchSpaceGroup.h @@ -54,6 +54,11 @@ struct SpaceGroupOperatorScore { static constexpr int OPERATOR_SHELLS = 4; std::array shell_cc{}; std::array shell_pairs{}; + // The same correlation over the pairs whose two members both lie in the merge's WEAK parity class + // (SearchSpaceGroupResult::weak_parity_class) - all of them, present or not - on E^2 normalised within + // that class. NaN and 0 where the merge has no such class. + double weak_cc = std::numeric_limits::quiet_NaN(); + int weak_pairs = 0; }; struct SpaceGroupCandidateScore { @@ -836,6 +841,13 @@ struct SearchSpaceGroupResult { double ratio = 0.0; // (suppressed)/(control) at the lowest resolution measurable }; std::vector pseudo_translations; + // The parity class a half-integer pseudo-translation suppresses over the whole merge: the reflections + // with p.h odd, p = weak_parity_class, whose mean E^2 is under half the rest's (weak_class_ratio). + // {0,0,0} when no class is, which is the usual case. Where there is one, it is the part of the data a + // pseudo-symmetry that holds for the average of the two halves of the cell does not obey, and Stage A + // judges every operator on it as well (see the weak-class gate in SearchSpaceGroup.cpp). + std::array weak_parity_class{}; + double weak_class_ratio = std::numeric_limits::quiet_NaN(); // The intensity-weighted operator R of the promotion that was ADOPTED (mean over the operators it // adds), that R over merge_r_floor, that R over the globally best-agreeing operator, and where it // falls on the scale between unrelated reflections and that best operator - the number the @@ -869,6 +881,10 @@ struct SearchSpaceGroupResult { // gate (the H ratio or the added-operator R) rather than from the merge chi^2. double refused_twin_fraction = std::numeric_limits::quiet_NaN(); bool refused_by_twin_gate = false; + // Whether the weak-class gate refused it: an added operator the merge's weak parity class shows + // is not obeyed (see weak_parity_class). Evidence a merge with fewer of that class's reflections + // cannot see, rather than a statistic it reads differently. + bool refused_by_weak_class = false; // The symmorphic representative of that group - the same thing point_group_representative is, and // for the same reason (a name cannot tell P321 from P312). It is what a caller hands back as // fixed_point_group to ask what merging under the refused group would give. Unset when nothing diff --git a/rugnux/RugnuxScaleMerge.cpp b/rugnux/RugnuxScaleMerge.cpp index a6afe40f0..d8f29b412 100644 --- a/rugnux/RugnuxScaleMerge.cpp +++ b/rugnux/RugnuxScaleMerge.cpp @@ -2594,7 +2594,19 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { alt = std::move(lower); } } - if (order_of(alt) < order_of(sg_search) + if (order_of(alt) < order_of(sg_search) && alt.refused_by_weak_class) { + // The weak-class gate reads the reflections the Lorentz filter thins out most - a weak + // class barely clears it - so the filtered arm's confirmation is not evidence against the + // all-observation arm's refusal. Not the other way round: on the filtered merge's few + // weak-class pairs a genuine operator has read 0.22 against 0.53 for another (a synthetic + // superstructure in its true 422), where the all-observation merge refused nothing. + logger.Warning("Space-group search: the all-observation merge's refusal of {} STANDS " + "- the weak parity class refuses an operator it adds; keeping {}", + alt.refused_point_group_hm, alt.point_group_hm); + overridden(alt, "all-observation", "Lorentz-filtered"); + result.space_group_arm_override += " - but the refusal stands (the weak parity class)"; + sg_search = alt; + } else if (order_of(alt) < order_of(sg_search) && !alt.refused_point_group_hm.empty() && TwinZoneVerdict(sm_all.merged, gemmi::UnitCell(*result.consensus_cell), sg_search, alt, result) < 0) { @@ -2922,8 +2934,15 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { const auto refusal_stands = [&](const SearchSpaceGroupResult &higher, SearchSpaceGroupResult &lower) { if (higher.point_group_order <= lower.point_group_order - || lower.refused_point_group_hm.empty() - || TwinZoneVerdict(merged_all, *o2.cell, higher, lower, result) >= 0) + || lower.refused_point_group_hm.empty()) + return false; + if (&lower == &s2 && lower.refused_by_weak_class) { // all observations only, as above + logger.Warning("Space-group search (metric lattice): the refusal of {} STANDS - the " + "weak parity class refuses an operator it adds; keeping {}", + lower.refused_point_group_hm, lower.point_group_hm); + return true; + } + if (TwinZoneVerdict(merged_all, *o2.cell, higher, lower, result) >= 0) return false; logger.Warning("Space-group search (metric lattice): the refusal of {} STANDS - the " "twin-immune zone of its added operators reads acentric; keeping {}", diff --git a/tests/SearchSpaceGroupTest.cpp b/tests/SearchSpaceGroupTest.cpp index f69b21963..a1d8ea691 100644 --- a/tests/SearchSpaceGroupTest.cpp +++ b/tests/SearchSpaceGroupTest.cpp @@ -1063,3 +1063,57 @@ TEST_CASE("Operator correlation of a half-integer pseudo-merohedral law") { CHECK(score->h_stat == Catch::Approx(0.0).margin(1e-6)); CHECK(score->op_triplet_hkl == "h/2-k/2,-3/2*h-k/2,-l"); } + +// A half-integer pseudo-translation along a makes the reflections with h odd a weak class that holds only +// how the two halves of the cell differ. Here the halves' common content obeys 222 and their difference +// only the 2-fold on c: the strong class reads every two-fold as real, and only the weak class can tell +// the crystal's own two-fold from the two its average adds. +TEST_CASE("SearchSpaceGroup holds each operator to the weak parity class of a pseudo-translation", + "[SearchSpaceGroup]") { + const gemmi::SpaceGroup& p222 = gemmi::get_spacegroup_by_name("P 2 2 2"); + const gemmi::SpaceGroup& p112 = gemmi::get_spacegroup_by_name("P 1 1 2"); + const gemmi::ReciprocalAsu asu222(&p222), asu112(&p112); + auto generate = [&](bool weak_obeys_222) { + std::vector merged; + for (int h = -10; h <= 10; ++h) + for (int k = -10; k <= 10; ++k) + for (int l = -10; l <= 10; ++l) { + const gemmi::Op::Miller hkl{{h, k, l}}, neg{{-h, -k, -l}}; + if ((h == 0 && k == 0 && l == 0) || hkl < neg) + continue; + const bool weak = h % 2 != 0; + const auto& sg = weak && !weak_obeys_222 ? p112 : p222; + const auto& rasu = weak && !weak_obeys_222 ? asu112 : asu222; + const auto [rep, plus] = rasu.to_asu_sign(hkl, sg.operations()); + // The weak class on its own scale: a different hash, a quarter of the strength. + const gemmi::Op::Miller key = weak ? gemmi::Op::Miller{{rep[0] + 50, rep[1], rep[2]}} : rep; + const double I = (weak ? 0.25 : 1.0) * SyntheticIntensityFromAsu(key); + merged.push_back(MergedReflection{.h = h, .k = k, .l = l, .I = static_cast(I), + .sigma = static_cast(I / 20.0), + .d = static_cast(CalcSyntheticD(h, k, l))}); + } + return merged; + }; + SearchSpaceGroupOptions opt; + opt.merge_friedel = true; + opt.lattice_system = gemmi::CrystalSystem::Orthorhombic; + opt.cell = gemmi::UnitCell(40.0, 50.0, 60.0, 90.0, 90.0, 90.0); + opt.enumerate_all_settings = true; + opt.enumerate_all_rotation_sets = true; // the crystal's own two-fold is on c + + SECTION("two-folds only the average obeys are refused") { + const auto result = SearchSpaceGroup(generate(false), opt); + INFO(SearchSpaceGroupResultToText(result)); + CHECK(result.weak_parity_class == std::array{1, 0, 0}); + CHECK(result.point_group_hm == "2"); + REQUIRE(result.point_group_representative.has_value()); + CHECK(result.point_group_representative->operations().find_by_rotation( + gemmi::parse_triplet("-x,-y,z").rot) != nullptr); + } + SECTION("a crystal whose weak class obeys the whole group keeps it") { + const auto result = SearchSpaceGroup(generate(true), opt); + INFO(SearchSpaceGroupResultToText(result)); + CHECK(result.weak_parity_class == std::array{1, 0, 0}); + CHECK(result.point_group_hm == "222"); + } +}