"""C7: pool the three held-out folds (held subjects x held stimuli) and score them against the sw2 baseline on the SAME trials.
Emissions: corpus LODO evidence of the matching recipe (C7 LODO for C7, sw2 for sw2). usage: python score_c7_cv.py -> results/research/c7cv_real.csv"""
import os
import pickle

import numpy as np
import pandas as pd

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

R = os.path.join(EXP, "results", "research")
PS = (0.9997, 0.998, 0.995, 0.99, 0.98, 0.95, 0.9)


def emis(exp):
    ev, _ = S.load_folds(exp, "causal")
    A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0) for ds in DS_KEYS for e in ev[ds].values()])
    return float(A.mean()), float(A.std())


rows = []
for seed in (0, 1):
    base = "direct_clf__eaP_n5_t60_w10b3_ptSPK" + (f"_s{seed}" if seed else "")
    me, mb = emis(base + "_aug-c7cv"), emis(base + "_aug-sw2")
    for ds in ("AASD", "Carta2026"):
        new = {}
        for f in range(3):
            new.update(pickle.load(open(os.path.join(R, f"ext_clf_{ds}_c7cvf{f}_evidence_s{seed}.pkl"), "rb")))
        b = pickle.load(open(os.path.join(R, f"ext_clf_{ds}_w10b3sw2P_evidence_s{seed}.pkl"), "rb"))
        b = {k: b[k] for k in new}
        for p in PS:
            x, y = E.score(b, *mb, p, 0.75), E.score(new, *me, p, 0.75)
            rows.append(dict(seed=seed, ds=ds, trials=len(new), p_stay=p, H_sw2=x["H"], H_c7=y["H"], dH=y["H"] - x["H"], Acc_sw2=x["Acc"], Acc_c7=y["Acc"], SDT_sw2=x["SDT"], SDT_c7=y["SDT"]))
df = pd.DataFrame(rows)
df.to_csv(os.path.join(R, "c7cv_real.csv"), index=False)
print(df.round(4).to_string(index=False))
