"""VICReg label-free pretrain of stem_ms96 encoder → LODO supervised FT.

Augmentation-invariant VICReg on ms feature windows (no attention labels during
pretrain). Then supervised BCE FT of stem+head. Per LODO fold: pretrain on the
four train datasets only; all5 for external. EMA 0.995 eval. HMM 0.994 / 0.8.
Also fuse w=0.5 with frozen stem_synth_switch (tip protocol).

Distinct from DANN/GRL, IRM, Mixup/focal retunes, InfoNCE ContrastAAD, CPC.

Smoke: short pretrain+FT on held-AVGC; abort if AVGC Acc≲55 or H≪0.7.

usage:
  PRE_STEPS=400 FT_STEPS=600 python stem_ms96_vicreg_pretrain.py
  SMOKE=1 python stem_ms96_vicreg_pretrain.py
"""
import copy
import json
import math
import os
import pickle
import time

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

import ensemble_clf_nested as N
import ensemble_clf_transformer_v2 as V
import ensemble_clf_transformer_v4 as V4
import eval_external as E
import stem_ms96_irm_swa_fuse as Fuse
from common import DS_KEYS

EXP = os.path.dirname(os.path.abspath(__file__))
SYNTH_DIR = os.environ.get(
    "SYNTH_DIR", os.path.join(EXP, "models", "stem_ms96_synth_switch"))
CKPT_DIR = os.path.join(EXP, "models", "stem_ms96_vicreg")
LOG = os.path.join(EXP, "experiments", "stem_ms96_vicreg_pretrain.log")
OUT = os.path.join(EXP, "results", "research", "stem_ms96_vicreg_pretrain.json")
EVID = "/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/decoder_lodo/results/research"
TEST_NAMES = ("AASD", "Carta2026")

PRE_STEPS = int(os.environ.get("PRE_STEPS", "400"))
FT_STEPS = int(os.environ.get("FT_STEPS", "600"))
BATCH = int(os.environ.get("BATCH", "512"))
SMOKE = os.environ.get("SMOKE", "0") == "1"
EMA_DECAY = 0.995
LR_PRE = float(os.environ.get("LR_PRE", "1e-3"))
LR_FT = float(os.environ.get("LR_FT", "8e-4"))
P_STAY = 0.994
SCALE = 0.8
CFG = ("stem_ms96", "ms", 96, 64, 2, True, LR_FT)
WINDOW = 96
D_MODEL = 64
NLAYERS = 2
FUSE_W = 0.5
PROJ_DIM = int(os.environ.get("PROJ_DIM", "256"))
# VICReg coefficients (Bardes et al. defaults scaled)
SIM_COEFF = float(os.environ.get("SIM_COEFF", "25.0"))
STD_COEFF = float(os.environ.get("STD_COEFF", "25.0"))
COV_COEFF = float(os.environ.get("COV_COEFF", "1.0"))
TIP_AVGC = dict(H=0.8226, Acc=75.54)
TIP_LODO = dict(H=0.8821, Acc=83.36)


class Expander(nn.Module):
    def __init__(self, d_in, d_out=PROJ_DIM):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(d_in, d_out),
            nn.BatchNorm1d(d_out),
            nn.ReLU(inplace=True),
            nn.Linear(d_out, d_out),
            nn.BatchNorm1d(d_out),
            nn.ReLU(inplace=True),
            nn.Linear(d_out, d_out),
        )

    def forward(self, x):
        return self.net(x)


def log_line(text):
    print(text, flush=True)
    os.makedirs(os.path.dirname(LOG), exist_ok=True)
    with open(LOG, "a") as fh:
        fh.write(text + "\n")


def encode_features(model, x):
    """Return (logit[B,T], h[B,T,d]) — h is post-encoder features before clf head."""
    raw = x
    if model.dw is not None:
        x = x + model.dw(F.pad(x.transpose(1, 2), (model.dw_pad, 0))).transpose(1, 2)
    h = model.proj(x)
    for block in model.blocks:
        h = block(h)
    h = h + model.pos[:, : h.size(1)]
    h = model.enc(h, mask=model.mask)
    logit = model.head(h).squeeze(-1) + model.skip(raw).squeeze(-1)
    return logit, h


