cxr-report-generation / src /dataset.py
adhisetiawan's picture
Deploy CXR report generation demo
16d6749
Raw
History Blame Contribute Delete
1.53 kB
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
def load_demo_cases(path: Path) -> list[dict[str, Any]]:
with path.open("r", encoding="utf-8") as file:
return [json.loads(line) for line in file if line.strip()]
def get_demo_case(cases: list[dict[str, Any]], case_id: str) -> dict[str, Any]:
for case in cases:
if case.get("case_id") == case_id:
return case
available = ", ".join(case["case_id"] for case in cases)
raise KeyError(f"Unknown case_id '{case_id}'. Available case IDs: {available}")
def resolve_demo_image_path(raw_path: str, dataset_root: Path) -> Path:
return dataset_root / raw_path.lstrip("/")
def resolve_case_images(case: dict[str, Any], dataset_root: Path) -> list[Path]:
raw_paths = case.get("images", [])
if not raw_paths:
raise ValueError(f"Case '{case.get('case_id')}' has no images.")
resolved_paths: list[Path] = []
missing_paths: list[Path] = []
for raw_path in raw_paths:
resolved = resolve_demo_image_path(raw_path, dataset_root)
if resolved.exists():
resolved_paths.append(resolved)
else:
missing_paths.append(resolved)
if missing_paths:
missing_list = "\n".join(f" - {path}" for path in missing_paths)
raise FileNotFoundError(
f"Resolved demo image(s) not found for case "
f"'{case.get('case_id')}'. Check --dataset-root.\n{missing_list}"
)
return resolved_paths