#!/usr/bin/env python """Export the index tables as CSV, including a human-readable join. llm_rerank.parquet is only ids + rank + verdict, which is unreadable on its own: you cannot tell whether 'Y' was right without seeing the two narrations. The readable export joins the narrations and the matcher's component scores so a row can be judged by eye. python 04_export_csv.py """ import argparse from pathlib import Path import pandas as pd DATA = Path(__file__).resolve().parent.parent IDX = DATA / "index" def main(): p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) p.add_argument("--only-y", action="store_true", help="readable export keeps Y rows only") a = p.parse_args() seg = pd.read_parquet(IDX / "segments.parquet") r = pd.read_parquet(IDX / "llm_rerank.parquet") r.to_csv(IDX / "llm_rerank.csv", index=False) print(f"[write] {len(r):,} rows -> llm_rerank.csv (exact twin of the parquet)") # narrations + the matcher scores the verdict is meant to correct cols = ["annotation_id", "video_uid", "scene", "narration_en", "start_sec", "end_sec"] ego = seg[cols].add_prefix("ego_").rename(columns={"ego_annotation_id": "ego_annotation_id"}) exo = seg[cols].add_prefix("exo_").rename(columns={"exo_annotation_id": "exo_annotation_id"}) m = pd.read_parquet(IDX / "ego2exo.parquet")[ ["ego_annotation_id", "exo_annotation_id", "score", "verb_j", "noun_j", "text_sim", "hand_match", "object_kind", "coverage_ratio"]] out = (r.merge(ego, on="ego_annotation_id", how="left") .merge(exo, on="exo_annotation_id", how="left") .merge(m, on=["ego_annotation_id", "exo_annotation_id"], how="left")) if a.only_y: out = out[out.llm_verdict == "Y"] order = ["llm_verdict", "hand_match", "object_kind", "coverage_ratio", "matcher_rank", "score", "verb_j", "noun_j", "text_sim", "ego_scene", "ego_narration_en", "exo_narration_en", "ego_annotation_id", "exo_annotation_id", "ego_video_uid", "exo_video_uid", "ego_start_sec", "ego_end_sec", "exo_start_sec", "exo_end_sec"] out = out[[c for c in order if c in out.columns]].sort_values( ["ego_annotation_id", "matcher_rank"]) f = IDX / ("llm_rerank_readable_Y.csv" if a.only_y else "llm_rerank_readable.csv") out.to_csv(f, index=False) print(f"[write] {len(out):,} rows -> {f.name} (narrations + matcher scores joined)") # a small file that opens instantly in any editor s = out.sample(min(300, len(out)), random_state=0).sort_values( ["llm_verdict", "matcher_rank"]) s.to_csv(IDX / "llm_rerank_sample300.csv", index=False) print(f"[write] {len(s):,} rows -> llm_rerank_sample300.csv (spot-check file)") if __name__ == "__main__": main()