"""Classifier-only ensemble of the four direct-classifier seeds. No reconstruction decoder.

For each held-out dataset the causal evidence of every seed is d = r0 - r1 (the classifier logit;
the folds already applied the synthetic switch to the classifier input). Two combinations, both
fit without the held-out labels:

  equal   arithmetic mean of the four d
  invvar  weighted mean, weight of a seed = 1 / variance of its signed evidence on the OTHER
          four datasets only (signed by those datasets' attention labels, same convention as
          the HMM emission fit). Weights are normalised to sum to 1.

The combined stream is scored by score.score_dataset at p_stay in {0.997, 0.998, 0.9997}.
That call keeps the scorer's synthetic-switch schedule; evidence is flagged pre_switched so the
schedule is not applied a second time. Emissions (mu, sd) are still fit inside the scorer on
the other four datasets.

usage: python ensemble_clf.py
writes results/research/ensemble_clf.csv
"""
import os

import numpy as np
import pandas as pd

import score as S
from common_exp import EXP
from common import DS_KEYS

SEEDS = [
    (0, "direct_clf__eaP_n5_t60_w10b3_ptSPK_aug-sw2"),
    (1, "direct_clf__eaP_n5_t60_w10b3_ptSPK_s1_aug-sw2"),
    (2, "direct_clf__eaP_n5_t60_w10b3_ptSPK_s2_aug-sw2"),
    (3, "direct_clf__eaP_n5_t60_w10b3_ptSPK_s3_aug-sw2"),
]
P_STAY = (0.997, 0.998, 0.9997)
OUT = os.path.join(EXP, "results", "research", "ensemble_clf.csv")


def delta(e):
    return (e["r0"] - e["r1"]).astype(np.float64)


def signed_variance(ev, datasets):
    """Variance of label-signed (r0 - r1) pooled over `datasets` (no held-out trials)."""
    parts = []
    for d in datasets:
        for e in ev[d].values():
            parts.append(delta(e) * np.where(e["att"] == 0, 1.0, -1.0))
    return float(np.concatenate(parts).var())


def invvar_weights(evs, held):
    train = [d for d in DS_KEYS if d != held]
    w = np.array([1.0 / max(signed_variance(ev, train), 1e-12) for ev in evs], dtype=np.float64)
    return w / w.sum()


def combine(evs, w):
    """r0 = weighted (r0-r1), r1 = 0, so the scorer's (r0-r1) is the combination. pre_switched kept."""
    ref = evs[0]
    out = {}
    for d in DS_KEYS:
        out[d] = {}
        keys = ref[d].keys()
        for ev in evs[1:]:
            if ev[d].keys() != keys:
                raise RuntimeError(f"trial keys differ on {d}")
        for k, e0 in ref[d].items():
            acc = None
            for wi, ev in zip(w, evs):
                di = delta(ev[d][k])
                if di.shape != delta(e0).shape:
                    raise RuntimeError(f"evidence length differs for {d}/{k}")
                acc = wi * di if acc is None else acc + wi * di
            out[d][k] = dict(r0=acc, r1=np.zeros_like(acc), att=e0["att"], t_end=e0["t_end"],
                             pre_switched=True)
    return out


def main():
    loaded, lookahead = [], None
    for seed, exp in SEEDS:
        ev, info = S.load_folds(exp, "causal")
        for ds in DS_KEYS:
            la = float(info[ds]["lookahead_s"])
            if lookahead is None:
                lookahead = la
            elif abs(la - lookahead) > 1e-9:
                raise RuntimeError(f"lookahead mismatch on {exp} {ds}: {la} vs {lookahead}")
            for e in ev[ds].values():
                if not e.get("pre_switched"):
                    raise RuntimeError(f"{exp} {ds} evidence is not pre_switched")
        loaded.append((seed, exp, ev))
        print(f"loaded {exp}", flush=True)

    evs = [ev for _, _, ev in loaded]
    jobs = []
    for seed, exp, ev in loaded:
        for p in P_STAY:
            jobs.append(dict(method=f"seed{seed}", exp=exp, p_stay=p, ev=ev, w=None))
    equal_w = np.full(len(evs), 1.0 / len(evs))
    ev_equal = combine(evs, equal_w)
    for p in P_STAY:
        jobs.append(dict(method="equal", exp="equal", p_stay=p, ev=ev_equal, w=equal_w))
    for held in DS_KEYS:
        w = invvar_weights(evs, held)
        ev_w = combine(evs, w)
        for p in P_STAY:
            jobs.append(dict(method="invvar", exp="invvar", p_stay=p, ev=ev_w, w=w, only=held))

    rows = []
    for job in jobs:
        targets = [job["only"]] if "only" in job else DS_KEYS
        w = job["w"]
        for ds in targets:
            r = S.score_dataset(ds, job["ev"], lookahead, p_stay=job["p_stay"])
            rec = dict(method=job["method"], exp=job["exp"], p_stay=job["p_stay"], ds=ds,
                       H=r["H"], Acc=r["Acc"], SDT=r["SDT"])
            if w is not None:
                for i, wi in enumerate(w):
                    rec[f"w{i}"] = float(wi)
            rows.append(rec)
            print(f"{job['method']:7s} p_stay={job['p_stay']} {ds:4s}  H {r['H']:.4f}  Acc {r['Acc']:.2f}  SDT {r['SDT']:.2f}",
                  flush=True)

    df = pd.DataFrame(rows)
    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    df.to_csv(OUT, index=False)

    mean = df.groupby(["method", "p_stay"], sort=False)[["H", "Acc", "SDT"]].mean().reset_index()
    pd.set_option("display.width", 200)
    pd.set_option("display.max_rows", 100)
    print("\nmean over the five held-out datasets:")
    print(mean.round(4).to_string(index=False))
    print(f"\nwrote {OUT}")

    ref = df[(df.method == "seed0") & (np.isclose(df.p_stay, 0.998))]
    print(f"seed0 @ 0.998 mean H {ref.H.mean():.4f} Acc {ref.Acc.mean():.2f} SDT {ref.SDT.mean():.2f}")


if __name__ == "__main__":
    main()
