| |
| """ |
| regen_captions_v2.py — regenerate captions (v2, disease/lesion-centric) from an |
| existing *_images_v1.parquet, WITHOUT re-running extraction. |
| |
| Works uniformly for the private / public-fundus / public-OCT manifests because |
| caption_v2.caption_rows_for() is a pure function of the manifest row. |
| |
| Writes: |
| {out_dir}/{out_name} unified captions_v2 parquet |
| {out_dir}/{cohort}_captions_v2.parquet per-cohort (HF staging) if --per-cohort |
| """ |
| import argparse |
| import time |
| from pathlib import Path |
|
|
| import pandas as pd |
|
|
| import caption_v2 |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--manifest", required=True, help="path to *_images_v1.parquet") |
| ap.add_argument("--out-dir", required=True) |
| ap.add_argument("--out-name", default="captions_v2.parquet") |
| ap.add_argument("--per-cohort", action="store_true", |
| help="also write one {cohort}_captions_v2.parquet per cohort") |
| ap.add_argument("--lesion-sidecar", default=None, |
| help="optional parquet (image_id, lesion_codes) to merge into " |
| "lesion_tags before captioning (from enrich_lesion_tags.py)") |
| args = ap.parse_args() |
|
|
| out_dir = Path(args.out_dir) |
| out_dir.mkdir(parents=True, exist_ok=True) |
|
|
| print(f"loading manifest: {args.manifest}") |
| df = pd.read_parquet(args.manifest) |
| print(f" {len(df)} image rows, {df.cohort.nunique()} cohort(s)") |
|
|
| sidecar = {} |
| if args.lesion_sidecar: |
| sc = pd.read_parquet(args.lesion_sidecar) |
| sidecar = {r["image_id"]: list(r["lesion_codes"]) for _, r in sc.iterrows()} |
| print(f" merged lesion sidecar: {len(sidecar)} images with extra lesion tags") |
|
|
| t0 = time.time() |
| records = df.to_dict("records") |
| all_caps, per_cohort = [], {} |
| for i, row in enumerate(records): |
| if sidecar: |
| extra = sidecar.get(row.get("image_id")) |
| if extra: |
| cur = caption_v2._as_list(row.get("lesion_tags")) |
| row["lesion_tags"] = list(dict.fromkeys(cur + extra)) |
| caps = caption_v2.caption_rows_for(row) |
| all_caps.extend(caps) |
| if args.per_cohort: |
| per_cohort.setdefault(row.get("cohort"), []).extend(caps) |
| if (i + 1) % 100000 == 0: |
| dt = time.time() - t0 |
| print(f" ... {i+1}/{len(records)} ({dt:.0f}s, {(i+1)/dt:.0f} img/s)") |
| dt = time.time() - t0 |
| print(f" done in {dt:.0f}s: {len(all_caps)} caption rows " |
| f"({len(all_caps)/max(len(records),1):.2f} per image)") |
|
|
| cap_df = pd.DataFrame(all_caps, columns=caption_v2.CAP_COLS) |
| uni = out_dir / args.out_name |
| cap_df.to_parquet(uni, index=False) |
| print(f"\n wrote unified: {uni} ({len(cap_df)} rows)") |
| print(" level distribution:") |
| print(cap_df["level"].value_counts().to_string()) |
|
|
| if args.per_cohort: |
| for cohort, caps in per_cohort.items(): |
| cp = out_dir / f"{cohort}_captions_v2.parquet" |
| pd.DataFrame(caps, columns=caption_v2.CAP_COLS).to_parquet(cp, index=False) |
| print(f" wrote {len(per_cohort)} per-cohort caption parquets") |
|
|
| |
| print("\n sample captions:") |
| for t in cap_df["prompt_text"].drop_duplicates().head(12): |
| print(f" {t}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|