"""EEGNet 10 s direct-classifier backbone (Lawhern 2018; reserved for eegnet-seed).

Drop-in for vlaai_ws: eaP_n5_t60 + sw2, antisymmetric g(EEG,A,B)-g(EEG,B,A).
Dense temporal output (B,W,d); no AvgPool stride>1. Random init (SPK shapes
mismatch). Swap-test before train. Chance-abort if loss~0.693 after ~50 steps
on the first fold → Path B (causal EEG filterbank → vlaai_ws), do not burn GPU
on a second MI backbone.

usage:
  GLIMPSE_THR=norm python eegnet_fir_lodo.py
  GLIMPSE_THR=norm PATH_B=1 python eegnet_fir_lodo.py   # force Path B
"""
import json
import os
import pickle
import time
from types import SimpleNamespace

import numpy as np
import torch
import torch.nn.functional as F
from scipy.signal import butter, sosfilt

import decoders
import direct_classifier as DC
import ensemble_clf_transformer_v4 as V4
import eval_external as E
import score as S
from common import DS_KEYS, SEED
from common_exp import CFG, EXP, FS, fold_file, load_trial, rows_of
from decoders import D, DEV, _targets
from e1_switch import draw_switch_times
from run_experiment import load_external, log_to

EXP_NAME = "direct_clf__eaP_n5_t60_w10_eegnet_aug-sw2"
TAG = "_w10_eegnet_aug-sw2"
LOG_PATH = os.path.join(EXP, "experiments", "eegnet_lodo.log")
OUT_JSON = os.path.join(EXP, "results", f"{EXP_NAME}.json")
ALL5_PT = os.path.join(EXP, "models", f"all5_direct_clf{TAG}.pt")
CKPT_DIR = os.path.join(EXP, "models", "eegnet")
EXT_DIR = os.path.join(EXP, "results", "research")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_eegnet.md")
# Path B with SPK warm-start learns like SE (~4 ep); without SPK needs ~12+ ep plateau.
MAX_EPOCHS = int(os.environ.get("MAX_EPOCHS", "8"))
MIN_EPOCHS = int(os.environ.get("MIN_EPOCHS", "4"))
P_STAY, SCALE = 0.994, 0.8
WINDOW_S = 10.0
BATCH = int(os.environ.get("BATCH", "64"))
BACKBONE = "eegnet"
CHANCE_STEPS = int(os.environ.get("CHANCE_STEPS", "50"))
CHANCE_LOSS = float(os.environ.get("CHANCE_LOSS", "0.685"))
FORCE_PATH_B = os.environ.get("PATH_B", "").strip() in ("1", "true", "yes")

# Path B: in-net causal FIR banks → zero-init mix residual → vlaai_ws (SPK into bb.bb).
# Distinct from filterbank-only C=232 (chance) and raw||banks C=290 expand (OOM).
FB_BANDS = ((1.0, 3.0), (3.0, 5.0), (5.0, 7.0), (7.0, 9.0))
FB_EXP_NAME = "direct_clf__eaP_n5_t60_w10b3_ptSPK_fbfir_aug-sw2"
FB_TAG = "_w10b3_ptSPK_fbfir_aug-sw2"
FB_CKPT_DIR = os.path.join(EXP, "models", "fbfir")
FB_ALL5_PT = os.path.join(EXP, "models", f"all5_direct_clf{FB_TAG}.pt")
FB_INIT = (
    "/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/"
    "decoder_lodo/models/pretrain_spk_direct_clf_w10_b3_s0.pt"
)
N_EEG = 58
N_EEG_FB = N_EEG * (1 + len(FB_BANDS))  # raw || 4 banks = 290 (expand path; unused)
FB_BACKBONE = "fb_vlaai"


class ChanceStuckError(RuntimeError):
    """Train loss stuck near chance — abort Path A per protocol."""


def log_line(text):
    print(text, flush=True)


def load_spk_into_fb(net, init_path, log):
    """Map published DirectNet SPK weights into FBExtraVLAAI (bb.* → bb.bb.*)."""
    from common_exp import resolve_model
    path = resolve_model(init_path)
    sd = torch.load(path, map_location=DEV, weights_only=False)
    mapped = {}
    for k, v in sd.items():
        if k.startswith("bb."):
            mapped["bb.bb." + k[3:]] = v
        else:
            mapped[k] = v  # head, tw
    missing, unexpected = net.load_state_dict(mapped, strict=False)
    # FIR/mix stay at identity residual; missing those is expected.
    log(f"  SPK→fb_vlaai from {path} (strict=False; missing {len(missing)} "
        f"unexpected {len(unexpected)})")
    return missing, unexpected


