"""M0 (MULTITASK_EXPERIMENTS.md): greedy ensemble selection (Caruana, with replacement) over the saved held-out probabilities.

Units are (variant, seed index i): on CV fold f the unit uses that variant's CV seed i, on the public split its public seed 42 + i.
The selection maximises the CV macro W-bMAE only; the public split is reported for the chosen ensemble, never used to choose.
W-bMAE per recording = sum over its windows of metric weight x |error| (identical to the official per-recording score).
usage: python ens_select.py [probs dir] [max steps]
"""
import glob
import os
import re
import sys

import numpy as np

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
sys.path.insert(0, os.path.join(ROOT, "experiments/stft_tree"))
import run_stft_tree as R  # noqa: E402

PD = os.path.join(HERE, sys.argv[1] if len(sys.argv) > 1 else "probs_e12")
STEPS = int(sys.argv[2]) if len(sys.argv) > 2 else 20
K = 121
_, _, win, rec = R.extract()
y = win.y.to_numpy(); wrec = win.rec.to_numpy(); widx = win.groupby("rec").indices
wmet = R.metric_weights(y, wrec)

files = {}
for p in glob.glob(os.path.join(PD, "*.npz")):
    m = re.match(r"(.+)_(cv|public)_f(\d+)_s(\d+)_(\w+)\.npz$", os.path.basename(p))
    files[(m[1], m[2], int(m[3]), int(m[4]), m[5])] = p
variants = sorted({k[0] for k in files})
groups = sorted({(k[2], k[4]) for k in files if k[1] == "cv"})                     # (fold, group)
cv_seeds = {v: sorted({k[3] for k in files if k[0] == v and k[1] == "cv"}) for v in variants}
pub_seeds = {v: sorted({k[3] for k in files if k[0] == v and k[1] == "public"}) for v in variants}
units = [(v, i) for v in variants for i in range(len(cv_seeds[v]))
         if all((v, "cv", f, cv_seeds[v][i], g) in files for f, g in groups)]


def load_split(tag, keys):
    """keys: [(fold, group)] -> rows (window indices), recording per row, and {unit: probs}."""
    out = {}
    for f, g in keys:
        recs = None
        P = {}
        for v, i in units:
            s = (cv_seeds if tag == "cv" else pub_seeds)[v]
            if i >= len(s) or (v, tag, f, s[i], g) not in files:
                continue
            d = np.load(files[(v, tag, f, s[i], g)])
            recs = list(d["recs"]) if recs is None else recs
            assert list(d["recs"]) == recs
            P[(v, i)] = d["probs"].astype(np.float32)
        ite = np.concatenate([widx[r] for r in recs])
        out[(f, g)] = (ite, P)
    return out


CV = load_split("cv", groups)
PUB = load_split("public", [(0, "test40")])
MID = 5.0 * np.arange(K)


def decode(p):
    p = p / p.sum(1, keepdims=True)
    cdf = p.cumsum(1)
    idx = np.minimum((cdf < 0.5).sum(1), K - 1)
    n = np.arange(len(p)); pk = p[n, idx]; prev = cdf[n, idx] - pk
    return np.clip(5.0 * (idx - 0.5 + np.clip((0.5 - prev) / np.maximum(pk, 1e-9), 0, 1)), 0, 600)


def per_rec(ite, p):
    e = wmet[ite] * np.abs(decode(p) - y[ite])
    r = wrec[ite]
    u, inv = np.unique(r, return_inverse=True)
    return dict(zip(u, np.bincount(inv, e)))


def score(split, sums, n):
    """sums: {(fold, group): summed probs}; returns macro (mean over folds of mean over groups) and per-recording dict."""
    res, per = {}, {}
    for key, (ite, _) in split.items():
        pr = per_rec(ite, sums[key] / n)
        res[key] = np.mean(list(pr.values()))
        per.update({(key, r): s for r, s in pr.items()})
    folds = sorted({f for f, _ in res})
    return float(np.mean([np.mean([res[k] for k in res if k[0] == f]) for f in folds])), per


def ens(split, members):
    if any(u not in P for _, P in split.values() for u in members):
        return float("nan"), {}
    sums = {key: sum(P[u] for u in members) for key, (_, P) in split.items()}
    return score(split, sums, len(members))


def paired(a, b):
    if not a:
        return "n/a"
    d = np.array([a[k] - b[k] for k in a if k in b])
    return f"{d.mean():+.2f} ± {d.std() / np.sqrt(len(d)):.2f}"


