"""E2: clipping threshold, soft clipping and clip-fraction features, on the best preprocessing (low-pass 45 Hz).

Baseline `lp45`: raw -> causal low-pass 45 Hz -> per-window mean removal, /40 uV and a hard clamp at +-400 uV in the CNN;
217 features from the filtered signal, the three artifact features (clip fraction, log max |x|, bad-channel count) from the raw
signal. Variants change what happens to large excursions:

  clip400    the raw signal is clipped to +-400 uV sample by sample (memoryless, so causal) BEFORE the low-pass filter; every
             downstream input (CNN window and the spectral features) comes from the clipped signal; CNN clamp at 400 uV
  clip780    same with +-780 uV (the ADC rail) and a CNN clamp at 780 uV
  soft200    soft clipping 200 * tanh(x / 200) before the filter; no hard clamp in the CNN
  clipfeat   baseline plus 16 clip features: for the temporal (TP9/TP10) and frontal (AF7/AF8) pairs, the fraction of samples
             with |x| >= 100, 200 and 400 uV and the longest run of consecutive samples at the rail (flat-top clipping,
             only in windows whose max |x| >= 700 uV), plus their causal EMA over 30 s
The three artifact features always come from the unclipped raw signal, so the information that a window saturated is kept.

Reference: the unmodified `lp45` of experiments/iir_filter (same folds, seeds, inner-validation people). Each variant is run on
5 CV folds x seeds 0-2 (with the public split for those seeds, to show the seed-to-seed spread) and on the public split with seed 42.
"""
import argparse
import os
import sys
import time

import mne
import numpy as np
import torch
from scipy.signal import lfilter
from torch import nn

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
sys.path.insert(0, os.path.join(ROOT, "experiments"))
sys.path.insert(0, os.path.join(ROOT, "experiments/iir_filter"))
import cvharness as H  # noqa: E402
import run_iir_filter as I  # noqa: E402

C, R, log = H.C, H.R, H.log
IIR = os.path.join(ROOT, "experiments/iir_filter")
CACHE = os.path.join(HERE, "cache")
WIN = R.WIN
CLIPS = {"clip400": ("hard", 400.0), "clip780": ("hard", 780.0), "soft200": ("soft", 200.0)}
CLAMP_UV = {"lp45": 400.0, "clip400": 400.0, "clip780": 780.0, "soft200": 1e9, "clipfeat": 400.0}
CF_NAMES = [f"{k}_{p}" for k in ("frac100", "frac200", "frac400", "flat") for p in ("TP", "AF")]
CF_NAMES += ["ema30_" + n for n in CF_NAMES]


def preclip(x, kind, t):
    return np.clip(x, -t, t) if kind == "hard" else t * np.tanh(x / t)


def longest_run(b):
    """b: (m, T) boolean -> longest run of True per row."""
    out = np.zeros(len(b))
    for i, row in enumerate(b):
        d = np.diff(np.concatenate([[0], row.astype(np.int8), [0]]))
        s, e = np.where(d == 1)[0], np.where(d == -1)[0]
        out[i] = (e - s).max() if len(s) else 0
    return out


def clip_window_features(xw):
    """xw: (n, 4, 640) raw uV -> (n, 8): fractions >= 100/200/400 uV and flat-top run, per pair (TP, AF)."""
    a = np.abs(xw)
    pairs = {"TP": [0, 3], "AF": [1, 2]}
    cols = []
    for thr in (100.0, 200.0, 400.0):
        f = (a >= thr).mean(-1)                                        # (n, 4)
        cols += [f[:, pairs["TP"]].mean(1), f[:, pairs["AF"]].mean(1)]
    mx = a.max(-1)                                                      # (n, 4)
    flat = np.zeros(mx.shape)
    for c in range(4):
        rows = np.where(mx[:, c] >= 700.0)[0]
        if len(rows):
            flat[rows, c] = longest_run(a[rows, c] >= 0.98 * mx[rows, c][:, None]) / WIN
    cols += [flat[:, pairs["TP"]].mean(1), flat[:, pairs["AF"]].mean(1)]
    return np.stack(cols, 1)


