"""Reverse-engineer how the organisers' stimulus envelopes were computed (needed before adding external data).

For KUL2016 trials, the original 'dry' wav files (Zenodo record 21415059, stimuli.zip) are turned into envelopes by
several candidate methods at 128 Hz and compared with the supplied raw envelope of the same stream
(data/superhuge/raw/stimuli/env, 128 Hz): Pearson correlation at the best lag within +-5 s, both broadband
(0.1 Hz high-pass, drift removed) and in the decoding band (1-9 Hz). The method with correlation ~1 is the one used.

Candidates (audio resampled to 8 kHz first, as in the KU Leuven preprocess_data.m):
  gt_pow06   gammatone filterbank 150-4000 Hz, 1.5-ERB spacing, |subband|^0.6, summed  (KU Leuven 'powerlaw subbands')
  gt_pow03   same with exponent 0.3
  gt_hilb    gammatone subbands, |Hilbert| (no compression), summed
  hilb       broadband |Hilbert|
  hilb_pow06 broadband |Hilbert|^0.6
  abs_pow06  broadband |x|^0.6
DTU (Zenodo 1199011 AUDIO.zip, its preproc_data.m uses an auditory filterbank, |.|^0.3, averaged) is tested the
same way with --ds DTU.
usage: python reverse.py [n_trials] [KUL|DTU]   -> results/reverse_<ds>.csv, printed summary
"""
import os
import sys
import zipfile

import numpy as np
import pandas as pd
from scipy.io import wavfile
from scipy.signal import butter, gammatone, hilbert, lfilter, resample_poly, sosfiltfilt

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(HERE, "..", ".."))
sys.path.insert(0, os.path.join(ROOT, "reports", "eda_full", "scripts"))
from common import load_env, load_labels, trial_index  # noqa: E402

FA, FE = 8000, 128
STIM = os.path.join(HERE, "raw", "stimuli")


def erb_space(lo, hi, spacing):
    erb = lambda f: 21.4 * np.log10(1 + 0.00437 * f)
    inv = lambda e: (10 ** (e / 21.4) - 1) / 0.00437
    return inv(np.arange(erb(lo), erb(hi) + 1e-9, spacing))


FREQS = erb_space(150, 4000, 1.5)
GT = [gammatone(f, "fir", fs=FA) for f in FREQS]


def to128(x, fs):
    return resample_poly(x, FE, fs, axis=0)


def candidates(audio, fs):
    a = resample_poly(audio.astype(np.float64), FA, fs)
    sub = np.stack([lfilter(b, aa, a) for b, aa in GT], 1)             # (T, bands)
    out = {}
    out["gt_pow06"] = to128((np.abs(sub) ** 0.6).sum(1), FA)
    out["gt_pow03"] = to128((np.abs(sub) ** 0.3).sum(1), FA)
    out["gt_hilb"] = to128(np.abs(hilbert(sub, axis=0)).sum(1), FA)
    h = np.abs(hilbert(a))
    out["hilb"] = to128(h, FA)
    out["hilb_pow06"] = to128(h ** 0.6, FA)
    out["abs_pow06"] = to128(np.abs(a) ** 0.6, FA)
    return out


HP = butter(2, 0.1, "highpass", fs=FE, output="sos")
BP = butter(4, [1, 9], "bandpass", fs=FE, output="sos")


def best_corr(ref, cand, maxlag=5 * FE):
    """max Pearson over lags (cand shifted), via FFT cross-correlation of standardised signals."""
    n = min(len(ref), len(cand))
    r, c = ref[:n] - ref[:n].mean(), cand[:n] - cand[:n].mean()
    r, c = r / (r.std() + 1e-12), c / (c.std() + 1e-12)
    L = 1 << int(np.ceil(np.log2(2 * n)))
    xc = np.fft.irfft(np.fft.rfft(r, L) * np.conj(np.fft.rfft(c, L)), L) / n
    lags = np.r_[np.arange(0, maxlag + 1), np.arange(-maxlag, 0)]
    vals = xc[lags % L]
    i = int(np.argmax(vals))
    return float(vals[i]), int(lags[i])


def main(n_trials=12, ds="KUL"):
    raw, zname = ("raw", "stimuli.zip") if ds == "KUL" else ("raw_dtu", "AUDIO.zip")
    if not any(f.endswith(".wav") for _, _, fs_ in os.walk(os.path.join(HERE, raw)) for f in fs_):
        with zipfile.ZipFile(os.path.join(HERE, raw, zname)) as z:
            z.extractall(os.path.join(HERE, raw))
    wavs = {}
    for dp, _, fs_ in os.walk(os.path.join(HERE, raw)):
        for f in fs_:
            if f.endswith(".wav"):
                wavs[f] = os.path.join(dp, f)
    ti = trial_index()
    rows = []
    cache = {}
    for _, r in ti[ti.ds == ds].head(n_trials).iterrows():
        lab = load_labels(r["key"])
        env = np.asarray(load_env(r["key"]), np.float64)                 # (T, 2) col 0 = majority-attended
        streams = list(lab["wav"])
        c0 = int(lab["col0_stream"]) - 1
        for col, si in ((0, c0), (1, 1 - c0)):
            w = streams[si]
            if w not in wavs:
                print("missing wav", w)
                continue
            if w not in cache:
                fs, audio = wavfile.read(wavs[w])
                audio = audio.mean(1) if audio.ndim > 1 else audio
                cache[w] = candidates(audio, fs)
            ref = env[:, col]
            for m, cand in cache[w].items():
                cb, lb = best_corr(sosfiltfilt(HP, ref), sosfiltfilt(HP, cand))
                c9, l9 = best_corr(sosfiltfilt(BP, ref), sosfiltfilt(BP, cand))
                rows.append(dict(key=r["key"], col=col, wav=w, method=m, r_broad=cb, lag_broad=lb, r_1_9=c9, lag_1_9=l9))
        print("done", r["key"], flush=True)
    R = pd.DataFrame(rows)
    os.makedirs(os.path.join(HERE, "results"), exist_ok=True)
    R.to_csv(os.path.join(HERE, "results", f"reverse_{ds}.csv"), index=False)
    S = R.groupby("method")[["r_broad", "r_1_9"]].agg(["mean", "min"]).round(4).sort_values(("r_broad", "mean"), ascending=False)
    print(S.to_string())
    print("typical lag (samples @128 Hz):", R.groupby("method").lag_broad.median().to_dict())


if __name__ == "__main__":
    main(int(sys.argv[1]) if len(sys.argv) > 1 else 12, sys.argv[2] if len(sys.argv) > 2 else "KUL")
