File size: 2,217 Bytes
a4326d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Resolve the locked CycleGate result table to Soft RadGraph pair inputs."""

from __future__ import annotations

import argparse
import csv
import json
import re
from pathlib import Path


def existing_path(raw: str) -> Path:
    path = Path(raw)
    if path.exists():
        return path
    alias = Path(str(path).replace("/opt/dlami/nvme/report", "/data1/report"))
    if alias.exists():
        return alias
    raise FileNotFoundError(path)


def slug(value: str) -> str:
    return re.sub(r"[^a-z0-9]+", "_", value.lower()).strip("_")


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--full-results", required=True, type=Path)
    parser.add_argument("--output-csv", required=True, type=Path)
    args = parser.parse_args()

    payload = json.loads(args.full_results.read_text(encoding="utf-8"))
    rows = []
    for result in payload["report_rows"]:
        aggregate_path = existing_path(result["source"])
        aggregate = json.loads(aggregate_path.read_text(encoding="utf-8"))
        pairs_path = existing_path(aggregate["pairs_csv"])
        if int(aggregate["rows"]) != 4394:
            raise ValueError(f"unexpected CycleGate row count in {aggregate_path}")
        aligned = aggregate["aligned_overall"]
        rows.append({
            "model_id": slug(result["model"]),
            "model": result["model"],
            "pairs_csv": str(pairs_path.resolve()),
            "radgraph_simple_f1": aligned["radgraph_simple_f1"],
            "radgraph_partial_f1": aligned["radgraph_partial_f1"],
            "radgraph_complete_f1": aligned["radgraph_complete_f1"],
            "aggregate_json": str(aggregate_path.resolve()),
        })

    if len(rows) != 12:
        raise ValueError(f"expected 12 locked report rows, found {len(rows)}")
    args.output_csv.parent.mkdir(parents=True, exist_ok=True)
    with args.output_csv.open("w", newline="", encoding="utf-8") as handle:
        writer = csv.DictWriter(handle, fieldnames=list(rows[0]))
        writer.writeheader()
        writer.writerows(rows)
    print(json.dumps({"models": len(rows), "output_csv": str(args.output_csv)}, indent=2))


if __name__ == "__main__":
    main()