def ema30(x):
    a = 1 - np.exp(-5.0 / 30)
    return lfilter([a], [1, -(1 - a)], x, axis=0, zi=(1 - a) * x[:1])[0]


def build(names, win, rec, X0):
    paths = {v: (f"{CACHE}/{v}_X.npy", f"{CACHE}/{v}_raw_f16.npy") for v in CLIPS}
    paths["clipfeat"] = (f"{CACHE}/clipfeat.npy",)
    if all(os.path.exists(p) for ps in paths.values() for p in ps):
        return
    os.makedirs(CACHE, exist_ok=True)
    art = [i for i, n in enumerate(names) if n.startswith(("clip_frac_", "log_maxabs_")) or n == "n_bad"]
    sos = I.design(None)
    counts = win.groupby("rec").size(); n_all = len(win)
    Xs = {v: np.empty((n_all, X0.shape[1]), np.float32) for v in CLIPS}
    raws = {v: np.empty((n_all, 4, WIN), np.float16) for v in CLIPS}
    cf = np.empty((n_all, 16), np.float32)
    pos, t0 = 0, time.time()
    for i, r in rec.iterrows():
        vhdr = f"{R.BIDS}/{r.subject}/{r.session_id}/eeg/{r.rec_id}_task-sleeponset_eeg.vhdr"
        x = mne.io.read_raw_brainvision(vhdr, preload=True, verbose="ERROR").get_data(picks=R.CH) * 1e6
        n = int(counts[i]); x = x[:, : n * WIN]
        for v, (kind, t) in CLIPS.items():
            xf, _ = I.filter_stream(sos, preclip(x, kind, t))
            xw = xf.reshape(4, n, WIN).transpose(1, 0, 2)
            F, _ = R.window_features(xw)
            Xs[v][pos:pos + n] = F
            raws[v][pos:pos + n] = xw.astype(np.float16)
        w8 = clip_window_features(x.reshape(4, n, WIN).transpose(1, 0, 2))
        cf[pos:pos + n] = np.concatenate([w8, ema30(w8)], 1)
        pos += n
        if (i + 1) % 100 == 0:
            log(f"E2 build: {i + 1}/{len(rec)} recordings, {time.time() - t0:.0f} s")
    assert pos == n_all
    for v in CLIPS:
        Xs[v][:, art] = X0[:, art]                                      # artifact features from the unclipped raw signal
        np.save(paths[v][0], Xs[v]); np.save(paths[v][1], raws[v])
    np.save(paths["clipfeat"][0], cf)
    log(f"E2 caches built in {time.time() - t0:.0f} s (artifact columns kept from the raw signal: {len(art)})")


