"""General ensemble + fusion: any set of reconstruction decoders and any set of direct classifiers.

For held-out dataset D (all weights / scales from the other four datasets' out-of-fold evidence):
  each decoder experiment -> z = LDA-weighted targets / sd ; each classifier experiment -> z = logit / sd
  z_dec = mean over the decoder experiments, z_clf = mean over the classifier experiments (re-standardised)
  d = (1 - a) z_dec + a z_clf -> HMM (p_stay) -> official scorer, identical synthetic-switch schedule.
usage: python fuse_general.py --dec D1 D2 ... --clf C1 C2 ... [--a 0.15 0.3] [--pstay 0.998 ...] [--name label]
       appends to results/research/fuse_general.csv"""
import argparse
import os

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

import score as S
from common_exp import EXP
from common import DS_KEYS
from fuse_evidence import LA, features


def build(decs, clfs):
    """Per held-out dataset: z_dec, z_clf per trial (ensemble means, standardised on the training datasets)."""
    Fd = [features(d, clfs[0]) for d in decs]                       # decoder part of X
    Fc = [features(decs[0], c) for c in clfs]                       # classifier part (last column)
    ref = Fd[0]
    sg = {d: {k: np.where(e["att"] == 0, 1.0, -1.0) for k, e in ref[d].items()} for d in DS_KEYS}
    out = {}
    for held in DS_KEYS:
        tr = [d for d in DS_KEYS if d != held]
        zd, zc = [], []
        for F in Fd:
            X = np.concatenate([e["X"][:, :-1] * sg[d][k][:, None] for d in tr for k, e in F[d].items()])
            w = np.linalg.solve(np.cov(X.T), X.mean(0)) if X.shape[1] > 1 else np.ones(1)
            sd = (X @ w).std()
            zd.append({d: {k: (e["X"][:, :-1] @ w) / sd for k, e in F[d].items()} for d in DS_KEYS})
        for F in Fc:
            sd = np.concatenate([e["X"][:, -1] * sg[d][k] for d in tr for k, e in F[d].items()]).std()
            zc.append({d: {k: e["X"][:, -1] / sd for k, e in F[d].items()} for d in DS_KEYS})
        md = {d: {k: np.mean([z[d][k] for z in zd], 0) for k in ref[d]} for d in DS_KEYS}
        mc = {d: {k: np.mean([z[d][k] for z in zc], 0) for k in ref[d]} for d in DS_KEYS}
        sdd = np.concatenate([md[d][k] * sg[d][k] for d in tr for k in md[d]]).std()
        sdc = np.concatenate([mc[d][k] * sg[d][k] for d in tr for k in mc[d]]).std()
        out[held] = ({d: {k: v / sdd for k, v in md[d].items()} for d in DS_KEYS}, {d: {k: v / sdc for k, v in mc[d].items()} for d in DS_KEYS})
    meta = {d: {k: (e["att"], e["t_end"]) for k, e in ref[d].items()} for d in DS_KEYS}
    return out, meta


def one(z, meta, held, a, p):
    zd, zc = z
    ev = {d: {k: dict(r0=(1 - a) * zd[d][k] + a * zc[d][k], r1=np.zeros(len(zd[d][k])), att=meta[d][k][0], t_end=meta[d][k][1],
                      pre_switched=True) for k in zd[d]} for d in DS_KEYS}
    r = S.score_dataset(held, ev, LA, p_stay=p)
    return dict(ds=held, a=a, p_stay=p, H=r["H"], Acc=r["Acc"], SDT=r["SDT"])


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--dec", nargs="+", required=True)
    ap.add_argument("--clf", nargs="+", required=True)
    ap.add_argument("--a", nargs="+", type=float, default=[0.0, 0.15, 0.3, 0.5, 1.0])
    ap.add_argument("--pstay", nargs="+", type=float, default=[0.998])
    ap.add_argument("--name", default="")
    a = ap.parse_args()
    Z, meta = build(a.dec, a.clf)
    df = pd.DataFrame(Parallel(n_jobs=int(os.environ.get("SCORE_JOBS", 3)))(
        delayed(one)(Z[h], meta, h, x, p) for h in DS_KEYS for x in a.a for p in a.pstay))
    df["name"], df["n_dec"], df["n_clf"] = a.name, len(a.dec), len(a.clf)
    out = os.path.join(EXP, "results", "research", "fuse_general.csv")
    df.to_csv(out, mode="a", header=not os.path.exists(out), index=False)
    g = df.groupby(["p_stay", "a"])[["H", "Acc", "SDT"]].mean().round(4)
    pv = df.pivot_table(index=["p_stay", "a"], columns="ds", values="H").round(3)
    print(f"=== {a.name}: {len(a.dec)} decoders, {len(a.clf)} classifiers ===\n", pd.concat([g, pv], axis=1).to_string())
