File size: 2,869 Bytes
f4931e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Manifest integrity, privacy, and split-leakage checks."""

from __future__ import annotations

from collections import Counter, defaultdict
from pathlib import Path
from typing import Any

from buddi_asr.data.manifest import read_manifest, resolve_audio_path


def validate_manifest(path: str | Path, *, check_audio: bool = True) -> dict[str, Any]:
    manifest_path = Path(path).resolve()
    records = list(read_manifest(manifest_path))
    errors: list[str] = []
    warnings: list[str] = []
    seen_audio: set[str] = set()
    speaker_splits: dict[str, set[str]] = defaultdict(set)
    split_counts: Counter[str] = Counter()
    language_counts: Counter[str] = Counter()
    source_counts: Counter[str] = Counter()
    parse_quality_counts: Counter[str] = Counter()
    missing_duration = 0

    for index, record in enumerate(records, start=1):
        if record.audio in seen_audio:
            errors.append(f"duplicate audio path at record {index}: {record.audio}")
        seen_audio.add(record.audio)
        if check_audio and not resolve_audio_path(record, manifest_path).is_file():
            errors.append(f"audio not found at record {index}: {record.audio}")
        if record.duration_sec is None:
            missing_duration += 1
        if record.split:
            speaker_splits[record.speaker_id].add(record.split)
            split_counts[record.split] += 1
        language_counts[record.language] += 1
        source_counts[record.source] += 1
        quality = record.metadata.get("speaker_parse_quality")
        if isinstance(quality, str):
            parse_quality_counts[quality] += 1

    leaking_speakers = {
        speaker: sorted(splits) for speaker, splits in speaker_splits.items() if len(splits) > 1
    }
    if leaking_speakers:
        examples = list(sorted(leaking_speakers.items()))[:5]
        errors.append(
            f"{len(leaking_speakers)} speaker IDs cross split boundaries; examples: {examples}"
        )
    session_only = parse_quality_counts.get("session_only", 0)
    if session_only:
        warnings.append(
            f"{session_only} records have session-only speaker grouping; final speaker-disjoint "
            "metrics require manual anonymous overrides or upstream speaker metadata"
        )
    if missing_duration:
        warnings.append(f"{missing_duration} records do not have a probed duration")

    return {
        "ok": not errors,
        "manifest": str(manifest_path),
        "records": len(records),
        "speakers": len({record.speaker_id for record in records}),
        "errors": errors,
        "warnings": warnings,
        "splits": dict(sorted(split_counts.items())),
        "languages": dict(sorted(language_counts.items())),
        "sources": dict(sorted(source_counts.items())),
        "speaker_parse_quality": dict(sorted(parse_quality_counts.items())),
    }