File size: 2,993 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import csv
import json
import math
import sys
from pathlib import Path


REPO_ROOT = Path(__file__).resolve().parents[4]
SHARED_SCRIPTS = REPO_ROOT / "fi1503_baseline_reproduction" / "soft_radgraph" / "scripts"
sys.path.insert(0, str(SHARED_SCRIPTS))

from soft_radgraph import phrase_similarity  # noqa: E402


def line_count(path: Path) -> int:
    with path.open(encoding="utf-8") as handle:
        return sum(1 for line in handle if line.strip())


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

    manifest_summary = json.loads((args.manifest_dir / "manifest_summary.json").read_text())
    manifest_n = sum(
        line_count(path)
        for path in sorted(args.manifest_dir.glob("annotation_manifest_shard*.jsonl"))
    )
    annotation_n = sum(
        line_count(path)
        for path in sorted((args.results_dir / "annotations").glob("annotations_shard*.jsonl"))
    )
    assert manifest_n == annotation_n == manifest_summary["unique_nonblank_texts"]

    rows = list(
        csv.DictReader(
            (args.results_dir / "soft_radgraph_summary.csv").open(
                newline="", encoding="utf-8"
            )
        )
    )
    assert len(rows) == manifest_summary["models"] == 12

    expected_cases = 4_394
    for row in rows:
        assert int(row["n_total"]) == int(row["pairs_n"]) == expected_cases
        per_sample = args.results_dir / row["model_id"] / "soft_radgraph_per_sample.csv"
        assert line_count(per_sample) - 1 == expected_cases
        for key in (
            "soft_radgraph_simple_f1",
            "soft_radgraph_partial_f1",
            "soft_radgraph_complete_f1",
        ):
            value = float(row[key])
            assert math.isfinite(value) and 0.0 <= value <= 1.0
        assert float(row["soft_radgraph_partial_f1"]) >= float(
            row["official_radgraph_partial_f1"]
        )

    assert phrase_similarity("heart", "cardiac silhouette", "ANAT-DP") == 0.0
    assert phrase_similarity("mild", "mildly", "OBS-DP") == 1.0
    assert phrase_similarity("effusion", "effusions", "OBS-DP") == 1.0
    assert phrase_similarity("left", "left lower", "ANAT-DP") == 0.5

    report = {
        "status": "passed",
        "models": len(rows),
        "cases_per_model": expected_cases,
        "manifest_annotations": manifest_n,
        "output_annotations": annotation_n,
        "per_sample_rows_verified": sum(int(row["n_total"]) for row in rows),
        "soft_partial_not_below_official": True,
        "semantic_synonym_nonmatch_verified": "heart != cardiac silhouette",
    }
    (args.results_dir / "validation.json").write_text(
        json.dumps(report, indent=2) + "\n"
    )
    print(json.dumps(report, indent=2))


if __name__ == "__main__":
    main()