"""Data augmentation batch (AUGMENT_EXPERIMENTS.md, A1-A11) on the best pipeline: low-pass 45 Hz + E4 (first second zeroed,
first-window loss mask, random start 0-3 min), 217 features + filtered window, CNN + GRU, distributional head `dist_w`.
Augmentations are applied while training only; validation, test and every probe use the untouched recordings.
Scored offline: 5 participant-grouped folds x seeds 0-2, public split with seeds 42-46.

Features of an augmented window are either transformed by an exact formula (gain, hemisphere swap) or recomputed on the GPU
from the augmented window (torch_features.TorchFeatures); the artifact columns (clip_frac, log_maxabs, n_bad) come from the
untouched raw signal in the cache and are kept as they are, except log_maxabs under a gain.

Variants
  base          the pipeline itself (same-session control)
  a1a_gain      gain per recording, uniform in +-3 dB (amplitude 0.71-1.41), on the window and on the features (exact)
  a1b_gainch    a1a + an extra gain per channel, uniform in +-1.5 dB
  a2_swap       with probability 0.5 per recording, AF7<->AF8 and TP9<->TP10 (window and features, exact permutation)
  a3a_jitter    the N2 onset of each recording shifted by U(-15, 15) s in each epoch; targets and metric weights recomputed
  a3b_sigma15   HL-Gauss label smoothing of the head with sigma 15 s instead of 7.5 s
  a4a_fnoise    Gaussian noise of SD 0.1 added to the standardised features
  a4b_fdrop     each standardised feature of each window set to 0 (the mean) with probability 0.1
  a5_extend     with probability 0.3 per recording, 2-10 minutes of the wake start of another training recording (another night
                of the same person when there is one; only windows that are >= 600 s from N2 in the donor) put before the
                recording; their target is the true time to N2 of the recording (y of its first window + 5 s per window), capped
  a6_fshift     with probability 0.5 per recording, the spectrum shifted by +-U(0.25, 0.5) Hz (analytic signal); features recomputed
  a7_mixup      with probability 0.5 per recording, mixed with another recording of the batch aligned at the N2 onset (the last
                windows of both), lambda ~ Beta(0.4, 0.4) folded to >= 0.5; targets mixed with the same lambda; features recomputed
  a8a_trev      each window reversed in time with probability 0.5 (CNN branch; the features are invariant to it)
  a8b_noise     Gaussian noise of SD 0.1 x the window's channel SD added to the window (CNN branch only)
  a10_covmix    with probability 0.5 per recording, recoloured to a channel covariance between its own and that of another
                recording of the batch: x <- C_t^(1/2) C_a^(-1/2) x, C_t = (1 - u) C_a + u C_b, u ~ U(0, 1); features recomputed
  a11_tilt      with probability 0.5 per recording, power spectrum multiplied by (f / 10 Hz)^beta, beta ~ U(-0.5, 0.5); recomputed
Combinations (A9) are given as "a9:<op>+<op>..." with the op names of OPS. Named combinations:
  a9_ngs        noise + gainch + swap
  a9_ngsm       noise + gainch + swap + mixup
  e9b_nfft128   features from 1 s STFT frames (E9b); e5c_dropout: bad-channel flags + channel dropout while training (E5c)
  e11           input variants of the E5-E10 batch, used at train AND test time: E7a features (AF7 lines removed) and E5b
                bad-channel flags with the flagged channel zeroed (CNN input and its per-channel features)
  e11_a9        e11 + noise + gainch + swap
  f1a_nospec, f1b_nospec_norel, f1c_top64, f1c_top128   smaller feature sets (FEATURE_EXPERIMENTS.md F1; see IMPORTANCE)
With --save-probs, the class probabilities of every held-out group are saved so that seed ensembles can be scored (ens_e12.py).

Extra probe for every variant ("extend", the risk that the model counts time since the start): 5 minutes (or as much as there
is) of the wake start of another held-out recording of the same group is put before each held-out recording and only the
recording's own windows are scored.
"""
import argparse
import math
import os
import sys
import time

import numpy as np
import torch
from torch import nn

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
for p in ("experiments", "experiments/dist_head", "experiments/e4_start", "experiments/iir_filter", "experiments/ensemble",
          "experiments/augment"):
    sys.path.insert(0, os.path.join(ROOT, p))
import cvharness as H  # noqa: E402
import run_dist_head as D  # noqa: E402
import run_e4 as E4  # noqa: E402
sys.path.insert(0, os.path.join(ROOT, "experiments/batch_e5_e10"))
import run_batch as RB  # noqa: E402
from torch_features import TorchFeatures  # noqa: E402

C, R, log = H.C, H.R, H.log
E4C = os.path.join(ROOT, "experiments/e4_start/cache")
CH = R.CH
FS, WIN = 128, R.WIN
E4CFG = dict(mask=1, zero=True, p_crop=0.5)
SWAP = [3, 2, 1, 0]                     # TP9 AF7 AF8 TP10 -> TP10 AF8 AF7 TP9

OPS = {"gain": dict(gain_db=3.0), "gainch": dict(gain_db=3.0, gain_ch_db=1.5), "swap": dict(p_swap=0.5),
       "jitter": dict(jitter=15.0), "sigma15": dict(sigma=15.0), "fnoise": dict(fnoise=0.1), "fdrop": dict(fdrop=0.1),
       "extend": dict(p_extend=0.3), "fshift": dict(p_fshift=0.5), "mixup": dict(p_mixup=0.5), "trev": dict(p_trev=0.5),
       "noise": dict(wnoise=0.1), "covmix": dict(p_covmix=0.5), "tilt": dict(p_tilt=0.5)}
VARIANTS = {"base": [], "a1a_gain": ["gain"], "a1b_gainch": ["gainch"], "a2_swap": ["swap"], "a3a_jitter": ["jitter"],
            "a3b_sigma15": ["sigma15"], "a4a_fnoise": ["fnoise"], "a4b_fdrop": ["fdrop"], "a5_extend": ["extend"],
            "a6_fshift": ["fshift"], "a7_mixup": ["mixup"], "a8a_trev": ["trev"], "a8b_noise": ["noise"],
            "a10_covmix": ["covmix"], "a11_tilt": ["tilt"]}