class Net(nn.Module):
    """Same network as experiments/e8_bipolar Net (raw CNN + feature MLP -> GRU -> 600*sigmoid) with a configurable clamp."""

    def __init__(self, n_feat, clamp_uv, dim=96, hidden=128, dropout=0.1):
        super().__init__()
        self.clamp = clamp_uv / 40.0

        def block(cin, cout, k, s):
            return nn.Sequential(nn.Conv1d(cin, cout, k, s, k // 2), nn.GroupNorm(8, cout), nn.GELU())
        self.cnn = nn.Sequential(block(4, 32, 15, 2), block(32, 64, 9, 2), block(64, 96, 9, 2), block(96, 128, 5, 2))
        self.fmlp = nn.Sequential(nn.Linear(n_feat, 128), nn.GELU(), nn.Linear(128, 64), nn.GELU())
        self.out = nn.Sequential(nn.Linear(256 + 64, dim), nn.GELU())
        self.drop = nn.Dropout(dropout)
        self.gru = nn.GRU(dim, hidden, 1, batch_first=True)
        self.head = nn.Sequential(nn.Linear(hidden, 64), nn.GELU(), nn.Linear(64, 1))

    def forward(self, xr, f, h=None):
        b, l = xr.shape[:2]
        x = xr.reshape(b * l, *xr.shape[2:]).float()
        x = x - x.mean(-1, keepdim=True)
        hc = self.cnn(torch.clamp(x / 40.0, -self.clamp, self.clamp))
        z = self.out(torch.cat([hc.mean(-1), hc.amax(-1), self.fmlp(f.reshape(b * l, -1))], 1))
        o, h = self.gru(self.drop(z.reshape(b, l, -1)), h)
        return 600.0 * torch.sigmoid(self.head(o).squeeze(-1)), h


def make_specs(win, X0, variants):
    y = win.y.to_numpy(); w = R.metric_weights(y, win.rec.to_numpy())
    Xlp = np.load(f"{IIR}/cache/lp45_X.npy")
    specs = {}
    for v in variants:
        if v == "clipfeat":
            F = np.concatenate([Xlp, np.load(f"{CACHE}/clipfeat.npy")], 1); raw = f"{IIR}/cache/lp45_raw_f16.npy"
        else:
            F = np.load(f"{CACHE}/{v}_X.npy"); raw = f"{CACHE}/{v}_raw_f16.npy"
            assert np.isfinite(F).all()
        specs[v] = dict(raw=True, feat=True, decode=lambda out, model: out, loss_scale=0.01,
                        loss=lambda out, y_, w_, model: (w_ * (out - y_).abs()).sum(1).mean(),
                        make_store=lambda F=F, raw=raw: C.Store(np.load(raw), F.astype(np.float64), y, w, win),
                        make_model=lambda store, tr, nf=F.shape[1], c=CLAMP_UV[v]: Net(nf, c))
    return specs


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--variants", nargs="+", default=["clip400", "clip780", "soft200", "clipfeat"])
    ap.add_argument("--seeds", type=int, nargs="+", default=[0, 1, 2])
    ap.add_argument("--public-seed", type=int, default=42)
    ap.add_argument("--epochs", type=int, default=25)
    ap.add_argument("--smoke", action="store_true")
    args = ap.parse_args()
    if args.smoke:
        args.seeds, args.epochs = [0], 2
    res = os.path.join(HERE, "results_smoke" if args.smoke else "results"); os.makedirs(res, exist_ok=True)
    t0 = time.time()
    X0, names, win, rec = R.extract()
    build(list(names), win, rec, X0)
    specs = make_specs(win, X0, args.variants)
    a = argparse.Namespace(folds=2 if args.smoke else 5, seeds=args.seeds, epochs=args.epochs, smoke=args.smoke,
                           skip_cv=False, public_seeds=None)
    fold, per = H.run_cv(specs, X0, win, rec, a, res)
    H.summarize(res, fold, per, a, "E2: ngưỡng cắt, cắt mềm và feature clip trên `cnn_feat_gru` (low-pass 45 Hz)",
                "Mốc `lp45` là cùng model, đầu vào low-pass 45 Hz, cắt cứng ±400 µV trong CNN, không chỉnh gì thêm. "
                "`clip400`/`clip780`: cắt tín hiệu thô ±400/±780 µV trước bộ lọc; `soft200`: 200·tanh(x/200); "
                "`clipfeat`: thêm 16 feature tỉ lệ mẫu vượt 100/200/400 µV và đoạn phẳng ở trần ADC (cùng bản EMA 30 s). "
                "Ba feature artifact luôn lấy từ tín hiệu chưa cắt.",
                [("lp45", os.path.join(IIR, "results"), "lp45")])
    b = argparse.Namespace(folds=5, seeds=[args.public_seed], epochs=args.epochs, smoke=args.smoke, skip_cv=True, public_seeds=None)
    res42 = os.path.join(res, "public_seed42"); os.makedirs(res42, exist_ok=True)
    fold42, _ = H.run_cv(specs, X0, win, rec, b, res42)
    fold42.to_csv(os.path.join(res42, "scores_by_fold.csv"), index=False)
    log(f"done in {(time.time() - t0) / 60:.1f} min")
