"""Table of the hyper-parameter sweep (experiments tagged _hp-<setting>) against their base configuration (same seed).
usage: python hp_summary.py"""
import glob
import json
import os

import pandas as pd

from common_exp import EXP

rows = []
for f in sorted(glob.glob(os.path.join(EXP, "results", "*_hp-*.json"))):
    e = os.path.basename(f)[:-5]
    base = e.split("_hp-")[0]
    r, b = json.load(open(f))["causal"], json.load(open(os.path.join(EXP, "results", base + ".json")))["causal"]
    rows.append(dict(model="decoder" if e.startswith("vlaai") else "classifier 10 s" if "_w10_" in e else "classifier 5 s", setting=e.split("_hp-")[1], H=r["mean"]["H"], base_H=b["mean"]["H"],
                     dH=r["mean"]["H"] - b["mean"]["H"], AVGC=r["per_dataset"]["AVGC"]["H"], dAVGC=r["per_dataset"]["AVGC"]["H"] - b["per_dataset"]["AVGC"]["H"],
                     Acc=r["mean"]["Acc"], SDT=r["mean"]["SDT"]))
pd.set_option("display.width", 200)
print(pd.DataFrame(rows).sort_values(["model", "dH"], ascending=[True, False]).round(4).to_string(index=False) if rows else "no sweep results yet")
