"""A2: DIRECT attention classifier (EEG + the two streams' stimulus features -> stream A or B), LODO, with shortcut controls.

Model  logit = g(EEG, A, B) - g(EEG, B, A): g = vlaai_ws backbone on the channel-concatenated input (58 EEG + 2 x F
       stimulus features: env, onset, dom, z-scored per window), linear read-out, learned temporal pooling. Exactly
       ANTISYMMETRIC in (A, B), so a constant preference for one column cannot be learned (column 0 is the majority-
       attended stream in the corpus).
Train  same preprocessing / augmentation / data as the best reconstruction decoder (prep mat_reac, aug 'cluster',
       + Fuglsang). 5-s windows; label = attended stream 1 s before the window end (the first sample the decision may
       label under the 1-s rule); in a fraction of the windows the two streams swap roles at a random time (synthetic
       switch inside the window). Binary cross-entropy; early stopping on one validation subject per dataset.
Score  identical synthetic-switch schedules and scorer as every other experiment: the schedule of score.py is replayed,
       the stimulus columns are swapped at the switch times, the classifier runs causally every 0.25 s on windows
       ending 0.75 s after the decision time, logit -> HMM (score.py, evidence flagged pre_switched).
Controls on the held-out dataset (no synthetic switch, one decision per second), fold info['controls']:
  normal      accuracy of the 5-s decisions
  zero_eeg    EEG set to 0: must be 50 %; above = the label leaks through the envelopes alone
  shift_eeg   EEG circularly shifted by half the trial: destroys the time-locking to the speech but keeps everything
              that is constant over the trial (gaze direction / EOG, subject, session): must be 50 %; above = shortcut
  no_frontal  frontal channels (Fp, AF, F7/F8: strongest ocular artefacts) set to 0: should stay close to normal
--zero-eeg trains an ENVELOPE-ONLY model (EEG zeroed in training and test): how much of the label is predictable from
the stimuli alone under LODO.
usage: python direct_classifier.py [--train-seed 1] [--tag _x] [--zero-eeg]  -> folds/, results/direct_clf__mat_reac<tag>.json"""
import argparse
import json
import os
import time

import numpy as np
import torch

import decoders
from common_exp import CFG, EXP, FS, fold_file, load_trial, rows_of, save_pickle
from common import DS_KEYS, MONTAGES, SEED
from decoders import D, DEV, _targets
from e1_switch import draw_switch_times
from run_experiment import load_external, log_to
from score import score_experiment

W, HOP, FUT = int(D["window_s"] * FS), int(D["infer_hop_s"] * FS), int(D["causal_future_s"] * FS)
POS0, LAB = W - FUT - HOP, W - FS                    # left padding at inference; label position inside a window
FRONTAL = [i for i, c in enumerate(MONTAGES[CFG["montage"]]) if c in ("FP1", "FPZ", "FP2", "AF3", "AF4", "F7", "F8")]


class LoRA(torch.nn.Module):
    """Frozen linear layer + trainable low-rank update that starts at zero."""

    def __init__(self, base, r=8, alpha=16.0):
        super().__init__()
        self.base, self.scale = base, alpha / r
        self.A = torch.nn.Linear(base.in_features, r, bias=False)
        self.B = torch.nn.Linear(r, base.out_features, bias=False)
        torch.nn.init.zeros_(self.B.weight)

    def forward(self, x):
        return self.base(x) + self.scale * self.B(self.A(x))


def fm_encoder(mode):
    """CBraMod. mode 1: pretrained, frozen, LoRA on the feed-forward layers of the last 3 blocks (D2). mode 2: random start, everything trainable (D3)."""
    import fm_embed
    from fm_models.cbramod import CBraMod
    m = CBraMod()
    if mode == 1:
        m.load_state_dict(torch.load(os.path.join(fm_embed.XT, "foundation_models", "cbramod", "pretrained_weights.pth"), map_location="cpu", weights_only=True))
    m.proj_out = torch.nn.Identity()
    if mode == 1:
        for p in m.parameters():
            p.requires_grad_(False)
        for ly in m.encoder.layers[-3:]:
            ly.linear1, ly.linear2 = LoRA(ly.linear1), LoRA(ly.linear2)
    return m, [torch.as_tensor(g, device=DEV) for g in fm_embed.groups()]


