"""Glimpse features (Raghavan et al.: glimpsed vs masked speech are encoded differently; the ignored talker is mostly
encoded in its glimpses, the attended talker also in its masked parts).

Per trial, from the RAW 128-Hz envelopes of both streams (unfiltered gammatone^0.6, supplied):
  level  L_s(t) = (20/0.6) log10(causal 1st-order smoothing (25 ms) of env_s)     (env ~ amplitude^0.6 -> dB)
  mask   m_s(t) = 1 if L_s(t) - L_other(t) > THR dB (talker s clearly louder: a glimpse of s), else 0
  glim_s = causal Butterworth band-pass 1-9 Hz (order 2) of env_s * m_s
  mask_s = causal band-pass of env_s * (1 - m_s)                                    (the masked part of s)
then 128 -> 32 Hz and the same 1-s edge trim as the decoder cache (alignment with cache env checked: lag 0).
Everything is CAUSAL (no exempted method is used a second time): smoothing, mask and band-pass only use the past.
Output cache/glimpse<THR>/<key>.npz: glim (T,2), mask (T,2), m (T,2) at 32 Hz; loaded by common_exp.load_trial.
Graded variant ('softW', e.g. soft20): dominance STRENGTH instead of the binary mask, m_s = clip((L_s - L_other) / W dB,
0, 1) (0 when not louder, 1 when >= W dB louder); glim = BP(env m), mask = BP(env (1 - m)) as before.
Normalised variant ('norm'): g_s = (e_s - e_o) / (e_s + e_o + eps) in [-1, 1] on the smoothed envelopes (independent of
the overall loudness; eps = 5% of the CAUSAL running mean (EW, 10 s, past only) of e_s + e_o); m_s = max(g_s, 0);
additionally the dominance target dom = causal band-pass of [g_0, g_1 = -g_0].
Onset variant ('onorm' = norm + relative onsets): o_s = positive first difference of the smoothed envelope (128 Hz);
  odiff = o_0 - o_1 ;  orel = (o_0 - o_1) / (o_0 + o_1 + eps), exactly 0 where neither talker has an onset
  (eps = 5% of the causal running mean (10 s, past only) of o_0 + o_1); both causally band-passed, target pair [x, -x].
  +1: only talker 0 has an onset, -1: only talker 1, 0: equal onsets or none.
'full' = onorm + the RAW envelope difference ediff = e_0 - e_1 (smoothed envelopes, causal band-pass, pair [x, -x]):
the un-normalised counterpart of dom (dom treats 0.9 vs 0.4 and 0.6 vs 0.1 differently, ediff does not).
usage: python build_glimpse.py [THR_dB=10 | softW | norm | onorm | full]"""
import os
import sys

import numpy as np
from joblib import Parallel, delayed
from scipy.signal import butter, lfilter, sosfilt

from common_exp import EXP, load_trial
from common import load_env, trial_index

ARG = sys.argv[1] if len(sys.argv) > 1 else "10"
SOFT = float(ARG[4:]) if ARG.startswith("soft") else None
NORM = ARG in ("norm", "onorm", "full")
THR = None if (SOFT or NORM) else float(ARG)
A_EPS = np.exp(-1 / (10.0 * 128))
OUT = os.path.join(EXP, "cache", f"glimpse{ARG}")
BP = butter(2, [1, 9], "bandpass", fs=128, output="sos")
A = np.exp(-1 / (0.025 * 128))


def past_mean(x):
    """Causal EW running mean (10 s) of the samples BEFORE t."""
    run = lfilter([1 - A_EPS], [1, -A_EPS], x)
    return np.concatenate([[x[0]], run[:-1]])


def features(e):
    """e: (N, 2) raw 128-Hz envelopes -> dict of (N, 2) arrays at 128 Hz, all causal: glim, mask, m (+ dom for 'norm',
    + orel, odiff for 'onorm')."""
    s = lfilter([1 - A], [1, -A], np.maximum(e, 1e-9), axis=0)
    pair = lambda x: sosfilt(BP, np.stack([x, -x], 1), axis=0)
    out = {}
    if NORM:
        tot = s.sum(1)
        g = (s[:, 0] - s[:, 1]) / (tot + 0.05 * past_mean(tot))
        m = np.stack([np.maximum(g, 0), np.maximum(-g, 0)], 1)
        out["dom"] = pair(g)
        if ARG == "full":
            out["ediff"] = pair(s[:, 0] - s[:, 1])
        if ARG in ("onorm", "full"):
            o = np.maximum(0.0, np.diff(s, axis=0, prepend=s[:1]))        # positive derivative per talker
            to = o.sum(1)
            out["orel"] = pair(np.where(to > 0, (o[:, 0] - o[:, 1]) / (to + 0.05 * past_mean(to) + 1e-12), 0.0))
            out["odiff"] = pair(o[:, 0] - o[:, 1])
    else:
        D = (20 / 0.6) * np.log10(s[:, 0] / s[:, 1])
        m = (np.stack([np.clip(D / SOFT, 0, 1), np.clip(-D / SOFT, 0, 1)], 1) if SOFT
             else np.stack([D > THR, -D > THR], 1).astype(np.float64))
    out.update(glim=sosfilt(BP, e * m, axis=0), mask=sosfilt(BP, e * (1 - m), axis=0), m=m)
    return out


def build(key):
    p = os.path.join(OUT, key + ".npz")
    if os.path.exists(p):
        return 0
    T = len(load_trial("mat_reac", key)["env"])
    F = {k: v[::4][32:32 + T].astype(np.float32) for k, v in features(load_env(key).astype(np.float64)).items()}
    assert len(F["glim"]) == T, (key, len(F["glim"]), T)
    np.savez(p, **F)
    return 1


if __name__ == "__main__":
    os.makedirs(OUT, exist_ok=True)
    keys = trial_index().key.tolist()
    n = sum(Parallel(n_jobs=4)(delayed(build)(k) for k in keys))
    print(f"glimpse{ARG}: built {n} of {len(keys)}")
