#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Aggregate all results///metrics.json into one comparison table. Writes results/summary.csv and prints a readable table grouped by dataset. """ import os, json, glob, csv from collections import defaultdict PROJ = "/mnt/tidal-alsh-share2/dataset/qinshengqian/research/c3/GPT-Image" RESULTS = os.environ.get("RESULTS_DIR", f"{PROJ}/results") MODELS = ["retfound", "resnet", "vit"] # metric key shown per task type COMMON = ["accuracy", "balanced_accuracy", "f1_macro", "precision_macro", "recall_macro", "cohen_kappa", "quadratic_weighted_kappa", "mcc"] BIN = ["auroc", "auprc", "sensitivity", "specificity"] MULTI = ["auroc_macro_ovr", "auprc_macro"] def fmt(v): return "" if v is None else (f"{v:.4f}" if isinstance(v, (int, float)) else str(v)) def main(): rows = [] by_ds = defaultdict(dict) for mj in sorted(glob.glob(os.path.join(RESULTS, "*", "*", "metrics.json"))): parts = mj.split(os.sep) ds, model = parts[-3], parts[-2] m = json.load(open(mj)) cols = COMMON + (BIN if m.get("task") == "binary" else MULTI) row = {"dataset": ds, "model": model, "task": m.get("task"), "n_test": m.get("n_test")} for k in COMMON + BIN + MULTI: row[k] = m.get(k) rows.append(row) by_ds[ds][model] = m # write full CSV os.makedirs(RESULTS, exist_ok=True) csv_path = os.path.join(RESULTS, "summary.csv") allcols = ["dataset", "model", "task", "n_test"] + COMMON + BIN + MULTI with open(csv_path, "w", newline="") as f: w = csv.DictWriter(f, fieldnames=allcols) w.writeheader() for r in rows: w.writerow({k: fmt(r.get(k)) for k in allcols}) print(f"wrote {csv_path} ({len(rows)} runs)\n") # pretty per-dataset table for ds in sorted(by_ds): task = next(iter(by_ds[ds].values())).get("task") keys = ["accuracy", "f1_macro"] + (["auroc", "auprc", "sensitivity", "specificity"] if task == "binary" else ["auroc_macro_ovr", "quadratic_weighted_kappa"]) print(f"### {ds} ({task})") header = " " + "model".ljust(10) + "".join(k[:14].ljust(15) for k in keys) print(header) for model in MODELS: if model not in by_ds[ds]: continue m = by_ds[ds][model] line = " " + model.ljust(10) + "".join(fmt(m.get(k)).ljust(15) for k in keys) print(line) print() if __name__ == "__main__": main()