RECOMPUTE = ("p_fshift", "p_mixup", "p_covmix", "p_tilt", "p_rot")
# input variants of the E5-E10 batch (not augmentations; also used at test time): E7a features without the AF7 lines,
# E5b bad-channel flags with the flagged channel zeroed in the CNN input and in its per-channel features
OPS.update({"af7": dict(feat="af7"), "zero": dict(zero=True), "nfft128": dict(feat="nfft128"), "chdrop": dict(zero="dropout")})
VARIANTS.update({"e9b_nfft128": ["nfft128"], "e5c_dropout": ["chdrop"]})       # E9b: STFT frames of 1 s; E5c: flags + channel dropout
VARIANTS.update({"e11": ["af7", "zero"], "a9_ngs": ["noise", "gainch", "swap"], "a9_ngsm": ["noise", "gainch", "swap", "mixup"], "e11_a9": ["af7", "zero", "noise", "gainch", "swap"]})
E510 = os.path.join(ROOT, "experiments/batch_e5_e10/cache")
# F1 of FEATURE_EXPERIMENTS.md: smaller feature sets. nospec drops the 88 1-Hz spectrogram columns (spec*_TP, spec*_AF), norel the 24
# relative band powers (rel_*); topK keeps the K columns with the largest permutation importance, computed inside each fold
# (f1_importance.py, on the inner-validation people of the fold's training set, never on held-out recordings).
OPS.update({"nospec": dict(drop_spec=True), "norel": dict(drop_rel=True), "top64": dict(topk=64), "top128": dict(topk=128)})
VARIANTS.update({"f1a_nospec": ["nospec"], "f1b_nospec_norel": ["nospec", "norel"], "f1c_top64": ["top64"], "f1c_top128": ["top128"]})
IMPORTANCE = os.path.join(HERE, "cache", "f1_importance.json")
# F2-F5 of FEATURE_EXPERIMENTS.md: extra feature blocks (build_ffeat.py -> cache/F{2,3,4}.npy, numpy/scipy only) appended to the 217
# features; F5 swaps the z-score for a per-column quantile transform (QStore). No augmentation in these variants.
OPS.update({"f2": dict(extra="F2"), "f3": dict(extra="F3"), "f4": dict(extra="F4"), "quant": dict(quantile=True)})
VARIANTS.update({"f2_complex": ["f2"], "f3_events": ["f3"], "f4_contact": ["f4"], "f5_quantile": ["quant"]})
# F6-F9 (ffeat2.py, build_ffeat2.py): within-window dynamics, inter-channel coherence, aperiodic fit, and the log-spectrogram
# (4 channels x 7 frames x 90 bins, appended as 2520 columns and read by SpecNet's 2-D CNN; f9a also drops the 88 1-Hz spectral columns)
NSG = 4 * 7 * 90
OPS.update({"f6": dict(extra="F6"), "f7": dict(extra="F7"), "f8": dict(extra="F8"), "sg": dict(extra="SG")})
VARIANTS.update({"f6_dynamics": ["f6"], "f7_connect": ["f7"], "f8_aperiodic": ["f8"], "f9a_spec2d": ["sg", "nospec"], "f9b_spec2d_add": ["sg"]})


# M1-M7 of MULTITASK_EXPERIMENTS.md (cache: build_mt_cache.py). Auxiliary tasks act on the GRU state while training only (the solver
# keeps the main head), so they add no inference cost.
#   m1  awake/asleep head; the training sequence is streamed past the N2 onset (up to 72 post-N2 windows, main loss weight 0)
#   m2  head for "N2 within 1, 2, 5 minutes" (BCE with the metric weights of the window)
#   m3  head that predicts 40 core standardised features of the next window (MSE)
#   hazard  discrete hazard head: h_k = sigmoid(l_k), P(k) = h_k prod_{j<k} (1 - h_j), P(120) = prod_j (1 - h_j); the network outputs
#           log P, so softmax/median/ensembling are unchanged and the loss is the same HL-Gauss weighted cross-entropy
#   featonly, gru2, lstm, tcn, lp30  other views of the same task (no CNN; 2-layer GRU; LSTM; causal dilated TCN; low-pass 30 Hz)
#   adv  gradient reversal of the GRU state against the person (203 classes)
OPS.update({"m1": dict(post=True, lam_sleep=0.3), "m2": dict(lam_near=0.3), "m3": dict(lam_next=0.3), "hazard": dict(hazard=True),
            "featonly": dict(arch="featonly"), "gru2": dict(arch="gru2"), "lstm": dict(arch="lstm"), "tcn": dict(arch="tcn"),
            "lp30": dict(lp=30), "adv": dict(lam_adv=0.05),
            "m1l01": dict(post=True, lam_sleep=0.1), "m1l1": dict(post=True, lam_sleep=1.0),
            "pre_t3": dict(post=True, pretrain="t3", pre_epochs=8), "pre_cpc": dict(post=True, pretrain="cpc", pre_epochs=8),
            "stack": dict(stack=True), "f12": dict(extra="F12"), "f11": dict(extra="F11"),
            "rot": dict(p_rot=0.5, rot_max=0.15)})
# C5 (montage robustness): random channel dropout with bad-channel flags (E5c), a small random rotation of the 4-channel space (a random
# orthogonal mixing of angle <= 0.15 rad, features recomputed) and the per-channel gain of A1b
VARIANTS.update({"c5_montage": ["chdrop", "gainch", "rot"]})
VARIANTS.update({"m1_awake": ["m1"], "m2_near": ["m2"], "m3_next": ["m3"], "m4_all": ["m1", "m2", "m3"], "m5_hazard": ["hazard"],
                 "m6a_featgru": ["featonly"], "m6b_gru2": ["gru2"], "m6c_lstm": ["lstm"], "m6d_tcn": ["tcn"], "m6e_lp30": ["lp30"],
                 "m7_adv": ["adv"], "m1_lp30": ["m1", "lp30"], "m1_ngs": ["m1", "noise", "gainch", "swap"],
                 "m1_l01": ["m1l01"], "m1_l1": ["m1l1"],
                 "m8a_t3pre": ["pre_t3"], "m8b_cpcpre": ["pre_cpc"], "m10_stack": ["stack"],
                 "f12a_cwt": ["f12"], "f12b_cwt_nospec": ["f12", "nospec"], "f11_reve": ["f11"]})
MT_KEYS = ("pretrain", "stack", "lam_sleep", "lam_near", "lam_next", "hazard", "arch", "lam_adv")
NEAR = (60.0, 120.0, 300.0)
CORE_PREFIX = ("log_delta_", "log_theta_", "log_alpha_", "log_sigma_", "log_beta_", "log_gamma_", "aper_slope_", "spec_entropy_",
               "log_sd_", "theta_alpha_")                                  # 10 x 4 channels = 40 columns


class GRL(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x, lam):
        ctx.lam = lam
        return x.view_as(x)

    @staticmethod
    def backward(ctx, g):
        return -ctx.lam * g, None