base5 = [("base", i) for i in range(5)]
b_cv, b_cvper = ens(CV, base5); b_pub, b_pubper = ens(PUB, base5)
print(f"units: {len(units)} ({', '.join(f'{v} x{len(cv_seeds[v])}' for v in variants)})")
print(f"base 5 seed: CV {b_cv:.2f}, public {b_pub:.2f}\n")
chosen, best_hist = [], []
sums = {key: 0 for key in CV}
for step in range(STEPS):
    best = None
    for u in units:
        trial = {key: sums[key] + CV[key][1][u] for key in CV}
        m, _ = score(CV, trial, len(chosen) + 1)
        if best is None or m < best[0]:
            best = (m, u)
    chosen.append(best[1])
    sums = {key: sums[key] + CV[key][1][best[1]] for key in CV}
    cvm, cvper = ens(CV, chosen); pm, pper = ens(PUB, chosen)
    best_hist.append((cvm, pm))
    print(f"step {step + 1:2d}: + {best[1][0]:>8s} s{best[1][1]}  CV {cvm:.2f} ({paired(cvper, b_cvper)} vs base 5)  "
          f"public {pm:.2f} ({paired(pper, b_pubper)})")
from collections import Counter  # noqa: E402
print("\nchosen (variant: count):", dict(Counter(v for v, _ in chosen)))
if best_hist:
    print("best CV step:", int(np.argmin([h[0] for h in best_hist])) + 1)


# ----------------------------------------------------------------------------- variant-level selection (less selection bias)
def variant_probs(split, v):
    """{key: mean of the variant's seeds} on a split (None if a seed is missing)."""
    out = {}
    for key, (_, P) in split.items():
        us = [u for u in P if u[0] == v]
        if len(us) < 5:
            return None
        out[key] = sum(P[u] for u in us) / len(us)
    return out


VP_CV = {v: variant_probs(CV, v) for v in variants}
VP_PUB = {v: variant_probs(PUB, v) for v in variants}
VAR = [v for v in variants if VP_CV[v] is not None]


def vscore(split, VP, counts, keys=None):
    n = sum(counts.values())
    sums = {key: sum(VP[v][key] * c for v, c in counts.items() if c) for key in split if keys is None or key in keys}
    sub = {key: split[key] for key in sums}
    return score(sub, sums, n)


def greedy(keys, steps=10):
    counts = {v: 0 for v in VAR}
    hist = []
    for _ in range(steps):
        best = min(VAR, key=lambda v: vscore(CV, VP_CV, {**counts, v: counts[v] + 1}, keys)[0])
        counts[best] += 1
        hist.append((vscore(CV, VP_CV, counts, keys)[0], dict(counts)))
    return min(hist, key=lambda h: h[0])[1]


print("\n=== variant-level greedy (each unit = the mean of a variant's 5 seeds) ===")
w = greedy(None)
cvm, cvper = vscore(CV, VP_CV, w)
b_cvm, b_cvper2 = vscore(CV, VP_CV, {"base": 1})
line = f"weights {w}: CV {cvm:.2f} ({paired(cvper, b_cvper2)} vs base 5)"
if all(VP_PUB[v] is not None for v, c in w.items() if c):
    pm, pper = vscore(PUB, VP_PUB, w); bpm, bpper = vscore(PUB, VP_PUB, {"base": 1})
    line += f", public {pm:.2f} ({paired(pper, bpper)} vs base 5 = {bpm:.2f})"
print(line)
print("\n=== nested: choose the weights on 4 folds, score the 5th (unbiased estimate of the selection) ===")
folds = sorted({f for f, _ in CV})
nested = {}
for f in folds:
    keys = {k for k in CV if k[0] != f}
    wf = greedy(keys)
    _, per = vscore(CV, VP_CV, wf, {k for k in CV if k[0] == f})
    nested.update(per)
    print(f"  fold {f}: weights {wf}")
d = np.array([nested[k] - b_cvper2[k] for k in nested])
fm = lambda per: np.mean([np.mean([np.mean([s for (kk, r), s in per.items() if kk == (f, g)]) for g in ("seen", "unseen")]) for f in folds])
print(f"nested CV {fm(nested):.2f} vs base 5 {fm(b_cvper2):.2f}: {d.mean():+.2f} ± {d.std() / np.sqrt(len(d)):.2f} per recording")
