"""Phase D1/D2: covariance and amplitude fingerprints of dataset and subject identity (H10).

D1 features: 5-s epochs, 1-9 Hz and 8-13 Hz covariances (common57, CAR, causal filters). CAR leaves rank 56,
so every covariance is projected onto the zero-sum subspace (57 -> 56) before the log-Euclidean tangent map.
Alignments: none (raw scale) | none + trace-normalised | EA per recording (subject) | causal running EA
(recursive mean of all covariances seen so far in that subject, epochs in time order, current included).
Classifier: PCA(100) -> multinomial logistic regression; 5-fold GroupKFold with trials as groups (no trial
is split). Tasks: dataset ID (all epochs) and subject ID (within each dataset).
D2 features: amplitude only (log SD, log RMS/SD per channel, relative band powers), raw vs causal running z.
Writes tables/fingerprint_results.csv, figures/d1_embedding.png
"""
import os
import sys

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt  # noqa: E402
import numpy as np  # noqa: E402
import pandas as pd  # noqa: E402
from sklearn.decomposition import PCA  # noqa: E402
from sklearn.linear_model import LogisticRegression  # noqa: E402
from sklearn.metrics import balanced_accuracy_score  # noqa: E402
from sklearn.model_selection import GroupKFold  # noqa: E402
from sklearn.pipeline import make_pipeline  # noqa: E402
from sklearn.preprocessing import StandardScaler  # noqa: E402

sys.path.insert(0, os.path.dirname(__file__))
from common import CACHE, COLORS, DS_KEYS, LABELS, SEED, TAB, get_logger, savefig, trial_index  # noqa: E402

log = get_logger("d1_fingerprint")
C = 57
IU = np.triu_indices(C)
_Q, _ = np.linalg.qr(np.eye(C) - 1.0 / C)       # orthonormal basis of the zero-sum subspace
Q = _Q[:, : C - 1]
IU56 = np.triu_indices(C - 1)
W56 = np.where(IU56[0] == IU56[1], 1.0, np.sqrt(2.0))


def unpack(v):
    out = np.empty((len(v), C - 1, C - 1), np.float64)
    for i in range(0, len(v), 4000):
        b = v[i:i + 4000].astype(np.float64)
        M = np.zeros((len(b), C, C))
        M[:, IU[0], IU[1]] = b
        M = M + np.transpose(M, (0, 2, 1)) - M * np.eye(C)[None]
        out[i:i + 4000] = np.einsum("ci,ncd,dj->nij", Q, M, Q, optimize=True)
    return out


def _eig(M):
    e, V = np.linalg.eigh(M)
    return np.maximum(e, 1e-12 * e.max(axis=-1, keepdims=True)), V


def fpow(M, p):
    e, V = _eig(M)
    if M.ndim == 2:
        return (V * e ** p) @ V.T
    return np.einsum("nij,nj,nkj->nik", V, e ** p, V, optimize=True)


def tangent(M):
    out = np.empty((len(M), len(IU56[0])), np.float32)
    for i in range(0, len(M), 4000):
        e, V = _eig(M[i:i + 4000])
        L = np.einsum("nij,nj,nkj->nik", V, np.log(e), V, optimize=True)
        out[i:i + 4000] = L[:, IU56[0], IU56[1]] * W56
    return out


def load_all():
    ti = trial_index()
    rows, c19, c813, a_raw, a_rz = [], [], [], [], []
    for _, r in ti.iterrows():
        z = np.load(os.path.join(CACHE, "spatial", f"{r['key']}.npz"))
        c19.append(z["cov5_19"])
        c813.append(z["cov5_813"])
        a_raw.append(z["amp5_raw"])
        a_rz.append(z["amp5_rz"])
        rows.append(pd.DataFrame(dict(key=r["key"], ds=r["ds"], subj=r["subj_uid"], trial=r["trial"], t=z["t5"])))
    meta = pd.concat(rows, ignore_index=True)
    return meta, np.concatenate(c19), np.concatenate(c813), np.concatenate(a_raw), np.concatenate(a_rz)