def hazard_logp(l):
    """(.., 121) logits -> log P over the 121 classes from 120 hazards (the 121st logit is unused)."""
    h = l[..., :K_H].float()
    lc = nn.functional.logsigmoid(-h)
    cs = lc.cumsum(-1)
    return torch.cat([nn.functional.logsigmoid(h) + cs - lc, cs[..., -1:]], -1)


K_H = 120


class CausalTCN(nn.Module):
    """Dilated causal convolutions over the window sequence; the state is the last (k-1) d inputs of every layer, so it streams."""

    def __init__(self, dim, hidden, dil=(1, 2, 4, 8, 16), k=3):
        super().__init__()
        self.pads = [(k - 1) * d for d in dil]
        self.ins = [dim] + [hidden] * (len(dil) - 1)
        self.convs = nn.ModuleList(nn.Conv1d(i, hidden, k, dilation=d) for i, d in zip(self.ins, dil))

    def forward(self, z, h=None):
        x = z.transpose(1, 2)
        new = []
        for i, conv in enumerate(self.convs):
            buf = h[i] if h is not None else x.new_zeros(x.shape[0], self.ins[i], self.pads[i])
            xin = torch.cat([buf, x], -1)
            new.append(xin[..., -self.pads[i]:])
            y = nn.functional.gelu(conv(xin))
            x = y + x if i > 0 else y
        return x.transpose(1, 2), tuple(new)


class MTNet(D.DistNet):
    """DistNet plus the options of M1-M7: sequence model, hazard head, auxiliary heads (read from the sequence output)."""

    def __init__(self, n_feat, cfg, core=(), n_subj=0, dim=96, hidden=128):
        super().__init__(n_feat, dim=dim, hidden=hidden)
        self.arch, self.hazard, self.lam_adv = cfg.get("arch", "gru"), bool(cfg.get("hazard")), cfg.get("lam_adv", 0.0)
        if self.arch == "featonly":
            self.enc = C.Encoder(False, False, True, n_feat, dim)
        elif self.arch == "gru2":
            self.gru = nn.GRU(dim, hidden, 2, batch_first=True, dropout=0.1)
        elif self.arch == "lstm":
            self.gru = nn.LSTM(dim, hidden, 1, batch_first=True)
        elif self.arch == "tcn":
            self.gru = CausalTCN(dim, hidden)
        if self.hazard:
            nn.init.constant_(self.head[-1].bias, -4.0)

        def mlp(n):
            return nn.Sequential(nn.Linear(hidden, 64), nn.GELU(), nn.Linear(64, n))
        if cfg.get("lam_sleep"):
            self.aux_sleep = mlp(1)
        if cfg.get("lam_near"):
            self.aux_near = mlp(len(NEAR))
        if cfg.get("lam_next") or cfg.get("pretrain") == "t3":
            self.aux_next = mlp(len(core))
            self.register_buffer("core", torch.tensor(list(core), dtype=torch.long))
        if cfg.get("pretrain") == "cpc":
            self.aux_cpc = nn.Sequential(nn.Linear(hidden, 128), nn.GELU(), nn.Linear(128, dim))
        self.n_in = n_feat
        if self.lam_adv:
            self.adv = mlp(n_subj)

    def forward(self, xr, f, h=None):
        b, l = f.shape[:2]
        if f.shape[-1] < self.n_in:                          # M10 stacking: the start-up streaming check feeds the base columns only
            f = nn.functional.pad(f, (0, self.n_in - f.shape[-1]))
        z = self.enc(None if self.arch == "featonly" else xr.reshape(b * l, *xr.shape[2:]), f.reshape(b * l, -1))
        z = z.reshape(b, l, -1)
        out, h = self.gru(self.drop(z), h)
        if self.training:
            self._out, self._f, self._z = out, f, z
        logits = self.head(out)
        return (hazard_logp(logits) if self.hazard else logits), h


class SelNet(D.DistNet):
    """DistNet on a fixed subset of the feature columns (the store keeps all of them)."""

    def __init__(self, cols):
        super().__init__(len(cols))
        self.register_buffer("cols", torch.tensor(cols, dtype=torch.long))

    def forward(self, xr, f, h=None):
        return super().forward(xr, f[..., self.cols], h)


def topk_cols(tr, k):
    """Columns of the fold whose training set contains the recordings `tr` (the smallest such set), by importance."""
    import json
    imp = json.load(open(IMPORTANCE))
    trs = set(int(r) for r in tr)
    cands = [(len(v["train"]), key) for key, v in imp.items() if trs <= set(v["train"])]
    key = min(cands)[1] if cands else sorted(imp)[0]                    # no match only in the start-up streaming check
    return sorted(imp[key]["order"][:k])


def variant_cfg(v):
    ops = v.split(":", 1)[1].split("+") if v.startswith("a9:") else VARIANTS[v]
    cfg = {}
    for o in ops:
        cfg.update(OPS[o])
    return cfg