def fit_net(train, val, log, a, check_chance=True, label="EEGNet", filterbank=False):
    Fs = train[0]["_S"].shape[-1]
    C_raw = int(train[0]["eeg"].shape[-1])
    C = N_EEG_FB if filterbank else C_raw
    seed = D.get("train_seed", CFG["seed"])
    torch.manual_seed(seed)
    net = DC.DirectNet(C, Fs, a.backbone).to(DEV)
    if getattr(a, "init_from", None) and a.backbone == FB_BACKBONE and not filterbank:
        load_spk_into_fb(net, a.init_from, log)
    n_param = sum(p.numel() for p in net.parameters())
    n_train = sum(p.numel() for p in net.parameters() if p.requires_grad)
    log(f"  {label} DirectNet params {n_param} (trainable {n_train}) C={C} (raw {C_raw}) "
        f"filterbank={filterbank} backbone={a.backbone} batch {D['batch_size']}")
    aug = decoders.DNNDec.__new__(decoders.DNNDec)
    aug.W, aug.fut, aug.hop, aug.C = DC.W, DC.FUT, DC.HOP, N_EEG
    rs = np.random.default_rng(seed)
    windows_fn = (lambda trials, n, rng, aug_, p_sw, zero:
                  windows_fb(trials, n, rng, aug_, p_sw, zero)) if filterbank else DC.windows
    Xv, Sv, yv = windows_fn(val, D["val_windows"], rs, None, a.p_switch, a.zero_eeg)
    opt = torch.optim.AdamW(
        [p for p in net.parameters() if p.requires_grad],
        lr=D["lr"], weight_decay=D["weight_decay"])
    sch = torch.optim.lr_scheduler.ReduceLROnPlateau(
        opt, mode="min", factor=0.5, patience=D["lr_patience"])
    best, bad, state = np.inf, 0, None
    global_step, early = 0, []
    for ep in range(D["max_epochs"]):
        t1, tl = time.time(), []
        net.train()
        for _ in range(D["steps_per_epoch"]):
            X, S_, y = [z.to(DEV) for z in windows_fn(
                train, D["batch_size"], rs, aug, a.p_switch, a.zero_eeg)]
            loss = F.binary_cross_entropy_with_logits(net(X, S_), y)
            opt.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(net.parameters(), 5.0)
            opt.step()
            lv = float(loss.detach())
            tl.append(lv)
            global_step += 1
            if check_chance and global_step <= CHANCE_STEPS:
                early.append(lv)
                if global_step == CHANCE_STEPS:
                    mean50 = float(np.mean(early))
                    log(f"    chance-probe step {CHANCE_STEPS} mean_loss {mean50:.4f} "
                        f"(abort if >={CHANCE_LOSS})")
                    if mean50 >= CHANCE_LOSS:
                        raise ChanceStuckError(
                            f"loss {mean50:.4f} still ~chance after {CHANCE_STEPS} steps")
        if filterbank:
            clear_fb_cache()  # drop train-cache before val rebuild / next epoch
        net.eval()
        with torch.no_grad():
            lv = torch.cat([
                net(Xv[i:i + 256].to(DEV), Sv[i:i + 256].to(DEV)).cpu()
                for i in range(0, len(Xv), 256)])
        vl = float(F.binary_cross_entropy_with_logits(lv, yv))
        va = float(((lv > 0).float() == yv).float().mean())
        if ep >= a.min_epochs:
            sch.step(vl)
        log(f"    epoch {ep:02d} train {np.mean(tl):.4f} val BCE {vl:.4f} acc {va:.3f} "
            f"({time.time() - t1:.0f}s)")
        if vl < best - 1e-4:
            best, bad, state = vl, 0, {k: v.detach().clone() for k, v in net.state_dict().items()}
        elif ep >= a.min_epochs:
            bad += 1
            if bad >= D["patience"]:
                break
    if state is None:
        state = {k: v.detach().clone() for k, v in net.state_dict().items()}
    net.load_state_dict(state)
    net.eval()
    return net, best


# Lazy full-trial filterbank cache with LRU cap (full 3k-trial expand OOMs).
_FB_CACHE = {}
_FB_CACHE_ORDER = []
_FB_CACHE_MAX = int(os.environ.get("FB_CACHE_MAX", "128"))


def clear_fb_cache():
    _FB_CACHE.clear()
    _FB_CACHE_ORDER.clear()


def eeg_fb(tr):
    key = id(tr)
    hit = _FB_CACHE.get(key)
    if hit is None:
        hit = filterbank_eeg(tr["eeg"])
        _FB_CACHE[key] = hit
        _FB_CACHE_ORDER.append(key)
        while len(_FB_CACHE_ORDER) > _FB_CACHE_MAX:
            old = _FB_CACHE_ORDER.pop(0)
            _FB_CACHE.pop(old, None)
    return hit


