File size: 1,995 Bytes
de01f6a | 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 | """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)
|