- apps/atlas: the web page (valley map, route flow, Dragon Register) - packages/core: coding rules and statistics (engine.js) and the shared route graph (graph.js): scene graph, decision-variable graph, bot-to-form crosswalk, Beta credible intervals - packages/ingest: DragonKind bot importer to anonymised trajectories (no prose, no account identifiers); data/bot holds Ken's sample - research/python: independent reference implementation + update_sources.py - tests: JS/Python parity checks and graph/importer unit tests - CI, GitHub Pages deploy, daily check of the community sheets and route PDF - Email addresses in free-text form answers are redacted; the raw spreadsheet is not committed - MIT code licence, CC BY 4.0 data licence Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
38 lines
1.8 KiB
Python
38 lines
1.8 KiB
Python
"""Compare factor tables and models between the JS engine and the Python reference."""
|
|
import json, sys
|
|
js = json.load(open("tests/out/results_js.json"))
|
|
py = json.load(open("tests/out/results_py.json"))
|
|
TOL = 1e-6
|
|
fails = 0
|
|
def close(a, b, rel=TOL):
|
|
if a is None or b is None:
|
|
return a is None and b is None
|
|
return abs(a - b) <= rel * max(1, abs(a), abs(b))
|
|
for key, ft in py["factors"].items():
|
|
jrows = {(r["factor"], r["level"]): r for r in js["factors"][key]["rows"]}
|
|
for r in ft["rows"]:
|
|
j = jrows.get((r["factor"], r["level"]))
|
|
if j is None:
|
|
print("missing in JS:", key, r["factor"], r["level"]); fails += 1; continue
|
|
for f in ("k", "n", "p", "lo", "hi", "or", "or_lo", "or_hi", "fisher_p", "q"):
|
|
if (f in r) != (f in j) or (f in r and not close(r[f], j[f], 1e-5)):
|
|
print("diff", key, r["factor"], r["level"], f, r.get(f), j.get(f)); fails += 1
|
|
for key, m in py["models"].items():
|
|
jm = js["models"][key]
|
|
if m["n"] != jm["n"] or m["events"] != jm["events"]:
|
|
print("model size diff", key); fails += 1
|
|
jt = {t["term"]: t for t in jm["terms"]}
|
|
for t in m["terms"]:
|
|
j = jt.get(t["term"])
|
|
if not j:
|
|
print("term missing", key, t["term"]); fails += 1; continue
|
|
for f in ("or", "lo", "hi"):
|
|
if not close(t[f], j[f], 1e-4):
|
|
print("model diff", key, t["term"], f, t[f], j[f]); fails += 1
|
|
if abs(t["p"] - j["p"]) > 1e-6:
|
|
print("model p diff", key, t["term"], t["p"], j["p"]); fails += 1
|
|
n_rows = sum(len(f["rows"]) for f in py["factors"].values())
|
|
n_terms = sum(len(m["terms"]) for m in py["models"].values())
|
|
print(f"checked {n_rows} factor rows and {n_terms} model terms: {'ALL MATCH' if not fails else str(fails) + ' differences'}")
|
|
sys.exit(1 if fails else 0)
|