"""Glimpse targets for Fuglsang 2020 (extra training data), same definition as decoder_lodo/build_glimpse.py.

No audio is distributed, only the authors' envelope feature (derivatives/stimuli, 512 Hz, 'woa' version, a
different compression than gammatone^0.6). So the level difference is taken as log10 of the causally smoothed
envelope ratio, with the threshold CALIBRATED so that each talker is glimpsed the same fraction of the time as
in the challenge corpus (0.420 with 10 dB on gammatone^0.6). Masker onset delay and lengths as stage_b_convert.py.
Output cache/glimpse10/FUG_*.npz (glim, mask, m at 32 Hz, aligned with cache/mat_reac).
With argument norm / onorm: the normalised dominance (and relative onsets) of decoder_lodo/build_glimpse.py, computed
by its features() (same formulas, no calibration needed).
With argument softW (e.g. soft20): graded dominance m = clip(D / (W/10 x calibrated 10-dB threshold), 0, 1) -> cache/glimpsesoftW."""
import glob
import os
import sys

import numpy as np
import scipy.io as sio
from scipy.signal import butter, lfilter, resample_poly, sosfilt

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "..", "decoder_lodo"))
import build_glimpse as BG  # noqa: E402  (reads the same command-line argument)
DER = os.path.join(HERE, "extract", "ds-eeg-snhl", "derivatives", "stimuli")
ARG = sys.argv[1] if len(sys.argv) > 1 else "10"
SOFT = float(ARG[4:]) / 10 if ARG.startswith("soft") else None       # width in units of the calibrated 10-dB threshold
OUT = os.path.join(HERE, "cache", f"glimpse{ARG}")
BP = butter(2, [1, 9], "bandpass", fs=128, output="sos")
A = np.exp(-1 / (0.025 * 128))
TARGET_FRAC = 0.420


def raw_env(stim_file):
    base = os.path.join(DER, os.path.splitext(stim_file)[0])
    f = base + "woa.mat" if os.path.exists(base + "woa.mat") else base + ".mat"
    d = sio.loadmat(f, squeeze_me=True, struct_as_record=False)["dat"]
    return resample_poly(np.ravel(d.feat).astype(np.float64), 128, int(np.ravel(d.fs)[0]))


def trial(f):
    z = np.load(f, allow_pickle=True)
    et, em = raw_env(str(z["target_file"])), raw_env(str(z["masker_file"]))
    dly = int(round(float(z["masker_delay_s"]) * 128))
    em = np.concatenate([np.zeros(max(dly, 0)), em])[max(-dly, 0):]
    n128 = min(len(et), len(em), len(z["eeg"]))
    e = np.stack([et[:n128], em[:n128]], 1)
    s = lfilter([1 - A], [1, -A], np.maximum(e, 1e-9), axis=0)
    return e, np.log10(s[:, 0] / s[:, 1]), s


if __name__ == "__main__":
    os.makedirs(OUT, exist_ok=True)
    files = sorted(glob.glob(os.path.join(HERE, "stage_a", "*.npz")))
    data = {f: trial(f) for f in files}
    allD = np.concatenate([D for _, D, _ in data.values()])
    ths = np.linspace(0.0, 1.0, 201)
    fr = [(np.mean(allD > t) + np.mean(-allD > t)) / 2 for t in ths]
    thr = float(ths[int(np.argmin(np.abs(np.array(fr) - TARGET_FRAC)))])
    for f, (e, D, s) in data.items():
        key = "FUG_" + os.path.basename(f)[:-4]
        T = len(np.load(os.path.join(HERE, "cache", "mat_reac", key + ".npz"))["env"])
        m = (np.stack([np.clip(D / (SOFT * thr), 0, 1), np.clip(-D / (SOFT * thr), 0, 1)], 1) if SOFT
             else np.stack([D > thr, -D > thr], 1).astype(np.float64))
        if ARG in ("norm", "onorm", "full"):
            F = {k: v[::4][32:32 + T].astype(np.float32) for k, v in BG.features(e).items()}
            assert len(F["glim"]) == T, (key, len(F["glim"]), T)
            np.savez(os.path.join(OUT, key + ".npz"), **F)
            continue
        extra = {}
        g, mk, m = (x[::4][32:32 + T] for x in (sosfilt(BP, e * m, axis=0), sosfilt(BP, e * (1 - m), axis=0), m))
        assert len(g) == T, (key, len(g), T)
        np.savez(os.path.join(OUT, key + ".npz"), glim=g.astype(np.float32), mask=mk.astype(np.float32), m=m.astype(np.float32), **extra)
    print(f"Fuglsang glimpse: {len(data)} trials, log10-ratio threshold {thr:.3f} "
          f"(= {20 * thr:.1f} dB of envelope ratio) -> glimpse fraction {np.interp(thr, ths, fr):.3f}")
