"""Pools of members from several training recipes (predictions already saved). Pools are declared here, before looking at results."""
import io, contextlib, sys
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
    import analyze as A
import numpy as np
from ens_utils import median_decode
pools = {
 "A: val25 x5": [("val25", 5)],
 "B: val25 x8": [("val25", 8)],
 "C: val25 x5 + full_ep12 x5": [("val25", 5), ("full_ep12", 5)],
 "D: val25 x5 + full_ep12 x5 + full_ep18 x5": [("val25", 5), ("full_ep12", 5), ("full_ep18", 5)],
 "E: 4 recipes x5 (20 models)": [("val25", 5), ("tr85_ep12", 5), ("full_ep12", 5), ("full_ep18", 5)],
 "F: val25 x8 + full_ep12 x5 + full_ep18 x5": [("val25", 8), ("full_ep12", 5), ("full_ep18", 5)],
 "G: full_ep12 x5 + full_ep18 x5": [("full_ep12", 5), ("full_ep18", 5)],
}
def members(pool, tag):
    out = []
    for v, n in pool:
        seeds = list(range(n)) if tag == "cv" else [42 + i for i in range(n)]
        out += [(v, s) for s in seeds]
    return out
print("| Pool | Số model | CV macro | Seen | Unseen | Test công khai (gốc seed 42) |\n|---|---|---|---|---|---|")
base = None
for name, pool in pools.items():
    mc, mp = members(pool, "cv"), members(pool, "public")
    cv = A.evaluate_cv(lambda k, g: median_decode(np.mean([A.probs(v, "cv", k, s, g) for v, s in mc], 0)))
    pb = A.evaluate_pub(median_decode(np.mean([A.probs(v, "public", 0, s, "test40") for v, s in mp], 0)))
    if base is None: base = cv
    m, se, pct, n = A.paired(cv, base)
    print(f"| {name} | {len(mc)} | **{cv['macro'].mean():.2f}** | {cv['seen']:.1f} | {cv['unseen']:.1f} | **{pb['score']:.1f}** |   (so với A: {m:+.2f} ± {se:.2f} s)")
