"""Re-score multi-target experiments with learned TARGET WEIGHTS in the evidence (no retraining).

score.py uses d = mean over targets of (r0 - r1). Here, for held-out dataset D, the weights are fitted on the OTHER four
datasets' out-of-fold per-target evidence (no test data) and the experiment is scored by the unchanged score.py code:
  lda   : w = Sigma^-1 mu (optimal linear combination for Gaussian evidence), normalised to sum |w| = 1
  noenv : equal weights without the 'env' target
usage: python rescore_weights.py [EXP ...]   (default: every experiment whose folds contain per-target evidence)
       -> results/research/rescore_weights.csv, paired comparison with the equal-weight H of results/<exp>.json"""
import json
import os
import pickle
import sys

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

import score as S
from common_exp import CFG, EXP, fold_file
from common import DS_KEYS

LA = CFG["dnn"]["evidence_lookahead_s"]


def weights(mode, ev, train, names):
    X = np.concatenate([(e["r0f"].astype(float) - e["r1f"].astype(float)) * np.where(e["att"] == 0, 1.0, -1.0)[:, None]
                        for d in train for e in ev[d].values()])
    if mode == "lda":
        w = np.linalg.solve(np.cov(X.T), X.mean(0))
    else:
        w = np.array([0.0 if n == "env" else 1.0 for n in names])
    return w / np.abs(w).sum()


def one(exp, ds, mode):
    ev, _ = S.load_folds(exp, "causal")
    names = json.load(open(os.path.join(EXP, "results", f"{exp}.json")))["targets"]
    w = weights(mode, ev, [d for d in DS_KEYS if d != ds], names)
    ev2 = {d: {k: {**e, "r0": e["r0f"].astype(float) @ w, "r1": e["r1f"].astype(float) @ w} for k, e in ev[d].items()}
           for d in DS_KEYS}
    r = S.score_dataset(ds, ev2, LA)
    return dict(exp=exp, ds=ds, mode=mode, H=r["H"], Acc=r["Acc"], SDT=r["SDT"], snr1=S.plain_metrics(ds, ev2[ds])["snr1"],
                w=" ".join(f"{n} {x:+.2f}" for n, x in zip(names, w)))


def has_pf(exp):
    try:
        f = pickle.load(open(fold_file(exp, DS_KEYS[0]), "rb"))
        return all(os.path.exists(fold_file(exp, d)) for d in DS_KEYS) and "r0f" in next(iter(f["evidence"]["causal"].values()))
    except Exception:
        return False


if __name__ == "__main__":
    exps = sys.argv[1:] or sorted(e for e in os.listdir(os.path.join(EXP, "folds"))
                                  if os.path.exists(os.path.join(EXP, "results", f"{e}.json")) and has_pf(e))
    print(f"{len(exps)} experiments with per-target evidence", flush=True)
    out = Parallel(n_jobs=int(os.environ.get("SCORE_JOBS", 3)))(
        delayed(one)(e, ds, m) for e in exps for ds in DS_KEYS for m in ("lda", "noenv"))
    df = pd.DataFrame(out)
    base = []
    for e in exps:
        c = json.load(open(os.path.join(EXP, "results", f"{e}.json")))["causal"]["per_dataset"]
        base += [dict(exp=e, ds=d, mode="equal", H=c[d]["H"], Acc=c[d]["Acc"], SDT=c[d]["SDT"], snr1=c[d]["snr1"], w="") for d in DS_KEYS]
    df = pd.concat([pd.DataFrame(base), df])
    df.to_csv(os.path.join(EXP, "results", "research", "rescore_weights.csv"), index=False)
    m = df.groupby(["exp", "mode"]).H.mean().unstack()[["equal", "lda", "noenv"]]
    m["lda-equal"], m["noenv-equal"] = m.lda - m.equal, m.noenv - m.equal
    pd.set_option("display.width", 250)
    print(m.round(4).to_string())
    for c in ("lda-equal", "noenv-equal"):
        d = m[c].dropna()
        print(f"{c}: mean {d.mean():+.4f}, sd {d.std():.4f}, positive in {(d > 0).sum()}/{len(d)} experiments, "
              f"paired t = {d.mean() / (d.std() / np.sqrt(len(d))):.1f}")
    pds = df.groupby(["mode", "ds"]).H.mean().unstack().loc[["equal", "lda", "noenv"]]
    print("\nH per held-out dataset (mean over experiments):\n", pds.round(4).to_string())
