"""Domain-adversarial (DANN/GRL) fine-tune of stem_ms96 from focal-IRM.

Ganin & Lempitsky: gradient-reversal on pooled stem features + domain CE
over train dataset ids. LODO: held-out dataset is never a domain label.
Init models/stem_ms96_focal_avgc/{held-*,all5}.pt (fallback IRM via INIT_DIR).
Restore causal -inf mask. EMA 0.995 eval. HMM preset 0.994 / 0.8.
Also fuse w=0.5 with frozen stem_synth_switch (tip protocol).

λ(p) = λ_max * (2/(1+exp(-10 p)) − 1), p=step/steps (classic DANN ramp).
Smoke: short AVGC-fold train; abandon if frame-acc ~chance or AVGC Acc≪tip.

Distinct from IRM penalty, PCGrad, CORAL-on-logits, Mixup/focal re-grids.

usage:
  DANN_LMAX=0.5 STEPS=800 python stem_ms96_dann_grl.py
  SMOKE=1 STEPS=40 python stem_ms96_dann_grl.py
  TUNE_LMAX=1 python ...   # smoke λ_max then full LODO
"""
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
from torch.autograd import Function

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__))
INIT_DIR = os.environ.get(
    "INIT_DIR", os.path.join(EXP, "models", "stem_ms96_focal_avgc"))
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_dann_grl")
LOG = os.path.join(EXP, "experiments", "stem_ms96_dann_grl.log")
OUT = os.path.join(EXP, "results", "research", "stem_ms96_dann_grl.json")
EVID = "/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/decoder_lodo/results/research"
TEST_NAMES = ("AASD", "Carta2026")

STEPS = int(os.environ.get("STEPS", "800"))
BATCH = int(os.environ.get("BATCH", "512"))
DANN_LMAX = float(os.environ.get("DANN_LMAX", "0.5"))
SMOKE = os.environ.get("SMOKE", "0") == "1"
EMA_DECAY = 0.995
LR = 8e-4
P_STAY = 0.994
SCALE = 0.8
CFG = ("stem_ms96", "ms", 96, 64, 2, True, LR)
WINDOW = 96
FUSE_W = 0.5
TIP_AVGC = dict(H=0.8226, Acc=75.54)
TIP_LODO = dict(H=0.8821, Acc=83.36)


class GradReverse(Function):
    @staticmethod
    def forward(ctx, x, lambd):
        ctx.lambd = float(lambd)
        return x.view_as(x)

    @staticmethod
    def backward(ctx, grad_output):
        return -ctx.lambd * grad_output, None


def grl(x, lambd):
    return GradReverse.apply(x, lambd)


class DomainHead(nn.Module):
    def __init__(self, d_model, n_domains, hidden=None):
        super().__init__()
        h = hidden or d_model
        self.net = nn.Sequential(
            nn.Linear(d_model, h),
            nn.ReLU(inplace=True),
            nn.Dropout(0.1),
            nn.Linear(h, n_domains),
        )

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


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 load_init(path, device):
    blob = torch.load(path, map_location="cpu", weights_only=False)
    name, family, window, d_model, nlayers, stem, _lr = blob["cfg"]
    if name != "stem_ms96" or not stem or window != WINDOW:
        raise RuntimeError(f"unexpected stem cfg {blob['cfg']}")
    c_in = int(np.asarray(blob["mu"]).shape[0])
    net = V.LogitTransformer(c_in, window, d_model=d_model, nlayers=nlayers, stem=True)
    good_mask = net.mask.detach().clone()
    net.load_state_dict(blob["state"])
    net.mask.copy_(good_mask)
    if torch.isnan(net.mask).any():
        raise RuntimeError(f"causal mask still has nan after restore from {path}")
    net = net.to(device)
    mu = np.asarray(blob["mu"], np.float32)
    sd = np.asarray(blob["sd"], np.float32)
    return net, mu, sd, list(blob["train"]), int(d_model)


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):
    """Masked mean pool over time → [B, d]."""
    w = m.unsqueeze(-1)
    return (h * w).sum(1) / w.sum(1).clamp_min(1.0)


