"""Do the F2-F5a feature variants help as ensemble members ("views")? Equal-size comparisons on the saved held-out distributions.

Ensemble = mean of the members' 121-class distributions, then the interpolated median (the submitted rule). All draws are random
subsets of a pool, D draws per cell; at equal size the per-recording scores are averaged over the draws and compared per recording.

Pools
  A  base       20 seeds (CV seeds 0-19, public 42-61)                          probs_e12 + probs_base_more
  B  feat       f2_complex, f3_events, f4_contact, f5_quantile; 3 CV seeds / 5 public seeds each   probs_feat
  C  mix8       e11, a9_ngs, e11_a9, a9_ngsm, e9b_nfft128, e5c_dropout, a2_swap, a10_covmix: 5 seeds each
  D  mix8 + feat
Table 1: A, B, C, D at the same ensemble size k.  Table 2: per view v, n members of v + n random `base` members against
2n random `base` members (n = 3 for CV, 5 for the public split).
The `base` re-run stored in probs_feat is left out (it would overwrite seeds 0-2 of the E12 `base`).
usage: python ens_feat_views.py [draws, default 20]
"""
import glob
import os
import re
import sys
import time

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

DRAWS = int(sys.argv[1]) if len(sys.argv) > 1 else 20
FEAT = ["f2_complex", "f3_events", "f4_contact", "f5_quantile"]
MIX8 = ["e11", "a9_ngs", "e11_a9", "a9_ngsm", "e9b_nfft128", "e5c_dropout", "a2_swap", "a10_covmix"]
REF = ["a2_swap", "e5c_dropout", "e9b_nfft128", "a10_covmix"]
K = 121
CVK = [(f, g) for f in range(5) for g in ("seen", "unseen")]
PUBK = [(0, "test40")]
_, _, 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 d in ("probs_e12", "probs_base_more", "probs_views2", "probs_feat"):
    for p in glob.glob(os.path.join(HERE, d, "*.npz")):
        m = re.match(r"(.+)_(cv|public)_f(\d+)_s(\d+)_(\w+)\.npz$", os.path.basename(p))
        if d == "probs_feat" and m[1] == "base":
            continue
        files.setdefault((m[1], m[2], int(m[4])), {})[(int(m[3]), m[5])] = p

ROWS, UINV = {}, {}
for tag, keys in (("cv", CVK), ("public", PUBK)):
    for (v, t, s), kp in files.items():
        if t == tag and all(k in kp for k in keys):
            for k in keys:
                recs = list(np.load(kp[k])["recs"])
                ROWS[(tag, k)] = np.concatenate([widx[r] for r in recs])
                UINV[(tag, k)] = np.unique(wrec[ROWS[(tag, k)]], return_inverse=True)
            break


def members(views, tag, keys):
    out = {}
    for (v, t, s), kp in files.items():
        if v in views and t == tag and all(k in kp for k in keys):
            out[(v, s)] = {k: np.load(kp[k])["probs"].astype(np.float16) for k in keys}
    return out


t0 = time.time()
POOL = {}
for tag, keys in (("cv", CVK), ("public", PUBK)):
    POOL[tag] = {"base": members(["base"], tag, keys), "feat": members(FEAT, tag, keys),
                 "mix8": members(MIX8, tag, keys), "ref": members(REF, tag, keys)}
    POOL[tag]["mix8f"] = {**POOL[tag]["mix8"], **POOL[tag]["feat"]}
print(f"members CV: " + ", ".join(f"{k} {len(v)}" for k, v in POOL["cv"].items()) + " | public: " +
      ", ".join(f"{k} {len(v)}" for k, v in POOL["public"].items()) + f"   (loaded in {time.time() - t0:.0f} s)")


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 scores(pool, chosen, tag, keys):
    out = {}
    for k in keys:
        p = np.mean([pool[m][k].astype(np.float32) for m in chosen], 0)
        ite = ROWS[(tag, k)]
        out[k] = np.bincount(UINV[(tag, k)][1], wmet[ite] * np.abs(decode(p) - y[ite]))
    return out


def macro(per, tag):
    if tag == "public":
        return float(per[PUBK[0]].mean())
    return float(np.mean([np.mean([per[(f, g)].mean() for g in ("seen", "unseen")]) for f in range(5)]))


