"""Export the current result tables of experiments/decoder_lodo to data.json for build_report.js.
Rebuild the report:  ../../.venv/bin/python export_data.py && node build_report.js"""
import json, os
import pandas as pd
HERE = os.path.dirname(os.path.abspath(__file__))
R = os.path.join(HERE, "..", "..", "experiments", "decoder_lodo", "results")
M = pd.read_csv(os.path.join(R, "summary_mean.csv")).set_index("experiment")
P = pd.read_csv(os.path.join(R, "summary_per_dataset.csv"))
out = {"main": []}
for d in ["wf", "wf_persubject", "wf_riemann", "wf_forward", "cca", "fbcca", "simple_cnn", "lsm_cnn", "deformer",
          "deformer_ws", "vlaai", "vlaai_ws", "ssmamba"]:
    r = {"decoder": d}
    for p in ["mat", "py", "mat_rz", "py_rz"]:
        e = f"{d}__{p}"
        r[p] = round(float(M.loc[e, "H"]), 3) if e in M.index else None
    for k, c in (("acc_mat", "Acc"), ("sdt_mat", "SDT")):
        r[k] = round(float(M.loc[f"{d}__mat", c]), 2) if f"{d}__mat" in M.index else None
    out["main"].append(r)
def grp(es):
    e = [x for x in es if x in M.index]
    if not e:
        return None
    s = M.loc[e]
    return dict(n=len(e), H=[round(v, 3) for v in s.H], meanH=round(s.H.mean(), 3), Acc=round(s.Acc.mean(), 2),
                SDT=round(s.SDT.mean(), 2), L=round(s.L.mean(), 3), snr=round(s.snr1.mean(), 3),
                per={k: round(v, 3) for k, v in P[P.experiment.isin(e)].groupby("held_out").H.mean().items()})
S = lambda b, ss: [b + s for s in ss]
s4, s2 = ["", "_s1", "_s2", "_s3"], ["", "_s1"]
out["groups"] = {
    "base": grp(S("vlaai_ws__mat_aug_cluster", s4)), "onset": grp(S("vlaai_ws__mat_aug_cluster_onset", s4)),
    "bands": grp(S("vlaai_ws__mat_aug_cluster_onset_bands", s2)), "nce": grp(S("vlaai_ws__mat_aug_cluster_onset_nce", s2)),
    "ea60": grp(S("vlaai_ws__mat_rea_aug_cluster_onset", s4)), "ea30": grp(S("vlaai_ws__mat_rea30_aug_cluster_onset", s2)),
    "ea120": grp(S("vlaai_ws__mat_rea120_aug_cluster_onset", s2)), "eacarry": grp(S("vlaai_ws__mat_reac_aug_cluster_onset", s2)),
    "adt": grp(S("adt__mat_aug_cluster_onset", s2)), "causal": grp(S("vlaai_ws__py_rz_aug_cluster_onset", s2)),
    "fft": grp(S("vlaai_ws__fft_aug_cluster", s2)), "fft15": grp(S("vlaai_ws__fft15_aug_cluster", s2)), "wf_mat": grp(["wf__mat"]),
}
single = ["vlaai_ws__mat", "vlaai_ws__mat_aug", "vlaai_ws__mat_aug_cluster", "vlaai_ws__mat_aug_balance", "vlaai_ws__mat_aug_renorm",
          "vlaai_ws__mat_aug_warplag", "vlaai_ws__mat_aug_all", "vlaai_ws__mat_w3s", "vlaai_ws__mat_w7s", "vlaai_ws__mat_aug_cluster_w10s",
          "vlaai__mat_wu10", "vlaai__py_wu10", "vlaai__mat_skip", "vlaai__mat_skip_aug", "vlaai__mat"]
out["single"] = {e: dict(H=round(float(M.loc[e, "H"]), 3), Acc=round(float(M.loc[e, "Acc"]), 2), SDT=round(float(M.loc[e, "SDT"]), 2),
                         snr=round(float(M.loc[e, "snr1"]), 3)) for e in single if e in M.index}
out["n_experiments"] = int(len(M))
json.dump(out, open(os.path.join(HERE, "data.json"), "w"), indent=1)
print("exported", out["n_experiments"], "experiments")
