NanoJev-Data / verify_dataset.py
aaroncool9's picture C-Tianyu's picture
Duplicate from C-Tianyu/NanoJev-Data
15ec7eb
Raw History Blame Contribute Delete
2.34 kB
#!/usr/bin/env python3
"""Check the local NanoJev dataset package using only Python's standard library."""
import argparse
from collections import Counter
import hashlib
import json
from pathlib import Path
def read_rows(path):
return [json.loads(line) for line in path.read_text().splitlines() if line.strip()]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--root', type=Path, default=Path(__file__).resolve().parent)
args = parser.parse_args()
manifest = json.loads((args.root / 'manifest.json').read_text())
for relative, info in manifest['files'].items():
path = args.root / relative
if hashlib.sha256(path.read_bytes()).hexdigest() != info['sha256']:
raise ValueError(f'File hash mismatch: {relative}')
rows = read_rows(path)
if len(rows) != info['records']:
raise ValueError(f'Record count mismatch: {relative}')
if 'questions' in info and sum(len(row['questions']) for row in rows) != info['questions']:
raise ValueError(f'Question count mismatch: {relative}')
for stage, info in manifest['stages'].items():
full = read_rows(args.root / stage / 'all.jsonl')
if len({row['id'] for row in full}) != len(full):
raise ValueError(f'Duplicate record IDs: {stage}')
source_splits = {}
for row in full:
group = row.get('metadata', {}).get('source_group_id')
if group is not None and source_splits.setdefault(group, row['split']) != row['split']:
raise ValueError(f'A source group crosses splits: {stage}')
if Counter(row['split'] for row in full) != {s: c['records'] for s, c in info['counts'].items()}:
raise ValueError(f'Split counts mismatch: {stage}')
for split in info['counts']:
actual = read_rows(args.root / stage / (split + '.jsonl'))
expected = [row for row in full if row['split'] == split]
if actual != expected:
raise ValueError(f'Split row values or order changed: {stage}/{split}')
print(json.dumps({'complete': True, 'data_files_checked': len(manifest['files']),
'stages': list(manifest['stages']), 'hashes_rows_and_split_order_verified': True}))
if __name__ == '__main__':
main()