"""Feature blocks F2 (complexity), F3 (events) and F4 (electrode contact / drift) of FEATURE_EXPERIMENTS.md.

numpy and scipy only: the Codabench image has no numba (nor anything outside its requirements), so the same functions can go
into a submission. Everything is causal: a window's features use that window plus streams filtered with carried state.

  F2  from the filtered window (E4 pipeline: first second zeroed, causal low-pass 45 Hz), per channel:
        hjorth_mob, hjorth_cpx   log10 of Hjorth mobility and complexity
        lz                       Lempel-Ziv (LZ76) complexity of the window binarised at its median, normalised by n / log2 n
        pe3                      permutation entropy, order 3, delay 1 (normalised by log 6)
        higuchi                  Higuchi fractal dimension, kmax 8
        sflat                    spectral flatness of the Welch spectrum, 0.5-45 Hz
  F3  from band-passed streams (causal Butterworth, state carried), per window:
        blink_n, blink_amp       peaks of the mean of the AF7 and AF8 0.5-5 Hz signal: prominence >= 60 uV, width 0.1-0.6 s
        spindle_{n,frac}_{TP,AF} 11-15 Hz RMS envelope (0.5 s), runs above max(2.5 x window median, 3 uV) lasting 0.5-2 s
        slow_n_TP, slow_logptp_TP  0.5-2 Hz mean of TP9/TP10: peaks (both polarities) with prominence >= 75 uV and width >= 0.3 s;
                                 log10(1 + peak-to-peak)
  F4  from the RAW (unfiltered, not zeroed) window, per channel:
        dc        signed log10(1 + |mean|)
        slope     signed log10(1 + |linear trend in uV/s|)
        lowpow    log10 power of the 0.2-0.4 Hz bins of the whole-window FFT
        n50, n60  log10 ratio of the mean power at 49-51 / 59-61 Hz to the mean power at 30-45 Hz
"""
import numpy as np
from scipy.ndimage import uniform_filter1d
from scipy.signal import butter, find_peaks, sosfilt, sosfilt_zi