def dann_lambda(step, total, lmax):
    p = step / max(total - 1, 1)
    return float(lmax) * (2.0 / (1.0 + math.exp(-10.0 * p)) - 1.0)


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 train_dann(model, domain_head, groups, y_all, mu, sd, device, steps, batch, tag, lmax):
    n_env = len(groups)
    pos = float(y_all.mean())
    pos_weight = torch.tensor([(1.0 - pos) / max(pos, 1e-3)], device=device)
    params = list(model.parameters()) + list(domain_head.parameters())
    opt = torch.optim.AdamW(params, lr=LR, weight_decay=1e-4)
    rng = np.random.default_rng(V.TRAIN_SEED)
    ema = copy.deepcopy(model).eval()
    model.train()
    domain_head.train()
    t0 = time.time()
    last = last_clf = last_dom = last_lam = last_dacc = None
    for step in range(steps):
        set_lr(opt, step, steps, LR)
        lam = dann_lambda(step, steps, lmax)
        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)
        # Dataset-balanced: row i → domain i % n_env (held-out never included in groups)
        domain_y = torch.arange(x.size(0), device=device) % n_env
        opt.zero_grad(set_to_none=True)
        logit, h = encode_features(model, x)
        clf_loss = weighted_bce(logit, y, m, pos_weight)
        feat = pool_feat(h, m)
        d_logits = domain_head(grl(feat, lam))
        dom_loss = F.cross_entropy(d_logits, domain_y)
        loss = clf_loss + lam * dom_loss
        if not torch.isfinite(loss):
            raise RuntimeError(
                f"non-finite loss at step {step} for {tag}: "
                f"clf={clf_loss.item()} dom={dom_loss.item()} lam={lam}")
        loss.backward()
        torch.nn.utils.clip_grad_norm_(params, 1.0)
        opt.step()
        V.ema_update(model, ema, EMA_DECAY)
        with torch.no_grad():
            dacc = float((d_logits.argmax(-1) == domain_y).float().mean().item())
        last = float(loss.item())
        last_clf = float(clf_loss.item())
        last_dom = float(dom_loss.item())
        last_lam = lam
        last_dacc = dacc
        if step == 0 or (step + 1) % 200 == 0 or step + 1 == steps:
            log_line(
                f"    {tag} step {step + 1}/{steps} loss {last:.4f} "
                f"clf {last_clf:.4f} dom {last_dom:.4f} λ {last_lam:.4f} "
                f"dacc {last_dacc:.3f} n_env {n_env}")
    ema.eval()
    acc = V.train_frame_acc(ema, groups, WINDOW, mu, sd, device)
    log_line(
        f"  trained {tag} loss {last:.4f} clf {last_clf:.4f} dom {last_dom:.4f} "
        f"λ_end {last_lam:.4f} dacc {last_dacc:.3f} frame-acc {acc:.3f} "
        f"n_env {n_env} lmax {lmax} ({time.time() - t0:.0f}s)")
    return ema, dict(
        loss=last, clf=last_clf, dom=last_dom, lam_end=last_lam,
        domain_acc=last_dacc, frame_acc=acc, n_env=n_env, lmax=lmax)


