File size: 2,062 Bytes
fd5366a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2026 CLSA contributors
# SPDX-License-Identifier: MIT
"""Check that every number in the generated tables exists in a results file.

Reads figures/table_values.json (written by figures/tables.py) and verifies each
value against results/*.json. Fails loudly rather than letting an untraceable
number reach the paper.
"""
from __future__ import annotations

import json
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
RESULTS = ROOT / "results"
TABLE_VALUES = ROOT / "figures" / "table_values.json"


def flatten(obj, prefix=""):
    out = {}
    if isinstance(obj, dict):
        for k, v in obj.items():
            out.update(flatten(v, f"{prefix}.{k}" if prefix else str(k)))
    elif isinstance(obj, list):
        for i, v in enumerate(obj):
            out.update(flatten(v, f"{prefix}[{i}]"))
    else:
        out[prefix] = obj
    return out


def main() -> None:
    if not TABLE_VALUES.exists():
        print("no table values file; run figures/tables.py first")
        raise SystemExit(1)
    values = json.loads(TABLE_VALUES.read_text())
    corpus = []
    for path in RESULTS.glob("*.json"):
        corpus.extend(flatten(json.loads(path.read_text())).values())
    numeric = [v for v in corpus if isinstance(v, (int, float)) and not isinstance(v, bool)]
    rounded = {round(float(v), 6) for v in numeric}

    missing = []
    for table, entries in values.items():
        for key, val in entries.items():
            if isinstance(val, (str, bool, dict, list)) or val is None:
                continue
            candidates = {round(float(val), 6), round(float(val), 4), round(float(val), 3), round(float(val), 2)}
            if not (candidates & rounded):
                missing.append((table, key, val))
    if missing:
        print(f"{len(missing)} table values not found in results/:")
        for m in missing[:20]:
            print("  ", m)
        raise SystemExit(1)
    print(f"all {sum(len(v) for v in values.values())} table values trace to results/")


if __name__ == "__main__":
    main()