FS, WIN = 128, 640
CH = ["TP9", "AF7", "AF8", "TP10"]
NFFT, HOP = 256, 64
HANN = np.hanning(NFFT)
FREQS = np.fft.rfftfreq(NFFT, 1 / FS)
FRAME_IDX = np.arange((WIN - NFFT) // HOP + 1)[:, None] * HOP + np.arange(NFFT)
SOS_BLINK = butter(2, [0.5, 5.0], "bandpass", fs=FS, output="sos")
SOS_SIGMA = butter(4, [11.0, 15.0], "bandpass", fs=FS, output="sos")
SOS_SLOW = butter(2, [0.5, 2.0], "bandpass", fs=FS, output="sos")
NAMES_F2 = [f"{f}_{c}" for f in ("hjorth_mob", "hjorth_cpx", "lz", "pe3", "higuchi", "sflat") for c in CH]
NAMES_F3 = ["blink_n", "blink_amp", "spindle_n_TP", "spindle_frac_TP", "spindle_n_AF", "spindle_frac_AF", "slow_n_TP", "slow_logptp_TP"]
NAMES_F4 = [f"{f}_{c}" for f in ("dc", "slope", "lowpow", "n50", "n60") for c in CH]


def stream_bandpass(sos, x):
    """x (T,) or (C, T) -> causal band-pass, state initialised at the steady state of the first sample."""
    zi = sosfilt_zi(sos)[:, None, :] * x[None, ..., 0:1] if x.ndim == 2 else sosfilt_zi(sos) * x[0]
    return sosfilt(sos, x, axis=-1, zi=zi)[0]


# ----------------------------------------------------------------------------- F2
def lz76(s):
    """Lempel-Ziv (LZ76) complexity: number of components of the exhaustive history of the 0/1 string s."""
    n, i, c = len(s), 0, 0
    find = s.find
    while i < n:
        l = 1
        while i + l <= n and find(s[i:i + l], 0, i + l - 1) != -1:
            l += 1
        c += 1
        i += l
    return c


def lz76_ks(s):
    """Kaspar and Schuster (1987) implementation of the same quantity (reference for the tests)."""
    n, i, k, l, c, k_max = len(s), 0, 1, 1, 1, 1
    while True:
        if s[i + k - 1] == s[l + k - 1]:
            k += 1
            if l + k > n:
                c += 1
                break
        else:
            if k > k_max:
                k_max = k
            i += 1
            if i == l:
                c += 1
                l += k_max
                if l + 1 > n:
                    break
                i, k, k_max = 0, 1, 1
            else:
                k = 1
    return c


def lz_norm(w):
    """w (n, C, T) -> (n, C) LZ76 complexity of the window binarised at its median, / (T / log2 T)."""
    n, c, t = w.shape
    bits = (w > np.median(w, axis=-1, keepdims=True)).astype(np.uint8).reshape(-1, t) + 48
    out = np.array([lz76(row.tobytes().decode("ascii")) for row in bits], dtype=np.float64)
    return (out / (t / np.log2(t))).reshape(n, c)


def hjorth(w):
    d1 = np.diff(w, axis=-1)
    d2 = np.diff(d1, axis=-1)
    v0, v1, v2 = w.var(-1) + 1e-9, d1.var(-1) + 1e-9, d2.var(-1) + 1e-9
    mob = np.sqrt(v1 / v0)
    return np.log10(mob), np.log10(np.sqrt(v2 / v1) / mob)


def perm_entropy3(w):
    """Order 3, delay 1: w (n, C, T) -> (n, C)."""
    a, b, c = w[..., :-2], w[..., 1:-1], w[..., 2:]
    code = (a < b) * 1 + (b < c) * 2 + (a < c) * 4                      # 0..7, six of them are possible
    n, ch, m = code.shape
    flat = code.reshape(-1, m) + 8 * np.arange(n * ch)[:, None]
    p = np.bincount(flat.ravel(), minlength=8 * n * ch).reshape(-1, 8) / m
    with np.errstate(divide="ignore", invalid="ignore"):
        h = -np.nansum(np.where(p > 0, p * np.log(p), 0.0), axis=1)
    return (h / np.log(6)).reshape(n, ch)


def higuchi(w, kmax=8):
    n, ch, t = w.shape
    x = w.reshape(-1, t)
    ks = np.arange(1, kmax + 1)
    lk = np.zeros((x.shape[0], kmax))
    for ki, k in enumerate(ks):
        parts = []
        for m in range(k):
            seg = x[:, m::k]
            nm = seg.shape[1] - 1
            if nm >= 1:
                parts.append(np.abs(np.diff(seg, axis=1)).sum(1) * (t - 1) / (nm * k) / k)
        lk[:, ki] = np.mean(parts, axis=0)
    y, xx = np.log(lk + 1e-12), np.log(1.0 / ks)
    xc = xx - xx.mean()
    return ((xc * (y - y.mean(1, keepdims=True))).sum(1) / (xc ** 2).sum()).reshape(n, ch)


def spectral_flatness(w, chunk=2000):
    sel = (FREQS >= 0.5) & (FREQS < 45)
    out = []
    for i in range(0, len(w), chunk):
        x = w[i:i + chunk]
        dm = x - x.mean(-1, keepdims=True)
        p = (np.abs(np.fft.rfft(dm[..., FRAME_IDX] * HANN, axis=-1)) ** 2).mean(axis=2)[..., sel] + 1e-9
        out.append(np.exp(np.log(p).mean(-1)) / p.mean(-1))
    return np.concatenate(out)


def features_f2(wf):
    """wf: (n, 4, 640) filtered windows -> (n, 24) in the order of NAMES_F2."""
    mob, cpx = hjorth(wf)
    cols = [mob, cpx, lz_norm(wf), perm_entropy3(wf), higuchi(wf), spectral_flatness(wf)]
    return np.concatenate(cols, axis=1).astype(np.float32)


# ----------------------------------------------------------------------------- F3
def _runs(above, lo, hi):
    """above (n, T) bool -> number of runs with lo <= length <= hi per row, and their summed length."""
    n = above.shape[0]
    d = np.diff(np.pad(above.astype(np.int8), ((0, 0), (1, 1))), axis=1)
    rs, cs = np.nonzero(d == 1)
    _, ce = np.nonzero(d == -1)
    ln = ce - cs
    keep = (ln >= lo) & (ln <= hi)
    return np.bincount(rs[keep], minlength=n), np.bincount(rs[keep], weights=ln[keep], minlength=n)


def _peak_stats(sig, prominence, width_lo, width_hi=None):
    """Per-window number of peaks (positive ones) and the largest prominence."""
    cnt, amp = np.zeros(len(sig)), np.zeros(len(sig))
    for i, s in enumerate(sig):
        pk, pr = find_peaks(s, prominence=prominence, width=(width_lo, width_hi))
        cnt[i] = len(pk)
        if len(pk):
            amp[i] = pr["prominences"].max()
    return cnt, amp


def features_f3(xf):
    """xf: (4, T) filtered stream (E4 pipeline) -> (n, 8) in the order of NAMES_F3 (n = T // 640)."""
    t = xf.shape[1] // WIN * WIN
    n = t // WIN
    af = stream_bandpass(SOS_BLINK, xf[[1, 2], :t]).mean(0).reshape(n, WIN)
    blink_n, blink_prom = _peak_stats(af, 60.0, 0.1 * FS, 0.6 * FS)
    sig = stream_bandpass(SOS_SIGMA, xf[:, :t]).reshape(4, n, WIN)
    env = np.sqrt(uniform_filter1d(sig ** 2, 64, axis=-1, mode="nearest"))
    out = []
    for grp in ((0, 3), (1, 2)):
        e = env[list(grp)].mean(0)
        thr = np.maximum(2.5 * np.median(e, axis=-1, keepdims=True), 3.0)
        nb, ln = _runs(e > thr, 0.5 * FS, 2.0 * FS)
        out += [nb, ln / WIN]
    slow = stream_bandpass(SOS_SLOW, xf[[0, 3], :t]).reshape(2, n, WIN)
    s = slow.mean(0)
    n_pos, _ = _peak_stats(s, 75.0, 0.3 * FS)
    n_neg, _ = _peak_stats(-s, 75.0, 0.3 * FS)
    ptp = (slow.max(-1) - slow.min(-1)).mean(0)
    cols = [blink_n, np.log10(1 + blink_prom), out[0], out[1], out[2], out[3], n_pos + n_neg, np.log10(1 + ptp)]
    return np.stack(cols, 1).astype(np.float32)


# ----------------------------------------------------------------------------- F4
def features_f4(wr):
    """wr: (n, 4, 640) RAW windows (microvolts) -> (n, 20) in the order of NAMES_F4."""
    m = wr.mean(-1)
    tt = (np.arange(WIN) - (WIN - 1) / 2) / FS
    slope = ((wr - m[..., None]) * tt).sum(-1) / (tt ** 2).sum()
    spec = np.abs(np.fft.rfft((wr - m[..., None]) * np.hanning(WIN), axis=-1)) ** 2
    f = np.fft.rfftfreq(WIN, 1 / FS)
    band = lambda lo, hi: spec[..., (f >= lo) & (f <= hi)].mean(-1) + 1e-6
    ref = band(30, 45)
    low = np.log10(spec[..., (f > 0.1) & (f < 0.5)].sum(-1) + 1e-6)
    signed = lambda v: np.sign(v) * np.log10(1 + np.abs(v))
    cols = [signed(m), signed(slope), low, np.log10(band(49, 51) / ref), np.log10(band(59, 61) / ref)]
    return np.concatenate(cols, axis=1).astype(np.float32)
