Refactor: lay the repo out by section (core, collection, analysis, planner, site) (#8)
Moves only; no behaviour changes. Every file now lives under the part of the project it belongs to, each section has a README, and docs/PRIVACY.md collects the rules all parts follow. core/ engine.js, graph.js, solver.js, python/clean.py collection/ sources/ (form, register, PDF check, daily update), bot/ (importer) analysis/ stats/ (Python statistics), network/ (build-graph.js), reports/ (POLICY.md) planner/ build-policy.js, policy-report.js site/ atlas/ (the page), build.js, qa/ tests/ core/ (unit), parity/ (JS vs Python), fixtures/ Checked against the previous layout: tests pass; coded runs, test outputs, graph.json and policy.json are byte-identical; the built site differs only in comments that name file paths. The published file names Ken's bot downloads (solver.js, graph.js, data/policy.json) are unchanged. Also removes the undefined/ folder of QA screenshots committed by mistake (the QA scripts now default to a temp folder) and test.txt, and drops the unused npm workspaces. Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
dcd5117373
commit
432e04c2c7
@@ -0,0 +1,145 @@
|
||||
"""
|
||||
analyze.py — Python reference implementation of the statistics shown on the route-map page.
|
||||
|
||||
python analyze.py ../../data/derived/runs_clean.csv ../../tests/out/results_py.json
|
||||
|
||||
It mirrors analyse() in core/engine.js (same denominators, same tests, same model); tests/parity/compare_results.py
|
||||
checks that both produce the same numbers. Edit either, then re-run the comparison.
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from stats_core import Z95, bh, logistic_ridge, odds_ratio, p_from_z, wilson
|
||||
|
||||
COLORS = ["Red", "Green", "Orange", "Brown", "Blue", "Black"]
|
||||
# answers with no information about the choice: treated as missing everywhere (mirrors MISSING in engine.js)
|
||||
MISSING = {"Unknown", "Unsure", "Unsure/unknown", "Multiple", "(not recorded)", "(not asked)",
|
||||
"(not asked/blank)", "Unknown/lost count", "Blank/not asked"}
|
||||
|
||||
|
||||
def is_missing(v):
|
||||
return v is None or (isinstance(v, float) and v != v) or v == "" or v in MISSING
|
||||
FACTORS = ["company", "direction", "first_sighting", "scream", "resolution", "combat_first",
|
||||
"post_path", "hour", "wing", "squad", "attempt_group", "source", "goal", "sighting_action", "combat_second", "malek_intent"]
|
||||
|
||||
|
||||
def W(k, n):
|
||||
p, lo, hi = wilson(k, n)
|
||||
return {"k": int(k), "n": int(n), "p": p, "lo": lo, "hi": hi}
|
||||
|
||||
|
||||
def outcomes():
|
||||
o = {
|
||||
"blue_meadow": (lambda d: d.branch == "Passed the Red: meadow", lambda d: d.saw_blue_meadow),
|
||||
"shadow": (lambda d: (d.hour == "Tree") & d.branch.notna(), lambda d: d.saw_shadow),
|
||||
"bond_blue_meadow": (lambda d: (d.branch == "Passed the Red: meadow") & d.enc_Blue.isin(["Bonded!", "Incinerated"]),
|
||||
lambda d: d.enc_Blue == "Bonded!"),
|
||||
"reach_hour": (lambda d: pd.Series(True, index=d.index), lambda d: d.hour.isin(["Tree", "Valley"])),
|
||||
}
|
||||
for c in COLORS:
|
||||
o["bonded_" + c] = (lambda d: d.hour.isin(["Tree", "Valley"]),
|
||||
(lambda c: lambda d: (d.outcome == "Bonded") & (d.outcome_color == c))(c))
|
||||
return o
|
||||
|
||||
|
||||
OUT = outcomes()
|
||||
|
||||
|
||||
def factor_table(d, key, min_n=5):
|
||||
den, ev = OUT[key]
|
||||
D = d[den(d)].copy()
|
||||
D["_e"] = ev(D).astype(bool)
|
||||
rows = []
|
||||
for f in FACTORS:
|
||||
sub = D[D[f].notna() & ~D[f].isin(MISSING)]
|
||||
nf, ef = len(sub), int(sub._e.sum())
|
||||
for lv in sorted(sub[f].astype(str).unique()):
|
||||
g = sub[sub[f].astype(str) == lv]
|
||||
k, n = int(g._e.sum()), len(g)
|
||||
row = {"factor": f, "level": lv, **W(k, n)}
|
||||
rk, rn = ef - k, nf - n
|
||||
if n >= min_n and rn >= min_n and 0 < ef < nf:
|
||||
or_, lo, hi, p, corr = odds_ratio(k, n - k, rk, rn - rk)
|
||||
row.update({"or": or_, "or_lo": lo, "or_hi": hi, "fisher_p": p, "corrected": corr})
|
||||
rows.append(row)
|
||||
tested = [r for r in rows if "fisher_p" in r]
|
||||
for r, q in zip(tested, bh([r["fisher_p"] for r in tested])):
|
||||
r["q"] = q
|
||||
return {"outcome": key, "overall": W(int(D._e.sum()), len(D)), "rows": rows}
|
||||
|
||||
|
||||
def model(d, key, factors, lam=1.0):
|
||||
den, ev = OUT[key]
|
||||
D = d[den(d)].copy()
|
||||
y = ev(D).astype(int).values
|
||||
F = pd.DataFrame({name: D.apply(fn, axis=1) for name, fn in factors.items()}, index=D.index)
|
||||
keep = F.notna().all(axis=1).values
|
||||
F, y = F[keep], y[keep]
|
||||
cols, names = [np.ones(len(F))], []
|
||||
refs = {}
|
||||
for f in factors:
|
||||
counts = F[f].value_counts()
|
||||
levels = sorted(counts.index, key=lambda lv: (-counts[lv], lv))
|
||||
refs[f] = levels[0]
|
||||
for lv in levels[1:]:
|
||||
cols.append((F[f] == lv).astype(float).values)
|
||||
names.append((f, lv, int(counts[lv])))
|
||||
X = np.column_stack(cols)
|
||||
b, se = logistic_ridge(X, y, lam)
|
||||
terms = [{"term": f"{f} = {lv}", "factor": f, "level": lv, "n": n, "or": float(np.exp(b[i + 1])),
|
||||
"lo": float(np.exp(b[i + 1] - Z95 * se[i + 1])), "hi": float(np.exp(b[i + 1] + Z95 * se[i + 1])),
|
||||
"p": float(p_from_z(b[i + 1] / se[i + 1]))} for i, (f, lv, n) in enumerate(names)]
|
||||
return {"outcome": key, "n": int(len(y)), "events": int(y.sum()), "refs": refs, "lambda": lam, "terms": terms}
|
||||
|
||||
|
||||
def goal2(r):
|
||||
if r.goal in ("Blue", "Black", "Blue or Black"):
|
||||
return r.goal
|
||||
return "Blank/not asked" if r.goal == "(not asked/blank)" else "Other colour/any"
|
||||
|
||||
|
||||
def src2(r):
|
||||
return "Form submission" if r.source == "Form" else "Bulk log (import)"
|
||||
|
||||
|
||||
def first2(r):
|
||||
return r.first_sighting if r.first_sighting in ("Red", "Brown", "Orange", "Green") else None
|
||||
|
||||
|
||||
def known(f):
|
||||
return lambda r: None if is_missing(r[f]) else r[f]
|
||||
|
||||
|
||||
def period(r):
|
||||
if not isinstance(r.date, str):
|
||||
return "No date (import)"
|
||||
return "Oct 4–10" if r.date < "2026-10-11" else "Oct 11 onward"
|
||||
|
||||
|
||||
def analyse(d):
|
||||
res = {"n": len(d), "factors": {k: factor_table(d, k) for k in OUT}}
|
||||
res["models"] = {
|
||||
"blue_meadow": model(d, "blue_meadow", {
|
||||
"source": src2, "goal": goal2, "direction": known("direction"), "first_sighting": first2,
|
||||
"resolution": known("resolution"), "company": known("company")}),
|
||||
"shadow": model(d, "shadow", {
|
||||
"source": src2, "goal": goal2, "attempt": known("attempt_group"),
|
||||
"direction": known("direction")}),
|
||||
"bond_blue_meadow": model(d, "bond_blue_meadow", {
|
||||
"source": src2, "goal": goal2, "direction": known("direction"), "resolution": known("resolution")}),
|
||||
}
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
src = sys.argv[1] if len(sys.argv) > 1 else "../../data/derived/runs_clean.csv"
|
||||
out = sys.argv[2] if len(sys.argv) > 2 else "../../tests/out/results_py.json"
|
||||
d = pd.read_csv(src, keep_default_na=False, na_values=[""])
|
||||
for c in ("saw_blue_meadow", "saw_shadow", "rescued"):
|
||||
d[c] = d[c].astype(str) == "True"
|
||||
res = analyse(d)
|
||||
json.dump(res, open(out, "w"), indent=1, default=float)
|
||||
print("wrote", out)
|
||||
@@ -0,0 +1,98 @@
|
||||
"""Small, dependency-light statistics used by the analysis (mirrored in core/engine.js)."""
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
from scipy.stats import fisher_exact, norm
|
||||
|
||||
Z95 = 1.959963984540054
|
||||
|
||||
|
||||
def wilson(k, n, z=Z95):
|
||||
"""Wilson score 95% interval for a proportion k/n. Returns (p, lo, hi); None when n == 0."""
|
||||
if n == 0:
|
||||
return (None, None, None)
|
||||
p = k / n
|
||||
d = 1 + z * z / n
|
||||
c = (p + z * z / (2 * n)) / d
|
||||
h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / d
|
||||
return (p, max(0.0, c - h), min(1.0, c + h))
|
||||
|
||||
|
||||
def odds_ratio(a, b, c, d):
|
||||
"""2x2 table [[a, b], [c, d]] = [[exposed&event, exposed&no-event], [unexposed&event, unexposed&no-event]].
|
||||
Returns OR, 95% CI (Woolf log method with Haldane-Anscombe +0.5 correction when any cell is 0),
|
||||
and the two-sided Fisher exact p-value (computed on the uncorrected table)."""
|
||||
corr = 0.5 if 0 in (a, b, c, d) else 0.0
|
||||
A, B, C, D = a + corr, b + corr, c + corr, d + corr
|
||||
or_ = (A * D) / (B * C)
|
||||
se = math.sqrt(1 / A + 1 / B + 1 / C + 1 / D)
|
||||
lo, hi = math.exp(math.log(or_) - Z95 * se), math.exp(math.log(or_) + Z95 * se)
|
||||
p = fisher_exact([[a, b], [c, d]], alternative="two-sided")[1]
|
||||
return or_, lo, hi, p, corr > 0
|
||||
|
||||
|
||||
def bh(pvals):
|
||||
"""Benjamini-Hochberg false-discovery-rate adjusted q-values."""
|
||||
m = len(pvals)
|
||||
if m == 0:
|
||||
return []
|
||||
order = sorted(range(m), key=lambda i: pvals[i])
|
||||
q = [0.0] * m
|
||||
prev = 1.0
|
||||
for rank in range(m, 0, -1):
|
||||
i = order[rank - 1]
|
||||
prev = min(prev, pvals[i] * m / rank)
|
||||
q[i] = prev
|
||||
return q
|
||||
|
||||
|
||||
def logistic_ridge(X, y, lam=1.0, iters=100, tol=1e-10):
|
||||
"""Logistic regression fitted by Newton-Raphson (IRLS) with a small L2 penalty `lam`
|
||||
on every coefficient except the intercept (column 0). The penalty keeps estimates finite
|
||||
when a category has 0 events (complete separation). Returns beta and Wald standard errors
|
||||
from the penalised information matrix."""
|
||||
X = np.asarray(X, float)
|
||||
y = np.asarray(y, float)
|
||||
n, k = X.shape
|
||||
beta = np.zeros(k)
|
||||
P = np.eye(k) * lam
|
||||
P[0, 0] = 0
|
||||
for _ in range(iters):
|
||||
eta = X @ beta
|
||||
mu = 1 / (1 + np.exp(-eta))
|
||||
W = mu * (1 - mu)
|
||||
grad = X.T @ (y - mu) - P @ beta
|
||||
H = (X * W[:, None]).T @ X + P
|
||||
step = np.linalg.solve(H, grad)
|
||||
beta += step
|
||||
if np.max(np.abs(step)) < tol:
|
||||
break
|
||||
mu = 1 / (1 + np.exp(-(X @ beta)))
|
||||
H = (X * (mu * (1 - mu))[:, None]).T @ X + P
|
||||
se = np.sqrt(np.diag(np.linalg.inv(H)))
|
||||
return beta, se
|
||||
|
||||
|
||||
def p_from_z(z):
|
||||
return 2 * (1 - norm.cdf(abs(z)))
|
||||
|
||||
|
||||
def mantel_haenszel(tables):
|
||||
"""Mantel-Haenszel common OR over 2x2 tables [a, b, c, d], Robins-Breslow-Greenland 95% CI,
|
||||
and the CMH chi-square p-value (1 df, continuity-corrected). Mirrors mantelHaenszel() in engine.js."""
|
||||
R = S = PR = PSQR = QS = sA = sE = sV = 0.0
|
||||
for a, b, c, d in tables:
|
||||
n = a + b + c + d
|
||||
if n < 2:
|
||||
continue
|
||||
r, s_ = a * d / n, b * c / n
|
||||
P, Q = (a + d) / n, (b + c) / n
|
||||
R += r; S += s_; PR += P * r; PSQR += P * s_ + Q * r; QS += Q * s_
|
||||
m1, m0, n1, n0 = a + b, c + d, a + c, b + d
|
||||
sA += a; sE += m1 * n1 / n; sV += m1 * m0 * n1 * n0 / (n * n * (n - 1))
|
||||
if not R or not S:
|
||||
return None
|
||||
or_ = R / S
|
||||
se = math.sqrt(PR / (2 * R * R) + PSQR / (2 * R * S) + QS / (2 * S * S))
|
||||
chi = max(0.0, abs(sA - sE) - 0.5) ** 2 / sV if sV > 0 else 0.0
|
||||
return or_, math.exp(math.log(or_) - Z95 * se), math.exp(math.log(or_) + Z95 * se), p_from_z(math.sqrt(chi))
|
||||
Reference in New Issue
Block a user