"""View-level ensemble selection over every saved probability set (E12 and F1 batches).

A *view* is one configuration; its prediction on a held-out group is the mean of its seeds' distributions. Views are mixed with
non-negative weights found by greedy forward selection with replacement (Caruana), maximising the CV macro W-bMAE.
Reported: the selection scored on the folds it was chosen on (optimistic), a NESTED estimate (weights chosen on 4 folds, scored
on the 5th, for every fold), and the public test with the weights chosen on all 5 CV folds (never used for choosing).
W-bMAE per recording = sum of metric weight x |error| over its windows (identical to the official per-recording score).
"""
import glob
import os
import re
import sys
from collections import Counter

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

SOURCES = {"probs_e12": ["base", "e11", "a9_ngs", "e11_a9", "a9_ngsm"],
           "probs_f1": ["f1a_nospec", "f1b_nospec_norel", "f1c_top128", "f1c_top64"],   # the F1 `base` re-runs E12's base
           "probs_views2": ["e9b_nfft128", "e5c_dropout", "a2_swap", "a10_covmix"]}
STEPS = 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)


def load_views():
    """{view: {(tag, fold, group): mean probs}}, the window rows per key, and the number of seeds per view and tag."""
    P, rows, nseeds = {}, {}, {}
    for d, views in SOURCES.items():
        files = glob.glob(os.path.join(HERE, d, "*.npz"))
        for v in views:
            groups = {}
            for p in files:
                m = re.match(rf"{re.escape(v)}_(cv|public)_f(\d+)_s(\d+)_(\w+)\.npz$", os.path.basename(p))
                if m:
                    groups.setdefault((m[1], int(m[2]), m[4]), []).append(p)
            P[v] = {}
            for key, ps in groups.items():
                arrs = [np.load(p) for p in ps]
                recs = list(arrs[0]["recs"])
                assert all(list(a["recs"]) == recs for a in arrs)
                P[v][key] = np.mean([a["probs"].astype(np.float32) for a in arrs], 0)
                rows.setdefault(key, np.concatenate([widx[r] for r in recs]))
                nseeds[(v, key[0])] = len(ps)
    return P, rows, nseeds


P, ROWS, NSEEDS = load_views()
VIEWS = list(P)
CVK = sorted(k for k in ROWS if k[0] == "cv")
PUBK = [k for k in ROWS if k[0] == "public"]


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(key, p):
    ite = ROWS[key]
    e = wmet[ite] * np.abs(decode(p) - y[ite])
    u, inv = np.unique(wrec[ite], return_inverse=True)
    return dict(zip(((key, int(r)) for r in u), np.bincount(inv, e)))


def mix(counts, key):
    n = sum(counts.values())
    return sum(P[v][key] * c for v, c in counts.items() if c) / n


def score(counts, keys):
    per = {}
    for k in keys:
        per.update(per_rec(k, mix(counts, k)))
    folds = sorted({k[1] for k in keys})
    if keys and keys[0][0] == "public":
        return float(np.mean(list(per.values()))), per
    m = np.mean([np.mean([np.mean([s for (kk, r), s in per.items() if kk == (t, f, g)]) for (t, ff, g) in keys if ff == f])
                 for f in folds])
    return float(m), per


def greedy(keys, steps=STEPS):
    counts = Counter()
    hist = []
    for _ in range(steps):
        best = min(VIEWS, key=lambda v: score(counts + Counter({v: 1}), keys)[0])
        counts[best] += 1
        hist.append((score(counts, keys)[0], Counter(counts)))
    return min(hist, key=lambda h: h[0])[1], hist


def paired(a, b):
    d = np.array([a[k] - b[k] for k in a])
    return f"{d.mean():+.2f} ± {d.std() / np.sqrt(len(d)):.2f} ({100 * (d < 0).mean():.0f}% better)"


def fmt(c):
    n = sum(c.values())
    return ", ".join(f"{v} {c[v] / n:.2f}" for v in sorted(c, key=lambda v: -c[v]))


if __name__ == "__main__":
    print("views (CV seeds / public seeds):", ", ".join(f"{v} {NSEEDS.get((v, 'cv'))}/{NSEEDS.get((v, 'public'))}" for v in VIEWS))
    base = Counter({"base": 1})
    b_cv, b_cvper = score(base, CVK); b_pub, b_pubper = score(base, PUBK)
    print(f"\nbase alone (5 seeds): CV {b_cv:.2f}, public {b_pub:.2f}")
    print("\nsingle views (each = mean of its seeds):")
    for v in VIEWS:
        c, cper = score(Counter({v: 1}), CVK); pb, pper = score(Counter({v: 1}), PUBK)
        print(f"  {v:18s} CV {c:.2f} ({paired(cper, b_cvper)})   public {pb:.2f} ({paired(pper, b_pubper)})")
    eq = Counter({v: 1 for v in VIEWS})
    c, cper = score(eq, CVK); pb, pper = score(eq, PUBK)
    print(f"\nall {len(VIEWS)} views, equal weights: CV {c:.2f} ({paired(cper, b_cvper)})   public {pb:.2f} ({paired(pper, b_pubper)})")
    w, hist = greedy(CVK)
    print("\ngreedy path on all 5 folds (CV of the chosen mix, optimistic):")
    for i, (m, cnt) in enumerate(hist[:12]):
        print(f"  step {i + 1:2d}: CV {m:.2f}  [{fmt(cnt)}]")
    c, cper = score(w, CVK); pb, pper = score(w, PUBK)
    print(f"\nchosen weights: {fmt(w)}")
    print(f"  CV (chosen on the same folds, optimistic): {c:.2f} ({paired(cper, b_cvper)})")
    print(f"  public test with these weights:            {pb:.2f} ({paired(pper, b_pubper)})")
    nested = {}
    for f in sorted({k[1] for k in CVK}):
        wf, _ = greedy([k for k in CVK if k[1] != f])
        nested.update(score(wf, [k for k in CVK if k[1] == f])[1])
        print(f"  nested, fold {f}: weights chosen on the other folds: {fmt(wf)}")
    folds = sorted({k[1] for k in CVK})
    fm = lambda per: np.mean([np.mean([np.mean([s for (kk, r), s in per.items() if kk == k]) for k in CVK if k[1] == f]) for f in folds])
    print(f"  NESTED CV (unbiased): {fm(nested):.2f} vs base {b_cv:.2f}: {paired(nested, b_cvper)}")
