File size: 4,956 Bytes
0e6d825 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | """Check that every published number of this benchmark comes from the stored files.
python score.py check [--results FILE] # every number recomputed from predictions/ and compared
python score.py card [--write] # the results table of README.md, from the stored outputs
check: every file against the SHA-256 in manifest.json (the scorer, the test files, the stored outputs); then every
score of every system in results/expected.json recomputed from predictions/ with the scorer of benchmark 1.0
(bench.py and scoring/, unchanged) and compared cell by cell; then the table in README.md rendered again from the
same outputs. --results compares with another results file instead of results/expected.json, such as the
results.json of benchmark 1.0 (its systems that are in this folder). The first run builds the test files that may not
be shared (build_testsets.py), once.
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
import json # noqa: E402
import bench # noqa: E402 (the scorer of benchmark 1.0, unchanged)
import build_testsets # noqa: E402
from benchmark import TASK, card_table, card_with_table, expected, manifest, scores_of # noqa: E402
def cells(d, path=()):
"""Every number of a nested score dict, as (path, value)."""
if isinstance(d, dict):
for k, v in d.items():
yield from cells(v, path + (k,))
else:
yield path, d
def check(results: Path | None) -> bool:
build_testsets.prepare()
files_ok = bench.check() # prints "N of N files match manifest.json"
if results is None:
want = expected()["scores"]
source = "results/expected.json"
else:
r = json.loads(Path(results).read_text(encoding="utf-8"))
want = r["tasks"][TASK]["scores"] if "tasks" in r else r["scores"]
source = str(results)
names = [n for n in manifest()["systems"] if (HERE / "predictions" / n).is_dir()]
n_cells, bad, absent = 0, [], []
try:
sc = bench.scorer(TASK) # reads every gold file
except (Exception, SystemExit) as e:
bad.append(f"the gold files cannot be read: {str(e)[:160]}")
names = []
for n in names:
if n not in want:
if results is None:
bad.append(f"{n}: not in {source}")
else: # a system added after that results file: checked against results/expected.json only
absent.append(n)
continue
try:
got = dict(cells(scores_of(HERE / "predictions" / n, sc)))
except (Exception, SystemExit) as e: # a changed gold or output file that no longer reads
bad.append(f"{n}: cannot be scored: {str(e)[:160]}")
continue
exp = dict(cells(want[n]))
for k in sorted(set(got) | set(exp)):
n_cells += 1
if got.get(k) != exp.get(k):
bad.append(f"{n} {'/'.join(k)}: {got.get(k)} recomputed, {exp.get(k)} in {source}")
missing = [n for n in expected()["scores"] if n not in names]
if missing:
bad.append(f"systems in results/expected.json without outputs in predictions/: {missing}")
print(f"{len(names) - len(absent)} systems, {n_cells} numbers recomputed from predictions/ and compared with "
f"{source}" + (f"; {len(absent)} systems are not in it (python score.py check compares them)" if absent else ""))
card = (HERE / "README.md").read_text(encoding="utf-8")
try:
same = card_with_table(card) == card
except (Exception, SystemExit) as e:
same, bad = False, bad + [f"the table in README.md cannot be rendered: {str(e)[:160]}"]
if same:
print("the table in README.md is the one rendered from the stored outputs")
else:
bad.append("the table in README.md differs from the stored outputs (python score.py card --write)")
if not files_ok:
bad.append("files differ from manifest.json (listed above)")
print("CHECK:", "every number equals the published one" if not bad
else f"{len(bad)} problems" + "".join(f"\n {b}" for b in bad))
return not bad
def main() -> None:
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("command", choices=("check", "card"))
ap.add_argument("--results", type=Path, default=None, help="compare with this results file (check)")
ap.add_argument("--write", action="store_true", help="write the table into README.md (card)")
a = ap.parse_args()
if a.command == "check":
sys.exit(0 if check(a.results) else 1)
build_testsets.prepare()
if a.write:
path = HERE / "README.md"
path.write_text(card_with_table(path.read_text(encoding="utf-8")), encoding="utf-8")
print(f"wrote the table into {path.name}")
else:
print(card_table())
if __name__ == "__main__":
main()
|