Files
Jungfraujoch/tools/battery/test_score.py
T
leonarski_fandClaude Opus 5.5 d28c8b8bb3 battery: same-absence groups of one Laue class pass as indistinguishable_alternative
sgequiv keyed space groups on their proper+improper rotation matrices, so groups with
identical merged intensities but different point groups (I41md vs I-42d, Pc vs P2/c,
Pna21 vs Pnma, P2 vs P2/m) were called a "REAL difference", contradicting its own
docstring, and score.py compared point groups literally on the XDS arms. A run that
wrote one member of such a pair and listed the other in SPACE_GROUP_ALTERNATIVES failed
as sym_other / sym_over.

sgequiv now keys each group, in every setting gemmi tabulates, on its Laue rotations
(operations plus their negatives), centring and absences; describe() separates the new
different-point-group pairs from the old same-point-group ones, and enantiomorph
handling is unchanged. score.py passes a row, on both arms, when some setting of the
reference has the same signature as our group in our own setting: verdict pass, cause
indistinguishable_alternative, the reason saying whether rugnux listed the reference.

Pass, not unscored: unlike an unmeasured screw axis, everything was measured and the
answer is as right as the data allow; only a model or a strong anomalous signal decides.
The separate cause keeps such rows countable. Being listed in SPACE_GROUP_ALTERNATIVES
alone is deliberately not enough: that list also carries the screw alternatives of an
undetermined axis, whose absences differ, and those stay under the screw rule. The
open arm's same-point-group UNDECIDABLE pass now also requires the setting check.

