"""Rebuild Table 5 of the paper (the 21 models, before and after fine-tuning) from the released predictions. Run: python scripts/02_rebuild_table5.py Needs: data/predictions/locked_21_models/*.csv (one file per model, with the columns path, class, label, pred_before, pred_after, ...) Makes: results/table5_rebuilt.csv """ import sys from pathlib import Path import pandas as pd ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from kernel_transfer.data import tray_id # noqa: E402 from kernel_transfer.metrics import macro_f1, hd_f1 # noqa: E402 from kernel_transfer.stats import TrayBootstrap, macro_f1_score # noqa: E402 def rebuild(pred_dir=ROOT / "data/predictions/locked_21_models"): rows = [] for file in sorted(Path(pred_dir).glob("pred_graindet_ft_*.csv")): p = pd.read_csv(file) # the model name is stored in the matching "check_" file next to the predictions in the original # folder; here we simply use the file name, which is unique. name = file.stem.replace("pred_graindet_ft_", "") boot = TrayBootstrap(p["label"], p["path"].map(tray_id), p["label"]) # every tray holds one class zero_point, _ = boot.resampled_scores(p["pred_before"], macro_f1_score) ft_point, ft_boot = boot.resampled_scores(p["pred_after"], macro_f1_score) lo, hi = boot.interval(ft_boot) rows.append(dict(model=name, zero_shot_macro_f1=100 * zero_point, fine_tuned_macro_f1=100 * ft_point, gain=100 * (ft_point - zero_point), fine_tuned_ci_lo=100 * lo, fine_tuned_ci_hi=100 * hi, fine_tuned_hd_f1=100 * hd_f1(p["label"], p["pred_after"]))) table = pd.DataFrame(rows).sort_values("fine_tuned_macro_f1", ascending=False).round(2) return table if __name__ == "__main__": out = rebuild() (ROOT / "results").mkdir(exist_ok=True) out.to_csv(ROOT / "results/table5_rebuilt.csv", index=False) print(out.to_string(index=False))