clsa-code / experiments /check_numbers.py
fahadhafeezofficial's picture
Upload folder using huggingface_hub
fd5366a verified
Raw History Blame Contribute Delete
2.06 kB
# 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()