"""Registered scorer (protocol-births-2013.json). Usage, from experiment/births:
  births_fresh_score.py <train w2011.csv> <test w2013.csv> <voice-2013.jsonl> > results-births-2013.json
The voice tuned on NSFG 2011-2013, on NSFG 2013-2015. Ranking: AUC of the voice and of age alone
(logistic in age and its square, fitted on 2011-2013), with a bootstrap interval of the difference.
Level, set once (births_level.py): age alone's probabilities carried by national age-specific birth
rates; the voice shifted on the log-odds scale to that mean, against the observed share and its 95%
interval. Reported beside: the regression on all facts, the voice's own mean, and the earlier rule
(the voice's own mean times the general fertility rate ratio)."""
import sys, os, json, hashlib, numpy as np, pandas as pd
here = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, here)
from births_level import level, period_ratio, shift_to
# NCHS general fertility rate (births per 1,000 women 15-44), NVSR 66(1) table 1, for the earlier rule only:
# windows by interview year, 2011-2013 (2008-2010, 2009-2011, 2010-2012) and 2013-2015 (2010-2012, 2011-2013, 2012-2014)
GFR = {2008: 68.1, 2009: 66.2, 2010: 64.1, 2011: 63.2, 2012: 63.0, 2013: 62.5, 2014: 62.9}
win = lambda s: np.mean([GFR[s + k] for k in range(3)])
GFR_RATIO = np.mean([win(s) for s in (2010, 2011, 2012)]) / np.mean([win(s) for s in (2008, 2009, 2010)])
args = sys.argv[1:]; sys.argv = [sys.argv[0], args[0]]
exec(open(os.path.join(here, "births_dev.py")).read().split("idx = rng.permutation")[0])  # design, fit, auc
tr, te = pd.read_csv(args[0], index_col=0), pd.read_csv(args[1], index_col=0)
v = {json.loads(l)["caseid"]: json.loads(l)["p"] for l in open(args[2])}
y = te.birth.to_numpy(); sv = np.array([v[i] for i in te.index])
pa = 1 / (1 + np.exp(-design(te, False) @ fit(design(tr, False), tr.birth.to_numpy())))
pf = 1 / (1 + np.exp(-design(te, True) @ fit(design(tr, True), tr.birth.to_numpy())))
L = level(pa, te, tr); cal = shift_to(sv, L)
rng = np.random.default_rng(20261011); B = [rng.integers(0, len(y), len(y)) for _ in range(2000)]
ci = lambda a, b: [round(float(x), 4) for x in np.quantile([auc(a[i], y[i]) - auc(b[i], y[i]) for i in B], [.025, .975])]
oci = [round(float(x), 4) for x in np.quantile([y[i].mean() for i in B], [.025, .975])]
res = {"protocolSha256": hashlib.sha256(open(os.path.join(here, "protocol-births-2013.json"), "rb").read()).hexdigest(),
       "women": int(len(y)), "births": int(y.sum()),
       "auc": {"voice": round(auc(cal, y), 4), "age": round(auc(pa, y), 4), "facts": round(auc(pf, y), 4)},
       "voiceMinusAge95": ci(cal, pa), "voiceMinusFacts95": ci(cal, pf), "factsMinusAge95": ci(pf, pa),
       "level": {"observed": round(float(y.mean()), 4), "observed95": oci, "voice": round(float(cal.mean()), 4),
                 "ageAlone": round(float(pa.mean()), 4), "periodRatio": round(float(period_ratio(te, tr).mean()), 4),
                 "voiceOwnMean": round(float(sv.mean()), 4), "factsMean": round(float(pf.mean()), 4),
                 "earlierRule": round(float(sv.mean() * GFR_RATIO), 4), "gfrRatio": round(float(GFR_RATIO), 4)}}
res["beatsAge"] = bool(res["voiceMinusAge95"][0] > 0)
res["levelInside"] = bool(oci[0] <= cal.mean() <= oci[1])
res["primary_met"] = res["beatsAge"] and res["levelInside"]
print(json.dumps(res, indent=1))
