ZefanW's picture
Add offline dataset integrity check
893cca3 verified
Raw History Blame Contribute Delete
2.83 kB
"""Validate the downloaded dataset. --index-only reads no videos."""
import argparse
import hashlib
import json
from collections import Counter
from pathlib import Path
def records(path):
with path.open(encoding='utf-8') as stream:
return [json.loads(line) for line in stream if line.strip()]
def verify_file(root, artifact):
path=root/artifact['path']
if not path.is_file() or path.stat().st_size!=artifact['bytes']:
raise ValueError(f"Missing or wrong file size: {artifact['path']}")
with path.open('rb') as fp:
h=hashlib.sha256()
for block in iter(lambda:fp.read(1024*1024),b''):
h.update(block)
if h.hexdigest()!=artifact['sha256']:raise ValueError(f"SHA-256 mismatch: {artifact['path']}")
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('root',nargs='?',type=Path,default=Path.cwd())
parser.add_argument('--index-only',action='store_true')
args=parser.parse_args();root=args.root
cases=records(root/'index/cases.jsonl'); methods=json.loads((root/'index/methods.json').read_text())
rows=records(root/'data/devset.jsonl')
all_cases={c['case_id'] for c in cases}
assert len(cases)==len(all_cases)==111
assert len(methods)==len({m['id'] for m in methods})==14
assert len(rows)==1554 and len(set((x['method_id'],x['case_id'])for x in rows))==len(rows)
i={c['case_id']:c for c in cases}
for method in methods:
subset=[r for r in rows if r['method_id']==method['id']]
assert len(subset)==111 and {r['case_id'] for r in subset}==all_cases,method['id']
assert subset==records(root/'manifests'/(method['id']+'.jsonl'))
for r in subset:
assert all(r[k]==i[r['case_id']][k] for k in ('task','variant','seed','scene','capability'))
assert [v['camera'] for v in r['videos']]==['head','left_wrist','right_wrist']
prefix=f"replays/{method['id']}/{r['case_id']}/"
for v in r['videos']:
assert v['path']==prefix+v['camera']+'.mp4' and len(v['sha256'])==64 and v['bytes']>0
if not args.index_only:verify_file(root,v)
assert r['actions_path']==prefix+'actions.json' and len(r['actions_sha256'])==64
if not args.index_only:
verify_file(root,{'path':r['actions_path'],'bytes':r['actions_bytes'],'sha256':r['actions_sha256']})
a=json.loads((root/r['actions_path']).read_text())
assert a['schema']=='robodojo.replay.actions.v1' and len(a['events'])==r['event_count'] and a['run_id']==r['replay_run_id']
print(json.dumps({'mode':'index_only' if args.index_only else 'files_and_sha256','methods':14,'cases_per_method':111,'rows':len(rows),'videos':3*len(rows),'actions':len(rows)}))
if __name__=='__main__':main()