video-vec2wav2-tokenizer / statistics.py
k9cli's picture
Flatten package contents to repo root
de01f6a verified
Raw History Blame Contribute Delete
2 kB
"""Compute dataset statistics and write report.json."""
from __future__ import annotations
import json
from collections import Counter
from dataclasses import dataclass, field, asdict
from pathlib import Path
from typing import Iterable, Sequence
from video_vec2wav2_tokenizer.utils.io import DatasetEntry, ensure_dir
from video_vec2wav2_tokenizer.utils.logging import get_logger
logger = get_logger(__name__)
@dataclass(slots=True)
class DatasetReport:
"""Aggregate statistics over a generated dataset."""
total_videos: int = 0
total_audio_files: int = 0
total_segments: int = 0
total_duration: float = 0.0
average_segment_length: float = 0.0
language_distribution: dict[str, int] = field(default_factory=dict)
def to_dict(self) -> dict:
d = asdict(self)
d["total_duration"] = round(self.total_duration, 3)
d["average_segment_length"] = round(self.average_segment_length, 3)
return d
def build_report(
entries: Sequence[DatasetEntry],
*,
total_videos: int,
total_audio_files: int,
) -> DatasetReport:
"""Aggregate per-segment entries into a :class:`DatasetReport`."""
total_segments = len(entries)
total_duration = sum(e.duration for e in entries)
languages = Counter(e.language for e in entries if e.language)
return DatasetReport(
total_videos=total_videos,
total_audio_files=total_audio_files,
total_segments=total_segments,
total_duration=total_duration,
average_segment_length=(total_duration / total_segments) if total_segments else 0.0,
language_distribution=dict(languages),
)
def write_report(path: str | Path, report: DatasetReport) -> Path:
"""Serialize a report to ``report.json``."""
ensure_dir(Path(path).parent)
with Path(path).open("w", encoding="utf-8") as fh:
json.dump(report.to_dict(), fh, indent=2, ensure_ascii=False)
logger.info("Wrote report to %s", path)
return Path(path)