File size: 2,896 Bytes
07b266f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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()