File size: 3,170 Bytes
4b3a024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
"""
Compute mean/min/max for sync-c and sync-d scores from a JSONL file.
By default, uses sync["0"][0] to align with export_speakervid_jsonl.py filters.
"""

import argparse
import json
import math
import sys
from typing import Iterable, Tuple, List


def iter_sync_pairs(sync_field: dict) -> Iterable[Tuple[float, float]]:
    for _, items in sync_field.items():
        for item in items:
            if not isinstance(item, list) or len(item) < 2:
                continue
            yield float(item[0]), float(item[1])


def get_primary_pair(sync_field: dict, sync_key: str, index: int) -> Tuple[float, float] | None:
    items = sync_field.get(sync_key)
    if not isinstance(items, list) or index >= len(items):
        return None
    item = items[index]
    if not isinstance(item, list) or len(item) < 2:
        return None
    return float(item[0]), float(item[1])


def main() -> int:
    parser = argparse.ArgumentParser(
        description="Compute mean/min/max for sync-c and sync-d from JSONL."
    )
    parser.add_argument("path", help="JSONL path.")
    parser.add_argument(
        "--sync-key",
        default="0",
        help='Sync dict key to read (default: "0").',
    )
    parser.add_argument(
        "--index",
        type=int,
        default=0,
        help="Index within sync list to use per sample (default: 0).",
    )
    args = parser.parse_args()

    path = args.path
    samples: List[Tuple[float, float, float]] = []

    with open(path, "r", encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            obj = json.loads(line)
            sync_field = obj.get("sync") or {}
            primary = get_primary_pair(sync_field, args.sync_key, args.index)
            if primary is None:
                continue
            duration = obj.get("duration")
            if duration is None:
                duration = 0.0
            samples.append((primary[0], primary[1], float(duration)))

    if not samples:
        print("No sync-c/sync-d pairs found.")
        return 1

    samples.sort(key=lambda x: x[0], reverse=True)
    total = len(samples)

    def summarize(subset: List[Tuple[float, float, float]]) -> str:
        count = len(subset)
        sum_c = sum(s[0] for s in subset)
        sum_d = sum(s[1] for s in subset)
        min_c = min(s[0] for s in subset)
        max_c = max(s[0] for s in subset)
        min_d = min(s[1] for s in subset)
        max_d = max(s[1] for s in subset)
        hours = sum(s[2] for s in subset) / 3600.0
        mean_c = sum_c / count
        mean_d = sum_d / count
        return (
            f"sync-c: mean={mean_c:.6f} min={min_c:.6f} max={max_c:.6f}; "
            f"sync-d: mean={mean_d:.6f} min={min_d:.6f} max={max_d:.6f}; "
            f"duration_hours={hours:.3f} (n={count})"
        )

    print(f"all: {summarize(samples)}")
    for pct in (10, 20, 30, 40, 50):
        k = max(1, math.ceil(total * (pct / 100.0)))
        subset = samples[:k]
        print(f"top {pct}%: {summarize(subset)}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())