# ----------------------------------------------------------------------------- data
class AugStore(C.Store):
    """C.Store plus what the augmentations need: unstandardised features and uncapped targets on the GPU, the standardisation
    constants, the leading far-wake run of each recording and its channel covariance."""

    def __init__(self, raw, X, y, w, win, rec, names):
        super().__init__(raw, X, y, w, win)
        self.names = list(names)
        self.Xg = torch.from_numpy(X).to(H.DEV)                                    # float64
        yu = rec.n2_onset.to_numpy()[win.rec.to_numpy()] - win.stop.to_numpy()
        self.yu = torch.from_numpy(yu.astype(np.float32)).to(H.DEV)
        self.subject = rec.subject.to_dict()
        yy = win.y.to_numpy()
        self.lead = {r: int(np.argmax(np.r_[yy[i] < 600, True])) for r, i in win.groupby("rec").indices.items()}
        self.art = [i for i, n in enumerate(self.names) if n.startswith(("clip_frac_", "log_maxabs_")) or n == "n_bad"]
        nm = self.names
        col = {n: i for i, n in enumerate(nm)}
        # gain: d X = a . log10(g_c); a = 2 for log powers, 1 for log amplitudes; the pair spectra take the pair's sum
        self.gain_map = torch.zeros(4, len(nm), dtype=torch.float64)
        for i, n in enumerate(nm):
            for c, ch in enumerate(CH):
                if n.endswith(f"_{ch}") and n.startswith(("logtot_", "log_delta_", "log_theta_", "log_alpha_", "log_sigma_",
                                                          "log_beta_", "log_gamma_")):
                    self.gain_map[c, i] = 2.0
                if n.endswith(f"_{ch}") and n.startswith(("log_sd_", "log_linelen_", "log_maxabs_")):
                    self.gain_map[c, i] = 1.0
            if n.startswith("spec") and n.endswith("_TP"):
                self.gain_map[[0, 3], i] = 1.0
            if n.startswith("spec") and n.endswith("_AF"):
                self.gain_map[[1, 2], i] = 1.0
        self.gain_map = self.gain_map.to(H.DEV)
        # hemisphere swap as a column permutation
        sw = {ch: CH[SWAP[c]] for c, ch in enumerate(CH)}
        perm = []
        for n in nm:
            if n.startswith("corr_"):
                a, b = n[5:].split("_")
                m = [k for k in nm if k.startswith("corr_") and set(k[5:].split("_")) == {sw[a], sw[b]}]
                perm.append(col[m[0]])
            elif any(n.endswith(f"_{ch}") for ch in CH):
                ch = n.rsplit("_", 1)[1]
                perm.append(col[n[: -len(ch)] + sw[ch]])
            else:
                perm.append(col[n])
        self.swap_perm = torch.tensor(perm, device=H.DEV)
        # channel covariance per recording (clean windows only when there are enough)
        self.cov = {}
        for r, ix in self.idx.items():
            x = self.raw[ix].float()
            x = x - x.mean(-1, keepdim=True)
            good = x.abs().amax((1, 2)) < 200
            if int(good.sum()) >= 10:
                x = x[good]
            c = torch.einsum("nct,ndt->cd", x.double(), x.double()) / (x.shape[0] * WIN)
            self.cov[r] = c + 1e-3 * torch.eye(4, device=H.DEV, dtype=torch.float64) * c.diagonal().mean()

    def standardise(self, train_rows):
        super().standardise(train_rows)
        Xt = self.X[train_rows]
        self.mu = torch.from_numpy(Xt.mean(0)).to(H.DEV)
        self.sd = torch.from_numpy(Xt.std(0) + 1e-6).to(H.DEV)

    def std(self, X):
        return torch.clamp((X - self.mu) / self.sd, -8, 8).float()