def windows_fb(trials, n, rng, aug, p_sw, zero_eeg):
    """Sample windows from lazily filterbanked full trials → (n, W, 58*n_bands).

    Augment on the 58-ch raw window then re-filter that window only when aug is
    on (rare relative to val); val path uses cached full-trial banks.
    """
    C_out = N_EEG_FB
    lens = np.array([len(t["eeg"]) - DC.W for t in trials])
    p = np.maximum(lens, 0).astype(float)
    ti = rng.choice(len(trials), n, p=p / p.sum())
    X = np.empty((n, DC.W, C_out), np.float32)
    S = np.empty((n, DC.W) + trials[0]["_S"].shape[1:], np.float32)
    y = np.empty(n, np.float32)
    A = (D.get("augment") or {}) if aug is not None else {}
    lag, p_fd, p_sw2 = int(A.get("clf_lag_jitter", 0)), A.get("clf_p_feat_drop", 0.0), A.get("clf_p_switch2", 0.0)
    use_aug = aug is not None
    for j, i in enumerate(ti):
        s = rng.integers(0, lens[i] + 1)
        tr = trials[i]
        s2 = int(np.clip(s + rng.integers(-lag, lag + 1), 0, lens[i])) if lag else s
        S[j] = tr["_S"][s2:s2 + DC.W]
        col = int(tr["att"][s + DC.LAB])
        if rng.random() < p_sw:
            tau = int(rng.integers(FS // 2, DC.W - FS // 2))
            S[j, tau:] = S[j, tau:, ::-1]
            col = 1 - col if tau <= DC.LAB else col
            if p_sw2 and rng.random() < p_sw2 and DC.W - FS // 2 - tau > 2 * FS:
                tau2 = int(rng.integers(tau + 2 * FS, DC.W - FS // 2))
                S[j, tau2:] = S[j, tau2:, ::-1]
                col = 1 - col if tau2 <= DC.LAB else col
        if p_fd and rng.random() < p_fd:
            S[j, :, :, rng.integers(S.shape[-1])] = 0.0
        y[j] = 1.0 if col == 0 else 0.0
        fb = eeg_fb(tr)
        chunk = fb[s:s + DC.W].copy()
        if use_aug:
            # Light EEG aug on filterbank features (gain + noise). Skip cluster
            # drop (montage is 58-ch); full-trial SOS state stays correct.
            g = 1.0 + 0.25 * (2 * rng.random(C_out).astype(np.float32) - 1)
            chunk *= g[None, :]
            chunk += (0.1 * rng.standard_normal(chunk.shape)).astype(np.float32)
        if zero_eeg:
            chunk[:] = 0.0
        X[j] = chunk
    return (torch.from_numpy(X), torch.from_numpy(S), torch.from_numpy(y))


def filterbank_eeg(eeg, bands=FB_BANDS, fs=FS):
    """Causal SOS banks stacked with raw EEG. (T,58)→(T,58*(1+n_bands))."""
    x = eeg.astype(np.float64)
    parts = [eeg.astype(np.float32)]
    for lo, hi in bands:
        sos = butter(2, [lo, hi], btype="bandpass", fs=fs, output="sos")
        parts.append(sosfilt(sos, x, axis=0).astype(np.float32))
    return np.concatenate(parts, axis=-1)


def logits_c(net, x, S, step=1):
    """Like DC.logits but pads with x.shape[-1] (Path B C≠58)."""
    C = int(x.shape[-1])
    T = len(x)
    xp = np.concatenate([
        np.zeros((DC.POS0, C), np.float32),
        x.astype(np.float32),
        np.zeros((DC.FUT + DC.HOP, C), np.float32)])
    Sp = np.concatenate([
        np.zeros((DC.POS0,) + S.shape[1:], np.float32),
        S,
        np.zeros((DC.FUT + DC.HOP,) + S.shape[1:], np.float32)])
    starts = np.arange(0, T, DC.HOP * step)
    out = np.empty(len(starts), np.float32)
    for i in range(0, len(starts), 1024):
        a = starts[i:i + 1024]
        out[i:i + len(a)] = net(
            torch.from_numpy(np.stack([xp[k:k + DC.W] for k in a])).to(DEV),
            torch.from_numpy(np.stack([Sp[k:k + DC.W] for k in a])).to(DEV)).cpu().numpy()
    return out, starts


def trial_logits(net, x, S, step=1):
    if int(x.shape[-1]) == N_EEG:
        return DC.logits(net, x, S, step=step)
    return logits_c(net, x, S, step=step)


def configure(path="a"):
    DC.W = int(WINDOW_S * FS)
    DC.POS0, DC.LAB = DC.W - DC.FUT - DC.HOP, DC.W - FS
    D["targets"] = ["env", "onset", "dom"]
    D["augment"] = {**CFG["dnn_augment"], **CFG["aug_variants"]["cluster"], "clf_p_switch2": 0.5}
    D["train_seed"] = int(CFG["seed"])
    D["max_epochs"] = MAX_EPOCHS
    D["steps_per_epoch"] = 250
    D["batch_size"] = BATCH
    D["patience"] = 6
    if path == "a":
        # F1=64 matches ATCNet/dense front capacity; classic Lawhern used F1=8 on MI.
        D["model_dropout"] = 0.5
        D["model_F1"] = 64
        D["model_depth"] = 2
        D["model_kern"] = 64
        D["model_sep_kern"] = 16
        D["model_d"] = 64
    else:
        D["model_blocks"] = 3
        D["model_dropout"] = 0.0
        D["model_kernels"] = 64
        D["model_n_raw"] = N_EEG
        D["model_n_bands"] = len(FB_BANDS)
        # Full-trial filterbank makes 2000 val windows expensive; 512 is enough.
        D["val_windows"] = int(os.environ.get("VAL_WINDOWS", "512"))
    decoders.apply_overrides([], ["clf_p_switch2=0.5"])


def namespace(path="a"):
    if path == "a":
        return SimpleNamespace(
            backbone=BACKBONE, p_switch=0.5, zero_eeg=False, min_epochs=MIN_EPOCHS,
            init_from=None, prep="eaP_n5_t60", tag=TAG,
        )
    return SimpleNamespace(
        backbone=FB_BACKBONE, p_switch=0.5, zero_eeg=False, min_epochs=MIN_EPOCHS,
        init_from=FB_INIT, prep="eaP_n5_t60", tag=FB_TAG,
    )


def swap_test(log, a, C=N_EEG):
    """Speaker-order must change the DirectNet logit."""
    torch.manual_seed(0)
    net = DC.DirectNet(C, 3, a.backbone).to(DEV).eval()
    with torch.no_grad():
        E = torch.randn(8, DC.W, C, device=DEV)
        A = torch.randn(8, DC.W, 3, device=DEV)
        B = torch.randn(8, DC.W, 3, device=DEV)
        S_ab = torch.stack([A, B], dim=2)
        S_ba = torch.stack([B, A], dim=2)
        y_ab = net(E, S_ab)
        y_ba = net(E, S_ba)
        anti_err = float((y_ab + y_ba).abs().mean())
        gab = net.g(E, A, B)
        gba = net.g(E, B, A)
        delta = float((gab - gba).abs().mean())
        gab0 = net.g(torch.zeros_like(E), A, B)
        gba0 = net.g(torch.zeros_like(E), B, A)
        delta0 = float((gab0 - gba0).abs().mean())
    n_param = sum(p.numel() for p in net.parameters())
    log(f"swap-test params {n_param} C={C} anti_err {anti_err:.3e} "
        f"swap_delta {delta:.4f} zero_eeg_delta {delta0:.4f}")
    del net
    if DEV.type == "cuda":
        torch.cuda.empty_cache()
    if anti_err > 1e-5:
        raise RuntimeError(f"antisymmetry broken: mean |y_ab+y_ba|={anti_err}")
    if delta < 1e-4:
        raise RuntimeError(
            f"swap-delta≈0 ({delta:.3e}): speaker order ignored — fix wiring before train")
    return delta, delta0, anti_err


def run_fold(held, exp, log, a, check_chance=False, ckpt_dir=CKPT_DIR, label="EEGNet",
             filterbank=False):
    rng = np.random.default_rng(CFG["seed"] + DS_KEYS.index(held))
    train, val = [], []
    for ds in DS_KEYS:
        if ds == held:
            continue
        rows = rows_of(ds)
        vs = rng.choice(sorted(rows.subject.unique()))
        for _, r in rows.iterrows():
            (val if r["subject"] == vs else train).append(load_trial(a.prep, r["key"]))
    train += load_external(["Fuglsang2020"], a.prep, log)
    for tr in train + val:
        tr["_S"] = _targets(tr)
    C = N_EEG_FB if filterbank else N_EEG
    if filterbank:
        clear_fb_cache()
        log(f"  Path B lazy filterbank raw||banks={list(FB_BANDS)} → C={C} "
            f"backbone={a.backbone}")
    log(f"  fold held-out={held}: {len(train)} train / {len(val)} val, W={DC.W / FS:g}s "
        f"C={C} chance_probe={'on' if check_chance else 'off'}")
    t0 = time.time()
    net, best = fit_net(train, val, log, a, check_chance=check_chance, label=label,
                        filterbank=filterbank)
    t_fit = time.time() - t0
    clear_fb_cache()
    del train, val
    os.makedirs(ckpt_dir, exist_ok=True)
    ckpt = os.path.join(ckpt_dir, f"held-{held}.pt")
    torch.save(net.state_dict(), ckpt)
    log(f"  saved {ckpt}")
    srng = np.random.default_rng(SEED + DS_KEYS.index(held))
    ev, ctl = {}, {k: {} for k in ("normal", "zero_eeg", "shift_eeg", "no_frontal")}
    for _, r in rows_of(held).iterrows():
        tr = load_trial(a.prep, r["key"])
        x_raw = tr["eeg"]
        x = filterbank_eeg(x_raw) if filterbank else x_raw
        Sfeat, att, T128 = _targets(tr), tr["att"].astype(int), int(r["n"])
        times = draw_switch_times(srng, T128 / 128)
        srng.integers(0, 2)
        st128, cur, prev = np.zeros(T128, bool), False, 0
        for tt in times:
            s0 = min(T128 - 1, int(round(tt * 128)))
            st128[prev:s0] = cur
            cur, prev = not cur, s0
        st128[prev:] = cur
        st = st128[np.minimum(T128 - 1, np.round((np.arange(len(x)) / FS + tr["t0"]) * 128).astype(int))]
        Ssw = np.where(st[:, None, None], Sfeat[:, ::-1], Sfeat)
        lg, starts = trial_logits(net, x, Ssw)
        truth = (att ^ st)[np.maximum(starts - DC.HOP, 0)]
        n10 = len(lg) // 40
        ev[r["key"]] = dict(
            r0=lg / 2, r1=-lg / 2, att=truth.astype(np.int8),
            t_end=(starts / FS + tr["t0"]).astype(np.float32),
            r0_10=lg[:n10 * 40].reshape(n10, 40).mean(1) / 2,
            r1_10=-lg[:n10 * 40].reshape(n10, 40).mean(1) / 2,
            att10=(truth[:n10 * 40].reshape(n10, 40).mean(1) > 0.5).astype(np.int8),
            pre_switched=True)
        xf = x.copy()
        if filterbank:
            # zero frontal on raw and every bank copy
            for b in range(1 + len(FB_BANDS)):
                base = b * N_EEG
                xf[:, [base + i for i in DC.FRONTAL]] = 0.0
        else:
            xf[:, DC.FRONTAL] = 0.0
        for name, xc in (("normal", x), ("zero_eeg", np.zeros_like(x)),
                         ("shift_eeg", np.roll(x, len(x) // 2, 0)), ("no_frontal", xf)):
            l1, s1 = trial_logits(net, xc, Sfeat, step=4)
            ctl[name].setdefault(r["subj_uid"], []).append(
                (l1 > 0) == (att[np.maximum(s1 - DC.HOP, 0)] == 0))
    controls = {
        k: dict(pooled=float(np.mean(np.concatenate([np.concatenate(v) for v in d.values()]))),
                subject_median=float(np.median([np.mean(np.concatenate(v)) for v in d.values()])))
        for k, d in ctl.items()}
    info = dict(decoder="direct_clf", prep=a.prep, held_out=held, fit_s=t_fit,
                total_s=time.time() - t0, lookahead_s=(DC.FUT + DC.HOP) / FS,
                best_val=best, channels=C, window_s=WINDOW_S, backbone=a.backbone,
                controls=controls)
    from common_exp import save_pickle
    save_pickle(dict(evidence={"causal": ev}, info=info), fold_file(exp, held))
    log(f"  fold held-out={held} done in {info['total_s']:.0f}s best_val {best:.4f} "
        + "  ".join(f"{k} {v['pooled']:.3f}" for k, v in controls.items()))
    del net
    if DEV.type == "cuda":
        torch.cuda.empty_cache()


def score_hp30(ev, la, log):
    hp_ev = {}
    for ds in DS_KEYS:
        hp_ev[ds] = {}
        for key, e in ev[ds].items():
            signed = (e["r0"] - e["r1"]).astype(np.float64)
            hp = V4.highpass(signed[:, None], tau=30.0, hop=0.25)[:, 0]
            hp_ev[ds][key] = dict(e)
            hp_ev[ds][key]["r0"] = (hp / 2).astype(np.float32)
            hp_ev[ds][key]["r1"] = (-hp / 2).astype(np.float32)
    per = {}
    for ds in DS_KEYS:
        A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
                            for d in DS_KEYS if d != ds for e in hp_ev[d].values()])
        mu, sd = float(A.mean()), float(A.std())
        r = S.score_dataset(ds, hp_ev, la, emis=(mu, sd * SCALE), p_stay=P_STAY)
        per[ds] = r
        log(f"  HP30 LODO {ds}  H {r['H']:.4f}  Acc {r['Acc']:.2f}")
    mH = float(np.mean([per[d]["H"] for d in DS_KEYS]))
    mA = float(np.mean([per[d]["Acc"] for d in DS_KEYS]))
    log(f"  HP30 LODO MEAN  H {mH:.4f}  Acc {mA:.2f}")
    return per, mH, mA


def score_preset(log, a, exp_name, tag, all5_pt, backbone, filterbank=False, label="EEGNet"):
    la = (DC.FUT + DC.HOP) / FS
    ev, _ = S.load_folds(exp_name, "causal")
    per = {}
    for ds in DS_KEYS:
        A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
                            for d in DS_KEYS if d != ds for e in ev[d].values()])
        mu, sd = float(A.mean()), float(A.std())
        r = S.score_dataset(ds, ev, la, emis=(mu, sd * SCALE), p_stay=P_STAY)
        per[ds] = r
        log(f"  PRESET LODO {ds}  H {r['H']:.4f}  Acc {r['Acc']:.2f}  L {r['L']:.3f}  SDT {r['SDT']:.2f}")
    mH = float(np.mean([per[d]["H"] for d in DS_KEYS]))
    mA = float(np.mean([per[d]["Acc"] for d in DS_KEYS]))
    log(f"  PRESET LODO MEAN  H {mH:.4f}  Acc {mA:.2f}")
    hp_per, hp_mH, hp_mA = score_hp30(ev, la, log)

    A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
                        for ds in DS_KEYS for e in ev[ds].values()])
    mu, sd = float(A.mean()), float(A.std())
    log(f"  external emissions mu {mu:.4f} sd {sd:.4f} scale {SCALE}")
    E.CONF.update(prep=a.prep, targets=list(D["targets"]))
    os.makedirs(EXT_DIR, exist_ok=True)
    ev_paths = {ds: os.path.join(EXT_DIR, f"ext_clf_{ds}{tag}_evidence.pkl") for ds in ("AASD", "Carta2026")}
    C_ext = N_EEG_FB if filterbank else N_EEG
    if any(not os.path.exists(p) for p in ev_paths.values()):
        if os.path.exists(all5_pt):
            net = DC.DirectNet(C_ext, 3, backbone).to(DEV)
            net.load_state_dict(torch.load(all5_pt, map_location=DEV, weights_only=False))
            net.eval()
            log(f"  loaded {all5_pt}")
        else:
            rng = np.random.default_rng(CFG["seed"] + 99)
            train, val = [], []
            for ds in DS_KEYS:
                rows = rows_of(ds)
                vs = rng.choice(sorted(rows.subject.unique()))
                for _, r in rows.iterrows():
                    (val if r["subject"] == vs else train).append(load_trial(a.prep, r["key"]))
            train += load_external(["Fuglsang2020"], a.prep, log)
            for tr in train + val:
                tr["_S"] = _targets(tr)
            log(f"  all-5 train {len(train)} / val {len(val)} filterbank={filterbank}")
            net, best = fit_net(train, val, log, a, check_chance=False, label=label,
                                filterbank=filterbank)
            torch.save(net.state_dict(), all5_pt)
            log(f"  saved {all5_pt} best val {best:.4f}")
            del train, val
        for ds, path in ev_paths.items():
            if os.path.exists(path):
                continue
            EV = {}
            trials = E.ext_trials(ds)
            log(f"  external {ds}: {len(trials)} trials")
            for i, tr in enumerate(trials):
                eeg = filterbank_eeg(tr["eeg"]) if filterbank else tr["eeg"]
                lg, starts = trial_logits(net, eeg, _targets(tr))
                truth = tr["att"].astype(int)[np.maximum(starts - DC.HOP, 0)]
                EV[tr["key"]] = dict(
                    r0=lg / 2, r1=-lg / 2, att=truth.astype(np.int8),
                    t_end=(starts / FS + tr["t0"]).astype(np.float32),
                    att128=tr["att128"], n128=tr["n128"], subject=tr["subject"])
                if (i + 1) % 40 == 0 or i + 1 == len(trials):
                    log(f"    {ds} {i + 1}/{len(trials)}")
            with open(path, "wb") as fh:
                pickle.dump(EV, fh)
            log(f"  wrote {path}")
        del net
        if DEV.type == "cuda":
            torch.cuda.empty_cache()

    hp_parts = []
    for ds in DS_KEYS:
        for e in ev[ds].values():
            signed = (e["r0"] - e["r1"]).astype(np.float64)
            hp = V4.highpass(signed[:, None], tau=30.0, hop=0.25)[:, 0]
            hp_parts.append(hp * np.where(e["att"] == 0, 1.0, -1.0))
    mu_hp = float(np.concatenate(hp_parts).mean())
    sd_hp = float(np.concatenate(hp_parts).std())

    ext = {}
    for ds, path in ev_paths.items():
        with open(path, "rb") as fh:
            EV = pickle.load(fh)
        r = E.score(EV, mu, sd * SCALE, P_STAY, 0.75)
        r998 = E.score(EV, mu, sd * SCALE, 0.998, 0.75)
        EV_hp = {}
        for key, e in EV.items():
            signed = (e["r0"] - e["r1"]).astype(np.float64)
            hp = V4.highpass(signed[:, None], tau=30.0, hop=0.25)[:, 0]
            EV_hp[key] = dict(e)
            EV_hp[key]["r0"] = (hp / 2).astype(np.float32)
            EV_hp[key]["r1"] = (-hp / 2).astype(np.float32)
        r_hp = E.score(EV_hp, mu_hp, sd_hp * SCALE, P_STAY, 0.75)
        ext[ds] = dict(raw=r, hp30=r_hp, p998=r998)
        log(f"  PRESET EXT {ds}  H {r['H']:.4f} Acc {r['Acc']:.2f}")
        log(f"  PRESET EXT_hp30 {ds}  H {r_hp['H']:.4f} Acc {r_hp['Acc']:.2f}")
        log(f"  PRESET EXT_p998 {ds}  H {r998['H']:.4f} Acc {r998['Acc']:.2f}")
    return per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA


def write_results(per, ext, mH, mA, hp_per, hp_mH, hp_mA, swap_delta, path="A",
                  chance_note=None):
    stem = dict(AVGC=(0.7945, 74.06), DTU=(0.9061, 89.85), KUL=(0.8755, 82.33),
                UHD=(0.8407, 79.47), USTC=(0.9219, 86.36))
    if path == "A":
        header = [
            "# results_eegnet",
            "",
            "**Path A — Backbone: EEGNet** (Lawhern et al., 2018; dense temporal + depthwise "
            "spatial + separable temporal). Verified absent from ALREADY_TRIED. No AvgPool "
            f"stride>1. Swap-test delta={swap_delta:.4f} before train.",
            "",
            f"Prep `eaP_n5_t60`, 10 s window (FS={FS} Hz → W={int(WINDOW_S * FS)}), sw2, random init, "
            f"max {MAX_EPOCHS} / min {MIN_EPOCHS} epochs ×250 steps, batch {BATCH}, "
            f"F1=64 D=2 kern=64 sep=16 d_out=64 dropout=0.5. HMM p_stay {P_STAY} scale {SCALE}. "
            "Also causal hp30 and p_stay 0.998.",
            "",
            "Code: `eegnet_fir_lodo.py`, `decoders.EEGNet`. Checkpoints: `models/eegnet/held-*.pt`, "
            f"`models/all5_direct_clf{TAG}.pt`. JSON: `{OUT_JSON}`.",
        ]
        exp_tag = TAG
    else:
        header = [
            "# results_eegnet",
            "",
            "**Path B — in-net causal FIR filterbank residual → vlaai_ws + SPK** (Path A EEGNet "
            + (chance_note or "abandoned at chance") + ").",
            "",
            f"In-net causal FIR approx of order-2 Butterworth banks {list(FB_BANDS)} Hz "
            "(depthwise Conv1d, frozen taps) → zero-init 1×1 mix residual on 58-ch EEG, then "
            "vlaai_ws (10 s × 3 blocks) with SPK w10_b3 weights remapped into `bb.bb.*`. "
            "Filterbank-only C=232 stayed at chance; raw||banks C=290 expand OOM'd. "
            "Distinct from spectral EQ `beq`, gamma envelopes, delta/theta phase, lp7, laplacian, "
            "mrate speech envelopes, SE/channel-mask/graph FiLM.",
            "",
            f"Swap-test delta={swap_delta:.4f}. max {MAX_EPOCHS}/min {MIN_EPOCHS} ep×250. "
            f"HMM p_stay {P_STAY} scale {SCALE}; also hp30 + p0.998.",
            "",
            f"Code: `eegnet_fir_lodo.py` / `decoders.FBExtraVLAAI`. Checkpoints: `models/fbfir/held-*.pt`, "
            f"`models/all5_direct_clf{FB_TAG}.pt`. Exp `{FB_EXP_NAME}`.",
        ]
        exp_tag = FB_TAG
    lines = header + [
        "",
        "## LODO (preset)",
        "",
        "| Held-out | H | Acc % | hp30 H/Acc | vs stem_ms96 |",
        "|---|---:|---:|---|---|",
    ]
    for ds in DS_KEYS:
        r, h = per[ds], hp_per[ds]
        sh, sa = stem[ds]
        lines.append(
            f"| {ds} | {r['H']:.4f} | {r['Acc']:.2f} | {h['H']:.4f}/{h['Acc']:.2f} | "
            f"stem {sh:.4f}/{sa:.2f} |")
    lines += [
        f"| **mean** | **{mH:.4f}** | **{mA:.2f}** | **{hp_mH:.4f}/{hp_mA:.2f}** | stem 0.8677/82.42 |",
        "",
        "## External (preset)",
        "",
        "| Set | H | Acc % | hp30 H/Acc | p0.998 H/Acc | linear_hp30 | stem_ms96 |",
        "|---|---:|---:|---|---|---|---|",
        f"| AASD | {ext['AASD']['raw']['H']:.4f} | {ext['AASD']['raw']['Acc']:.2f} | "
        f"{ext['AASD']['hp30']['H']:.4f}/{ext['AASD']['hp30']['Acc']:.2f} | "
        f"{ext['AASD']['p998']['H']:.4f}/{ext['AASD']['p998']['Acc']:.2f} | "
        f"0.6422/53.12 | 0.6158/51.54 |",
        f"| Carta2026 | {ext['Carta2026']['raw']['H']:.4f} | {ext['Carta2026']['raw']['Acc']:.2f} | "
        f"{ext['Carta2026']['hp30']['H']:.4f}/{ext['Carta2026']['hp30']['Acc']:.2f} | "
        f"{ext['Carta2026']['p998']['H']:.4f}/{ext['Carta2026']['p998']['Acc']:.2f} | "
        f"0.6520/60.54 | 0.5695/51.03 |",
        "",
        "## Targets",
        "",
        "| Target | Result |",
        "|---|---|",
        f"| AVGC H>=0.85 Acc>=78 | "
        f"{'met' if per['AVGC']['H'] >= 0.85 and per['AVGC']['Acc'] >= 78 else 'miss'} "
        f"({per['AVGC']['H']:.4f}/{per['AVGC']['Acc']:.2f}) |",
        f"| UHD H>=0.85 | "
        f"{'met' if per['UHD']['H'] >= 0.85 else 'miss'} ({per['UHD']['H']:.4f}) |",
        f"| AASD Acc>60 with H>0.63 | "
        f"{'met' if ext['AASD']['raw']['Acc'] > 60 and ext['AASD']['raw']['H'] > 0.63 else 'miss'} "
        f"({ext['AASD']['raw']['H']:.4f}/{ext['AASD']['raw']['Acc']:.2f}; "
        f"hp30 {ext['AASD']['hp30']['H']:.4f}/{ext['AASD']['hp30']['Acc']:.2f}) |",
        f"| LODO mean vs SWA 0.8700/82.61 | {mH:.4f}/{mA:.2f} |",
        f"| vs linear_hp30 AASD Acc 53.12 | {ext['AASD']['raw']['Acc']:.2f} "
        f"(hp30 {ext['AASD']['hp30']['Acc']:.2f}) |",
        "",
        f"Path taken: **{path}**. Tag `{exp_tag}`.",
        "",
    ]
    open(RESULTS_MD, "w").write("\n".join(lines) + "\n")
    return RESULTS_MD


def run_path_a(log):
    configure("a")
    a = namespace("a")
    log(f"=== {EXP_NAME} EEGNet 10s Path A device {DEV} max_epochs {MAX_EPOCHS} "
        f"min_epochs {MIN_EPOCHS} W={DC.W} FS={FS} batch={BATCH} ===")
    swap_delta, swap_delta0, anti_err = swap_test(log, a)

    net = DC.DirectNet(58, 3, BACKBONE).to(DEV)
    try:
        net.train()
        xb = torch.randn(BATCH, DC.W, 58, device=DEV)
        sb = torch.randn(BATCH, DC.W, 2, 3, device=DEV)
        loss = F.binary_cross_entropy_with_logits(net(xb, sb), torch.zeros(BATCH, device=DEV))
        loss.backward()
        log(f"memory probe batch {BATCH} ok; peak "
            f"{torch.cuda.max_memory_allocated() / 1e9:.2f} GB" if DEV.type == "cuda" else "cpu")
    except RuntimeError as e:
        if "out of memory" not in str(e).lower():
            raise
        log(f"OOM at batch {BATCH}; abort so caller can halve once")
        raise SystemExit(2)
    del net, xb, sb, loss
    if DEV.type == "cuda":
        torch.cuda.empty_cache()
    log("forward check ok")

    chance_done = any(os.path.exists(fold_file(EXP_NAME, h)) for h in DS_KEYS)
    try:
        for held in DS_KEYS:
            path = fold_file(EXP_NAME, held)
            if os.path.exists(path):
                log(f"  fold held-out={held}: cached {path}")
                chance_done = True
                continue
            run_fold(held, EXP_NAME, log, a, check_chance=not chance_done)
            chance_done = True
    except ChanceStuckError as e:
        log(f"ABANDON Path A: {e}")
        return dict(status="abandoned", reason=str(e),
                    swap=dict(delta=swap_delta, zero_eeg_delta=swap_delta0, anti_err=anti_err))

    per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA = score_preset(
        log, a, EXP_NAME, TAG, ALL5_PT, BACKBONE, filterbank=False, label="EEGNet")
    rpath = write_results(per, ext, mH, mA, hp_per, hp_mH, hp_mA, swap_delta, path="A")
    out = dict(
        experiment=EXP_NAME, path="A", decoder="direct_clf", prep="eaP_n5_t60",
        targets=D["targets"], p_switch=0.5, init_from=None, min_epochs=MIN_EPOCHS,
        max_epochs=MAX_EPOCHS, backbone=BACKBONE, window_s=WINDOW_S, batch=BATCH,
        train_seed=D["train_seed"], extra_train="Fuglsang2020", augment=D["augment"],
        swap_test=dict(delta=swap_delta, zero_eeg_delta=swap_delta0, anti_err=anti_err),
        preset=dict(p_stay=P_STAY, scale=SCALE, emis_mu=mu, emis_sd=sd,
                    lodo={d: per[d] for d in DS_KEYS},
                    lodo_hp30={d: hp_per[d] for d in DS_KEYS},
                    external=ext),
        mean_H=mH, mean_Acc=mA, hp30_mean_H=hp_mH, hp30_mean_Acc=hp_mA,
        results_md=rpath, status="done",
    )
    os.makedirs(os.path.dirname(OUT_JSON), exist_ok=True)
    with open(OUT_JSON, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    log(f"wrote {OUT_JSON} and {rpath}")
    return out


def run_path_b(log, path_a_reason=None, path_a_swap=None):
    configure("b")
    a = namespace("b")
    C_fb = N_EEG_FB
    log(f"=== {FB_EXP_NAME} Path B raw||filterbank residual→vlaai device {DEV} "
        f"max_epochs {MAX_EPOCHS} C=58 in-net-FIR bands={list(FB_BANDS)} "
        f"backbone={FB_BACKBONE} ===")
    if path_a_reason:
        log(f"  Path A failed: {path_a_reason}")
    swap_delta, swap_delta0, anti_err = swap_test(log, a, C=N_EEG)
    if path_a_swap is not None:
        log(f"  Path A swap_delta was {path_a_swap}")

    # vlaai_ws is known to learn; skip chance-abort (Path A already used that budget).
    for held in DS_KEYS:
        path = fold_file(FB_EXP_NAME, held)
        if os.path.exists(path):
            log(f"  fold held-out={held}: cached {path}")
            continue
        run_fold(held, FB_EXP_NAME, log, a, check_chance=False,
                 ckpt_dir=FB_CKPT_DIR, label="FB-vlaai", filterbank=False)

    per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA = score_preset(
        log, a, FB_EXP_NAME, FB_TAG, FB_ALL5_PT, FB_BACKBONE,
        filterbank=False, label="FB-vlaai")
    rpath = write_results(
        per, ext, mH, mA, hp_per, hp_mH, hp_mA, swap_delta, path="B",
        chance_note=path_a_reason)
    out = dict(
        experiment=FB_EXP_NAME, path="B", path_a_reason=path_a_reason,
        decoder="direct_clf", prep="eaP_n5_t60", backbone=FB_BACKBONE,
        filterbank_bands=list(FB_BANDS), channels=N_EEG,
        targets=D["targets"], p_switch=0.5, min_epochs=MIN_EPOCHS,
        max_epochs=MAX_EPOCHS, window_s=WINDOW_S, batch=BATCH,
        train_seed=D["train_seed"], extra_train="Fuglsang2020",
        swap_test=dict(delta=swap_delta, zero_eeg_delta=swap_delta0, anti_err=anti_err),
        path_a_swap=path_a_swap,
        preset=dict(p_stay=P_STAY, scale=SCALE, emis_mu=mu, emis_sd=sd,
                    lodo={d: per[d] for d in DS_KEYS},
                    lodo_hp30={d: hp_per[d] for d in DS_KEYS},
                    external=ext),
        mean_H=mH, mean_Acc=mA, hp30_mean_H=hp_mH, hp30_mean_Acc=hp_mA,
        results_md=rpath, status="done",
    )
    os.makedirs(os.path.dirname(OUT_JSON), exist_ok=True)
    # Keep Path A abandon record + Path B scores under results_eegnet naming
    with open(OUT_JSON, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    fb_json = os.path.join(EXP, "results", f"{FB_EXP_NAME}.json")
    with open(fb_json, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    log(f"wrote {OUT_JSON}, {fb_json}, and {rpath}")
    return out


def main():
    log = log_to(LOG_PATH)
    log("eegnet_fir_lodo: Path B only (in-net FIR residual fb_vlaai + SPK; C=58)")
    run_path_b(log, path_a_reason="Path A EEGNet chance (swapΔ~0.014, loss~0.693/50); "
               "C=232 filterbank-only chance; residual FIR in-net + SPK→bb.bb")


if __name__ == "__main__":
    main()
