"""How different are the views (decoders vs direct classifiers, seed vs seed)? Headroom of combining them.

For the evidence z of each member (sign-corrected by the true attended column, so + = towards the truth) report
  d'       mean(z) / std(z): per-step signal to noise
  corr     Pearson correlation between members (a high correlation means ensembling removes little)
  d'(opt)  d' of the best linear combination of the two ensemble means (LDA): the headroom of fusing decoder and classifier
Synthetic: out-of-fold LODO evidence, 5 datasets (fuse_general.build conventions). Real: AASD and Carta2026 (fuse_external.build_external).
usage: python view_diversity.py -> results/research/view_diversity.txt"""
import itertools
import os

import numpy as np

import fuse_external as FX
from common import DS_KEYS
from common_exp import EXP
from fuse_evidence import features

DEC = ["vlaai_ws__eaP_n5_t60_aug_cluster_onset_dom_xFUG_ptSPK" + t for t in ("", "_s1", "_s2", "_s3")]
CLF = ["direct_clf__eaP_n5_t60_w10b3_ptSPK" + t + "_aug-sw2" for t in ("", "_s1", "_s2", "_s3")]
out = []


def say(s=""):
    print(s, flush=True)
    out.append(s)


def stats(name, z):
    say(f"  {name:34s} d' {z.mean() / z.std():.3f}")


def combo(zd, zc):
    X = np.stack([zd, zc], 1)
    w = np.linalg.solve(np.cov(X.T), X.mean(0))
    return (X @ w).mean() / (X @ w).std(), w / np.abs(w).sum()


def report(title, zds, zcs):
    """zds, zcs: lists of 1-D arrays (one per decoder seed / classifier seed), sign-corrected, pooled over trials."""
    say(f"\n== {title}")
    pair = lambda L: np.mean([np.corrcoef(a, b)[0, 1] for a, b in itertools.combinations(L, 2)])
    md, mc = np.mean(zds, 0), np.mean(zcs, 0)
    say(f"  mean pairwise corr: decoder seeds {pair(zds):.3f} | classifier seeds {pair(zcs):.3f} | decoder vs classifier "
        f"{np.mean([np.corrcoef(a, b)[0, 1] for a in zds for b in zcs]):.3f}")
    say(f"  d' single decoder {np.mean([z.mean() / z.std() for z in zds]):.3f} -> 4-seed ensemble {md.mean() / md.std():.3f}")
    say(f"  d' single classifier {np.mean([z.mean() / z.std() for z in zcs]):.3f} -> 4-seed ensemble {mc.mean() / mc.std():.3f}")
    say(f"  d' equal mix (a 0.5) {((md + mc) / 2).mean() / ((md + mc) / 2).std():.3f}")
    d, w = combo(md, mc)
    say(f"  d' best linear mix {d:.3f} (weights decoder {w[0]:+.2f}, classifier {w[1]:+.2f})")


# ---- synthetic LODO (all five datasets pooled, evidence out of fold)
Fd = [features(d, CLF[0]) for d in DEC]
Fc = [features(DEC[0], c) for c in CLF]
sg = {(ds, k): np.where(Fd[0][ds][k]["att"] == 0, 1.0, -1.0) for ds in DS_KEYS for k in Fd[0][ds]}
keys = list(sg)
zds = []
for F in Fd:
    X = np.concatenate([F[ds][k]["X"][:, :-1] * sg[(ds, k)][:, None] for ds, k in keys])
    w = np.linalg.solve(np.cov(X.T), X.mean(0))
    z = X @ w
    zds.append(z / z.std())
zcs = []
for F in Fc:
    z = np.concatenate([F[ds][k]["X"][:, -1] * sg[(ds, k)] for ds, k in keys])
    zcs.append(z / z.std())
report("synthetic LODO, 5 datasets pooled (LDA weights fitted on the pooled data: slightly optimistic)", zds, zcs)

# ---- real switches
decs, clfs = [[d, str(i)] for i, d in enumerate(DEC)], [[c, "_w10b3sw2P", str(i)] for i, c in enumerate(CLF)]
EV, _ = FX.build_external(decs, clfs, [0.0, 1.0], ["AASD", "Carta2026"], "_bestP")
for ds in EV:
    sgn = lambda e: np.where(e["att"] == 0, 1.0, -1.0)
    zd = np.concatenate([e["r0"] * sgn(e) for e in EV[ds][0.0].values()])
    zc = np.concatenate([e["r0"] * sgn(e) for e in EV[ds][1.0].values()])
    say(f"\n== real switches {ds} (ensemble means only; the per-seed traces are not kept by build_external)")
    say(f"  corr decoder vs classifier {np.corrcoef(zd, zc)[0, 1]:.3f}")
    say(f"  d' decoder ensemble {zd.mean() / zd.std():.3f} | classifier ensemble {zc.mean() / zc.std():.3f} | equal mix {((zd + zc) / 2).mean() / ((zd + zc) / 2).std():.3f}")
    d, w = combo(zd, zc)
    say(f"  d' best linear mix {d:.3f} (weights decoder {w[0]:+.2f}, classifier {w[1]:+.2f})")
open(os.path.join(EXP, "results", "research", "view_diversity.txt"), "w").write("\n".join(out) + "\n")