class SpecNet(D.DistNet):
    """F9: DistNet whose feature MLP reads the first n_tab columns; the last 4 x 7 x 90 columns are the log-spectrogram
    (channel, frame, 0.5 Hz bin), read by a small 2-D CNN (kernels over frames x frequency) whose output joins the fusion layer."""

    def __init__(self, n_tab, shape=(4, 7, 90), dim=96):
        super().__init__(n_tab, dim=dim)
        self.n_tab, self.shape = n_tab, shape

        def blk(i, o, k):
            return nn.Sequential(nn.Conv2d(i, o, k, padding=(k[0] // 2, k[1] // 2)), nn.GroupNorm(4, o), nn.GELU())
        self.spec = nn.Sequential(blk(shape[0], 24, (3, 5)), nn.MaxPool2d((1, 2)), blk(24, 48, (3, 5)), nn.MaxPool2d((1, 2)), blk(48, 64, (3, 3)))
        self.spec_pool = nn.AdaptiveAvgPool1d(4)
        self.enc.out = nn.Sequential(nn.Linear(256 + 64 + 64 * 4, dim), nn.GELU())

    def forward(self, xr, f, h=None):
        b, l = xr.shape[:2]
        e = self.enc
        x = xr.reshape(b * l, *xr.shape[2:]).float()
        x = x - x.mean(-1, keepdim=True)
        hc = e.cnn(torch.clamp(x / e.scale, -10.0, 10.0))
        ff = f.reshape(b * l, -1)
        sp = self.spec(ff[:, self.n_tab:].reshape(b * l, *self.shape))                  # (N, 64, 7, 22)
        sp = self.spec_pool(sp.mean(2)).flatten(1)                                        # mean over frames, 4 coarse frequency ranges
        z = e.out(torch.cat([hc.mean(-1), hc.amax(-1), e.fmlp(ff[:, :self.n_tab]), sp], 1))
        out, h = self.gru(self.drop(z.reshape(b, l, -1)), h)
        return self.head(out), h


class QStore(AugStore):
    """F5: every feature column mapped to a standard normal by a quantile transform fitted on the training rows of the fold
    (replaces the z-score; the transformed features play the role of the "unstandardised" ones: mu = 0, sd = 1)."""

    def standardise(self, train_rows):
        from sklearn.preprocessing import QuantileTransformer
        qt = QuantileTransformer(n_quantiles=1000, output_distribution="normal", subsample=200_000, random_state=0)
        qt.fit(self.X[train_rows])
        Xq = qt.transform(self.X)
        self.f = torch.from_numpy(np.clip(Xq, -8, 8).astype(np.float32)).to(H.DEV)
        self.Xg = torch.from_numpy(Xq).to(H.DEV)
        self.mu = torch.zeros(Xq.shape[1], dtype=torch.float64, device=H.DEV)
        self.sd = torch.ones(Xq.shape[1], dtype=torch.float64, device=H.DEV)


def sqrtm(c, inv=False):
    e, v = torch.linalg.eigh(c)
    e = e.clamp(min=1e-9) ** (-0.5 if inv else 0.5)
    return (v * e) @ v.T


def freq_shift(x, df):
    """x (n, 4, T) float; every frequency moved by df Hz (single-sideband, analytic signal per window)."""
    m = x.mean(-1, keepdim=True)
    X = torch.fft.fft(x - m, dim=-1)
    T = x.shape[-1]
    h = torch.zeros(T, device=x.device, dtype=X.dtype)
    h[0] = 1; h[1:(T + 1) // 2] = 2
    if T % 2 == 0:
        h[T // 2] = 1
    z = torch.fft.ifft(X * h, dim=-1)
    t = torch.arange(T, device=x.device, dtype=torch.float64) / FS
    return (z * torch.exp(2j * math.pi * df * t)).real.to(x.dtype) + m


def tilt(x, beta):
    m = x.mean(-1, keepdim=True)
    X = torch.fft.rfft(x - m, dim=-1)
    f = torch.fft.rfftfreq(x.shape[-1], 1 / FS).to(x.device)
    g = torch.where(f > 0, (f.clamp(min=1e-6) / 10.0) ** (beta / 2.0), torch.zeros_like(f))
    return torch.fft.irfft(X * g, n=x.shape[-1], dim=-1) + m


# ----------------------------------------------------------------------------- training
def build_batch(store, recs, rng, cfg, pool, tf):
    """Augmented training batch: (xr, f, y, w) padded. pool: training recordings (donors for a5)."""
    seqs = []
    for r in recs:
        own = store.idx[r]
        n = len(own)
        s = dict(rec=r, mask=True, recompute=False)
        donors = [d for d in pool if d != r and store.lead[d] >= 24]
        if cfg.get("p_extend") and donors and rng.random() < cfg["p_extend"]:
            same = [d for d in donors if store.subject[d] == store.subject[r]]
            d = same[rng.integers(len(same))] if same else donors[rng.integers(len(donors))]
            m = int(rng.integers(24, min(120, store.lead[d]) + 1))
            pre = store.idx[d][:m]
            idx = torch.cat([pre, own])
            ypre = (store.yu[own[0]] + 5.0 * torch.arange(m, 0, -1, device=H.DEV)).clamp(0, 600)
            y = torch.cat([ypre, store.y[own]])
        else:
            c = int(rng.integers(0, min(E4.MAX_CROP, n - 12) + 1)) if rng.random() < E4CFG["p_crop"] else 0
            idx = own[c:]
            s["mask"] = c == 0
            y = store.y[idx].clone()
            if cfg.get("jitter"):
                y = (store.yu[idx] + float(rng.uniform(-cfg["jitter"], cfg["jitter"]))).clamp(0, 600)
        s.update(idx=idx, x=store.raw[idx].float(), X=store.Xg[idx].clone(), y=y)
        pi = store.post_idx.get(r) if cfg.get("post") and not s.get("ext") else None
        if pi is not None:                                  # M1: stream past the N2 onset; the main loss ignores these windows
            s["npre"] = len(idx)
            s["x"] = torch.cat([s["x"], store.post_raw[pi].float()])
            s["X"] = torch.cat([s["X"], store.post_Xg[pi]])
            s["y"] = torch.cat([y, torch.zeros(len(pi), device=H.DEV)])
            s["pflag"] = store.post_asleep[pi]
        seqs.append(s)
    orig = [dict(x=s["x"], X=s["X"], y=s["y"], rec=s["rec"]) for s in seqs]
    B = len(seqs)
    for k, s in enumerate(seqs):
        if cfg.get("p_mixup") and B > 1 and rng.random() < cfg["p_mixup"]:
            o = orig[(k + 1 + int(rng.integers(B - 1))) % B]
            lam = float(rng.beta(0.4, 0.4)); lam = max(lam, 1 - lam)
            L = min(len(s["y"]), len(o["y"]))
            s["x"] = lam * s["x"][-L:] + (1 - lam) * o["x"][-L:]
            s["y"] = lam * s["y"][-L:] + (1 - lam) * o["y"][-L:]
            s["X"] = s["X"][-L:]
            s["recompute"], s["mask"] = True, True
        if cfg.get("p_covmix") and B > 1 and rng.random() < cfg["p_covmix"]:
            o = orig[(k + 1 + int(rng.integers(B - 1))) % B]
            ca, cb = store.cov[s["rec"]], store.cov[o["rec"]]
            u = float(rng.uniform())
            M = (sqrtm((1 - u) * ca + u * cb) @ sqrtm(ca, inv=True)).float()
            s["x"] = torch.einsum("cd,ndt->nct", M, s["x"])
            s["recompute"] = True
        if cfg.get("p_rot") and rng.random() < cfg["p_rot"]:
            A = torch.from_numpy(rng.normal(size=(4, 4))).to(H.DEV)
            S = A - A.T
            S = S / S.norm() * math.sqrt(2.0)                                # generator of a unit-angle rotation in one plane
            Rm = torch.linalg.matrix_exp(float(rng.uniform(0, cfg["rot_max"])) * S).float()
            s["x"] = torch.einsum("cd,ndt->nct", Rm, s["x"])
            s["recompute"] = True
        if cfg.get("p_fshift") and rng.random() < cfg["p_fshift"]:
            s["x"] = freq_shift(s["x"], float(rng.choice([-1, 1]) * rng.uniform(0.25, 0.5)))
            s["recompute"] = True
        if cfg.get("p_tilt") and rng.random() < cfg["p_tilt"]:
            s["x"] = tilt(s["x"], float(rng.uniform(-0.5, 0.5)))
            s["recompute"] = True
        if s["recompute"]:
            Xn = tf(s["x"].double())
            Xn[:, store.art] = s["X"][:, store.art]
            if s["X"].shape[1] > Xn.shape[1]:                                # E5c flag columns follow the 217 features
                Xn = torch.cat([Xn, s["X"][:, Xn.shape[1]:]], 1)
            s["X"] = Xn
        if cfg.get("gain_db"):
            db = rng.uniform(-cfg["gain_db"], cfg["gain_db"]) + rng.uniform(-1, 1, 4) * cfg.get("gain_ch_db", 0.0)
            lg = torch.from_numpy(db / 20.0).to(H.DEV)                                # log10 of the amplitude gain
            s["x"] = s["x"] * (10 ** lg).float()[None, :, None]
            s["X"] = s["X"] + lg @ store.gain_map
        if cfg.get("p_swap") and rng.random() < cfg["p_swap"]:
            s["x"] = s["x"][:, SWAP]
            s["X"] = s["X"][:, store.swap_perm]
        if cfg.get("p_trev"):
            flip = torch.from_numpy(rng.random(len(s["x"])) < cfg["p_trev"]).to(H.DEV)
            s["x"] = torch.where(flip[:, None, None], s["x"].flip(-1), s["x"])
        if cfg.get("wnoise"):
            s["x"] = s["x"] + cfg["wnoise"] * s["x"].std(-1, keepdim=True) * torch.randn_like(s["x"])
    pad = nn.utils.rnn.pad_sequence
    xr = pad([s["x"] for s in seqs], batch_first=True)
    f = pad([store.std(s["X"]) for s in seqs], batch_first=True)
    y = pad([s["y"] for s in seqs], batch_first=True)
    w, valid, asleep, amask = (torch.zeros_like(y) for _ in range(4))
    for k, s in enumerate(seqs):
        n = len(s["y"]); npre = s.get("npre", n)
        wk = np.zeros(n)
        wk[:npre] = E4.weights_for(s["y"][:npre].cpu().numpy())
        if s["mask"]:
            wk[:E4CFG["mask"]] = 0.0
        w[k, :n] = torch.from_numpy(wk.astype(np.float32)).to(H.DEV)
        valid[k, :n] = 1.0
        amask[k, int(s["mask"]):npre] = 1.0                 # awake windows (label 0), first window excluded when it is masked
        if "pflag" in s:                                    # asleep windows (label 1); the window that straddles the onset is skipped
            asleep[k, npre:n] = s["pflag"]; amask[k, npre:n] = s["pflag"]
    if cfg.get("fnoise"):
        f = f + cfg["fnoise"] * torch.randn_like(f)
    if cfg.get("fdrop"):
        f = f * (torch.rand_like(f) >= cfg["fdrop"]).float()
    if cfg.get("lam_adv") and not hasattr(store, "subj_id"):
        subs = sorted(set(store.subject.values()))
        store.subj_id = {r: subs.index(sub) for r, sub in store.subject.items()}
    subj = torch.tensor([store.subj_id[s["rec"]] for s in seqs], device=H.DEV) if cfg.get("lam_adv") else None
    return xr, f, y, w, dict(valid=valid, asleep=asleep, amask=amask, subj=subj)


def pretrain(model, store, tr, rng, cfg, tf, batch_recs=16, lr=2e-3):
    """M8: self-supervised pre-training of the encoder + GRU on every window of the training recordings of the fold (pre- and post-N2).
    t3: predict 40 core standardised features of the next window. cpc: InfoNCE, the GRU state at t must pick the encoder output of
    window t+1 among all windows t'+1 of the batch (cosine similarity, temperature 0.1)."""
    epochs = cfg["pre_epochs"]
    opt = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=1e-2)
    sched = torch.optim.lr_scheduler.OneCycleLR(opt, max_lr=lr, total_steps=epochs * int(np.ceil(len(tr) / batch_recs)), pct_start=0.1)
    nf = nn.functional
    for ep in range(epochs):
        model.train()
        order = rng.permutation(len(tr))
        for i in range(0, len(order), batch_recs):
            xr, f, _, _, ex = build_batch(store, [tr[j] for j in order[i:i + batch_recs]], rng, cfg, tr, tf)
            model(xr, f)
            v = ex["valid"][:, :-1] * ex["valid"][:, 1:]
            h = model._out.float()
            if cfg["pretrain"] == "t3":
                loss = rec_mean(((model.aux_next(h[:, :-1]) - model._f[:, 1:, model.core].detach().float()) ** 2).mean(-1), v)
            else:
                m = v > 0
                pred = nf.normalize(model.aux_cpc(h[:, :-1])[m], dim=-1)
                tgt = nf.normalize(model._z[:, 1:].float()[m], dim=-1)
                if len(pred) > 4096:                                  # subsample the windows of very large batches
                    k = torch.randperm(len(pred), device=pred.device)[:4096]
                    pred, tgt = pred[k], tgt[k]
                loss = nf.cross_entropy(pred @ tgt.T / 0.1, torch.arange(len(pred), device=pred.device))
            opt.zero_grad(set_to_none=True)
            loss.backward()
            nn.utils.clip_grad_norm_(model.parameters(), 1.0)
            opt.step(); sched.step()


def stack_features(store, tr, seed, tf, k=3, epochs=12, batch_recs=16, lr=2e-3):
    """M10: out-of-fold stacking. K small feature-only GRUs (awake/asleep + N2 within 1, 2, 5 min) are trained on K-1 parts of the
    training recordings; each training recording gets the prediction of the model that did not see it (the inner-validation and
    every other recording get the mean of the K models). The 4 logits are appended to the features of the store (and standardised)."""
    rng = np.random.default_rng(seed + 777)
    if not hasattr(store, "Xg0"):                            # the store keeps the base columns; extra ones are added per call
        store.Xg0 = store.Xg
    store.Xg = store.Xg0
    store.f0, store.mu0, store.sd0 = store.f, store.mu, store.sd          # fresh base columns: store.standardise ran just before
    acfg = dict(post=True, lam_sleep=1.0, lam_near=1.0, arch="featonly")
    lossf = make_loss(D.SIGMA, acfg)
    nb = store.Xg0.shape[1]
    perm = rng.permutation(len(tr))
    parts = [[tr[j] for j in perm[i::k]] for i in range(k)]
    E = torch.zeros(len(store.y), 4, device=H.DEV)
    n_models = {}
    models = []
    for i in range(k):
        mtr = [r for j, p in enumerate(parts) if j != i for r in p]
        torch.manual_seed(seed + 100 + i)
        m = MTNet(nb, acfg).to(H.DEV)
        opt = torch.optim.AdamW(m.parameters(), lr=lr, weight_decay=1e-2)
        sched = torch.optim.lr_scheduler.OneCycleLR(opt, max_lr=lr, total_steps=epochs * int(np.ceil(len(mtr) / batch_recs)), pct_start=0.1)
        for ep in range(epochs):
            m.train()
            order = rng.permutation(len(mtr))
            for a in range(0, len(order), batch_recs):
                xr, f, y, w, ex = build_batch(store, [mtr[j] for j in order[a:a + batch_recs]], rng, acfg, mtr, tf)
                # the main head is not trained here: only the two auxiliary losses (lossf adds them to a zero-weight main term)
                out, _ = m(xr, f)
                loss = lossf(out, y, w * 0.0, m, ex)
                opt.zero_grad(set_to_none=True); loss.backward(); nn.utils.clip_grad_norm_(m.parameters(), 1.0); opt.step(); sched.step()
        models.append(m.eval())

    @torch.no_grad()
    def predict(m, recs):
        for a in range(0, len(recs), 24):
            ch = recs[a:a + 24]
            _, f, _, _, idxs = store.batch(ch, False, True)
            h = m.gru(m.enc(None, f.reshape(-1, f.shape[-1])).reshape(f.shape[0], f.shape[1], -1))[0]
            o = torch.cat([m.aux_sleep(h), m.aux_near(h)], -1)
            for q, ix in enumerate(idxs):
                yield ix, o[q, : len(ix)]
    held = set(parts[i][j] for i in range(k) for j in range(len(parts[i])))
    for i, m in enumerate(models):
        for ix, o in predict(m, parts[i]):
            E[ix] = o
    others = [r for r in store.idx if r not in held]
    for m in models:
        for ix, o in predict(m, others):
            E[ix] += o / k
    E = E.clamp(-8, 8)
    Ef = (E - E[torch.cat([store.idx[r] for r in tr])].mean(0)) / (E[torch.cat([store.idx[r] for r in tr])].std(0) + 1e-6)
    store.f = torch.cat([store.f0, Ef.clamp(-8, 8).float()], 1)
    store.Xg = torch.cat([store.Xg0, E.double()], 1)
    store.mu = torch.cat([store.mu0, E[torch.cat([store.idx[r] for r in tr])].mean(0).double()])
    store.sd = torch.cat([store.sd0, E[torch.cat([store.idx[r] for r in tr])].std(0).double() + 1e-6])


def train_aug(spec, store, tr, va, seed, epochs, lr=2e-3, batch_recs=16):
    """E4.train_e4 with the augmentations of spec['aug'] applied to each training batch."""
    cfg = spec["aug"]
    torch.manual_seed(seed)
    rng = np.random.default_rng(seed)
    tf = spec["tf"]
    if cfg.get("stack"):
        stack_features(store, list(tr), seed, tf)
    model = spec["make_model"](store, tr).to(H.DEV)
    if cfg.get("pretrain"):
        pretrain(model, store, tr, rng, cfg, tf)
    steps = epochs * int(np.ceil(len(tr) / batch_recs))
    opt = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=1e-2)
    sched = torch.optim.lr_scheduler.OneCycleLR(opt, max_lr=lr, total_steps=steps, pct_start=0.1)
    best, best_ep, best_state, hist = 1e9, -1, None, []
    va_w = [store.w[store.idx[r]].cpu().numpy() for r in va]
    va_y = [store.y[store.idx[r]].cpu().numpy() for r in va]
    for ep in range(epochs):
        model.train()
        order = rng.permutation(len(tr))
        tl = 0.0
        for i in range(0, len(order), batch_recs):
            recs = [tr[j] for j in order[i:i + batch_recs]]
            xr, f, y, w, ex = build_batch(store, recs, rng, cfg, tr, tf)
            out, _ = model(xr, f)
            loss = spec["loss"](out, y, w, model, ex)
            opt.zero_grad(set_to_none=True)
            loss.backward()
            nn.utils.clip_grad_norm_(model.parameters(), 1.0)
            opt.step(); sched.step()
            tl += loss.item() * len(recs)
        preds = H.predict_generic(model, store, va, spec)
        v = float(np.mean([(w_ * np.abs(np.clip(p, 0, 600) - y_)).sum() for w_, y_, p in zip(va_w, va_y, preds)]))
        hist.append((tl / len(tr), v))
        if v < best:
            best, best_ep = v, ep
            best_state = {k: t.detach().clone() for k, t in model.state_dict().items()}
    model.load_state_dict(best_state)
    return model, best, best_ep, hist


def rec_mean(v, m):
    """Mean over the valid positions of each recording, then over the recordings."""
    return ((v * m).sum(1) / m.sum(1).clamp(min=1)).mean()


def make_loss(sigma, cfg=None):
    cfg = cfg or {}

    def loss(out, y, w, model, ex=None):
        ce = -(D.soft_targets(y, sigma) * torch.log_softmax(out.float(), -1)).sum(-1)
        total = (w * ce).sum(1).mean()
        if ex is None or not any(cfg.get(k) for k in ("lam_sleep", "lam_near", "lam_next", "lam_adv")):
            return total
        h, bce = model._out.float(), nn.functional.binary_cross_entropy_with_logits
        if cfg.get("lam_sleep"):
            total = total + cfg["lam_sleep"] * rec_mean(bce(model.aux_sleep(h).squeeze(-1), ex["asleep"], reduction="none"), ex["amask"])
        if cfg.get("lam_near"):
            lab = (y[..., None] < torch.tensor(NEAR, device=y.device)).float()
            near = bce(model.aux_near(h), lab, reduction="none").mean(-1)
            total = total + cfg["lam_near"] * (w * near).sum(1).mean()
        if cfg.get("lam_next"):
            v = ex["valid"][:, :-1] * ex["valid"][:, 1:]
            mse = ((model.aux_next(h[:, :-1]) - model._f[:, 1:, model.core].detach().float()) ** 2).mean(-1)
            total = total + cfg["lam_next"] * rec_mean(mse, v)
        if cfg.get("lam_adv"):
            lg = model.adv(GRL.apply(h, cfg["lam_adv"]))
            ce_s = nn.functional.cross_entropy(lg.transpose(1, 2), ex["subj"][:, None].expand(-1, lg.shape[1]), reduction="none")
            total = total + rec_mean(ce_s, ex["valid"])
        return total
    return loss


# ----------------------------------------------------------------------------- probe
@torch.no_grad()
def extend_probe(model, store, recs, spec, m_max=60, bs=24):
    """W-bMAE of each recording's own windows when the wake start of another held-out recording is streamed before it."""
    model.eval()
    cand = sorted([r for r in recs if store.lead[r] >= 24], key=lambda r: (-store.lead[r], r))
    pad = nn.utils.rnn.pad_sequence
    out = {}
    for i in range(0, len(recs), bs):
        chunk = recs[i:i + bs]
        idxs, ms = [], []
        for r in chunk:
            ds = [d for d in cand if d != r]
            d = ds[r % len(ds)] if ds else None
            m = min(m_max, store.lead[d]) if d is not None else 0
            idxs.append(torch.cat([store.idx[d][:m], store.idx[r]]) if m else store.idx[r]); ms.append(m)
        xr = pad([store.raw[ix] for ix in idxs], batch_first=True)
        f = pad([store.f[ix] for ix in idxs], batch_first=True)
        o, _ = model(xr, f)
        p = spec["decode"](o, model).float().cpu().numpy()
        for k, r in enumerate(chunk):
            yy = store.y[store.idx[r]].cpu().numpy()
            pp = np.clip(p[k, ms[k]:ms[k] + len(yy)], 0, 600)
            out[r] = float(R.metrics.binned_mae(yy, pp, bin_edges=tuple(R.EDGES), bin_weights=tuple(R.BW)))
    return out


def save_probs_hook(out_dir, v):
    """on_model hook: save the predicted class probabilities (float16) of every held-out group, with the recording order."""
    os.makedirs(out_dir, exist_ok=True)
    tagv = v.replace(":", "_").replace("+", "-")

    @torch.no_grad()
    def hook(model, store, spec, tag, fold, seed, groups):
        model.eval()
        for gname, recs in groups.items():
            out = []
            for i in range(0, len(recs), 24):
                xr, f, _, _, idxs = store.batch(recs[i:i + 24], True, True)
                p = torch.softmax(model(xr, f)[0].float(), -1)
                out += [p[k, : len(ix)].cpu().numpy().astype(np.float16) for k, ix in enumerate(idxs)]
            np.savez(os.path.join(out_dir, f"{tagv}_{tag}_f{fold}_s{seed}_{gname}.npz"), probs=np.concatenate(out), recs=np.array(recs))
    return hook


def make_specs(win, rec, variants, names, probs_dir=None):
    y = win.y.to_numpy(); w = R.metric_weights(y, win.rec.to_numpy())
    Xm = np.load(f"{E4C}/mask_X.npy")
    base = D.build_specs(win, Xm, None)["dist_w"]
    tf = TorchFeatures(H.DEV)
    assert tf.names == list(names)
    stores = {}

    def inputs(cfg):
        X, fn = Xm, list(names)
        if cfg.get("lp"):
            X = np.load(f"{HERE}/cache/lp{cfg['lp']}_X.npy")
        if cfg.get("feat") in ("af7", "nfft128"):
            X, fn = np.load(f"{E510}/X_{cfg['feat']}.npy"), [str(n) for n in np.load(f"{E510}/X_{cfg['feat']}_names.npy")]
            assert fn == list(names)
        if cfg.get("zero"):
            X, fn = np.concatenate([X, np.load(f"{E510}/flags.npy").astype(np.float32)], 1), fn + [f"flag_{c}" for c in CH]
        keep = [i for i, n in enumerate(fn) if not (cfg.get("drop_spec") and n.startswith("spec"))
                and not (cfg.get("drop_rel") and n.startswith("rel_"))]
        if len(keep) < len(fn):
            X, fn = X[:, keep], [fn[i] for i in keep]
        if cfg.get("extra"):
            X = np.concatenate([X, np.load(os.path.join(HERE, "cache", f"{cfg['extra']}.npy"))], 1)
            fn = fn + [str(n) for n in np.load(os.path.join(HERE, "cache", f"{cfg['extra']}_names.npy"))]
        return X, fn

    def make_store(key, cfg):
        skey = key + (cfg.get("extra"), bool(cfg.get("quantile")), cfg.get("lp"), bool(cfg.get("post")), bool(cfg.get("stack")))
        if skey not in stores:                                      # one GPU store per input set, shared by the variants
            X, fn = inputs(cfg)
            cls = QStore if cfg.get("quantile") else AugStore
            dt = np.float32 if cfg.get("extra") == "SG" else np.float64          # the spectrogram makes X 2737 columns wide
            raw = f"{HERE}/cache/lp{cfg['lp']}_raw_f16.npy" if cfg.get("lp") else f"{E4C}/mask_raw_f16.npy"
            st = stores[skey] = cls(np.load(raw), X.astype(dt), y, w, win, rec, fn)
            if cfg.get("post") or cfg.get("stack"):              # M1: windows after the N2 onset (build_mt_cache.py)
                pq = np.load(f"{HERE}/cache/post_rec.npy")
                st.post_raw = torch.from_numpy(np.load(f"{HERE}/cache/post_raw_f16.npy")).to(H.DEV)
                st.post_Xg = torch.from_numpy(np.load(f"{HERE}/cache/post_X.npy").astype(np.float64)).to(H.DEV)
                st.post_asleep = torch.from_numpy(np.load(f"{HERE}/cache/post_asleep.npy").astype(np.float32)).to(H.DEV)
                st.post_idx = {int(r): torch.from_numpy(np.where(pq == r)[0]).to(H.DEV) for r in np.unique(pq)}
        return stores[skey]
    specs = {}
    for v in variants:
        cfg = variant_cfg(v)
        key = (cfg.get("feat", "base"), bool(cfg.get("zero")), bool(cfg.get("drop_spec")), bool(cfg.get("drop_rel")))
        assert (key[0] == "base" and not key[2] and not key[3]) or not any(cfg.get(k) for k in RECOMPUTE), "feature recomputation assumes the base features"
        assert not (cfg.get("topk") and key != ("base", False, False, False)), "top-K is ranked on the base features"
        X, fn = inputs(cfg)
        nf = X.shape[1]
        if cfg.get("zero"):
            flag_cols = [fn.index(f"flag_{c}") for c in CH]
            ch_cols = [[i for i, n in enumerate(fn) if n.endswith(f"_{c}") and not n.startswith(("corr_", "flag_"))] for c in CH]
            mode = "dropout" if cfg.get("zero") == "dropout" else "zero"
            mk = lambda store, tr, nf=nf, fc=flag_cols, cc=ch_cols, mode=mode: RB.BCNet(nf, fc, cc, mode)
        elif cfg.get("topk"):
            mk = lambda store, tr, k=cfg["topk"]: SelNet(topk_cols(tr, k))
        elif any(cfg.get(k) for k in MT_KEYS):
            core = [i for i, n in enumerate(fn) if n.startswith(CORE_PREFIX) and n.rsplit("_", 1)[1] in CH]
            assert not (cfg.get("lam_next") or cfg.get("pretrain") == "t3") or len(core) == 40, len(core)
            nsub = len(set(rec.subject))
            nin = nf + 4 if cfg.get("stack") else nf                      # M10: 4 stacked logits appended to the features
            mk = lambda store, tr, nin=nin, cfg=cfg, core=core, nsub=nsub: MTNet(nin, cfg, core, nsub)
        elif cfg.get("extra") == "SG":
            mk = lambda store, tr, nt=nf - NSG: SpecNet(nt)
        else:
            mk = lambda store, tr, nf=nf: D.DistNet(nf)
        specs[v] = dict(base, make_store=lambda key=key, cfg=cfg: make_store(key, cfg), make_model=mk, aug=cfg, tf=tf,
                        train=train_aug, loss=make_loss(cfg.get("sigma", D.SIGMA), cfg), probes={"wbmae_extend": extend_probe})
        if probs_dir:
            specs[v]["on_model"] = save_probs_hook(probs_dir, v)
    return specs


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--variants", nargs="+", required=True)
    ap.add_argument("--res", required=True)
    ap.add_argument("--seeds", type=int, nargs="+", default=[0, 1, 2])
    ap.add_argument("--public-seeds", type=int, nargs="+", default=[42, 43, 44, 45, 46])
    ap.add_argument("--epochs", type=int, default=25)
    ap.add_argument("--smoke", action="store_true")
    ap.add_argument("--public-only", action="store_true", help="skip the CV part (its results already exist elsewhere)")
    ap.add_argument("--save-probs", default=None, help="folder (under augment/) where the held-out class probabilities are saved")
    args = ap.parse_args()
    if args.smoke:
        args.seeds, args.public_seeds, args.epochs = [0], [42], 2
    if H.DEV.type != "cuda":
        log("WARNING: CUDA not available, training on CPU")
    res = os.path.join(HERE, args.res); os.makedirs(res, exist_ok=True)
    t0 = time.time()
    X0, names, win, rec = R.extract()
    specs = make_specs(win, rec, args.variants, names, os.path.join(HERE, args.save_probs) if args.save_probs else None)
    a = argparse.Namespace(folds=2 if args.smoke else 5, seeds=args.seeds, epochs=args.epochs, smoke=args.smoke,
                           skip_cv=False, public_seeds=[])
    if not args.public_only:
        H.run_cv(specs, X0, win, rec, a, res)
    b = argparse.Namespace(folds=5, seeds=args.public_seeds, epochs=args.epochs, smoke=args.smoke, skip_cv=True, public_seeds=None)
    res42 = os.path.join(res, "public"); os.makedirs(res42, exist_ok=True)
    H.run_cv(specs, X0, win, rec, b, res42)
    log(f"done in {(time.time() - t0) / 60:.1f} min")
