"""A4 on the synthetic-switch LODO: classifier folds trained with the auxiliary 'switch in the last 5 s' head, scored with the plain HMM
(p_stay 0.998) and with the gated HMM. usage: python lodo_gated.py EXP [EXP ...] -> results/research/lodo_gated.csv"""
import os
import sys

import pandas as pd

import score as S
from common import DS_KEYS
from common_exp import EXP
from hmm_gated import score_dataset_gated

GRID = ((0.998, 0.95), (0.998, 0.9), (0.998, 0.8), (0.99, 0.9), (0.99, 0.7))

if __name__ == "__main__":
    rows = []
    for exp in sys.argv[1:]:
        ev, info = S.load_folds(exp, "causal")
        la = float(info[DS_KEYS[0]]["lookahead_s"])
        for ds in DS_KEYS:
            r = score_dataset_gated(ds, ev, la, 0.998, 0.998, use_gate=False)
            rows.append(dict(exp=exp, ds=ds, mode="no gate", p_hi=0.998, p_lo=0.998, **r))
            for ph, pl in GRID:
                r = score_dataset_gated(ds, ev, la, ph, pl)
                rows.append(dict(exp=exp, ds=ds, mode="gate", p_hi=ph, p_lo=pl, **r))
            print(rows[-1], flush=True)
    df = pd.DataFrame(rows)
    out = os.path.join(EXP, "results", "research", "lodo_gated.csv")
    df.to_csv(out, mode="a", header=not os.path.exists(out), index=False)
    print(df.groupby(["exp", "mode", "p_hi", "p_lo"])[["H", "Acc", "SDT"]].mean().round(4).to_string())
