"""Convert Broderick et al. 2018 'Cocktail Party' (Dryad 10.5061/dryad.070jc) to the decoder_lodo cache format
(prep 'mat_reac'), for use as extra TRAINING data only.

  EEG  : 128-ch BioSemi (A1..D32), 128 Hz, unreferenced -> spherical-spline interpolation to the 58 10-10 montage
         channels (tools/interp_biosemi128_to_58.npy, mne) -> filtfilt Butterworth order 2, 1-9 Hz -> 32 Hz -> CAR
         -> full-trial z-score -> causal running EA carried across the subject's runs -> 1-s edge trim
  ENV  : the supplied envelopes (no audio in the release; Hilbert-type, 128 Hz) -> filtfilt order 2, 1-9 Hz ->
         32 Hz -> full-trial z-score -> trim. Column 0 = attended story (subjects 1-17: '20000', 18-33: 'Journey').
  LABEL: attention fixed per subject (att = 0).
Output: cache/mat_reac/BRO_S{s:02d}_R{r:02d}.npz ; cache/index.csv"""
import os
import sys

import numpy as np
import pandas as pd
import scipy.io as sio
from joblib import Parallel, delayed
from scipy.signal import butter, 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, "experiments", "decoder_lodo"))
from prepare_data import causal_ea, zscore_trial  # noqa: E402

CP = os.path.join(HERE, "raw", "Cocktail Party")
OUT = os.path.join(HERE, "cache", "mat_reac")
W = np.load(os.path.join(HERE, "tools", "interp_biosemi128_to_58.npy"))          # (58, 128)
BP = butter(2, [1, 9], "bandpass", fs=128, output="sos")
TRIM = 32


def env(story, run):
    d = sio.loadmat(os.path.join(CP, "Stimuli", "Envelopes", story, f"{story}_{run}_env.mat"))
    assert int(d["fsEnv"].ravel()[0]) == 128
    return sosfiltfilt(BP, d["envelope"].ravel().astype(np.float64))


def subject(s):
    att_story, other = ("20000", "Journey") if s <= 17 else ("Journey", "20000")
    R, rows = None, []
    os.makedirs(OUT, exist_ok=True)
    for run in range(1, 31):
        f = os.path.join(CP, "EEG", f"Subject{s}", f"Subject{s}_Run{run}.mat")
        if not os.path.exists(f):
            continue
        x = sio.loadmat(f)["eegData"].astype(np.float64)
        if x.shape[1] != 128 and x.shape[0] == 128:                              # some runs are stored channels x time
            x = x.T
        x = x @ W.T                                                              # (T, 58) interpolated
        x = sosfiltfilt(BP, x, axis=0)[::4]
        x -= x.mean(1, keepdims=True)
        x = zscore_trial(x)
        x, R = causal_ea(x, 32, tau_s=60.0, R0=R, return_R=True)
        e = np.stack([env(att_story, run), env(other, run)], 1)[::4]
        n = min(len(x), len(e))
        key = f"BRO_S{s:02d}_R{run:02d}"
        np.savez(os.path.join(OUT, key + ".npz"), eeg=x[TRIM:n - TRIM].astype(np.float32),
                 env=zscore_trial(e[:n])[TRIM:n - TRIM].astype(np.float32), att=np.zeros(n - 2 * TRIM, np.int8), t0=TRIM / 32)
        rows.append(dict(key=key, subject=s, run=run, n32=n))
    return rows


if __name__ == "__main__":
    rows = sum(Parallel(n_jobs=4)(delayed(subject)(s) for s in range(1, 34)), [])
    I = pd.DataFrame(rows)
    I.to_csv(os.path.join(HERE, "cache", "index.csv"), index=False)
    print(f"Broderick: {I.subject.nunique()} subjects, {len(I)} runs, {I.n32.sum() / 32 / 3600:.1f} h")
