Datasets:
File size: 2,787 Bytes
21e9982 819fe8c 4c7ff9e 21e9982 8eb75cd 21e9982 819fe8c 21e9982 819fe8c 21e9982 819fe8c | 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 | """Rebuild this partial v2 release from pinned v1 tasks and metadata/selection.json.
Terminal errors score zero. Historical recovery evidence is preserved.
"""
import argparse,copy,hashlib,json
from pathlib import Path
import pyarrow as pa,pyarrow.parquet as pq
p=argparse.ArgumentParser();p.add_argument('--out',type=Path,required=True);p.add_argument('--source',type=Path);a=p.parse_args()
s=json.loads((Path(__file__).parent/'metadata/selection.json').read_text())
if a.source:f=a.source
else:
from huggingface_hub import hf_hub_download
f=Path(hf_hub_download(s['repo_id'],s['source_folder']+'/tasks.parquet',repo_type='dataset',revision=s['source_revision']))
assert hashlib.sha256(f.read_bytes()).hexdigest()==s['source_sha256']
source=pq.ParquetFile(f,memory_map=True);schema=source.schema_arrow.remove_metadata()
for name in s['result_columns']:schema=schema.append(pa.field(name,pa.int64()))
names={task for task,e in s['tasks'].items() if e['kept']}
assert len(names)==s['partial_refresh']['summary']['kept']
meta=dict(source.schema_arrow.metadata or {})
if b'ot.images.v1' in meta:
im=json.loads(meta[b'ot.images.v1']);im['tasks']={k:v for k,v in im['tasks'].items() if k in names};used={e['bundle'] for v in im['tasks'].values() for e in v['environments']};im['bundles']={k:v for k,v in im['bundles'].items() if k in used}
if 'missing' in im:im['missing']=[x for x in im['missing'] if x.get('task') in names]
if 'uncached_task_ids' in im:im['uncached_task_ids']=sorted(names-set(im['tasks']))
if 'coverage' in im:im['coverage']={'cached_tasks':len(im['tasks']),'total_tasks':len(names)}
meta[b'ot.images.v1']=json.dumps(im).encode()
meta[b'ot.release.v2']=json.dumps({k:v for k,v in s.items() if k!='tasks'}).encode()
a.out.mkdir(parents=True,exist_ok=True);assert not (a.out/'tasks.parquet').exists()
written=0
with pq.ParquetWriter(a.out/'tasks.parquet',schema.with_metadata(meta),compression='zstd',use_dictionary=['path','content_sha256'],write_statistics=['path','content_sha256',*s['result_columns']],write_batch_size=1) as writer:
for batch in source.iter_batches(batch_size=16,use_threads=False):
table=pa.Table.from_batches([batch]);paths=table['path'].to_pylist();hashes=table['content_sha256'].to_pylist()
for task,h in zip(paths,hashes):assert h==s['tasks'][task]['content_sha256']
keep=[i for i,task in enumerate(paths) if task in names]
if not keep:continue
selected=table.take(keep).replace_schema_metadata(None);selected_paths=[paths[i] for i in keep]
for name in s['result_columns']:selected=selected.append_column(name,pa.array([s['tasks'][task][name] for task in selected_paths],type=pa.int64()))
writer.write_table(selected.replace_schema_metadata(meta));written+=len(keep)
assert written==len(names)
print(written,'retained tasks')
|