temp_dataset / metadb_code /sync_score_stats.py
xingzhaohu's picture
Initial upload
4b3a024 verified
#!/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())