| |
| 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 |
|
|
|
|
| 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() |
|
|