class DirectNet(torch.nn.Module):
    def __init__(self, C, Fs, backbone="vlaai_ws"):
        super().__init__()
        self.front = decoders.FMFront(D["fm_front"], C, W) if D.get("fm_front") else None      # H5: frozen foundation model on the EEG only
        if self.front is not None:
            C = self.front.out_dim
        self.bb = decoders.build_backbone(backbone, C + 2 * Fs, W)      # vlaai_ws | adt | happyquokka (transformers)
        with torch.no_grad():
            o = self._bb(torch.zeros(1, W, C + 2 * Fs))
        self.head = torch.nn.Linear(o.shape[-1], 1)
        self.tw = torch.nn.Parameter(torch.zeros(W))
        if D.get("fm_dim"):                                                  # D1: foundation-model embedding, added to the EEG by a linear map that starts at 0
            self.fm_proj = torch.nn.Linear(int(D["fm_dim"]), C, bias=False)
            torch.nn.init.zeros_(self.fm_proj.weight)
            if D.get("fm_online"):                                           # D2 (1): pretrained encoder + LoRA; D3 (2): same encoder, random start, all trainable
                self.fm_enc, self.fm_groups = fm_encoder(int(D["fm_online"]))
                self.fm_pool = torch.nn.Linear(600, int(D["fm_dim"]), bias=False)
        if D.get("aux_sw"):                                                  # A4: "a switch happened in the last 5 s" (swap-invariant read-out)
            self.head_sw = torch.nn.Linear(o.shape[-1], 1)
            self.tw_sw = torch.nn.Parameter(torch.zeros(W))
        if D.get("aux_rate"):
            self.head_rate = torch.nn.Linear(o.shape[-1], 1)
            self.tw_rate = torch.nn.Parameter(torch.zeros(W))

    def _bb(self, x):
        y = self.bb(x)
        return y[0] if isinstance(y, (tuple, list)) else y

    def g(self, E, A, B, feat=False):
        f = self._bb(torch.cat([E, A, B], -1))                               # (B, W, d)
        h = self.head(f)[..., 0]                                             # (B, W)
        out = (h * torch.softmax(self.tw, 0)).sum(1)
        return (out, f) if feat else out

    def forward_both(self, E, S):                                           # A4: (attention logit, logit of "a switch occurred in the last 5 s")
        S = (S - S.mean(1, keepdim=True)) / (S.std(1, keepdim=True) + 1e-6)
        a, fa = self.g(E, S[:, :, 0], S[:, :, 1], True)
        b, fb = self.g(E, S[:, :, 1], S[:, :, 0], True)
        sw = (self.head_sw(0.5 * (fa + fb))[..., 0] * torch.softmax(self.tw_sw, 0)).sum(1)
        return a - b, sw

    def forward_rate(self, E, S):
        """Attention is odd and the training-only rate estimate is even on swap."""
        S = (S - S.mean(1, keepdim=True)) / (S.std(1, keepdim=True) + 1e-6)
        a, fa = self.g(E, S[:, :, 0], S[:, :, 1], True)
        b, fb = self.g(E, S[:, :, 1], S[:, :, 0], True)
        rate = (self.head_rate(0.5 * (fa + fb))[..., 0] * torch.softmax(self.tw_rate, 0)).sum(1)
        return a - b, rate

    def fm_encode(self, P):
        """P (B, N, 64, 58): 1-s patches at 64 Hz -> (B, N, fm_dim)."""
        B, N = P.shape[:2]
        x = torch.nn.functional.interpolate(P.reshape(B * N, 64, 58).permute(0, 2, 1) * 0.15, size=200, mode="linear", align_corners=True)[:, :, None, :]
        tok = self.fm_enc(x)[:, :, 0, :]
        return self.fm_pool(torch.cat([tok[:, g].mean(1) for g in self.fm_groups], 1)).reshape(B, N, -1)

    def forward(self, E, S, feat=False):                                    # S (B, W, 2, F), any scale
        if self.front is not None:
            E = self.front(E)
        if D.get("fm_dim"):                                                  # E (B, W, 58 + fm_dim); subtracting g(.., B, A) below keeps the antisymmetry
            E = E[..., :58] + self.fm_proj(E[..., 58:])
        S = (S - S.mean(1, keepdim=True)) / (S.std(1, keepdim=True) + 1e-6)
        if feat:                                                            # + backbone features of both stream orders
            a, fa = self.g(E, S[:, :, 0], S[:, :, 1], True)
            b, fb = self.g(E, S[:, :, 1], S[:, :, 0], True)
            return a - b, 0.5 * (fa + fb)
        return self.g(E, S[:, :, 0], S[:, :, 1]) - self.g(E, S[:, :, 1], S[:, :, 0])


def _swap_streams(S, tau, fade_samples=0):
    """Swap the two feature streams, optionally cross-fading about tau.

    The fade is training augmentation on already-computed features, not a
    reconstruction of the original audio or its derived features. The hard
    attention label switches at tau, the midpoint of the transition.
    """
    if fade_samples <= 0:
        S[tau:] = S[tau:, ::-1]
        return
    alpha = np.clip((np.arange(len(S)) - (tau - fade_samples / 2.0)) / fade_samples, 0.0, 1.0)
    alpha = alpha.astype(S.dtype)[:, None, None]
    original = S.copy()
    S[:] = (1.0 - alpha) * original + alpha * original[:, ::-1]