def pool_feat(h, m):
    w = m.unsqueeze(-1)
    return (h * w).sum(1) / w.sum(1).clamp_min(1.0)


def vicreg_loss(z1, z2, sim_coeff=SIM_COEFF, std_coeff=STD_COEFF, cov_coeff=COV_COEFF):
    """Standard VICReg on expander outputs [B, D]."""
    # invariance
    inv = F.mse_loss(z1, z2)

    def std_loss(z):
        # per-dimension std across batch; hinge at 1
        z = z - z.mean(0)
        std = torch.sqrt(z.var(dim=0, unbiased=False) + 1e-4)
        return torch.mean(F.relu(1.0 - std))

    def cov_loss(z):
        B, D = z.shape
        z = z - z.mean(0)
        cov = (z.T @ z) / max(B - 1, 1)
        off = cov.fill_diagonal_(0)
        return off.pow(2).sum() / D

    return (
        sim_coeff * inv
        + std_coeff * 0.5 * (std_loss(z1) + std_loss(z2))
        + cov_coeff * 0.5 * (cov_loss(z1) + cov_loss(z2)),
        float(inv.item()),
    )


def strong_augment(x):
    """Two independent strong augs of ms windows (noise + ch-drop + cutout + scale)."""
    x = V.augment(x)
    # per-sample gain
    gain = 0.8 + 0.4 * torch.rand(x.size(0), 1, 1, device=x.device)
    x = x * gain
    # extra channel noise
    x = x + torch.randn_like(x) * 0.05
    return x


def weighted_bce(logit, y, m, pos_weight):
    sw = V.switch_weights(y, m)
    bce = F.binary_cross_entropy_with_logits(
        logit, y, pos_weight=pos_weight, reduction="none")
    w = sw * m
    loss = (bce * w).sum() / w.sum().clamp_min(1.0)
    dv = (logit[:, 1:] - logit[:, :-1]).abs()
    m2 = m[:, 1:] * m[:, :-1]
    loss = loss + 0.05 * (dv * m2).sum() / m2.sum().clamp_min(1.0)
    return loss