def align(M, meta, mode):
    if mode == "none":
        return M
    if mode == "trace":
        return M / np.trace(M, axis1=1, axis2=2)[:, None, None] * (C - 1)
    out = np.empty_like(M)
    for s, g in meta.groupby("subj"):
        idx = g.sort_values(["trial", "t"]).index.values
        if mode == "ea":
            P = fpow(M[idx].mean(0), -0.5)
            out[idx] = P[None] @ M[idx] @ P[None]
        else:  # causal running EA
            run = np.cumsum(M[idx], axis=0) / np.arange(1, len(idx) + 1)[:, None, None]
            P = fpow(run, -0.5)
            out[idx] = P @ M[idx] @ P
    return out


def cv_acc(X, y, groups, n_pca=100, seed=SEED):
    y = np.asarray(y)
    pred = np.empty(len(y), dtype=y.dtype)
    for tr, te in GroupKFold(5).split(X, y, groups):
        steps = [StandardScaler()]
        if X.shape[1] > n_pca:
            steps.append(PCA(n_pca, random_state=seed))
        clf = make_pipeline(*steps, LogisticRegression(max_iter=500, C=1.0))
        clf.fit(X[tr], y[tr])
        pred[te] = clf.predict(X[te])
    vals, cnt = np.unique(y, return_counts=True)
    return dict(acc=float((pred == y).mean()), bal_acc=float(balanced_accuracy_score(y, pred)),
                chance_majority=float(cnt.max() / cnt.sum()), chance_uniform=1.0 / len(vals), n=len(y),
                n_classes=len(vals))


if __name__ == "__main__":
    meta, c19, c813, a_raw, a_rz = load_all()
    log.info(f"{len(meta)} epochs from {meta.key.nunique()} trials")
    M19, M813 = unpack(c19), unpack(c813)
    del c19, c813
    res, emb = [], {}
    for mode in ("none", "trace", "ea", "running_ea"):
        X = np.c_[tangent(align(M19, meta, mode)), tangent(align(M813, meta, mode))]
        r = cv_acc(X, meta.ds.values, meta.key.values)
        res.append(dict(task="dataset", features="cov 1-9 + 8-13 Hz tangent", alignment=mode, scope="all", **r))
        log.info(f"dataset-ID {mode}: {r}")
        for ds in DS_KEYS:
            m = (meta.ds == ds).values
            r = cv_acc(X[m], meta.subj.values[m], meta.key.values[m])
            res.append(dict(task="subject", features="cov 1-9 + 8-13 Hz tangent", alignment=mode, scope=ds, **r))
        emb[mode] = PCA(2, random_state=SEED).fit_transform(StandardScaler().fit_transform(X))
        del X
    for name, A in (("amplitude raw", a_raw), ("amplitude after causal running z", a_rz)):
        r = cv_acc(np.nan_to_num(A), meta.ds.values, meta.key.values)
        res.append(dict(task="dataset", features=name, alignment="none", scope="all", **r))
        log.info(f"dataset-ID {name}: {r}")
    out = pd.DataFrame(res)
    out.to_csv(os.path.join(TAB, "fingerprint_results.csv"), index=False)
    log.info("\n" + out[["task", "features", "alignment", "scope", "acc", "bal_acc", "chance_majority",
                         "n_classes"]].to_string())
    fig, axs = plt.subplots(1, 4, figsize=(20, 4.6))
    sel = np.random.default_rng(0).choice(len(meta), min(len(meta), 8000), replace=False)
    em = []
    for ax, mode in zip(axs, emb):
        for ds in DS_KEYS:
            m = meta.ds.values[sel] == ds
            ax.scatter(emb[mode][sel][m, 0], emb[mode][sel][m, 1], s=2, color=COLORS[ds], alpha=0.4, label=LABELS[ds])
        acc = out[(out.task == "dataset") & (out.alignment == mode) & out.features.str.startswith("cov")].acc.iloc[0]
        ax.set_title(f"{mode}: dataset-ID accuracy {acc:.3f}", fontsize=9)
        ax.set_xticks([])
        ax.set_yticks([])
        em.append(pd.DataFrame(dict(mode=mode, ds=meta.ds.values[sel], pc1=emb[mode][sel][:, 0], pc2=emb[mode][sel][:, 1])))
    axs[0].legend(markerscale=5, fontsize=7)
    fig.suptitle(f"Tangent-space PCA of 5-s covariances (n={len(meta)} epochs from 1979 trials; 8000 shown)")
    savefig(fig, "d1_embedding", pd.concat(em))
