File size: 2,623 Bytes
3c366de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Aggregate all results/<dataset>/<model>/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()