diff --git a/.gitea/workflows/battery.yml b/.gitea/workflows/battery.yml index 785024627..9be7f04ef 100644 --- a/.gitea/workflows/battery.yml +++ b/.gitea/workflows/battery.yml @@ -1,6 +1,6 @@ name: Validation Battery -# Every push runs the ci tier of tools/battery (25 sets, about 10 minutes of rugnux on an RTX 5080, +# Every push runs the ci tier of tools/battery (28 sets, about 10 minutes of rugnux on an RTX 5080, # plus the build); the full battery (open + inhouse arms, about 2 hours of rugnux there) runs only # when started by hand. Both run on the one machine that holds the battery data, one at a time (the # concurrency group below), under their own runner label, with the data mounted read-only and diff --git a/rugnux/Rugnux.cpp b/rugnux/Rugnux.cpp index 05667ee9e..6879ffa79 100644 --- a/rugnux/Rugnux.cpp +++ b/rugnux/Rugnux.cpp @@ -107,13 +107,28 @@ bool ValidationEvidencePrefers(const ValidationSpotEvidence ¤t, const Vali } bool ValidationEvidenceBeatsChance(const ValidationSpotEvidence &e) { + return ValidationEvidenceBeatsChance(e, std::erfc(SPOT_BUDGET_SIGNIFICANCE_Z / std::sqrt(2.0))); +} + +double ValidationEvidenceLog10Tail(const ValidationSpotEvidence &e) { const int64_t n = e.reflections + e.reflections_by_chance; const double p = 1.0 / (1.0 + VALIDATION_NULL_DISPLACEMENTS); - double tail = 0.0; + // Summed in logs: a real lattice's tail is far below the smallest double. + std::vector log_terms; for (int64_t j = e.reflections; j <= n; j++) - tail += std::exp(std::lgamma(n + 1.0) - std::lgamma(j + 1.0) - std::lgamma(n - j + 1.0) - + j * std::log(p) + (n - j) * std::log(1.0 - p)); - return tail < std::erfc(SPOT_BUDGET_SIGNIFICANCE_Z / std::sqrt(2.0)); + log_terms.push_back(std::lgamma(n + 1.0) - std::lgamma(j + 1.0) - std::lgamma(n - j + 1.0) + + j * std::log(p) + (n - j) * std::log(1.0 - p)); + if (log_terms.empty()) + return 0.0; + const double top = *std::max_element(log_terms.begin(), log_terms.end()); + double sum = 0.0; + for (const double t : log_terms) + sum += std::exp(t - top); + return (top + std::log(sum)) / std::log(10.0); +} + +bool ValidationEvidenceBeatsChance(const ValidationSpotEvidence &e, double significance) { + return ValidationEvidenceLog10Tail(e) < std::log10(significance); } RotationScaleWalk WalkRotationScale(double first_fit, diff --git a/rugnux/Rugnux.h b/rugnux/Rugnux.h index cfa6392aa..3aff1cf03 100644 --- a/rugnux/Rugnux.h +++ b/rugnux/Rugnux.h @@ -231,8 +231,24 @@ constexpr int VALIDATION_NULL_DISPLACEMENTS = 5; // 1 + VALIDATION_NULL_DISPLACEMENTS arrangements, so each of the reflections the arrangements take // between them falls on the recorded one with probability 1 / (1 + VALIDATION_NULL_DISPLACEMENTS); // the lattice is this crystal's when the recorded arrangement takes so many that the binomial tail -// is below the significance of SPOT_BUDGET_SIGNIFICANCE_Z. +// is below `significance` - by default that of SPOT_BUDGET_SIGNIFICANCE_Z. bool ValidationEvidenceBeatsChance(const ValidationSpotEvidence &e); +bool ValidationEvidenceBeatsChance(const ValidationSpotEvidence &e, double significance); +// The binomial tail ValidationEvidenceBeatsChance compares with its significance, as log10. +double ValidationEvidenceLog10Tail(const ValidationSpotEvidence &e); + +// The significance the run's no-crystal test (RejectChanceLattice) asks of the lattice the first +// pass ended on - Z ~ 6 instead of the 0.001 the comparisons between candidate lattices use. The +// binomial null holds for a lattice chosen independently of the validation frames, and this one is +// not: it is the winner of some fifty indexing attempts (schemes, ladder rungs, beam centres, the +// short-axis pass), each fitting the orientation, cell and geometry to an accumulated cloud that +// already holds the long-lived reflections it then re-finds on the validation frames, while the +// displaced arrangements get none of that selection. That makes the 0.001 null optimistic by about +// the number of attempts, ~1.7 decades, before the fit's own freedom is counted. A real crystal's +// lattice, even a weak one on a few frames, clears the test by tens of decades, so the strict level +// costs a crystal nothing; with no crystal it asks about three times the stray reflections the +// selection can supply. +constexpr double NO_CRYSTAL_SIGNIFICANCE = 1e-9; // Whether `candidate` puts a larger share of its validation spots on its lattice, over and above what // a wrong spindle angle puts there, than `current` does - by more than the binomial noise of the two diff --git a/rugnux/RugnuxFirstPassAccept.cpp b/rugnux/RugnuxFirstPassAccept.cpp index a78b2c645..55fd883f5 100644 --- a/rugnux/RugnuxFirstPassAccept.cpp +++ b/rugnux/RugnuxFirstPassAccept.cpp @@ -147,18 +147,23 @@ auto Rugnux::FirstPassRun::MeasureVerdict(const FirstPass &best, const std::vect void Rugnux::FirstPassRun::RejectChanceLattice(const FirstPass &best, const Verdict &v) const { const PooledEvidence &evidence = v.evidence; const double pooled = v.pooled, pooled_chance = v.pooled_chance; - // Near tie (log only): the distinct reflections against the count at which the binomial tail - // ValidationEvidenceBeatsChance tests reaches its significance, in the normal approximation. + // Near tie (log only): the exact binomial tail against the significance, both as log10. The + // noise is how far the tail moves for one binomial standard deviation of the count. { const double n = static_cast(evidence.reflections + evidence.reflections_by_chance); const double p = 1.0 / (1.0 + VALIDATION_NULL_DISPLACEMENTS); - const double sd = std::sqrt(n * p * (1.0 - p)); - const double bar = n * p + SPOT_BUDGET_SIGNIFICANCE_Z * sd; - NearTie("no-crystal test", fmt::format("{} distinct reflections", evidence.reflections), - fmt::format("bar {:.1f}", bar), static_cast(evidence.reflections) - bar, sd, - "binomial on distinct reflections"); + const double log10_tail = ValidationEvidenceLog10Tail(evidence); + PooledEvidence one_more = evidence; + one_more.reflections++; + const double per_reflection = log10_tail - ValidationEvidenceLog10Tail(one_more); + NearTie("no-crystal test", + fmt::format("{} distinct reflections vs {} by chance, log10 tail {:.2f}", + evidence.reflections, evidence.reflections_by_chance, log10_tail), + fmt::format("log10 significance {:.2f}", std::log10(NO_CRYSTAL_SIGNIFICANCE)), + std::log10(NO_CRYSTAL_SIGNIFICANCE) - log10_tail, + per_reflection * std::sqrt(n * p * (1.0 - p)), "exact binomial on distinct reflections, decades"); } - if (!ValidationEvidenceBeatsChance(evidence)) { + if (!ValidationEvidenceBeatsChance(evidence, NO_CRYSTAL_SIGNIFICANCE)) { { // Name the cell and Bravais class that was rejected. The commonest cause is a metric // symmetry promoted one class too far - the constrained cell then misses every diff --git a/tests/RotationScaleWalkTest.cpp b/tests/RotationScaleWalkTest.cpp index 7fc4e1abb..617610485 100644 --- a/tests/RotationScaleWalkTest.cpp +++ b/tests/RotationScaleWalkTest.cpp @@ -147,3 +147,22 @@ TEST_CASE("ValidationEvidenceBeatsChance_ExchangeableArrangements", "[RotationSc // As many at the recorded angles as at an average wrong one is no lattice. CHECK_FALSE(ValidationEvidenceBeatsChance({.reflections = 100, .reflections_by_chance = 500})); } + +TEST_CASE("ValidationEvidenceBeatsChance_NoCrystalSignificance", "[RotationScale]") { + // A handful of stray reflections passes 0.001 but not the no-crystal test's own level. + CHECK(ValidationEvidenceBeatsChance({.reflections = 5, .reflections_by_chance = 1})); + CHECK_FALSE(ValidationEvidenceBeatsChance({.reflections = 5, .reflections_by_chance = 1}, NO_CRYSTAL_SIGNIFICANCE)); + // Against two by chance it takes 15 (tail 10^-9.7), and 14 is not enough (10^-8.96). + CHECK(ValidationEvidenceBeatsChance({.reflections = 15, .reflections_by_chance = 2}, NO_CRYSTAL_SIGNIFICANCE)); + CHECK_FALSE(ValidationEvidenceBeatsChance({.reflections = 14, .reflections_by_chance = 2}, NO_CRYSTAL_SIGNIFICANCE)); + // A weak real lattice on a few frames is still tens of decades past it. + CHECK(ValidationEvidenceBeatsChance({.reflections = 115, .reflections_by_chance = 4}, NO_CRYSTAL_SIGNIFICANCE)); +} + +TEST_CASE("ValidationEvidenceLog10Tail_Exact", "[RotationScale]") { + CHECK(ValidationEvidenceLog10Tail({.reflections = 4, .reflections_by_chance = 2}) == Catch::Approx(-2.0604).margin(1e-4)); + CHECK(ValidationEvidenceLog10Tail({.reflections = 5, .reflections_by_chance = 2}) == Catch::Approx(-2.6981).margin(1e-4)); + // Far below the smallest double, still finite. + CHECK(ValidationEvidenceLog10Tail({.reflections = 1354, .reflections_by_chance = 777}) == Catch::Approx(-509.691).margin(1e-3)); + CHECK(ValidationEvidenceLog10Tail({}) == 0.0); +} diff --git a/tools/battery/README.md b/tools/battery/README.md index cd3cc6d0f..0a9ddb54e 100644 --- a/tools/battery/README.md +++ b/tools/battery/README.md @@ -281,7 +281,7 @@ run's crash shows as one too. - **smoke** covers the readers, the symmetry classes and some known hard cases; several of its sets fail on purpose. -- **ci** is the routine regression check: 26 sets that passed with the same space group and steady +- **ci** is the routine regression check: 28 sets that passed with the same space group and steady d_min and ISa in every full run of rc174 and rc175, with no warning that questions the lattice or the group (supercell, harmonic, twinning, untested centring). It spans HDF5, miniCBF, cbf.gz, marCCD (both formats) and SMV; every crystal system, centred and high-symmetry lattices, small @@ -289,7 +289,9 @@ run's crash shows as one too. (8qaw, about 2 minutes) - about 10 minutes of rugnux in all on the RTX 5080. A set that starts failing for a known, accepted reason leaves the tier; one that is merely slow stays. KDP joined later than the rest: it passes as an `indistinguishable_alternative` (I41md for I-42d), and its - twinning warning is most likely a false alarm from four heavy atoms in the cell. + twinning warning is most likely a false alarm from four heavy atoms in the cell. The two no-crystal + controls (nothing_1, nothing_2) joined once the no-crystal test got its own significance (1e-9): + they pass by reporting no lattice, about seven decades short of that level. **Exit status.** `run` exits with status 1 when any set crashed, in or out of the ci tier, or when a ci-tier set in the run failed - a wrong lattice or space group, a timeout or a missing input - and diff --git a/tools/battery/inhouse.json b/tools/battery/inhouse.json index 230fc0d91..e796742f0 100644 --- a/tools/battery/inhouse.json +++ b/tools/battery/inhouse.json @@ -36,8 +36,8 @@ {"id": "myob_x06da_powder_2", "input": "myob_x06da_powder_2/MyoB2-2_5ddd7d_master.h5", "wavelength": 0.95373, "ref": {"sgno": 3, "cell": [35.576, 28.811, 63.529, 90.0, 105.593, 90.0], "anomalous": false, "isa": 2.19, "completeness": 58.0, "r_meas": 1.106, "cc_half": 0.655, "multiplicity": 4.71, "dmin_low": 2.98, "r_meas_low": 0.706, "cc_half_high": 0.788, "dmin": 0.99, "dmin_rule": "xds_range", "dmin_xds": 0.99, "dmax": 999.0}, "tags": ["h5", "myoglobin", "twin"], "ref_override": {"sg": "P 1 21 1", "sgno": 4}, "ref_override_why": "myoglobin, P 1 21 1. XDS does not judge screw axes (it reports P 2)", "model": "4dc8"}, {"id": "myob_x06da_split", "input": "myob_x06da_split/MyoB2-4_079740_master.h5", "wavelength": 0.95373, "ref": {"sgno": 3, "cell": [35.39, 28.783, 63.626, 90.0, 105.544, 90.0], "anomalous": false, "isa": 12.41, "completeness": 77.7, "r_meas": 0.577, "cc_half": 0.984, "multiplicity": 4.38, "dmin_low": 4.48, "r_meas_low": 0.091, "cc_half_high": 0.994, "dmin": 1.506, "dmin_rule": "xds_range", "dmin_xds": 1.506, "dmax": 50.0}, "tags": ["h5", "myoglobin", "twin"], "ref_override": {"sg": "P 1 21 1", "sgno": 4}, "ref_override_why": "myoglobin, P 1 21 1. XDS does not judge screw axes (it reports P 2)", "model": "4dc8"}, {"id": "myob_x06da_sparse", "input": "myob_x06da_sparse/MyoB2-5_7cef9c_master.h5", "wavelength": 0.95373, "ref": {"sgno": 3, "cell": [35.388, 28.752, 64.083, 90.0, 106.352, 90.0], "anomalous": false, "isa": 5.46, "completeness": 73.7, "r_meas": 0.326, "cc_half": 0.972, "multiplicity": 5.21, "dmin_low": 5.92, "r_meas_low": 0.146, "cc_half_high": 0.254, "dmin": 2.0, "dmin_rule": "xds_range", "dmin_xds": 2.0, "dmax": 50.0}, "tags": ["h5", "myoglobin", "twin"], "ref_override": {"sg": "P 1 21 1", "sgno": 4}, "ref_override_why": "myoglobin, P 1 21 1. XDS does not judge screw axes (it reports P 2)", "model": "4dc8"}, - {"id": "nothing_2", "input": "nothing_2/test-28_3400ac_master.h5", "wavelength": 0.99988, "expect": "no_lattice", "tags": ["h5", "control"]}, - {"id": "nothing_1", "input": "nothing_1/test-28_9bf604_master.h5", "wavelength": 0.99988, "expect": "no_lattice", "tags": ["h5", "control"]}, + {"id": "nothing_2", "input": "nothing_2/test-28_3400ac_master.h5", "wavelength": 0.99988, "expect": "no_lattice", "tags": ["h5", "control"], "tiers": {"ci": "no-crystal control: must report no lattice"}}, + {"id": "nothing_1", "input": "nothing_1/test-28_9bf604_master.h5", "wavelength": 0.99988, "expect": "no_lattice", "tags": ["h5", "control"], "tiers": {"ci": "no-crystal control: must report no lattice"}}, {"id": "aspirin_x10sa_20keV", "input": "aspirin_x10sa_20keV/aspirin_1_002_master.h5", "wavelength": 0.61993, "cod": "7050897", "tags": ["h5", "monoclinic", "small-molecule"], "ref": {"sgno": 4, "cell": [11.262, 6.543, 11.257, 90.0, 95.916, 90.0], "anomalous": true, "isa": 27.4, "completeness": 84.0, "r_meas": 0.033, "cc_half": 0.999, "multiplicity": 3.14, "dmin_low": 2.01, "r_meas_low": 0.033, "cc_half_high": 0.997, "dmin": 0.68, "dmin_rule": "xds_range", "dmin_xds": 0.68, "dmax": 50.0}, "ref_override": {"sg": "P 1 21/c 1", "sgno": 14}, "ref_override_why": "aspirin form I, P 21/c; COD 7050897 - C. C. Wilson, New J. Chem. 26 (2002) 1733-1739, doi:10.1039/b203775k. XDS reports only Sohncke groups (P 21)", "tiers": {"ci": "small molecule, centrosymmetric P21/c"}}, {"id": "aspirin_x10sa_25keV", "input": "aspirin_x10sa_25keV/aspirin_1_001_master.h5", "wavelength": 0.49594, "cod": "7050897", "tags": ["h5", "monoclinic", "small-molecule"], "ref": {"sgno": 4, "cell": [11.255, 6.541, 11.259, 90.0, 95.916, 90.0], "anomalous": true, "isa": 29.25, "completeness": 86.6, "r_meas": 0.034, "cc_half": 0.999, "multiplicity": 3.15, "dmin_low": 1.63, "r_meas_low": 0.032, "cc_half_high": 0.994, "dmin": 0.55, "dmin_rule": "xds_range", "dmin_xds": 0.55, "dmax": 50.0}, "ref_override": {"sg": "P 1 21/c 1", "sgno": 14}, "ref_override_why": "aspirin form I, P 21/c; COD 7050897 - C. C. Wilson, New J. Chem. 26 (2002) 1733-1739, doi:10.1039/b203775k. XDS reports only Sohncke groups (P 21)"}, {"id": "citricacid_x10sa_20keV", "input": "citricacid_x10sa_20keV/citricacid_1_005_master.h5", "wavelength": 0.61993, "cod": "5000063", "tags": ["h5", "monoclinic", "small-molecule"], "ref": {"sgno": 4, "cell": [11.454, 5.579, 12.67, 90.0, 111.587, 90.0], "anomalous": true, "isa": 20.44, "completeness": 82.0, "r_meas": 0.041, "cc_half": 0.997, "multiplicity": 3.1, "dmin_low": 2.02, "r_meas_low": 0.05, "cc_half_high": 0.997, "dmin": 0.68, "dmin_rule": "xds_range", "dmin_xds": 0.68, "dmax": 50.0}, "ref_override": {"sg": "P 1 21/c 1", "sgno": 14}, "ref_override_why": "anhydrous citric acid, P 21/a (P 21/c in the standard setting); COD 5000063 - J. P. Glusker, J. A. Minkin, A. L. Patterson, Acta Cryst. B25 (1969) 1066-1072, doi:10.1107/S0567740869003542. XDS reports only Sohncke groups (P 21)"},