"""Score C1 and its matched sw2 baseline with the same HMM p_stay grid.

All inputs are saved LODO evidence. Emissions are fitted from the other four
folds by score.score_dataset; synthetic switches use the original schedule.
The external classifier job separately writes the same p_stay grid for
AASD and Carta. Existing result files are never overwritten.
"""
import csv
import os

import numpy as np

import score
from common import DS_KEYS
from common_exp import EXP


PSTAYS = (0.9, 0.95, 0.98, 0.99, 0.995, 0.998, 0.9997)
EXPERIMENTS = (
    (0, "sw2", "direct_clf__eaP_n5_t60_w10b3_ptSPK_aug-sw2"),
    (0, "c1", "direct_clf__eaP_n5_t60_w10b3_ptSPK_aug-c1xfade"),
    (1, "sw2", "direct_clf__eaP_n5_t60_w10b3_ptSPK_s1_aug-sw2"),
    (1, "c1", "direct_clf__eaP_n5_t60_w10b3_ptSPK_s1_aug-c1xfade"),
)
FIELDS = ("seed", "variant", "experiment", "dataset", "p_stay", "H", "Acc", "SDT", "L", "miss_rate")


def main():
    out = os.path.join(EXP, "results", "research", "c1_pstay_lodo.csv")
    # Exclusive creation preserves prior results on accidental reruns.
    with open(out, "x", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=FIELDS)
        writer.writeheader()
        f.flush()
        for seed, variant, exp in EXPERIMENTS:
            print(f"Loading {exp}", flush=True)
            ev, _ = score.load_folds(exp, "causal")
            for p in PSTAYS:
                per = []
                for ds in DS_KEYS:
                    r = score.score_dataset(ds, ev, lookahead=0.75, p_stay=p)
                    per.append(r)
                    writer.writerow(dict(seed=seed, variant=variant, experiment=exp,
                                         dataset=ds, p_stay=p, **{k: r[k] for k in FIELDS[5:]}))
                    f.flush()
                mean = {k: float(np.mean([r[k] for r in per])) for k in FIELDS[5:]}
                writer.writerow(dict(seed=seed, variant=variant, experiment=exp,
                                     dataset="MEAN", p_stay=p, **mean))
                f.flush()
                print(f"{variant} seed={seed} p={p}: "
                      f"H={mean['H']:.4f} Acc={mean['Acc']:.2f} SDT={mean['SDT']:.2f}", flush=True)
    print(f"Saved {out}", flush=True)


if __name__ == "__main__":
    main()