def fit_with_oom(init_path, groups, y_all, device, steps, batch, tag, lmax, halved=False):
    model, mu, sd, _train, d_model = load_init(init_path, device)
    n_env = len(groups)
    domain_head = DomainHead(d_model, n_env).to(device)
    try:
        ema, stats = train_dann(
            model, domain_head, groups, y_all, mu, sd, device, steps, batch, tag, lmax)
        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, domain_head
        if device.type == "cuda":
            torch.cuda.empty_cache()
        return fit_with_oom(
            init_path, groups, y_all, device, steps, batch // 2, tag, lmax, 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(dann=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=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=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(dann=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(dann_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(dann_streams[ds], synth_all[ds], FUSE_W) for ds in dann_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_lmax(feat_all, device):
    """Short train on held-AVGC; pick λ_max; abort if near-chance."""
    held = "AVGC"
    path = os.path.join(INIT_DIR, f"held-{held}.pt")
    if not os.path.isfile(path):
        # fallback IRM
        alt = os.path.join(EXP, "models", "stem_ms96_irm", f"held-{held}.pt")
        if os.path.isfile(alt):
            path = alt
            log_line(f"SMOKE init fallback {alt}")
        else:
            raise FileNotFoundError(path)
    train_ds = [d for d in DS_KEYS if d != held]
    groups, y_all = V.trial_groups(feat_all, train_ds)
    lmaxs = [float(x) for x in os.environ.get("SMOKE_LMAXS", "0.1,0.5,1.0").split(",")]
    smoke_steps = int(os.environ.get("SMOKE_STEPS", "60"))
    results = []
    for lmax in lmaxs:
        tag = f"smoke-lmax{lmax}"
        log_line(f"SMOKE λ_max={lmax} steps={smoke_steps}")
        try:
            ema, mu, sd, used, stats = fit_with_oom(
                path, groups, y_all, device, smoke_steps, BATCH, tag, lmax)
            # Quick AVGC official score after smoke
            scores, _ = score_lodo(ema, feat_all, mu, sd, held, device)
            avgc_acc = float(scores["dann"]["Acc"])
            avgc_h = float(scores["dann"]["H"])
            del ema
            if device.type == "cuda":
                torch.cuda.empty_cache()
            near_chance = (stats["frame_acc"] < 0.55) or (avgc_acc < 55.0)
            ok = (stats["loss"] is not None and math.isfinite(stats["loss"])
                  and stats["frame_acc"] > 0.55 and avgc_acc >= 55.0)
            row = dict(
                ok=ok, near_chance=near_chance, batch=used,
                avgc_H=avgc_h, avgc_Acc=avgc_acc, **stats)
            results.append(row)
            log_line(
                f"SMOKE λ_max={lmax} ok={ok} near_chance={near_chance} "
                f"loss={stats['loss']:.4f} frame={stats['frame_acc']:.3f} "
                f"AVGC {avgc_h:.4f}/{avgc_acc:.2f} dacc={stats['domain_acc']:.3f}")
        except Exception as exc:
            log_line(f"SMOKE λ_max={lmax} FAILED: {exc}")
            results.append(dict(lmax=lmax, ok=False, near_chance=True, error=str(exc)))
            if device.type == "cuda":
                torch.cuda.empty_cache()
    ok_rows = [r for r in results if r.get("ok")]
    if not ok_rows:
        return None, results
    best = max(ok_rows, key=lambda r: (r["avgc_H"], r["frame_acc"], -abs(r["lmax"] - 0.5)))
    log_line(
        f"SMOKE chose λ_max={best['lmax']} AVGC {best['avgc_H']:.4f}/{best['avgc_Acc']:.2f} "
        f"frame={best['frame_acc']:.3f}")
    return float(best["lmax"]), results


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} steps {STEPS} batch {BATCH} λ_max {DANN_LMAX} "
        f"init {INIT_DIR} 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)

    init_dir = INIT_DIR
    if not os.path.isfile(os.path.join(init_dir, "held-AVGC.pt")):
        alt = os.path.join(EXP, "models", "stem_ms96_irm")
        if os.path.isfile(os.path.join(alt, "held-AVGC.pt")):
            init_dir = alt
            log_line(f"init_dir fallback → {init_dir}")

    lmax = DANN_LMAX
    smoke_info = None
    if SMOKE or os.environ.get("TUNE_LMAX", "0") == "1":
        # smoke_lmax reads INIT_DIR env/module; temporarily point it
        globals()["INIT_DIR"] = init_dir
        chosen, smoke_info = smoke_lmax(feat_all, device)
        if chosen is None:
            log_line("SMOKE ABORT_CHANCE: all λ_max near-chance or failed")
            out = dict(smoke=smoke_info, abort="chance", init_dir=init_dir)
            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)
        lmax = chosen
        if SMOKE:
            out = dict(smoke=smoke_info, chosen_lmax=lmax, init_dir=init_dir)
            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:
        path = os.path.join(init_dir, f"held-{held}.pt")
        if not os.path.isfile(path):
            raise FileNotFoundError(path)
        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} init {path} λ_max={lmax} domains={train_ds}")
        ema, mu, sd, used, stats = fit_with_oom(
            path, groups, y_all, device, STEPS, BATCH, f"held-{held}", lmax)
        torch.save(
            {"state": ema.state_dict(), "mu": mu, "sd": sd, "init": path,
             "train": train_ds, "steps": STEPS, "batch": used,
             "dann_lmax": lmax, "stats": stats, "cfg": CFG},
            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 DANN fine-tune for AASD and Carta, "
             f"init {init_dir}/all5.pt λ_max={lmax}")
    groups, y_all = V.trial_groups(feat_all, list(DS_KEYS))
    all_path = os.path.join(init_dir, "all5.pt")
    ema, mu, sd, used, stats = fit_with_oom(
        all_path, groups, y_all, device, STEPS, BATCH, "all5", lmax)
    torch.save(
        {"state": ema.state_dict(), "mu": mu, "sd": sd, "init": all_path,
         "train": list(DS_KEYS), "steps": STEPS, "batch": used,
         "dann_lmax": lmax, "stats": stats, "cfg": CFG},
        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())
        dann_ext = V.infer_all(ema, ext_feat, mu, sd, WINDOW, device)
        fuse_ext = {}
        for ds in TEST_NAMES:
            blend_stream = blend_one(dann_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=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"]["dann"]["H"] for d in DS_KEYS]))
    mean_acc = float(np.mean([per[d]["scores"]["dann"]["Acc"] for d in DS_KEYS]))
    log_line(f"LODO mean H {mean_h:.4f} Acc {mean_acc:.2f}")
    avgc = per["AVGC"]["scores"]["dann"]
    log_line(
        f"AVGC dann {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, steps=STEPS, batch=BATCH,
        dann_lmax=lmax, init_dir=init_dir, 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()
