"""Fusion by a mixing ratio: d = (1 - a) z_dec + a z_clf, z = evidence / its sd on the training datasets
(z_dec = LDA-weighted decoder targets). Reports H for a grid of a (per held-out dataset) and the NESTED choice
(a picked by the mean H on the other four datasets, no test data). usage: python fuse_mix.py DEC_EXP CLF_EXP"""
import os
import sys

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

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

GRID = (0.0, 0.15, 0.3, 0.5, 0.7, 1.0)


def one(F, fit_ds, ds, a):
    """Weights / scales fitted on the datasets other than fit_ds; score dataset ds."""
    tr = [d for d in DS_KEYS if d != fit_ds]
    X = np.concatenate([e["X"] * np.where(e["att"] == 0, 1.0, -1.0)[:, None] for d in tr for e in F[d].values()])
    nF = X.shape[1] - 1
    w = np.linalg.solve(np.cov(X[:, :nF].T), X[:, :nF].mean(0))
    sd_d, sd_c = (X[:, :nF] @ w).std(), X[:, nF].std()

    def ev(e):
        d = (1 - a) * (e["X"][:, :nF] @ w) / sd_d + a * e["X"][:, nF] / sd_c
        return dict(r0=d, r1=np.zeros_like(d), att=e["att"], t_end=e["t_end"], pre_switched=True)
    ev2 = {d: {k: ev(e) for k, e in F[d].items()} for d in DS_KEYS}
    r = S.score_dataset(ds, ev2, LA)
    return dict(fit=fit_ds, ds=ds, a=a, H=r["H"], Acc=r["Acc"], SDT=r["SDT"])


if __name__ == "__main__":
    F = features(sys.argv[1], sys.argv[2])
    # held-out score for every a, and the training-dataset scores needed for the nested choice
    jobs = [(h, h, a) for h in DS_KEYS for a in GRID] + [(h, d, a) for h in DS_KEYS for d in DS_KEYS if d != h for a in GRID]
    df = pd.DataFrame(Parallel(n_jobs=int(os.environ.get("SCORE_JOBS", 3)))(delayed(one)(F, *j) for j in jobs))
    te = df[df.fit == df.ds]
    print("H on the held-out dataset per mixing ratio a (0 = decoder only, 1 = classifier only):")
    print(te.pivot(index="a", columns="ds", values="H").assign(MEAN=te.groupby("a").H.mean(), Acc=te.groupby("a").Acc.mean(),
                                                                SDT=te.groupby("a").SDT.mean()).round(4).to_string())
    pick = {h: df[(df.fit == h) & (df.ds != h)].groupby("a").H.mean().idxmax() for h in DS_KEYS}
    nest = [te[(te.ds == h) & (te.a == pick[h])].iloc[0] for h in DS_KEYS]
    print("nested choice of a:", pick, "-> H %.4f, Acc %.2f, SDT %.2f" % (np.mean([r.H for r in nest]), np.mean([r.Acc for r in nest]), np.mean([r.SDT for r in nest])))
