"""Population prior for the causal Euclidean alignment (prep 'mat_reap').

Test rule (Terms 6.3): only data of the trial under test may be used, so the EA state cannot be carried across a
subject's trials (prep mat_reac). Without carry the alignment of every trial starts at the identity (prep mat_rea) and
the first minute is poorly whitened. Here it starts at a FIXED population covariance instead: the mean spatial
covariance of the z-scored, CAR-referenced 32-Hz EEG over an external training set (Fuglsang 2020: 44 subjects, 1401
trials, same 58-channel montage). It is a model parameter learnt from training data: no test data, no ID.
Fuglsang only (not the five corpus datasets) so that one cache serves every LODO fold without the held-out dataset
contributing to the prior; the final submission may add the corpus.
Output cache/ea_prior_fug.npy (58, 58); channels never present get identity rows.
usage: python build_ea_prior.py"""
import glob
import os

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

from common_exp import EXP
from prepare_data import zscore_trial

BP = butter(2, [1, 9], "bandpass", fs=128, output="sos")
OUT = os.path.join(EXP, "cache", "ea_prior_fug.npy")


def trial_cov(f):
    z = np.load(f, allow_pickle=True)
    pres = z["present"].astype(bool)
    x = sosfiltfilt(BP, z["eeg"].astype(np.float64), axis=0)[::4]           # as stage_b_convert.py
    x[:, pres] -= x[:, pres].mean(1, keepdims=True)
    x[:, ~pres] = 0.0
    x = zscore_trial(x)[32:-32]
    return x.T @ x / len(x), np.outer(pres, pres).astype(float)


if __name__ == "__main__":
    files = sorted(glob.glob(os.path.join(EXP, "..", "external_data", "Fuglsang2020", "stage_a", "*.npz")))
    res = Parallel(n_jobs=4)(delayed(trial_cov)(f) for f in files)
    S, N = sum(r[0] for r in res), sum(r[1] for r in res)
    R = np.where(N > 0, S / np.maximum(N, 1), 0.0)
    miss = np.diag(N) == 0
    R[miss, miss] = 1.0
    np.save(OUT, R)
    w = np.linalg.eigvalsh(R)
    print(f"EA prior from {len(files)} Fuglsang trials -> {OUT}: diag {np.diag(R).min():.3f}..{np.diag(R).max():.3f}, "
          f"eigenvalues {w.min():.2e}..{w.max():.1f}, mean |off-diagonal| {np.abs(R - np.diag(np.diag(R))).mean():.3f}, "
          f"channels without data {int(miss.sum())}")
