"""Fusion weights LEARNED with a sequence loss (instead of the grid of fuse_mix.py).

d_t = sum_f w_f x_tf over the decoder targets and the classifier logit (each standardised on the training datasets);
u_t = d_t is the log-odds fed to the 2-state forward filter (p_stay 0.998); loss = cross-entropy of the causal
FILTERING posterior against the true state, summed over the trials (with their synthetic switches) of the four
training datasets. The loss sees whole sequences, so the temporal redundancy of the 5-s classifier windows is priced in
(a per-step loss such as LDA over-weights them). The learned SCALE is irrelevant afterwards: score.py re-fits the
Gaussian emissions on the fused evidence, only the ratios of w matter. No test data: weights for held-out dataset D
come from the other four. usage: python fuse_learn.py DEC_EXP CLF_EXP"""
import math
import sys

import numpy as np
import pandas as pd
import torch

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

DEV = torch.device("cuda" if torch.cuda.is_available() else "cpu")
LS, LSW = math.log(0.998), math.log(0.002)


def filter_ce(u, y, mask):
    """u (B, n) log-odds of column 0, y (B, n) true column, mask (B, n): mean filtering cross-entropy."""
    lp = torch.full((u.shape[0], 2), math.log(0.5), device=u.device)
    ce = 0.0
    for j in range(u.shape[1]):
        if j > 0:
            lp = torch.stack([torch.logaddexp(lp[:, 0] + LS, lp[:, 1] + LSW), torch.logaddexp(lp[:, 1] + LS, lp[:, 0] + LSW)], 1)
        lp = lp + torch.stack([u[:, j] / 2, -u[:, j] / 2], 1)
        lp = lp - torch.logsumexp(lp, 1, keepdim=True)
        ce = ce - (lp.gather(1, y[:, j:j + 1])[:, 0] * mask[:, j]).sum()
    return ce / mask.sum()


def learn(F, held):
    tr = [e for d in DS_KEYS if d != held for e in F[d].values()]
    X = np.concatenate([e["X"] * np.where(e["att"] == 0, 1.0, -1.0)[:, None] for e in tr])
    sd = X.std(0)
    n = max(len(e["att"]) for e in tr)
    Xp, Yp, Mp = np.zeros((len(tr), n, X.shape[1]), np.float32), np.zeros((len(tr), n), np.int64), np.zeros((len(tr), n), np.float32)
    for i, e in enumerate(tr):
        k = len(e["att"])
        Xp[i, :k], Yp[i, :k], Mp[i, :k] = e["X"] / sd, e["att"], 1.0
    Xp, Yp, Mp = [torch.from_numpy(z).to(DEV) for z in (Xp, Yp, Mp)]
    w = torch.nn.Parameter(torch.full((X.shape[1],), 0.1, device=DEV))
    opt = torch.optim.Adam([w], lr=0.03)
    for it in range(150):
        loss = filter_ce(Xp @ w, Yp, Mp)
        opt.zero_grad()
        loss.backward()
        opt.step()
    return w.detach().cpu().numpy() / sd, loss.item()


if __name__ == "__main__":
    dec, clf = sys.argv[1], sys.argv[2]
    F = features(dec, clf)
    rows = []
    for ds in DS_KEYS:
        w, ce = learn(F, ds)

        def ev(e):
            d = e["X"] @ w
            return dict(r0=d, r1=np.zeros_like(d), att=e["att"], t_end=e["t_end"], pre_switched=True)
        r = S.score_dataset(ds, {d: {k: ev(e) for k, e in F[d].items()} for d in DS_KEYS}, LA)
        X = np.concatenate([e["X"] * np.where(e["att"] == 0, 1.0, -1.0)[:, None] for d in DS_KEYS if d != ds for e in F[d].values()])
        share = np.abs(w * X.std(0)) / np.abs(w * X.std(0)).sum()             # weight per standardised source
        rows.append(dict(ds=ds, H=r["H"], Acc=r["Acc"], SDT=r["SDT"], train_ce=ce, **{f"w{i}": s for i, s in enumerate(share)}))
        print(f"{ds}: H {r['H']:.4f} Acc {r['Acc']:.2f} SDT {r['SDT']:.2f} | train filter-CE {ce:.4f} | share per source "
              f"(decoder targets..., classifier) {np.round(share, 3)} signs {np.sign(w).astype(int)}", flush=True)
    d = pd.DataFrame(rows)
    print(f"MEAN learned fusion: H {d.H.mean():.4f}  Acc {d.Acc.mean():.2f}  SDT {d.SDT.mean():.2f} | mean share {np.round(d.filter(like='w').mean().values, 3)}")
