"""Reliability-weighted evidence for the HMM (improvement plan, decision stage; no retraining).

Idea: the evidence d of a 1-s window cannot be informative when the two envelopes are silent or alike in that
window, whatever the decoder. Per window we compute, from the two stimulus envelopes of the SAME 1-s window
(no extra lookahead, available at test time):
    sep = SD(e0 - e1)                      (small when both are silent or when they are similar)
    act = (SD(e0) + SD(e1)) / 2            (small in pauses)
On the other 4 datasets (true labels, no synthetic switches) the signed evidence d * sign is binned by these
features and each bin gets its own emission (mu_b >= 0, sd_b). The HMM log-likelihood ratio of a window is then
2 mu_b d / sd_b^2 (heteroscedastic emissions) instead of one global (mu, sd).
Schemes: none (= score.py), sep (10 quantile bins of sep), sep x act (5 x 5 quantile bins).
Each scheme x (p_stay, temp) grid, GPU batch filter, official scorer; nested selection (other 4 datasets).

usage: python reliability.py EXP [EXP ...]   -> results/research/reliability_<exp>.{csv,json}, logs/reliability.log
"""
import itertools
import json
import os
import pickle
import sys
import time

import numpy as np
import pandas as pd
import torch
from joblib import Parallel, delayed

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from common_exp import CFG, EXP, LAB, fold_file, load_trial, rows_of  # noqa: E402
from common import DS_KEYS  # noqa: E402
from integrators import prepare, score_config  # noqa: E402

DEV = torch.device("cuda" if torch.cuda.is_available() else "cpu")
FS = CFG["fs"]
W, H = int(CFG["evidence"]["win_s"] * FS), int(CFG["evidence"]["hop_s"] * FS)
LOG = open(os.path.join(EXP, "logs", "reliability.log"), "a", buffering=1)
GRID = [dict(p_stay=p, temp=t) for p, t in itertools.product((0.995, 0.997, 0.998, 0.999), (0.25, 0.5, 1.0, 1.5))]
SCHEMES = ["none", "sep", "sep_act"]


def log(m):
    line = f"{time.strftime('%F %T')} {m}"
    print(line, flush=True)
    LOG.write(line + "\n")


def win_sd(x):
    """SD over the evidence windows (same starts as common_exp.sliding_corr)."""
    n = len(x)
    st = np.arange(0, n - W + 1, H)
    c1 = np.r_[0, np.cumsum(x)]
    c2 = np.r_[0, np.cumsum(x * x)]
    s1, s2 = c1[st + W] - c1[st], c2[st + W] - c2[st]
    return np.sqrt(np.maximum(s2 / W - (s1 / W) ** 2, 0.0))


def features(exp, prep):
    """Per trial: (sep, act, signed d) on the ORIGINAL evidence windows (true labels)."""
    out = {}
    for ds in DS_KEYS:
        ev = pickle.load(open(fold_file(exp, ds), "rb"))["evidence"]["causal"]
        for _, r in rows_of(ds).iterrows():
            e = load_trial(prep, r["key"])["env"].astype(np.float64)
            k = ev[r["key"]]
            n = len(k["r0"])
            sep, act = win_sd(e[:, 0] - e[:, 1])[:n], 0.5 * (win_sd(e[:, 0]) + win_sd(e[:, 1]))[:n]
            assert len(sep) == n, (r["key"], len(sep), n)
            sd = (k["r0"] - k["r1"]) * np.where(k["att"] == 0, 1.0, -1.0)
            out[r["key"]] = dict(ds=ds, subj=r["subj_uid"], sep=sep, act=act, signed=sd.astype(np.float64))
    return out


def fit_bins(F, dss, scheme):
    rows = [v for v in F.values() if v["ds"] in dss]
    sep = np.concatenate([v["sep"] for v in rows])
    act = np.concatenate([v["act"] for v in rows])
    y = np.concatenate([v["signed"] for v in rows])
    if scheme == "none":
        return dict(scheme=scheme, mu=np.array([max(y.mean(), 1e-4)]), sd=np.array([y.std()]))
    qs = np.quantile(sep, np.linspace(0, 1, 11 if scheme == "sep" else 6)[1:-1])
    qa = np.quantile(act, np.linspace(0, 1, 6)[1:-1]) if scheme == "sep_act" else np.zeros(0)
    b = np.digitize(sep, qs) * (len(qa) + 1) + np.digitize(act, qa)
    nb = (len(qs) + 1) * (len(qa) + 1)
    mu = np.array([y[b == i].mean() if (b == i).sum() > 50 else y.mean() for i in range(nb)])
    sd = np.array([y[b == i].std() if (b == i).sum() > 50 else y.std() for i in range(nb)])
    return dict(scheme=scheme, qs=qs, qa=qa, mu=np.maximum(mu, 1e-4), sd=sd)


def emis(bins, sep, act):
    if bins["scheme"] == "none":
        return np.full(len(sep), bins["mu"][0]), np.full(len(sep), bins["sd"][0])
    b = np.digitize(sep, bins["qs"]) * (len(bins["qa"]) + 1) + np.digitize(act, bins["qa"])
    return bins["mu"][b], bins["sd"][b]