def fm_attach(tr, net=None):
    """D1: embeddings of the trial, one per 0.25 s: k = embedding of the 1-s EEG patch ending after 32-Hz sample 8k + 7.
    D2 / D3: the 64-Hz input (memory-mapped) for training; with net: the embeddings of the trained encoder, for inference."""
    if not D.get("fm_dim"):
        return
    if not D.get("fm_online"):
        if "_Z" not in tr:
            tr["_Z"] = np.load(os.path.join(EXP, "cache", "fm_cbramod", tr["key"] + ".npy"))
        return
    if "_P" not in tr:
        tr["_P"] = np.load(os.path.join(EXP, "cache", "fm_input", tr["key"] + ".npy"), mmap_mode="r")
    if net is not None:
        ends = np.arange(len(tr["eeg"]) // 8) * 8 + 7
        with torch.no_grad():
            Z = [net.fm_encode(torch.from_numpy(fm_patches(tr, ends[i:i + 256])[None]).to(DEV))[0].cpu().numpy() for i in range(0, len(ends), 256)]
        tr["_Z"] = np.concatenate(Z)


def fm_patches(tr, ends):
    """1-s patches (len(ends), 64, 58) ending after the 32-Hz samples `ends`; before the data start: zeros."""
    x = tr["_P"]
    out = np.zeros((len(ends), 64, 58), np.float32)
    for j, e in enumerate(np.round((tr["t0"] + (np.asarray(ends) + 1) / 32) * 64).astype(int)):
        e = min(e, len(x))
        p = np.asarray(x[max(e - 64, 0):e], np.float32)
        out[j, 64 - len(p):] = p
    return out


def fm_add(net, X, P):
    """D2 / D3: encode the 10 patches of each window; every 32-Hz sample sees the latest patch that has ended (sample 31 = end of the first)."""
    Z = net.fm_encode(P).repeat_interleave(32, 1)
    Z = torch.cat([torch.zeros_like(Z[:, :31]), Z[:, :W - 31]], 1)
    return torch.cat([X, Z], -1)


def fm_slice(tr, s, n):
    """Embedding seen by 32-Hz samples s .. s + n - 1: the latest patch ending at or before each sample; no patch yet = 0 (causal)."""
    k = (np.arange(s, s + n) + 1) // 8 - 1
    Zt = tr["_Z"]
    z = Zt[np.clip(k, 0, len(Zt) - 1)].copy()
    z[k < 0] = 0.0
    return z.astype(np.float32)


def wide(tr, x=None):
    """EEG with the embedding columns, for logits()."""
    x = tr["eeg"] if x is None else x
    return np.concatenate([x, fm_slice(tr, 0, len(x))], 1) if D.get("fm_dim") else x


def windows(trials, n, rng, aug, p_sw, zero_eeg):
    lens = np.array([len(t["eeg"]) - W for t in trials])
    p = np.maximum(lens, 0).astype(float)
    A0 = (D.get("augment") or {}) if aug is not None else {}
    rf = A0.get("clf_real_frac")                                            # A2 (REAL_SWITCH_STRATEGY.md): share of windows from the real-switch trials
    if rf is None:
        ti = rng.choice(len(trials), n, p=p / p.sum())
    else:
        real = np.array([bool(t.get("_real")) for t in trials])
        from_real = rng.random(n) < rf
        ti = np.empty(n, np.int64)
        for sel, pool in ((from_real, real), (~from_real, ~real)):
            if sel.any():
                idx = np.flatnonzero(pool)
                ti[sel] = rng.choice(idx, int(sel.sum()), p=p[idx] / p[idx].sum())
    windows.last_ti = ti                                                     # trial of every window (adversarial domain loss)
    aux = bool(D.get("aux_sw"))
    ysw = np.zeros(n, np.float32)                                            # A4 target: a switch (real or synthetic) in the 5 s before the label
    yrate = np.zeros(n, np.float32)
    X = np.empty((n, W, 58), np.float32)
    S = np.empty((n, 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 {}                  # classifier-specific augmentations (training only)
    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)
    fade_min = float(A.get("clf_xfade_min_s", 0.0))
    fade_max = float(A.get("clf_xfade_max_s", fade_min))
    if fade_min < 0 or fade_max < fade_min or (fade_min == 0 and fade_max > 0):
        raise ValueError("cross-fade bounds must both be positive and min <= max, or both zero")
    p_lapse = float(A.get("clf_p_lapse", 0.0))                                # C3: attention lapse (both streams replaced by an unrelated segment)
    lap_min, lap_max = float(A.get("clf_lapse_min_s", 1.0)), float(A.get("clf_lapse_max_s", 5.0))
    lap_soft = bool(A.get("clf_lapse_soft", 0))                               # 1: the label is pulled to 0.5 by the share of the last 5 s that the lapse covers
    if p_lapse and (lap_min <= 0 or lap_max < lap_min or lap_max * FS >= W):
        raise ValueError("lapse bounds must satisfy 0 < min <= max < window")
    p_bab = float(A.get("clf_p_babble", 0.0))                                 # C4: background talkers added to the envelope of both streams
    snr_lo, snr_hi = float(A.get("clf_babble_snr_min_db", 0.0)), float(A.get("clf_babble_snr_max_db", 15.0))
    n_bab = int(A.get("clf_babble_talkers", 16))
    tg = list(D.get("targets", []))
    if p_bab and "env" not in tg:
        raise ValueError("C4 needs the env feature")
    pn = p / p.sum() if (p_lapse or p_bab) else None
    p_warp = float(A.get("clf_p_warp", 0.0))                                  # C6: same time warp for EEG and stimulus features
    warp_lo, warp_hi = float(A.get("clf_warp_min", 0.9)), float(A.get("clf_warp_max", 1.1))
    dly_max = float(A.get("clf_label_delay_max_s", 0.0))                      # C2: the stimulus-EEG relation changes dly ~ U(0, dly_max) s AFTER the label does
    rate_recipe = bool(A.get("recipe_var_switch", 0))
    if rate_recipe and (p_warp or dly_max or fade_max or p_lapse or rf is not None):
        raise ValueError("recipe_var_switch cannot be mixed with warp/delay/fade/lapse/real-mix")
    starts = np.zeros(n, np.int64)
    for j, i in enumerate(ti):
        s = rng.integers(0, lens[i] + 1)
        tr = trials[i]
        is_real = rf is not None and tr.get("_real")
        if is_real and A0.get("clf_real_straddle", 0) > 0 and rng.random() < A0["clf_real_straddle"]:
            if "_sw" not in tr:
                tr["_sw"] = np.flatnonzero(np.diff(tr["att"].astype(int)) != 0) + 1     # sample index of every real switch
            if len(tr["_sw"]):                                               # window with a real switch at a random position inside it
                s = int(np.clip(tr["_sw"][rng.integers(len(tr["_sw"]))] - rng.integers(FS // 2, W - FS // 2), 0, max(lens[i], 0)))
        s2 = int(np.clip(s + rng.integers(-lag, lag + 1), 0, lens[i])) if lag else s   # EEG-stimulus timing jitter
        X[j], S[j] = tr["eeg"][s:s + W], tr["_S"][s2:s2 + W]
        starts[j] = s
        if p_bab and rng.random() < p_bab:                                   # C4: same babble in both streams; dom is left as it is (needs the 128-Hz envelopes)
            bab = np.zeros(W, np.float32)
            for i2 in rng.choice(len(trials), n_bab, p=pn):
                Sx = trials[i2]["_S"]
                b0 = int(rng.integers(0, len(Sx) - W + 1))
                bab += Sx[b0:b0 + W, int(rng.integers(2)), tg.index("env")]
            bab = (bab - bab.mean()) / (bab.std() + 1e-6)
            g = 10.0 ** (-rng.uniform(snr_lo, snr_hi) / 20.0)
            ie = tg.index("env")
            S[j, :, :, ie] = (S[j, :, :, ie] + g * bab[:, None]) / np.sqrt(1.0 + g * g)
            if "onset" in tg:                                                # as decoders._targets: half-wave rectified first difference
                e = S[j, :, :, ie]
                S[j, :, :, tg.index("onset")] = np.maximum(0.0, np.diff(e, axis=0, prepend=e[:1]))
        lab_t = s + LAB
        if p_warp and rng.random() < p_warp:                                 # C6: a raw stretch of Lr samples is read as W samples; the label follows
            Lr = int(round(W * rng.uniform(warp_lo, warp_hi)))
            s3 = min(s, len(tr["eeg"]) - Lr)
            if s3 >= 0:
                tq = np.linspace(0, Lr - 1, W)
                t0_, fr = np.floor(tq).astype(int), (tq - np.floor(tq)).astype(np.float32)
                t1_ = np.minimum(t0_ + 1, Lr - 1)
                e, f = tr["eeg"][s3:s3 + Lr], tr["_S"][s3:s3 + Lr]
                X[j] = e[t0_] * (1 - fr[:, None]) + e[t1_] * fr[:, None]
                S[j] = f[t0_] * (1 - fr[:, None, None]) + f[t1_] * fr[:, None, None]
                lab_t = s3 + int(round(tq[LAB]))
        col = int(tr["att"][lab_t])
        soft = None
        if aug is not None and tr.get("_real") and A0.get("clf_real_lag", 0) > 0:      # A3: delay-compensated label of a real-switch window
            c = int(np.clip(s + LAB - round(A0["clf_real_lag"] * FS), 0, len(tr["att"]) - 1))
            col = int(tr["att"][c])
            hw = round(A0.get("clf_real_soft", 0) * FS)                       # +-hw samples: soft label = share of column 0 around c
            if hw > 0:
                soft = float(np.mean(tr["att"][max(c - hw, 0):c + hw + 1] == 0))
        col_in = col
        if aux and tr.get("_real"):
            if "_sw" not in tr:
                tr["_sw"] = np.flatnonzero(np.diff(tr["att"].astype(int)) != 0) + 1
            ysw[j] = float(np.any((tr["_sw"] > s + LAB - 5 * FS) & (tr["_sw"] <= s + LAB)))
        if rate_recipe:
            # Simulate independent 12/30/60 s mean dwell rates inside the
            # training window, with unbiased initial column exchange. This
            # augments column order, not physiological EEG switching.
            swap = bool(rng.integers(0, 2))
            label_seq = tr["att"][s:s + W].astype(np.int8).copy()
            if swap:
                S[j] = S[j, :, ::-1]
                label_seq ^= 1
            mean_s = float(rng.choice([12.0, 30.0, 60.0]))
            time_s = float(rng.exponential(mean_s))
            while time_s < W / FS:
                tau = min(W - 1, int(round(time_s * FS)))
                _swap_streams(S[j], tau)
                label_seq[tau:] ^= 1
                time_s += max(1.0 / FS, float(rng.exponential(mean_s)))
            col = int(label_seq[LAB])
            # Use transitions only up to the decision label; no future label.
            left = max(0, LAB - 8 * FS)
            count = np.count_nonzero(np.diff(label_seq[left:LAB + 1]))
            yrate[j] = np.log1p(60.0 * count / max((LAB - left) / FS, 1e-6))
        elif rng.random() < (p_sw * A0.get("clf_real_synth", 1.0) if is_real else p_sw):   # synthetic switch inside the window (A2: optionally off on real windows)
            tau = int(rng.integers(FS // 2, W - FS // 2))
            fade = max(1, round(rng.uniform(fade_min, fade_max) * FS)) if fade_max else 0
            _swap_streams(S[j], min(tau + (round(rng.uniform(0, dly_max) * FS) if dly_max else 0), W), fade)   # C2: attention arrives late
            col = 1 - col if tau <= LAB else col
            ysw[j] = max(ysw[j], float(LAB - 5 * FS < tau <= LAB))
            if p_sw2 and rng.random() < p_sw2 and W - FS // 2 - tau > 2 * FS:  # a second switch (back) >= 2 s later
                tau2 = int(rng.integers(tau + 2 * FS, W - FS // 2))
                fade2 = max(1, round(rng.uniform(fade_min, fade_max) * FS)) if fade_max else 0
                _swap_streams(S[j], min(tau2 + (round(rng.uniform(0, dly_max) * FS) if dly_max else 0), W), fade2)   # delay <= 2 s keeps the order of the two swaps
                col = 1 - col if tau2 <= LAB else col
                ysw[j] = max(ysw[j], float(LAB - 5 * FS < tau2 <= LAB))
        keep = 1.0
        if p_lapse and rng.random() < p_lapse:                               # C3: the listener is not tracking either stream for 1-5 s
            n_l = max(1, round(rng.uniform(lap_min, lap_max) * FS))
            a0 = int(rng.integers(0, W - n_l + 1))
            Sx = trials[int(rng.choice(len(trials), p=pn))]["_S"]             # an unrelated stretch of speech features (any trial)
            if len(Sx) > n_l:
                b0 = int(rng.integers(0, len(Sx) - n_l + 1))
                S[j, a0:a0 + n_l] = Sx[b0:b0 + n_l]
                if lap_soft:
                    keep = 1.0 - max(0, min(a0 + n_l, LAB) - max(a0, LAB - 5 * FS)) / (5 * FS)
        if p_fd and rng.random() < p_fd:                                     # drop one stimulus feature of both streams
            S[j, :, :, rng.integers(S.shape[-1])] = 0.0
        if soft is not None:
            y[j] = 1.0 - soft if col != col_in else soft                      # synthetic swaps flip the soft label too
        else:
            y[j] = 1.0 if col == 0 else 0.0
        if keep < 1.0:
            y[j] = 0.5 + (y[j] - 0.5) * keep                                  # C3 soft label
    windows.last_sw = torch.from_numpy(ysw)
    windows.last_rate = torch.from_numpy(yrate)
    if aug is not None:
        X = aug._augment(X, rng)
    if zero_eeg:
        X[:] = 0.0
    if D.get("fm_online"):
        windows.last_P = torch.from_numpy(np.stack([fm_patches(trials[i], st + 31 + 32 * np.arange(W // 32)) for i, st in zip(ti, starts)]))
    elif D.get("fm_dim"):
        Z = np.stack([fm_slice(trials[i], st, W) for i, st in zip(ti, starts)])
        X = np.concatenate([X, Z], -1)
    return torch.from_numpy(X), torch.from_numpy(S), torch.from_numpy(y)


@torch.no_grad()
def logits(net, x, S, step=1):
    """Causal decisions: hop start a -> window x[a - POS0 : a + HOP + FUT] (zero padded), every `step` hops."""
    T = len(x)
    if D.get("fm_dim") and x.shape[1] == 58:                                 # controls (zero / shifted EEG): no embedding
        x = np.concatenate([x, np.zeros((T, int(D["fm_dim"])), np.float32)], 1)
    C = x.shape[1]
    xp = np.concatenate([np.zeros((POS0, C), np.float32), x.astype(np.float32), np.zeros((FUT + HOP, C), np.float32)])
    Sp = np.concatenate([np.zeros((POS0,) + S.shape[1:], np.float32), S, np.zeros((FUT + HOP,) + S.shape[1:], np.float32)])
    starts = np.arange(0, T, HOP * step)
    out = np.empty(len(starts), np.float32)
    aux = bool(D.get("aux_sw")) and hasattr(net, "head_sw")
    outsw = np.empty(len(starts), np.float32) if aux else None
    bs = int(D.get("infer_bs", 1024))
    for i in range(0, len(starts), bs):
        a = starts[i:i + bs]
        xb, sb = (torch.from_numpy(np.stack([z[k:k + W] for k in a])).to(DEV) for z in (xp, Sp))
        if aux:
            lg, sw = net.forward_both(xb, sb)
            out[i:i + len(a)], outsw[i:i + len(a)] = lg.cpu().numpy(), torch.sigmoid(sw).cpu().numpy()
        else:
            out[i:i + len(a)] = net(xb, sb).cpu().numpy()
    logits.last_sw = outsw                                                   # P(switch in the last 5 s) per hop (A4), else None
    return out, starts


def fit(train, val, log, a, init_from=None):
    """Train a DirectNet (binary cross-entropy, early stopping on the validation windows). Returns (net, best val)."""
    Fs = train[0]["_S"].shape[-1]
    seed = D.get("train_seed", CFG["seed"])
    torch.manual_seed(seed)
    net = DirectNet(58, Fs, a.backbone).to(DEV)
    if init_from:
        net.load_state_dict(torch.load(init_from, map_location=DEV), strict=not (D.get("aux_sw") or D.get("aux_rate") or D.get("fm_dim")))
        log(f"  initialised from {init_from}")
    aug = decoders.DNNDec.__new__(decoders.DNNDec)                          # only for its _augment (EEG augmentation)
    aug.W, aug.fut, aug.hop, aug.C = W, FUT, HOP, 58
    rs = np.random.default_rng(seed)
    Xv, Sv, yv = windows(val, D["val_windows"], rs, None, a.p_switch, a.zero_eeg)
    Pv = windows.last_P if D.get("fm_online") else None
    adv, step, params = None, 0, list(net.parameters())
    if D.get("adv"):
        dom, n_dom = decoders.domain_ids(train, D["adv"])
        adv = decoders.AdvHead(net.head.in_features, n_dom).to(DEV)
        params += list(adv.parameters())
        log(f"  adversarial {D['adv']} loss: {n_dom} classes, lambda {D.get('adv_lambda', 0.05)}")
    opt = torch.optim.AdamW(decoders.fm_param_groups(net, params), lr=D["lr"], weight_decay=D["weight_decay"])
    sch = torch.optim.lr_scheduler.ReduceLROnPlateau(opt, mode="min", factor=0.5, patience=D["lr_patience"])
    cos = decoders.cosine_sched(opt)
    n_step = 0
    bce = torch.nn.functional.binary_cross_entropy_with_logits
    best, bad, state = np.inf, 0, None
    for ep in range(D["max_epochs"]):
        t1, tl, da = time.time(), [], []
        net.train()
        for _ in range(D["steps_per_epoch"]):
            decoders.fm_lpft(net, n_step)
            n_step += 1
            X, S, y = [z.to(DEV) for z in windows(train, D["batch_size"], rs, aug, a.p_switch, a.zero_eeg)]
            if D.get("fm_online"):
                X = fm_add(net, X, windows.last_P.to(DEV))
            if adv is None:
                if D.get("aux_rate"):
                    lg, rate = net.forward_rate(X, S)
                    # Logit noise/gain/dropout are training-only analogues of
                    # the stage-2 perturbations, not EEG-level augmentation.
                    A = D.get("augment") or {}
                    if A.get("recipe_logit_gain", 0):
                        g = float(A["recipe_logit_gain"])
                        lg = lg * (1.0 - g + 2.0 * g * torch.rand_like(lg))
                    if A.get("recipe_logit_noise", 0):
                        lg = lg + float(A["recipe_logit_noise"]) * torch.randn_like(lg)
                    if A.get("recipe_logit_drop_p", 0):
                        lg = lg.masked_fill(torch.rand_like(lg) < float(A["recipe_logit_drop_p"]), 0.0)
                    loss = bce(lg, y) + float(D.get("aux_rate_weight", 0.1)) * torch.nn.functional.mse_loss(rate, windows.last_rate.to(DEV))
                elif D.get("aux_sw"):
                    lg, sw = net.forward_both(X, S)
                    loss = bce(lg, y) + D.get("aux_lambda", 0.5) * bce(sw, windows.last_sw.to(DEV))
                else:
                    loss = bce(net(X, S), y)
                tl.append(loss.item())
            else:
                lg, f = net(X, S, feat=True)
                loss = bce(lg, y)
                tl.append(loss.item())
                dl = adv(f, decoders.adv_lambda(step))
                dt = torch.from_numpy(dom[windows.last_ti]).to(DEV)
                loss = loss + torch.nn.functional.cross_entropy(dl, dt)
                da.append((dl.argmax(1) == dt).float().mean().item())
                step += 1
            opt.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(params, D.get("clip", 5.0))
            opt.step()
            if cos is not None:
                cos.step()
        net.eval()
        with torch.no_grad():
            vb = int(D.get("infer_bs", 128))
            lv = torch.cat([net(fm_add(net, Xv[i:i + vb].to(DEV), Pv[i:i + vb].to(DEV)) if Pv is not None else Xv[i:i + vb].to(DEV), Sv[i:i + vb].to(DEV)).cpu()
                            for i in range(0, len(Xv), vb)])
        vl, va = bce(lv, yv).item(), ((lv > 0).float() == yv).float().mean().item()
        if ep >= a.min_epochs and cos is None:     # the antisymmetric net often sits at chance for several epochs before it
            sch.step(vl)                           # takes off: no LR decay / early stopping during the first min_epochs
        log(f"    epoch {ep:02d} train {np.mean(tl):.4f} val BCE {vl:.4f} acc {va:.3f} ({time.time() - t1:.0f}s)"
            + (f" | adversary acc {np.mean(da):.3f}" if da else ""))
        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
    net.load_state_dict(state)
    net.eval()
    return net, best


def run_fold(held, exp, log, a):
    D["fm_held"] = held                                                      # H1: which per-fold stimulus-pretrained front to load
    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"]))
    if not D.get("no_fuglsang"):                                             # --set no_fuglsang=1: the organisers' five datasets only
        train += load_external(["Fuglsang2020"], a.prep, log)
    for nm in [x for x in a.extra_real.split(",") if x]:                    # real-switch sets (never a LODO test set): in every fold's training data
        ext = load_external([nm], a.prep, log)
        vs = rng.choice(sorted({t["key"].split("_")[1] for t in ext}))
        for t in ext:
            t["_real"] = True
            (val if t["key"].split("_")[1] == vs else train).append(t)
    for tr in train + val:
        tr["_S"] = _targets(tr)
        fm_attach(tr)
    log(f"  fold held-out={held}: {len(train)} train / {len(val)} val trials, {train[0]['_S'].shape[-1]} stimulus features per stream")
    t0 = time.time()
    net, best = fit(train, val, log, a, a.init_from)
    t_fit = time.time() - t0
    del train, val
    # ---- held-out dataset: synthetic-switch evidence (schedule of score.py replayed) + shortcut controls
    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"])
        fm_attach(tr, net)
        S, x, att, T128 = _targets(tr), tr["eeg"], tr["att"].astype(int), int(r["n"])
        times = draw_switch_times(srng, T128 / 128)
        srng.integers(0, 2)                                                 # the scorer's start guess (same stream)
        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], S[:, ::-1], S)
        xe = np.zeros_like(x) if a.zero_eeg else x
        lg, starts = logits(net, wide(tr, xe), Ssw)
        sw_p = logits.last_sw                                                # A4: P(switch in the last 5 s) per hop, else None
        truth = (att ^ st)[np.maximum(starts - HOP, 0)]                     # attended column 1 s before the window end
        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)
        if sw_p is not None:
            ev[r["key"]]["sw"] = sw_p
        xf = x.copy()
        xf[:, FRONTAL] = 0.0
        for name, xc in (("normal", xe), ("zero_eeg", np.zeros_like(x)), ("shift_eeg", np.roll(xe, len(x) // 2, 0)), ("no_frontal", xf)):
            l1, s1 = logits(net, xc, S, step=4)
            ctl[name].setdefault(r["subj_uid"], []).append((l1 > 0) == (att[np.maximum(s1 - 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=(FUT + HOP) / FS,
                best_val=best, channels=58, controls=controls)
    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: controls (5-s decisions, pooled accuracy) "
        + "  ".join(f"{k} {v['pooled']:.3f}" for k, v in controls.items()))
    if os.environ.get("SAVE_FOLD_WEIGHTS"):                                # for clf_reschedule.py (other switch schedules)
        torch.save(net.state_dict(), os.path.join(EXP, "models", f"fold_{exp}__{held}.pt"))
    del net
    torch.cuda.empty_cache()


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--tag", default="")
    ap.add_argument("--train-seed", type=int, default=None)
    ap.add_argument("--targets", default="env,onset,dom", help="stimulus features given for each stream")
    ap.add_argument("--p-switch", type=float, default=0.3, help="share of training windows with a synthetic switch inside")
    ap.add_argument("--backbone", default="vlaai_ws", choices=["vlaai_ws", "adt", "happyquokka", "cbramod", "labram", "eegpt", "brainomni", "biot"],
                    help="network g: vlaai_ws (CNN, default) or a transformer: adt, happyquokka (ICASSP 2023 regression winner)")
    ap.add_argument("--window-s", type=float, default=None, help="classifier window in s (default config dnn.window_s = 5); "
                    "the read-out timing (0.75 s lookahead, label 1 s before the window end) is unchanged")
    ap.add_argument("--min-epochs", type=int, default=0, help="epochs without LR decay / early stopping (training stability)")
    ap.add_argument("--max-epochs", type=int, default=None)
    ap.add_argument("--init-from", default=None, help="DirectNet state_dict to start every fold from")
    ap.add_argument("--pretrain-spk", action="store_true", help="pretrain on SparrKULee (single talker): stream A = the "
                    "presented speech, stream B = its mismatched (half-run shifted) envelope, i.e. match-mismatch; "
                    "saves models/pretrain_spk_direct_clf_s<seed>.pt. Needs GLIMPSE_THR=onorm (dom for SparrKULee)")
    ap.add_argument("--set", nargs="*", default=[], help="hyper-parameter overrides of config dnn: key=value ...")
    ap.add_argument("--aug-set", nargs="*", default=[], help="overrides of the augmentation settings: key=value ...")
    ap.add_argument("--adv", default=None, choices=["dataset", "subject"], help="adversarial loss (gradient reversal) removing "
                    "dataset / subject information from the backbone features")
    ap.add_argument("--adv-lambda", type=float, default=0.05)
    ap.add_argument("--prep", default="mat_reac", help="mat_reac (EA state carried across a subject's trials) | mat_rea (EA restarts "
                    "in every trial: needs no subject / trial ID at test time); the SparrKULee pretraining always uses mat_reac")
    ap.add_argument("--aug-variant", default="cluster", help="EEG augmentation: dnn_augment + config aug_variants[NAME]")
    ap.add_argument("--zero-eeg", action="store_true", help="envelope-only model (EEG zeroed in training and test)")
    ap.add_argument("--extra-real", default="", help="comma list of real-switch sets (AASD, Carta2026) added to the training data of every LODO fold "
                    "(REAL_SWITCH_STRATEGY.md: improve on real switches AND keep the synthetic LODO score)")
    a = ap.parse_args()
    if a.window_s:
        W = int(a.window_s * FS)
        POS0, LAB = W - FUT - HOP, W - FS
    D["targets"] = a.targets.split(",")
    D["augment"] = {**CFG["dnn_augment"], **CFG["aug_variants"][a.aug_variant]}
    decoders.apply_overrides(a.set, a.aug_set)
    if a.adv:
        D.update(adv=a.adv, adv_lambda=a.adv_lambda)
    if a.train_seed is not None:
        D["train_seed"] = a.train_seed
    if a.max_epochs:
        D["max_epochs"] = a.max_epochs
    torch.set_num_threads(4)
    if a.pretrain_spk:
        seed = D.get("train_seed", 0)
        D["train_seed"] = seed
        log = log_to(os.path.join(EXP, "logs", f"pretrain_spk_direct_clf{a.tag}_s{seed}.log"))
        tr = load_external(["SparrKULee"], "mat_reac", log)
        for t in tr:
            t["_S"] = _targets(t)
        subs = sorted({t["key"].split("_")[1] for t in tr})
        vs = set(np.random.default_rng(1234).choice(subs, 4, replace=False))
        log(f"pretrain direct_clf seed {seed} on SparrKULee: {len(subs) - 4} train subjects, val {sorted(vs)}")
        net, best = fit([t for t in tr if t["key"].split("_")[1] not in vs], [t for t in tr if t["key"].split("_")[1] in vs], log, a)
        os.makedirs(os.path.join(EXP, "models"), exist_ok=True)
        out = os.path.join(EXP, "models", f"pretrain_spk_direct_clf{a.tag}_s{seed}.pt")
        torch.save(net.state_dict(), out)
        log(f"saved {out} (best val BCE {best:.4f})")
        raise SystemExit
    exp = f"direct_clf__{a.prep}{a.tag}"
    log = log_to(os.path.join(EXP, "logs", f"{exp}.log"))
    log(f"=== experiment {exp} ===")
    for held in DS_KEYS:
        if os.path.exists(fold_file(exp, held)):
            log(f"  fold held-out={held}: cached")
            continue
        run_fold(held, exp, log, a)
    out = dict(experiment=exp, decoder="direct_clf", prep=a.prep, targets=D["targets"], zero_eeg=a.zero_eeg, p_switch=a.p_switch, init_from=a.init_from, min_epochs=a.min_epochs, backbone=a.backbone, window_s=W / FS,
               train_seed=D.get("train_seed", CFG["seed"]), extra_train="Fuglsang2020", augment=D["augment"],
               switch=f"HMM p_stay={CFG['switch']['p_stay']} per 0.25-s step, SYNTHETIC switches applied to the classifier INPUT, LODO",
               causal=score_experiment(exp, "causal", (FUT + HOP) / FS))
    out["controls"] = {d: out["causal"]["info"][d]["controls"] for d in DS_KEYS}
    m = out["causal"]["mean"]
    log(f"  MEAN over held-out datasets: Acc {m['Acc']:.2f}  SDT {m['SDT']:.2f}  L {m['L']:.3f}  H {m['H']:.3f}  (window acc {m['acc1']:.3f})")
    for k in ("normal", "zero_eeg", "shift_eeg", "no_frontal"):
        log(f"  control {k:10s}: " + "  ".join(f"{d} {out['controls'][d][k]['pooled']:.3f}" for d in DS_KEYS)
            + f"  | mean {np.mean([out['controls'][d][k]['pooled'] for d in DS_KEYS]):.3f}")
    with open(os.path.join(EXP, "results", f"{exp}.json"), "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    log(f"=== done {exp} ===")
