appworld-tmp / tools /validate_dataset.py
hbiny's picture
Upload folder using huggingface_hub (part 10)
178f8f9 verified
Raw History Blame Contribute Delete
5.92 kB
#!/usr/bin/env python3
"""Validate a relocated AppWorld-Vox export using only Python's standard library."""
import argparse
import collections
import hashlib
import json
from pathlib import Path
import wave
PERTURBATION_TYPES = {'filler', 'false-start', 'addition', 'revision', 'cancel', 'halt'}
TYPES = PERTURBATION_TYPES | {'clean'}
def digest(path):
h = hashlib.sha256()
with path.open('rb') as f:
for chunk in iter(lambda: f.read(1048576), b''):
h.update(chunk)
return h.hexdigest()
def load(path):
return json.loads(path.read_text())
def validate(root):
root = root.resolve()
def local(relative):
p = (root/relative).resolve()
assert p.is_relative_to(root), f'Path outside dataset: {relative}'
return p
rows = [json.loads(s) for s in (root/'manifest.jsonl').read_text().splitlines() if s.strip()]
assert rows, 'Empty manifest'
splits = load(root/'splits.json')
assert set(splits) == {'train', 'test'}
assert not set(splits['train']) & set(splits['test'])
seen, wavs, family_splits, samples = set(), set(), collections.defaultdict(set), collections.defaultdict(dict)
for r in rows:
key = r['task_id'], r['type']
assert key not in seen and r['type'] in TYPES, key
seen.add(key)
assert r['task_id'] in splits[r['split']]
assert r['family_id'] == r['task_id'].rsplit('_', 1)[0]
family_splits[r['family_id']].add(r['split'])
wav, mp = local(r['audio']), local(r['metadata'])
sample = root/r['split']/r['task_id']
assert wav == sample/'audio'/(r['type']+'.wav')
assert mp == sample/'metadata'/(r['type']+'.json')
assert digest(wav) == r['wav_sha256'], str(wav)
wavs.add(wav)
with wave.open(str(wav), 'rb') as w:
i = r['audio_info']
assert (w.getframerate(), w.getnframes(), w.getnchannels(), w.getsampwidth()) == (
i['sample_rate'], i['frames'], i['channels'], i['sample_width_bytes'])
assert i['duration_seconds'] == i['frames']/i['sample_rate']
assert len(w.readframes(i['frames'])) == i['frames']*i['channels']*i['sample_width_bytes']
m = load(mp)
assert (m['task_id'], m['type'], m['source_type']) == (r['task_id'], r['type'], r['source_type'])
assert m['audio_info'] == r['audio_info']
assert m['source_metadata']['wav_sha256'] == r['wav_sha256']
if r['type'] == 'clean':
assert m['llm_selection'] is None
assert m['import_selection']['method'] == 'user_requested_clean_original'
assert not m['inserted_pause_intervals'] and not m['source_metadata']['long_boundaries']
assert all(not u.get('timeline') and u.get('edited_events') == 0 for u in m['source_metadata']['utterance_reports'])
else:
assert m['llm_selection']['is_default']
assert m['llm_selection']['audio_semantic']['status'] == 'equivalent'
if r['type'] in {'false-start','addition','revision'}:
assert m['llm_selection']['semantic']['status'] == 'equivalent'
assert load(sample/'specs.json')['instruction'] == load(sample/'annotation.json')['instruction'] == m['source_metadata']['original_instruction']
assert (sample/'dbs').is_dir() and (sample/'ground_truth').is_dir()
for event in m['inserted_pause_intervals']:
assert 0 <= event['start_frame'] < event['end_frame'] <= i['frames']
assert event['start_seconds'] == event['start_frame']/i['sample_rate']
assert event['end_seconds'] == event['end_frame']/i['sample_rate']
samples[sample][r['type']] = r
assert all(len(s) == 1 for s in family_splits.values()), 'Family crosses splits'
assert wavs == {p.resolve() for p in root.rglob('*.wav')}, 'Unlisted or missing WAV'
for sample, available in samples.items():
v = load(sample/'variants.json')
assert v['available'] == available
assert set(v['missing_types']) == TYPES-set(available)
for split, ids in splits.items():
assert len(ids) == len(set(ids))
assert set(ids) == {p.name for p in samples if p.parent.name == split}
missing = load(root/'reports/missing.json')
assert {(r['task_id'],r['type']) for r in missing} == {
(p.name,t) for p,available in samples.items() for t in TYPES-set(available)}
inventory = load(root/'reports/file_inventory.json')
listed = {e['path'] for e in inventory}
excluded = set(load(root/'reports/inventory_policy.json')['excluded_paths'])
assert listed == {str(p.relative_to(root)) for p in root.rglob('*') if p.is_file()}-excluded
for e in inventory:
p = local(e['path'])
assert p.stat().st_size == e['bytes'] and digest(p) == e['sha256'], e['path']
return {'status':'passed', 'samples':len(samples), 'wavs':len(rows), 'families':len(family_splits),
'complete_six_type_samples':sum(PERTURBATION_TYPES <= set(a) for a in samples.values()),
'complete_seven_type_samples':sum(set(a)==TYPES for a in samples.values()),
'split_sample_counts': {s:len(ids) for s,ids in splits.items()},
'type_counts': dict(collections.Counter(r['type'] for r in rows)),
'hashed_files':len(inventory), 'family_split_overlap':0,
'checks':['all manifest and sample paths', 'WAV hashes, headers and full PCM payloads',
'instruction and LLM selection consistency', 'pause frame/second bounds',
'split membership and missing types', 'inventory coverage and all file hashes']}
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('root', nargs='?', type=Path, default=Path(__file__).resolve().parents[1])
args = parser.parse_args()
print(json.dumps(validate(args.root), indent=2))