@torch.no_grad()
def run_gpu(dsd, feats_by_subj, bins):
    subs = list(dsd["subj"])
    seqs, mus, vs, starts = [], [], [], []
    for s in subs:
        trs, fs = dsd["subj"][s], feats_by_subj[s]
        m_, s_ = zip(*[emis(bins, f["sep"], f["act"]) for f in fs])
        seqs.append(np.concatenate([t["d"] for t in trs]))
        mus.append(np.concatenate(m_))
        vs.append(np.concatenate(s_) ** 2)
        starts.append(np.cumsum([0] + [len(t["d"]) for t in trs][:-1]))
    Sn, Tm, K = len(subs), max(len(x) for x in seqs), len(GRID)
    T_ = lambda arrs, fill: torch.stack([torch.from_numpy(np.r_[a, np.full(Tm - len(a), fill)]) for a in arrs]).to(DEV)
    D, MU, V = T_(seqs, 0.0), T_(mus, 1e-4), T_(vs, 1.0)
    M = T_([np.ones(len(a)) for a in seqs], 0.0) > 0
    B = torch.zeros(Sn, Tm, device=DEV, dtype=torch.bool)
    for i, st in enumerate(starts):
        B[i, torch.from_numpy(st.astype(np.int64))] = True
    ps = torch.tensor([g["p_stay"] for g in GRID], device=DEV, dtype=torch.float64)[:, None]
    temp = torch.tensor([g["temp"] for g in GRID], device=DEV, dtype=torch.float64)[:, None]
    ls, lsw = torch.log(ps), torch.log1p(-ps)
    lp = torch.full((K, Sn, 2), np.log(0.5), device=DEV, dtype=torch.float64)
    out = torch.zeros(K, Sn, Tm, device=DEV, dtype=torch.bool)
    for t in range(Tm):
        a0 = torch.logaddexp(lp[..., 0] + ls, lp[..., 1] + lsw)
        a1 = torch.logaddexp(lp[..., 1] + ls, lp[..., 0] + lsw)
        b = B[:, t][None]
        a0 = torch.where(b, torch.full_like(a0, np.log(0.5)), a0)
        a1 = torch.where(b, torch.full_like(a1, np.log(0.5)), a1)
        llr = temp * 2.0 * MU[:, t] * D[:, t] / V[:, t]
        n0, n1 = a0 + 0.5 * llr, a1 - 0.5 * llr
        z = torch.logaddexp(n0, n1)
        lp = torch.where(M[:, t][None, :, None], torch.stack([n0 - z, n1 - z], -1), lp)
        out[:, :, t] = lp[..., 1] > lp[..., 0]
    out = out.cpu().numpy()
    return {s: [out[:, i, st:st + len(tr["d"])] for st, tr in zip(starts[i], dsd["subj"][s])] for i, s in enumerate(subs)}


def main(exp):
    js = json.load(open(os.path.join(EXP, "results", f"{exp}.json")))
    L = js["causal"]["info"]["AVGC"]["lookahead_s"]
    log(f"=== reliability {exp}: schemes {SCHEMES} x {len(GRID)} HMM configs")
    t0 = time.time()
    data = prepare(exp)
    F = features(exp, js["prep"])
    rows = []
    for ds in DS_KEYS:
        fbs = {}
        for _, r in rows_of(ds).iterrows():
            fbs.setdefault(r["subj_uid"], []).append(F[r["key"]])
        for sc in SCHEMES:
            bins = fit_bins(F, [d for d in DS_KEYS if d != ds], sc)
            dec = run_gpu(data[ds], fbs, bins)
            res = Parallel(n_jobs=int(os.environ.get("SCORE_JOBS", 4)))(delayed(score_config)(k, dec, data[ds]["subj"], L) for k in range(len(GRID)))
            rows += [dict(ds=ds, scheme=sc, **GRID[k], **r) for k, r in enumerate(res)]
            if sc != "none":
                snr = bins["mu"] / bins["sd"]
                log(f"  {LAB[ds]} {sc}: per-bin SNR min {snr.min():.3f} median {np.median(snr):.3f} max {snr.max():.3f}")
        log(f"  {LAB[ds]} done ({time.time() - t0:.0f}s)")
    R = pd.DataFrame(rows)
    os.makedirs(os.path.join(EXP, "results", "research"), exist_ok=True)
    R.to_csv(os.path.join(EXP, "results", "research", f"reliability_{exp}.csv"), index=False)
    R["cfg"] = R.scheme + "|" + R.p_stay.astype(str) + "|" + R.temp.astype(str)
    out = {"experiment": exp, "window_hmm_mean": {m: js["causal"]["mean"][m] for m in ("Acc", "SDT", "L", "H")}}
    for name, q in [("none", R.scheme == "none"), ("sep", R.scheme == "sep"), ("sep_act", R.scheme == "sep_act"),
                    ("any", R.scheme.notna())]:
        pv = R[q].pivot_table(index="cfg", columns="ds", values=["Acc", "SDT", "L", "H"])
        per = {}
        for ds in DS_KEYS:
            c = pv["H"][[d for d in DS_KEYS if d != ds]].mean(1).idxmax()
            per[ds] = {m: float(pv[m].loc[c, ds]) for m in ("Acc", "SDT", "L", "H")} | {"cfg": c}
        out[f"nested_{name}"] = {m: float(np.mean([per[d][m] for d in DS_KEYS])) for m in ("Acc", "SDT", "L", "H")}
        out[f"per_dataset_{name}"] = per
        log(f"  nested {name:8s}: " + "  ".join(f"{m} {out[f'nested_{name}'][m]:.3f}" for m in ("Acc", "SDT", "L", "H")))
    json.dump(out, open(os.path.join(EXP, "results", "research", f"reliability_{exp}.json"), "w"), indent=1, default=float)
    log(f"=== done {exp} in {time.time() - t0:.0f}s")


if __name__ == "__main__":
    for e in sys.argv[1:]:
        if not os.path.exists(os.path.join(EXP, "results", "research", f"reliability_{e}.json")):
            main(e)