def set_lr(opt, step, total, base):
    warm = min(40, max(total // 10, 1))
    if step < warm:
        lr = base * (step + 1) / warm
    else:
        p = (step - warm) / max(total - warm, 1)
        lr = 1e-5 + 0.5 * (base - 1e-5) * (1.0 + math.cos(math.pi * p))
    for group in opt.param_groups:
        group["lr"] = lr
    return lr


def make_model(c_in, device, seed):
    torch.manual_seed(seed)
    if device.type == "cuda":
        torch.cuda.manual_seed_all(seed)
    net = V.LogitTransformer(
        c_in, WINDOW, d_model=D_MODEL, nlayers=NLAYERS, stem=True).to(device)
    return net


def restore_mask(net):
    good = nn.Transformer.generate_square_subsequent_mask(WINDOW).to(net.mask.device)
    net.mask.copy_(good)
    if torch.isnan(net.mask).any():
        raise RuntimeError("causal mask has nan after restore")


def train_vicreg(model, expander, groups, mu, sd, device, steps, batch, tag, seed):
    """Label-free VICReg on encoder+expander. Head/skip frozen (unused)."""
    for p in model.head.parameters():
        p.requires_grad_(False)
    for p in model.skip.parameters():
        p.requires_grad_(False)
    params = [p for p in model.parameters() if p.requires_grad] + list(expander.parameters())
    opt = torch.optim.AdamW(params, lr=LR_PRE, weight_decay=1e-4)
    rng = np.random.default_rng(seed)
    model.train()
    expander.train()
    t0 = time.time()
    last = last_inv = None
    for step in range(steps):
        set_lr(opt, step, steps, LR_PRE)
        xb, _yb, mb = V.sample_batch(rng, groups, WINDOW, batch, mu, sd)
        x = torch.from_numpy(xb).to(device, non_blocking=True)
        m = torch.from_numpy(mb).to(device, non_blocking=True)
        v1 = strong_augment(x)
        v2 = strong_augment(x)
        opt.zero_grad(set_to_none=True)
        _, h1 = encode_features(model, v1)
        _, h2 = encode_features(model, v2)
        z1 = expander(pool_feat(h1, m))
        z2 = expander(pool_feat(h2, m))
        loss, inv = vicreg_loss(z1, z2)
        if not torch.isfinite(loss):
            raise RuntimeError(f"non-finite VICReg loss at step {step} for {tag}")
        loss.backward()
        torch.nn.utils.clip_grad_norm_(params, 1.0)
        opt.step()
        last = float(loss.item())
        last_inv = inv
        if step == 0 or (step + 1) % 100 == 0 or step + 1 == steps:
            log_line(
                f"    {tag} pre step {step + 1}/{steps} loss {last:.4f} inv {last_inv:.4f}")
    # re-enable head/skip for FT
    for p in model.head.parameters():
        p.requires_grad_(True)
    for p in model.skip.parameters():
        p.requires_grad_(True)
    # re-init head to zero (SSL should not leak into classifier); keep skip default
    with torch.no_grad():
        model.head.weight.zero_()
        model.head.bias.zero_()
    log_line(
        f"  pretrained {tag} VICReg loss {last:.4f} inv {last_inv:.4f} "
        f"({time.time() - t0:.0f}s)")
    return dict(pre_loss=last, pre_inv=last_inv)


def train_ft(model, groups, y_all, mu, sd, device, steps, batch, tag, seed):
    pos = float(y_all.mean())
    pos_weight = torch.tensor([(1.0 - pos) / max(pos, 1e-3)], device=device)
    opt = torch.optim.AdamW(model.parameters(), lr=LR_FT, weight_decay=1e-4)
    rng = np.random.default_rng(seed)
    ema = copy.deepcopy(model).eval()
    model.train()
    t0 = time.time()
    last = None
    for step in range(steps):
        set_lr(opt, step, steps, LR_FT)
        xb, yb, mb = V.sample_batch(rng, groups, WINDOW, batch, mu, sd)
        x = torch.from_numpy(xb).to(device, non_blocking=True)
        y = torch.from_numpy(yb).to(device, non_blocking=True)
        m = torch.from_numpy(mb).to(device, non_blocking=True)
        x = V.augment(x)
        opt.zero_grad(set_to_none=True)
        logit = model(x)
        loss = weighted_bce(logit, y, m, pos_weight)
        if not torch.isfinite(loss):
            raise RuntimeError(f"non-finite FT loss at step {step} for {tag}")
        loss.backward()
        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
        opt.step()
        V.ema_update(model, ema, EMA_DECAY)
        last = float(loss.item())
        if step == 0 or (step + 1) % 200 == 0 or step + 1 == steps:
            log_line(f"    {tag} ft step {step + 1}/{steps} loss {last:.4f}")
    ema.eval()
    restore_mask(ema)
    acc = V.train_frame_acc(ema, groups, WINDOW, mu, sd, device)
    log_line(
        f"  FT {tag} loss {last:.4f} frame-acc {acc:.3f} ({time.time() - t0:.0f}s)")
    return ema, dict(ft_loss=last, frame_acc=acc)


def fit_fold(groups, y_all, feat_family, train_ds, device, pre_steps, ft_steps, batch, tag,
             seed=V.TRAIN_SEED, halved=False):
    mu, sd = V.norm_stats(feat_family, train_ds)
    c_in = groups[0]["trials"][0][0].shape[1]
    model = make_model(c_in, device, seed)
    expander = Expander(D_MODEL, PROJ_DIM).to(device)
    try:
        pre_stats = train_vicreg(
            model, expander, groups, mu, sd, device, pre_steps, batch, tag, seed)
        del expander
        if device.type == "cuda":
            torch.cuda.empty_cache()
        ema, ft_stats = train_ft(
            model, groups, y_all, mu, sd, device, ft_steps, batch, tag, seed + 1)
        stats = {**pre_stats, **ft_stats}
        return ema, mu, sd, batch, stats
    except torch.cuda.OutOfMemoryError:
        if halved:
            raise
        log_line(f"  OOM on {tag} at batch {batch}; halving once")
        del model, expander
        if device.type == "cuda":
            torch.cuda.empty_cache()
        return fit_fold(
            groups, y_all, feat_family, train_ds, device, pre_steps, ft_steps,
            batch // 2, tag, seed=seed, halved=True)


def hp30_stream(stream):
    out = {}
    for key, d in stream.items():
        out[key] = V4.highpass(np.asarray(d, np.float64)[:, None]).reshape(-1).astype(np.float64)
    return out


def blend_one(a_stream, b_stream, w):
    out = {}
    for key in a_stream:
        a = np.asarray(a_stream[key], np.float64)
        b = np.asarray(b_stream[key], np.float64)
        n = min(len(a), len(b))
        out[key] = (w * a[:n] + (1.0 - w) * b[:n]).astype(np.float64)
    return out


def score_lodo(model, feat, mu, sd, held, device):
    streams = V.infer_all(model, feat, mu, sd, WINDOW, device)
    ev = N.to_ev(streams)
    off = N.official(held, ev, P_STAY, SCALE)
    hp = {ds: hp30_stream(streams[ds]) for ds in streams}
    off_hp = N.official(held, N.to_ev(hp), P_STAY, SCALE)
    log_line(f"LODO {held} p {P_STAY} sc {SCALE}  H {off['H']:.4f} Acc {off['Acc']:.2f} "
             f"L {off['L']:.3f} SDT {off['SDT']:.2f}")
    log_line(f"LODO_hp30 {held} p {P_STAY} sc {SCALE}  H {off_hp['H']:.4f} Acc {off_hp['Acc']:.2f}")
    return dict(vicreg=off, hp30=off_hp), streams


def score_external(model, feat_ext, meta, mu_in, sd_in, corpus_streams, corpus_att, device):
    parts = []
    for ds, table in corpus_streams.items():
        labels = corpus_att[ds]
        for key, d in table.items():
            parts.append(np.asarray(d, np.float64) * np.where(labels[key] == 0, 1.0, -1.0))
    mu_e = float(np.concatenate(parts).mean())
    sd_e = float(np.concatenate(parts).std())
    log_line(f"  external emissions mu {mu_e:.4f} sd {sd_e:.4f} scale {SCALE}")

    hp_corpus = {ds: hp30_stream(corpus_streams[ds]) for ds in corpus_streams}
    parts_hp = []
    for ds, table in hp_corpus.items():
        labels = corpus_att[ds]
        for key, d in table.items():
            parts_hp.append(np.asarray(d, np.float64) * np.where(labels[key] == 0, 1.0, -1.0))
    mu_hp = float(np.concatenate(parts_hp).mean())
    sd_hp = float(np.concatenate(parts_hp).std())
    log_line(f"  external hp30 emissions mu {mu_hp:.4f} sd {sd_hp:.4f} scale {SCALE}")

    rows = {}
    for ds in TEST_NAMES:
        stream = V.infer_all(model, {ds: feat_ext[ds]}, mu_in, sd_in, WINDOW, device)[ds]
        ev = {}
        for key, d in stream.items():
            meta_k = meta[ds][key]
            d = np.asarray(d, np.float64)
            ev[key] = dict(r0=d, r1=np.zeros(len(d)), att=meta_k["att"], t_end=meta_k["t_end"],
                           att128=meta_k["att128"], n128=int(meta_k["n128"]),
                           subject=meta_k["subject"])
        r = E.score(ev, mu_e, sd_e * SCALE, P_STAY, 0.75)
        stream_hp = hp30_stream(stream)
        ev_hp = {}
        for key, d in stream_hp.items():
            meta_k = meta[ds][key]
            d = np.asarray(d, np.float64)
            ev_hp[key] = dict(r0=d, r1=np.zeros(len(d)), att=meta_k["att"], t_end=meta_k["t_end"],
                              att128=meta_k["att128"], n128=int(meta_k["n128"]),
                              subject=meta_k["subject"])
        r_hp = E.score(ev_hp, mu_hp, sd_hp * SCALE, P_STAY, 0.75)
        rows[ds] = dict(vicreg=r, hp30=r_hp)
        log_line(f"EXT {ds} p {P_STAY} sc {SCALE}  H {r['H']:.4f} Acc {r['Acc']:.2f} "
                 f"L {r['L']:.3f} SDT {r['SDT']:.2f}")
        log_line(f"EXT_hp30 {ds} p {P_STAY} sc {SCALE}  H {r_hp['H']:.4f} Acc {r_hp['Acc']:.2f}")
    return rows


def load_external():
    E.CONF.update(prep="eaP_n5_t60", targets=["env", "onset", "dom"])
    logits, meta = {}, {}
    for ds in TEST_NAMES:
        evs = []
        for i in range(4):
            path = os.path.join(EVID, f"ext_clf_{ds}_w10b3sw2P_evidence_s{i}.pkl")
            with open(path, "rb") as fh:
                evs.append(pickle.load(fh))
        logits[ds], meta[ds] = {}, {}
        for key in evs[0]:
            cols = [(ev[key]["r0"] - ev[key]["r1"]).astype(np.float32) for ev in evs]
            e0 = evs[0][key]
            logits[ds][key] = np.stack(cols, 1)
            meta[ds][key] = dict(
                att=np.asarray(e0["att"]), t_end=np.asarray(e0["t_end"], np.float64),
                att128=np.asarray(e0["att128"]), n128=int(e0["n128"]), subject=int(e0["subject"]))
        log_line(f"  external {ds}: {len(logits[ds])} trials")
    return logits, meta


def score_fuse_lodo(vic_streams, feat_all, held, device):
    path = os.path.join(SYNTH_DIR, f"held-{held}.pt")
    if not os.path.isfile(path):
        log_line(f"FUSE skip {held}: missing {path}")
        return None
    synth_net, synth_mu, synth_sd = Fuse.load_ckpt(path, device)
    synth_all = V.infer_all(synth_net, feat_all, synth_mu, synth_sd, WINDOW, device)
    del synth_net
    if device.type == "cuda":
        torch.cuda.empty_cache()
    blended = {ds: blend_one(vic_streams[ds], synth_all[ds], FUSE_W) for ds in vic_streams}
    off = N.official(held, N.to_ev(blended), P_STAY, SCALE)
    log_line(
        f"FUSE_w{FUSE_W} {held} p {P_STAY} sc {SCALE}  "
        f"H {off['H']:.4f} Acc {off['Acc']:.2f}")
    return off


def smoke_avgc(feat_all, device):
    held = "AVGC"
    train_ds = [d for d in DS_KEYS if d != held]
    groups, y_all = V.trial_groups(feat_all, train_ds)
    pre = int(os.environ.get("SMOKE_PRE", "80"))
    ft = int(os.environ.get("SMOKE_FT", "80"))
    log_line(f"SMOKE held-AVGC pre={pre} ft={ft}")
    ema, mu, sd, used, stats = fit_fold(
        groups, y_all, feat_all, train_ds, device, pre, ft, BATCH, "smoke-AVGC")
    scores, _ = score_lodo(ema, feat_all, mu, sd, held, device)
    avgc_acc = float(scores["vicreg"]["Acc"])
    avgc_h = float(scores["vicreg"]["H"])
    del ema
    if device.type == "cuda":
        torch.cuda.empty_cache()
    near_chance = (stats["frame_acc"] < 0.55) or (avgc_acc < 55.0) or (avgc_h < 0.70)
    ok = (stats.get("ft_loss") is not None and math.isfinite(stats["ft_loss"])
          and stats["frame_acc"] > 0.55 and avgc_acc >= 55.0 and avgc_h >= 0.70)
    row = dict(
        ok=ok, near_chance=near_chance, batch=used,
        avgc_H=avgc_h, avgc_Acc=avgc_acc, **stats)
    log_line(
        f"SMOKE ok={ok} near_chance={near_chance} frame={stats['frame_acc']:.3f} "
        f"AVGC {avgc_h:.4f}/{avgc_acc:.2f}")
    return row


def main():
    os.makedirs(os.path.dirname(LOG), exist_ok=True)
    open(LOG, "w").close()
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    log_line(
        f"device {device} pre {PRE_STEPS} ft {FT_STEPS} batch {BATCH} "
        f"sim/std/cov {SIM_COEFF}/{STD_COEFF}/{COV_COEFF} "
        f"synth {SYNTH_DIR} smoke={SMOKE}")
    torch.backends.cuda.matmul.allow_tf32 = True
    torch.backends.cudnn.allow_tf32 = True
    V.assert_causal(device)
    N.load_all()
    feat_all = V.build_features()["ms"]
    os.makedirs(CKPT_DIR, exist_ok=True)

    smoke_info = None
    if SMOKE or os.environ.get("FORCE_SMOKE", "1") == "1":
        smoke_info = smoke_avgc(feat_all, device)
        if smoke_info.get("near_chance") or not smoke_info.get("ok"):
            log_line("SMOKE ABORT_CHANCE")
            out = dict(smoke=smoke_info, abort="chance")
            os.makedirs(os.path.dirname(OUT), exist_ok=True)
            with open(OUT.replace(".json", "_smoke.json"), "w") as fh:
                json.dump(out, fh, indent=1, default=float)
            raise SystemExit(2)
        if SMOKE:
            out = dict(smoke=smoke_info)
            os.makedirs(os.path.dirname(OUT), exist_ok=True)
            with open(OUT.replace(".json", "_smoke.json"), "w") as fh:
                json.dump(out, fh, indent=1, default=float)
            log_line("SMOKE done; exiting without full score")
            return

    per = {}
    for held in DS_KEYS:
        train_ds = [d for d in DS_KEYS if d != held]
        groups, y_all = V.trial_groups(feat_all, train_ds)
        log_line(f"fold held-out={held} train={train_ds} pre={PRE_STEPS} ft={FT_STEPS}")
        ema, mu, sd, used, stats = fit_fold(
            groups, y_all, feat_all, train_ds, device, PRE_STEPS, FT_STEPS, BATCH,
            f"held-{held}")
        torch.save(
            {"state": ema.state_dict(), "mu": mu, "sd": sd,
             "train": train_ds, "pre_steps": PRE_STEPS, "ft_steps": FT_STEPS,
             "batch": used, "stats": stats, "cfg": CFG,
             "method": "vicreg-stem-pretrain"},
            os.path.join(CKPT_DIR, f"held-{held}.pt"))
        scores, streams = score_lodo(ema, feat_all, mu, sd, held, device)
        fuse_off = score_fuse_lodo(streams, feat_all, held, device)
        if fuse_off is not None:
            scores["fuse_w05"] = fuse_off
        per[held] = dict(scores=scores, stats=stats, batch=used)
        del ema
        if device.type == "cuda":
            torch.cuda.empty_cache()

    log_line(f"all-5 VICReg pretrain + FT for AASD and Carta")
    groups, y_all = V.trial_groups(feat_all, list(DS_KEYS))
    ema, mu, sd, used, stats = fit_fold(
        groups, y_all, feat_all, list(DS_KEYS), device, PRE_STEPS, FT_STEPS, BATCH, "all5")
    torch.save(
        {"state": ema.state_dict(), "mu": mu, "sd": sd,
         "train": list(DS_KEYS), "pre_steps": PRE_STEPS, "ft_steps": FT_STEPS,
         "batch": used, "stats": stats, "cfg": CFG,
         "method": "vicreg-stem-pretrain"},
        os.path.join(CKPT_DIR, "all5.pt"))
    corpus_streams = V.infer_all(ema, feat_all, mu, sd, WINDOW, device)
    ext_logits, ext_meta = load_external()
    ext_feat = V4.build_features_from(ext_logits)["ms"]
    ext = score_external(ema, ext_feat, ext_meta, mu, sd, corpus_streams, N.G["att"], device)

    fuse_ext = None
    synth_all5 = os.path.join(SYNTH_DIR, "all5.pt")
    if os.path.isfile(synth_all5):
        synth_net, synth_mu, synth_sd = Fuse.load_ckpt(synth_all5, device)
        synth_corpus = V.infer_all(synth_net, feat_all, synth_mu, synth_sd, WINDOW, device)
        synth_ext = V.infer_all(synth_net, ext_feat, synth_mu, synth_sd, WINDOW, device)
        del synth_net
        if device.type == "cuda":
            torch.cuda.empty_cache()
        blend_corpus = {
            ds: blend_one(corpus_streams[ds], synth_corpus[ds], FUSE_W)
            for ds in corpus_streams}
        parts = []
        for ds, table in blend_corpus.items():
            labels = N.G["att"][ds]
            for key, d in table.items():
                parts.append(np.asarray(d, np.float64) * np.where(labels[key] == 0, 1.0, -1.0))
        mu_e = float(np.concatenate(parts).mean())
        sd_e = float(np.concatenate(parts).std())
        vic_ext = V.infer_all(ema, ext_feat, mu, sd, WINDOW, device)
        fuse_ext = {}
        for ds in TEST_NAMES:
            blend_stream = blend_one(vic_ext[ds], synth_ext[ds], FUSE_W)
            ev = {}
            for key, d in blend_stream.items():
                meta_k = ext_meta[ds][key]
                d = np.asarray(d, np.float64)
                ev[key] = dict(
                    r0=d, r1=np.zeros(len(d)), att=meta_k["att"], t_end=meta_k["t_end"],
                    att128=meta_k["att128"], n128=int(meta_k["n128"]),
                    subject=meta_k["subject"])
            r = E.score(ev, mu_e, sd_e * SCALE, P_STAY, 0.75)
            fuse_ext[ds] = r
            log_line(
                f"FUSE_w{FUSE_W} EXT {ds} p {P_STAY} sc {SCALE}  "
                f"H {r['H']:.4f} Acc {r['Acc']:.2f}")
        del ema
    else:
        del ema
        log_line(f"FUSE ext skipped; missing {synth_all5}")

    mean_h = float(np.mean([per[d]["scores"]["vicreg"]["H"] for d in DS_KEYS]))
    mean_acc = float(np.mean([per[d]["scores"]["vicreg"]["Acc"] for d in DS_KEYS]))
    log_line(f"LODO mean H {mean_h:.4f} Acc {mean_acc:.2f}")
    avgc = per["AVGC"]["scores"]["vicreg"]
    log_line(
        f"AVGC vicreg {avgc['H']:.4f}/{avgc['Acc']:.2f} vs tip "
        f"{TIP_AVGC['H']:.4f}/{TIP_AVGC['Acc']:.2f} "
        f"ΔH {avgc['H'] - TIP_AVGC['H']:+.4f} ΔAcc {avgc['Acc'] - TIP_AVGC['Acc']:+.2f}")
    fuse_mean_h = fuse_mean_acc = None
    if "fuse_w05" in per["AVGC"]["scores"]:
        fav = per["AVGC"]["scores"]["fuse_w05"]
        log_line(
            f"AVGC fuse_w{FUSE_W} {fav['H']:.4f}/{fav['Acc']:.2f} vs tip "
            f"{TIP_AVGC['H']:.4f}/{TIP_AVGC['Acc']:.2f} "
            f"ΔH {fav['H'] - TIP_AVGC['H']:+.4f} ΔAcc {fav['Acc'] - TIP_AVGC['Acc']:+.2f}")
        fuse_mean_h = float(np.mean([per[d]["scores"]["fuse_w05"]["H"] for d in DS_KEYS
                                     if "fuse_w05" in per[d]["scores"]]))
        fuse_mean_acc = float(np.mean([per[d]["scores"]["fuse_w05"]["Acc"] for d in DS_KEYS
                                       if "fuse_w05" in per[d]["scores"]]))
        log_line(f"FUSE_w{FUSE_W} LODO mean H {fuse_mean_h:.4f} Acc {fuse_mean_acc:.2f}")

    out = dict(
        p_stay=P_STAY, scale=SCALE, pre_steps=PRE_STEPS, ft_steps=FT_STEPS, batch=BATCH,
        sim_coeff=SIM_COEFF, std_coeff=STD_COEFF, cov_coeff=COV_COEFF,
        synth_dir=SYNTH_DIR, fuse_w=FUSE_W, smoke=smoke_info,
        tip_avgc=TIP_AVGC, tip_lodo=TIP_LODO,
        lodo={d: per[d]["scores"] for d in DS_KEYS},
        lodo_stats={d: per[d]["stats"] for d in DS_KEYS},
        lodo_mean=dict(H=mean_h, Acc=mean_acc),
        fuse_lodo_mean=(dict(H=fuse_mean_h, Acc=fuse_mean_acc)
                        if fuse_mean_h is not None else None),
        external=ext,
        fuse_external=fuse_ext,
        all5_stats=stats,
    )
    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    with open(OUT, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    log_line(f"wrote {OUT}")


if __name__ == "__main__":
    main()
