diff --git a/image_analysis/scale_merge/SearchSpaceGroup.cpp b/image_analysis/scale_merge/SearchSpaceGroup.cpp index 51846557d..d4d4f4df8 100644 --- a/image_analysis/scale_merge/SearchSpaceGroup.cpp +++ b/image_analysis/scale_merge/SearchSpaceGroup.cpp @@ -475,6 +475,26 @@ SearchSpaceGroupResult SearchSpaceGroup( // crystal, so the population this is normalised over means the same thing on all of them. const std::vector Ecc = shell_normalised(pass_cc); + // The merge's own random-noise R floor from the two half-dataset merges (see + // SearchSpaceGroupResult::merge_r_floor). Over the same present (pass_cc) reflections the operator + // R is measured on, so the two are at the same intensity range and multiplicity and their ratio + // means the same on every crystal - intensity-weighted, sigma-free, and built without applying any + // candidate symmetry so a false operator cannot inflate it. + { + double r_num = 0.0, r_den = 0.0; + for (size_t i = 0; i < n; ++i) { + if (!pass_cc[i]) + continue; + const double a = merged[i].I_half[0], b = merged[i].I_half[1]; + if (std::isfinite(a) && std::isfinite(b) && a + b > 0.0) { + r_num += std::fabs(a - b); + r_den += a + b; + } + } + if (r_den > 0.0) + result.merge_r_floor = r_num / r_den; + } + std::unordered_map key_to_index; key_to_index.reserve(n * 2); for (size_t i = 0; i < n; ++i) @@ -521,16 +541,22 @@ SearchSpaceGroupResult SearchSpaceGroup( // a margin of 1.8%. H is calibrated on raw I and stays there. std::vector hv; hv.reserve(x.size()); + double r_num = 0.0, r_den = 0.0; // intensity-weighted R across the operator (see r_stat) for (size_t p = 0; p < x.size(); ++p) { const double denom = x[p] + y[p]; - if (denom > 0.0) + if (denom > 0.0) { hv.push_back(std::fabs(x[p] - y[p]) / denom); + r_num += std::fabs(x[p] - y[p]); + r_den += denom; + } } if (!hv.empty()) { const size_t mid = hv.size() / 2; std::nth_element(hv.begin(), hv.begin() + mid, hv.end()); s.h_stat = hv[mid]; } + if (r_den > 0.0) + s.r_stat = r_num / r_den; s.present = s.n_pairs >= opt.min_pairs_per_operator && std::isfinite(s.cc) && s.cc >= opt.min_operator_cc; return s; @@ -736,7 +762,13 @@ SearchSpaceGroupResult SearchSpaceGroup( struct PGCand { const PointGroupInfo* pg; int order; double min_class_cc; double chi2; double b_extra; // Filled by the selection loop below and carried so the adopted candidate's H // ratio can be reported whether or not the bound had anything to say about it. - double h_ratio = std::numeric_limits::quiet_NaN(); }; + double h_ratio = std::numeric_limits::quiet_NaN(); + // The added operators' mean intensity-weighted R, and that over the merge's random + // R floor - the full-resolution merge-degradation gate's numbers (see r_stat, + // max_operator_r_over_best and max_operator_r_over_floor). Carried for the same + // reason as h_ratio: to report the adopted candidate's whether or not it decided. + double r_added = std::numeric_limits::quiet_NaN(); + double r_over_floor = std::numeric_limits::quiet_NaN(); }; int refused_order = 0; std::string refused_pg_hm, refused_why; std::vector pg_cands; @@ -779,6 +811,22 @@ SearchSpaceGroupResult SearchSpaceGroup( chi2_ref = std::min(chi2_ref, cand_chi2[i]); } + // The best-agreeing operator anywhere in the data: the smallest intensity-weighted R over every + // CC-confirmed operator (see the merge-degradation gate below). A genuine symmetry operator + // relates equal intensities, so its R is small; a low R is thus itself the mark of a genuine + // operator, and the smallest one is the cleanest reference the data offers. It is GLOBAL, not + // per-candidate, because a false point group can be built entirely from operators that agree + // among THEMSELVES - a pseudo-tetragonal 4 whose 4-folds relate the same wrong intensities - and + // only an operator outside that group (the one true 2-fold) exposes them. Confirmed operators + // only: an unconfirmed (low-CC) one has, by construction, a high R and never sets this minimum. + double global_best_r = std::numeric_limits::infinity(); + int n_confirmed_ops = 0; + for (const auto& [rk, s] : op_cache) + if (s.present && s.n_pairs >= opt.min_pairs_per_operator && s.r_stat > 0.0) { + global_best_r = std::min(global_best_r, s.r_stat); + ++n_confirmed_ops; + } + // 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 -> higher min class CC). // Identity (no operators) is always consistent, so it stays the P1 fallback. @@ -846,8 +894,9 @@ SearchSpaceGroupResult SearchSpaceGroup( // tie (see above), the promotion is judged on the most damning of them. double h_ratio = std::numeric_limits::quiet_NaN(); double h_added = 0.0; // the added operators' own H, for the twin fraction below + double r_added = std::numeric_limits::quiet_NaN(); // added operators' mean R (full-res gate) for (const auto *parent : parents) { - double h_new = 0.0, h_par = 0.0; + double h_new = 0.0, h_par = 0.0, r_new = 0.0; int n_new = 0, n_par = 0, pairs_new = 0, pairs_par = 0; for (const auto &rot : c.pg->rotations) { if (rot.rot == gemmi::Op::identity().rot) @@ -858,17 +907,42 @@ SearchSpaceGroupResult SearchSpaceGroup( const bool in_parent = std::binary_search(parent->rotation_set.begin(), parent->rotation_set.end(), RotKey(rot)); if (in_parent) { h_par += os.h_stat; ++n_par; pairs_par += os.n_pairs; } - else { h_new += os.h_stat; ++n_new; pairs_new += os.n_pairs; } + else { h_new += os.h_stat; ++n_new; pairs_new += os.n_pairs; r_new += os.r_stat; } } if (n_new > 0 && n_par > 0 && pairs_new >= opt.min_pairs_for_h && pairs_par >= opt.min_pairs_for_h && h_par > 0.0) { const double r = (h_new / n_new) / (h_par / n_par); + // Record the added operators' R at the same (max-H-ratio) parent the H ratio reports. + if (!std::isfinite(h_ratio) || r > h_ratio) + r_added = r_new / n_new; if (!std::isfinite(h_ratio) || r > h_ratio) { h_ratio = r; h_added = h_new / n_new; } } } + // The first step out of P1 has no parent with operators, so the H loop above left r_added + // unset: the added set is then ALL of this candidate's operators, and the only reference is + // the merge's random-noise floor (there is no other operator to compare against). This is the + // one guard that step has - the H ratio and the b veto both need a parent group. + if (!std::isfinite(r_added) && !c.pg->rotations.empty()) { + double r_new = 0.0; int n_new = 0, pairs_new = 0; + for (const auto &rot : c.pg->rotations) { + if (rot.rot == gemmi::Op::identity().rot) + continue; + const auto &os = operator_score(rot); + if (os.n_pairs < opt.min_pairs_per_operator) + continue; + r_new += os.r_stat; ++n_new; pairs_new += os.n_pairs; + } + if (n_new > 0 && pairs_new >= opt.min_pairs_for_h) + r_added = r_new / n_new; + } + c.r_added = r_added; + c.r_over_floor = (std::isfinite(r_added) && std::isfinite(result.merge_r_floor) + && result.merge_r_floor > 0.0) + ? r_added / result.merge_r_floor + : std::numeric_limits::quiet_NaN(); c.h_ratio = h_ratio; // The chi^2 ratio is only trustworthy when the error model is calibrated. When even the best // subgroup's reduced chi^2 (chi2_ref) is far above 1 - weak, low-resolution data whose merged @@ -908,6 +982,33 @@ SearchSpaceGroupResult SearchSpaceGroup( if (h_refused) consistent = false; + // Full-resolution merge-degradation test. A necessary condition like H, but read against a + // CLEAN reference rather than against a parent that can itself be contaminated - so it catches + // a pseudo-symmetric cascade the parent-normalised gates wave through (a 2 -> 222 -> 422 built + // by pooling one real 2-fold with false ones), and it is the only gate that can act on the + // first step out of P1. Not rescuable: the b rescue above only lifts a chi^2-borderline case. + // - When the data confirm more than one operator, judge the added operators' R against the + // GLOBALLY best-agreeing operator (the smallest R anywhere): on a genuine group every + // operator agrees about as well as that best one, so the ratio is ~1; a false operator - + // even one whose group's members all agree among themselves, like a pseudo-tetragonal 4 - + // sits far above it (see max_operator_r_over_best). + // - The first step out of P1, where only one operator is confirmed at all, has no other + // operator to be the best - fall back to the merge's own random-noise R floor + // (max_operator_r_over_floor). + bool r_refused = false; + double r_gate_value = std::numeric_limits::quiet_NaN(); + if (std::isfinite(c.r_added)) { + if (n_confirmed_ops >= 2 && std::isfinite(global_best_r) && global_best_r > 0.0) { + r_gate_value = c.r_added / global_best_r; + r_refused = r_gate_value > opt.max_operator_r_over_best; + } else if (std::isfinite(result.merge_r_floor) && result.merge_r_floor > 0.0) { + r_gate_value = c.r_added / result.merge_r_floor; + r_refused = r_gate_value > opt.max_operator_r_over_floor; + } + } + if (r_refused) + consistent = false; + if (!consistent) { // Record the highest-order refusal so the caller can say WHY it is processing lower. if (c.order > refused_order && c.pg->representative) { @@ -924,6 +1025,16 @@ SearchSpaceGroupResult SearchSpaceGroup( + ") - the added operator relates unequal intensities, as a twin law of " "fraction " + FormatDouble(std::max(0.0, 0.5 - h_added), 2) + " or more would"; + else if (r_refused) + refused_why = "its added operators' intensity-weighted R is " + + FormatDouble(r_gate_value, 1) + "x " + + (n_confirmed_ops >= 2 + ? "the best-agreeing operator in the data (bound " + + FormatDouble(opt.max_operator_r_over_best, 1) + : "the merge's own random-noise floor (bound " + + FormatDouble(opt.max_operator_r_over_floor, 1)) + + "x) - at full resolution the added operator relates reflections that " + "disagree far beyond measurement error, i.e. it is not a real symmetry"; else if (b_refused) // Against the FLOORED parent b, which is what the test compared - quoting the raw // parent b reads as a far bigger balloon than the one that actually tripped the veto. @@ -977,9 +1088,16 @@ SearchSpaceGroupResult SearchSpaceGroup( // The H ratio of the promotion that was ADOPTED, reported whether the bound had anything to say // about it or not. Read after the choice is final, so a fixed_point_group override reports the // ratio of the group it forced rather than of the one Stage A would have taken. + result.global_best_operator_r = std::isfinite(global_best_r) ? global_best_r + : std::numeric_limits::quiet_NaN(); for (const auto& c : pg_cands) - if (c.pg == best_pg) + if (c.pg == best_pg) { result.h_ratio = c.h_ratio; + result.r_added = c.r_added; + result.r_over_floor = c.r_over_floor; + if (std::isfinite(c.r_added) && std::isfinite(global_best_r) && global_best_r > 0.0) + result.r_over_best = c.r_added / global_best_r; + } result.h_ratio_bound = opt.max_operator_h_ratio; // Only report a refusal that is actually ABOVE what was adopted. @@ -1343,13 +1461,14 @@ std::string SearchSpaceGroupResultToText(const SearchSpaceGroupResult& result, os << " " << std::setw(14) << std::left << "operator" << std::right << std::setw(9) << "CC" << std::setw(10) << "pairs" << std::setw(9) << "symm" - << std::setw(9) << "H" << "\n"; + << std::setw(9) << "H" << std::setw(9) << "R" << "\n"; for (const auto& s : result.operator_scores) { os << " " << std::setw(14) << std::left << s.op_triplet_hkl << std::right << std::setw(9) << std::fixed << std::setprecision(3) << s.cc << std::setw(10) << s.n_pairs << std::setw(9) << (s.present ? "yes" : "no") - << std::setw(9) << std::fixed << std::setprecision(3) << s.h_stat << "\n"; + << std::setw(9) << std::fixed << std::setprecision(3) << s.h_stat + << std::setw(9) << std::fixed << std::setprecision(3) << s.r_stat << "\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" @@ -1360,6 +1479,23 @@ std::string SearchSpaceGroupResultToText(const SearchSpaceGroupResult& result, << FormatDouble(result.h_ratio_bound, 2) << ").\n"; else os << " H ratio not available (no parent group to normalise against, or too few pairs).\n"; + // R = intensity-weighted sum|I1-I2|/sum(I1+I2) across the operator's pairs (strong reflections + // dominate, where the median H under-weights them). The merge-degradation gate scores the added + // operators' R against a clean reference - the globally best-agreeing operator, or the merge's + // own random-noise R floor (from the half-dataset merges) on the first step out of P1 - so it + // catches false symmetry the parent-normalised H waves through. + if (std::isfinite(result.r_added)) { + os << " Added-operator R " << FormatDouble(result.r_added, 3); + if (std::isfinite(result.r_over_best)) + os << " = " << FormatDouble(result.r_over_best, 2) << "x the best operator (" + << FormatDouble(result.global_best_operator_r, 3) << ")"; + if (std::isfinite(result.r_over_floor)) + os << ", " << FormatDouble(result.r_over_floor, 2) << "x the random-noise floor (" + << FormatDouble(result.merge_r_floor, 3) << ")"; + os << " for the adopted point group.\n"; + } else if (std::isfinite(result.merge_r_floor)) { + os << " Merge random-noise R floor " << FormatDouble(result.merge_r_floor, 3) << ".\n"; + } os << "\nSpace-group candidates\n"; os << " " << std::setw(10) << std::left << "SG" << std::right diff --git a/image_analysis/scale_merge/SearchSpaceGroup.h b/image_analysis/scale_merge/SearchSpaceGroup.h index 0382fdd9e..5e2f29411 100644 --- a/image_analysis/scale_merge/SearchSpaceGroup.h +++ b/image_analysis/scale_merge/SearchSpaceGroup.h @@ -41,6 +41,13 @@ struct SpaceGroupOperatorScore { // twin law (see max_operator_h_ratio). The median rather than the mean because a merohedral twin // perturbs EVERY pair while a badly integrated minority perturbs only the tail. double h_stat = 0.0; + // INTENSITY-WEIGHTED disagreement across this operator's pairs: sum|I1-I2| / sum(I1+I2), an + // R-factor form. Unlike h_stat (a median) it is dominated by the STRONG reflections, which is where + // a false operator relating unequal intensities shows the largest contrast and where a genuine one + // agrees best - the same reflections a median under-weights. Sigma-free, so it does not saturate + // with the merge's ISa the way the chi^2 and systematic-b gates do on a starved search merge. Used + // as the full-resolution merge-degradation gate (see max_operator_r_over_floor). + double r_stat = 0.0; }; struct SpaceGroupCandidateScore { @@ -345,6 +352,42 @@ struct SearchSpaceGroupOptions { // The H test needs at least this many pairs on both sides to mean anything. int min_pairs_for_h = 200; + // Full-resolution merge-degradation gate. The operators a promotion ADDS are scored by their + // intensity-weighted R (r_stat: a SUM, so the strong low-resolution reflections that carry the + // contrast dominate it, where the median H under-weights them), and that R is judged against a + // CLEAN reference. This is the one Stage-A gate whose reference cannot be contaminated by false + // operators: the H ratio and the systematic-b veto both normalise against the PARENT group, and a + // parent can itself be a false promotion (a pseudo-tetragonal 2 -> 222 -> 422 cascade confirms + // 222 by pooling the one real 2-fold with two false ones, so the 422 step is then measured + // against an already-contaminated 222, and every ratio reads ~1.4 and passes). It also acts on + // the first step out of P1, where there is no parent to normalise against at all and the operator + // CC floor stood alone. Sigma-free, so unlike the chi^2 and b gates it does not saturate with the + // merge's ISa on a starved search merge. + // + // Two references, each scale-free so the bound means the same on every crystal (the property an + // absolute R bound lacks): + // + // - max_operator_r_over_best: when the data confirm more than one operator, the added operators' + // mean R over the GLOBALLY best-agreeing operator (the smallest r_stat anywhere). A genuine + // operator relates equal intensities, so a small R is itself the signature of a real operator + // and the smallest one is the cleanest reference the data hold. On a genuine group EVERY + // operator agrees about as well as the best (ratio ~1.0-1.2 measured, cubic 3-folds and a + // genuine 6 included); a false operator sits far above it, INCLUDING one whose own group's + // members agree among themselves (a pseudo-tetragonal 4 reads ~3.8, because the reference is + // the true 2-fold OUTSIDE that group). Measured over the probe crystals: genuine promotions + // reach 1.2, the false 2-fold/4-fold/6-fold-holohedry steps 2.2-3.8. 2.0 sits in that gap. + double max_operator_r_over_best = 2.0; + // + // - max_operator_r_over_floor: the first step out of P1 confirms only the one operator, so there + // is no other to be the best - fall back to the merge's own random-noise R floor (merge_r_floor, + // from the half-dataset merges). The floor is a genuine-symmetry disagreement (two halves of + // the SAME reflections) at the same multiplicity, so it too is a clean scale-free reference. + // Measured: a genuine first 2-fold sits at ~1.3-2.3x the floor, a false one (a pseudo-C-centred + // metric coincidence) at ~4.7. 3.5 sits in that gap. Wider than the best-operator bound because + // the floor is a noisier reference than a confirmed operator (it can also be inflated by an + // indexing ambiguity that mixes hands), and the safe direction here is to stay in P1. + double max_operator_r_over_floor = 3.5; + // Above this reduced chi^2 for the best subgroup (chi2_ref), the merged error model is treated as // badly miscalibrated (weak, low-resolution data whose sigmas are far too small): the fixed-sigma // chi^2 ratio then grows with point-group order for genuine high symmetry too and can no longer @@ -498,6 +541,24 @@ struct SearchSpaceGroupResult { double h_ratio = std::numeric_limits::quiet_NaN(); double h_ratio_bound = 0.0; + // The merge's own RANDOM-noise R floor: sum|Ihalf0-Ihalf1| / sum(Ihalf0+Ihalf1) over the present + // reflections, from the two half-dataset merges. This is the intensity-weighted disagreement two + // GENUINELY equivalent groups of observations of the SAME reflection show - i.e. the value the + // r_stat of a real symmetry operator would tend to (plus a modest systematic floor), measured on + // this very crystal at this very multiplicity. It is the clean, uncontaminated reference the + // operator R is judged against (see max_operator_r_over_floor), the one a false operator cannot + // move because it is built without applying any candidate symmetry. NaN if the merge carried no + // half-dataset intensities. Reported so a run shows the margin the gate had. + double merge_r_floor = 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, and that R over the globally best-agreeing operator - the + // numbers the merge-degradation gate acts on (best-operator where two or more operators are + // confirmed, floor on the first step out of P1). Reported so a run shows the margin the gate had. + double r_added = std::numeric_limits::quiet_NaN(); + double r_over_floor = std::numeric_limits::quiet_NaN(); + double r_over_best = std::numeric_limits::quiet_NaN(); + double global_best_operator_r = std::numeric_limits::quiet_NaN(); + // A HIGHER point group whose operators the intensities confirmed (Stage A) but whose promotion the // consistency tests refused, with the reason. Processing continues in the lower group, which is the // safe direction: merging a twinned crystal in the twin's holohedry averages non-equivalent