def cell(tag, chooser):
    """D draws of `chooser(rng)` -> (pool dict, member list); mean per-recording scores and the macro of each draw."""
    keys = CVK if tag == "cv" else PUBK
    rng = np.random.default_rng(0)
    acc = {k: 0.0 for k in keys}; per_draw = []
    for _ in range(DRAWS):
        pool, chosen = chooser(rng)
        sc = scores(pool, chosen, tag, keys)
        for k in keys:
            acc[k] = acc[k] + sc[k] / DRAWS
        per_draw.append(macro(sc, tag))
    return acc, np.array(per_draw)


def draw(pool, k):
    names = sorted(pool)
    return lambda rng: (pool, [names[i] for i in rng.choice(len(names), k, replace=False)])


def paired(a, b, tag):
    keys = CVK if tag == "cv" else PUBK
    d = np.concatenate([a[k] - b[k] for k in keys])
    return f"{d.mean():+.2f} ± {d.std() / np.sqrt(len(d)):.2f}"


# ----------------------------------------------------------------------------- table 1
print(f"\n{DRAWS} random draws per cell. Table 1: ensembles of the same size k drawn from each pool (macro W-bMAE, mean ± SD over draws; paired diff = per recording)")
print(f"{'k':>3s} {'pool':>14s} | {'CV':>13s} {'vs base':>14s} | {'public':>13s} {'vs base':>14s}")
for k in (4, 8, 12):
    cells = {}
    for name, label in (("base", "A base"), ("feat", "B feat views"), ("mix8", "C mix8"), ("mix8f", "D mix8+feat")):
        row = {}
        for tag in ("cv", "public"):
            pool = POOL[tag][name]
            row[tag] = cell(tag, draw(pool, k)) if k <= len(pool) else None
        cells[name] = (label, row)
    for name, (label, row) in cells.items():
        txt = []
        for tag in ("cv", "public"):
            if row[tag] is None:
                txt.append(f"{'n/a':>13s} {'':>14s}"); continue
            acc, pd_ = row[tag]; ref = cells["base"][1][tag][0]
            txt.append(f"{pd_.mean():7.2f}±{pd_.std():4.2f} {'' if name == 'base' else paired(acc, ref, tag):>14s}")
        print(f"{k:3d} {label:>14s} | {txt[0]} | {txt[1]}")
    cmpk = {}
    for tag in ("cv", "public"):
        cmpk[tag] = (cells["mix8f"][1][tag][0], cells["mix8"][1][tag][0]) if cells["mix8f"][1][tag] and cells["mix8"][1][tag] else None
    print(f"      D − C (does adding the 4 feature views help the old mix?): CV {paired(*cmpk['cv'], 'cv') if cmpk['cv'] else 'n/a'} | public {paired(*cmpk['public'], 'public') if cmpk['public'] else 'n/a'}")

# ----------------------------------------------------------------------------- table 2
print("\nTable 2: per view, n members of the view + n random base members, against 2n random base members "
      "(CV: n = 3, public: n = 5; paired diff, negative = the view helps)")
print(f"{'view':>14s} | {'CV mix':>8s} {'CV base':>8s} {'diff':>14s} | {'pub mix':>8s} {'pub base':>8s} {'diff':>14s}")
for v in FEAT + REF:
    row = []
    for tag, n in (("cv", 3), ("public", 5)):
        keys = CVK if tag == "cv" else PUBK
        bp = POOL[tag]["base"]; vp = {m: a for m, a in {**POOL[tag]["feat"], **POOL[tag]["ref"]}.items() if m[0] == v}
        pool = {**bp, **vp}
        bnames, vnames = sorted(bp), sorted(vp)
        mixed = lambda rng, bn=bnames, vn=vnames, n=n, pool=pool: (pool, [bn[i] for i in rng.choice(len(bn), n, replace=False)] +
                                                                 [vn[i] for i in rng.choice(len(vn), n, replace=False)])
        a, ad = cell(tag, mixed); b, bd = cell(tag, draw(bp, 2 * n))
        row.append((ad.mean(), bd.mean(), paired(a, b, tag)))
    print(f"{v:>14s} | {row[0][0]:8.2f} {row[0][1]:8.2f} {row[0][2]:>14s} | {row[1][0]:8.2f} {row[1][1]:8.2f} {row[1][2]:>14s}")
print(f"\ndone in {time.time() - t0:.0f} s")