results.json gains sg_alternatives (rugnux's SPACE_GROUP_ALTERNATIVES). KDP joins the
ci tier. Re-scoring the open and in-house arms of the two latest full runs changes one
verdict in each: kdp_x10sa_20keV fail/sym_other -> pass/indistinguishable_alternative.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi
2026-10-09 16:27:23 +02:00

196 lines
10 KiB
Python

"""Targeted tests of score.py, the part a wrong answer would be laundered through.
Plain stdlib unittest, so it runs with `python3 tools/battery/test_score.py` (or
`python3 -m unittest`) from this directory, without pytest and without a battery run.
"""
import unittest
import score
def report(sg, sgno, cell, **extra):
"""The three report keys the scorer needs, plus whatever a case adds."""
return dict({"SPACE_GROUP_NAME": sg, "SPACE_GROUP_NUMBER": str(sgno),
"UNIT_CELL_CONSTANTS": " ".join(f"{x:.3f}" for x in cell)}, **extra)
# A deposited trigonal reference and the higher group our reduction reads on it: the shape of
# the accepted-alternative rows, with a synthetic cell, not a real one.
TRIGONAL = [80.0, 80.0, 150.0, 90.0, 90.0, 120.0]
DEPOSITED = {"sg": "P 32", "sgno": 145, "cell": TRIGONAL, "dmin": 2.0}
def entry(**extra):
return dict({"id": "test_set", "arm": "open", "ref": DEPOSITED}, **extra)
class AcceptedAlternatives(unittest.TestCase):
def test_deposition_still_decides_on_its_own(self):
"""Without alternatives the higher group is a symmetry failure, as before."""
r = score.judge(entry(), report("P 31 2 1", 152, TRIGONAL), "")
self.assertEqual((r["verdict"], r["cause"]), ("fail", "sym_over"))
def test_either_answer_passes(self):
"""Both the deposited group and the alternative pass, and only the second is marked."""
e = entry(ref_alternatives=[{"sg": "P 31 2 1", "sgno": 152, "why": "a stated reason"}])
dep = score.judge(e, report("P 32", 145, TRIGONAL), "")
self.assertEqual((dep["verdict"], dep["cause"], dep["accepted_alt"]), ("pass", None, None))
alt = score.judge(e, report("P 31 2 1", 152, TRIGONAL), "")
self.assertEqual((alt["verdict"], alt["cause"]), ("pass", "accepted_alternative"))
self.assertEqual(alt["accepted_alt"], "P 31 2 1")
self.assertIn("a stated reason", alt["accepted_alt_why"])
# the row still describes the answer against the DEPOSITION, not against the alternative
self.assertEqual(alt["sg_ref"], "P 32")
def test_the_other_hand_of_an_alternative_passes_too(self):
"""P 31 2 1 and P 32 2 1 differ by hand only, which intensities cannot decide."""
e = entry(ref_alternatives=[{"sg": "P 31 2 1", "sgno": 152, "why": "a stated reason"}])
r = score.judge(e, report("P 32 2 1", 154, TRIGONAL), "")
self.assertEqual((r["verdict"], r["cause"]), ("pass", "accepted_alternative"))
def test_a_third_answer_still_fails(self):
"""An alternative accepts one other answer, not any other answer."""
e = entry(ref_alternatives=[{"sg": "P 31 2 1", "sgno": 152, "why": "a stated reason"}])
r = score.judge(e, report("P 31 1 2", 151, TRIGONAL), "")
self.assertEqual(r["verdict"], "fail")
def test_alternative_cell(self):
"""A supercell alternative: the deposited cell is its (0,1/2,1/2) sublattice, so the
deposition alone scores the answer as a doubled lattice and the alternative accepts it."""
sub = [35.869, 39.297, 100.916, 98.3, 90.32, 90.09]
sup = [35.869, 39.297, 199.976, 87.087, 90.341, 90.09] # c' = b + 2c, centring removed
e = {"id": "test_cell", "arm": "open", "ref": {"sg": "P 1", "sgno": 1, "cell": sub}}
r = score.judge(e, report("P 1", 1, sup), "")
self.assertEqual((r["verdict"], r["cause"]), ("fail", "lattice_doubled"))
e["ref_alternatives"] = [{"cell": sup, "why": "a stated reason"}]
r = score.judge(e, report("P 1", 1, sup), "")
self.assertEqual((r["verdict"], r["cause"]), ("pass", "accepted_alternative"))
self.assertTrue(r["accepted_alt"].startswith("cell "))
self.assertEqual(r["volume_ratio"], 2.0) # still reported against the deposition
def test_a_justification_is_mandatory(self):
"""An accepted alternative with no reason is a schema error, not a silent pass: this is
the check that keeps the mechanism from becoming a way to launder a failure."""
for alt in ({"sg": "P 31 2 1", "sgno": 152},
{"sg": "P 31 2 1", "sgno": 152, "why": " "}):
with self.assertRaises(ValueError) as cm:
score.judge(entry(ref_alternatives=[alt]), report("P 31 2 1", 152, TRIGONAL), "")
self.assertIn("why", str(cm.exception))
def test_an_alternative_must_replace_something(self):
with self.assertRaises(ValueError):
score.judge(entry(ref_alternatives=[{"why": "a stated reason"}]),
report("P 32", 145, TRIGONAL), "")
def test_the_manifest_rows_are_valid(self):
"""Every committed row's alternatives pass the schema check."""
import json
import os
for name in ("open.json", "inhouse.json"):
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), name)
with open(path) as f:
for e in json.load(f)["sets"]:
score.alternatives(e)
# A screw along an axis the sweep never recorded: an unanswerable question, not a wrong answer.
# The cells are synthetic, and the second is the first cyclically permuted.
ORTHO = [97.0, 108.0, 113.0, 90.0, 90.0, 90.0]
ORTHO_PERMUTED = [113.0, 97.0, 108.0, 90.0, 90.0, 90.0]
class ScrewUndetermined(unittest.TestCase):
def judge(self, ref_cell, undetermined, alternatives):
"""Our P 21 21 21 on ORTHO against a deposited P 21 21 2 on ref_cell."""
e = {"id": "test_screw", "arm": "open",
"ref": {"sg": "P 21 21 2", "sgno": 18, "cell": ref_cell}}
return score.judge(e, report("P 21 21 21", 19, ORTHO,
SPACE_GROUP_SCREW_UNDETERMINED=undetermined,
SPACE_GROUP_ALTERNATIVES=alternatives), "")
def test_same_setting(self):
"""The plain case: the reference is offered verbatim and the open axis is the one they
differ on."""
r = self.judge(ORTHO, "c", "P 21 21 2")
self.assertEqual((r["verdict"], r["cause"]), ("unscored", "screw_undetermined"))
def test_permuted_setting(self):
"""The deposition's axes are a cyclic permutation of ours, so it names the open screw c
while we name it b. The comparison must be made in OUR setting - against the offered
group of the reference's number - or the rule misses a row it covers."""
r = self.judge(ORTHO_PERMUTED, "b", "P 21 2 21")
self.assertEqual((r["verdict"], r["cause"]), ("unscored", "screw_undetermined"))
self.assertIn("the screw along b", r["reason"])
def test_a_wrong_screw_still_fails(self):
"""The open axis is not the axis the two differ on: in our own setting the disagreement
sits on b while the run says it could not determine a, so the b row WAS measured. That is
a wrong answer and stays one - the new framing must not turn every permutation into an
excuse."""
r = self.judge(ORTHO_PERMUTED, "a", "P 21 2 21")
self.assertEqual((r["verdict"], r["cause"]), ("fail", "sym_screw"))
def test_nothing_undetermined_still_fails(self):
"""Every axial row measured: no rule to apply."""
r = self.judge(ORTHO, "NONE", "")
self.assertEqual((r["verdict"], r["cause"]), ("fail", "sym_screw"))
def test_the_reference_must_be_among_the_offered(self):
"""An open axis alone does not excuse a group the run never offered."""
r = self.judge(ORTHO, "b", "P 21 21 21")
self.assertEqual((r["verdict"], r["cause"]), ("fail", "sym_screw"))
# Space groups of one Laue class with identical absences: the merged intensities cannot separate
# them. Synthetic cells.
TETRA_I = [7.4, 7.4, 6.9, 90.0, 90.0, 90.0]
MONO = [11.0, 6.5, 11.0, 90.0, 96.0, 90.0]
ORTHO_SMALL = [8.3, 9.6, 27.0, 90.0, 90.0, 90.0]
class IndistinguishableAlternatives(unittest.TestCase):
def judge(self, arm, ref_sg, ref_sgno, cell, sg, sgno, alternatives=""):
e = {"id": "test_same_absences", "arm": arm,
"ref": {"sg": ref_sg, "sgno": ref_sgno, "cell": cell}}
return score.judge(e, report(sg, sgno, cell, SPACE_GROUP_ALTERNATIVES=alternatives), "")
def test_same_absences_pass_on_both_arms(self):
"""Acentric vs acentric (I41md / I-42d), acentric vs centric (Pc / P2/c, Pna21 / Pnma,
P2 / P2/m): a pass with its own cause, whichever arm, whichever way round."""
cases = [("I -4 2 d", 122, TETRA_I, "I 41 m d", 109),
("P 1 2/c 1", 13, MONO, "P 1 c 1", 7),
("P 1 c 1", 7, MONO, "P 1 2/c 1", 13),
("P n m a", 62, ORTHO_SMALL, "P n a m", 62), # same number: plain match
("P n a 21", 33, ORTHO_SMALL, "P n a m", 62),
("P 1 2 1", 3, MONO, "P 1 2/m 1", 10)]
for arm in ("open", "inhouse"):
for ref_sg, ref_sgno, cell, sg, sgno in cases:
r = self.judge(arm, ref_sg, ref_sgno, cell, sg, sgno)
want = (("pass", None) if sgno == ref_sgno
else ("pass", "indistinguishable_alternative"))
self.assertEqual((r["verdict"], r["cause"]), want, (arm, ref_sg, sg))
def test_the_reason_says_whether_rugnux_listed_it(self):
r = self.judge("inhouse", "I -4 2 d", 122, TETRA_I, "I 41 m d", 109, "I -4 2 d")
self.assertIn("listed in SPACE_GROUP_ALTERNATIVES", r["reason"])
self.assertEqual(r["sg_alternatives"], "I -4 2 d")
r = self.judge("inhouse", "I -4 2 d", 122, TETRA_I, "I 41 m d", 109)
self.assertIn("NOT listed", r["reason"])
def test_different_absences_still_fail(self):
"""Same Laue class, different absences: a real difference, listed or not."""
r = self.judge("open", "P 1 21/c 1", 14, MONO, "P 1 21 1", 4, "P 1 21/c 1")
self.assertEqual((r["verdict"], r["cause"]), ("fail", "sym_under"))
r = self.judge("open", "I -4 2 d", 122, TETRA_I, "I 41 2 2", 98, "I -4 2 d")
self.assertEqual(r["verdict"], "fail")
r = self.judge("inhouse", "I -4 2 d", 122, TETRA_I, "I 4 m m", 107, "I -4 2 d")
self.assertEqual(r["verdict"], "fail")
def test_different_laue_class_still_fails(self):
r = self.judge("open", "P 4 2 2", 89, [50.0, 50.0, 70.0, 90, 90, 90], "P 4", 75, "P 4 2 2")
self.assertEqual((r["verdict"], r["cause"]), ("fail", "sym_under"))
if __name__ == "__main__":
unittest.main()