"""Build public data artifacts from completed RefSeg-CA experiments. No authentication or publication occurs here. Natural-image pixels and natural expression text are reconstructed from their upstream release, not redistributed. """ from pathlib import Path import argparse, collections, csv, gzip, hashlib, json, shutil, tarfile import pyarrow as pa import pyarrow.parquet as pq ROOT = Path(__file__).resolve().parents[1] SEEDS = [11, 23, 37, 53, 71] def digest(path): h = hashlib.sha256() with path.open('rb') as f: for chunk in iter(lambda: f.read(1024 * 1024), b''): h.update(chunk) return h.hexdigest() def write_gz_jsonl(path, rows): with gzip.open(path, 'wt', encoding='utf-8') as f: for row in rows: f.write(json.dumps(row, ensure_ascii=False) + '\n') def build(dest): for name in ['data', 'archives', 'manifests', 'results', 'release']: (dest / name).mkdir(parents=True, exist_ok=True) records = [json.loads(s) for s in (ROOT / 'data/rich_synthetic.jsonl').read_text().splitlines()] scenes = [json.loads(s) for s in (ROOT / 'data/rich_scenes.jsonl').read_text().splitlines()] assert len(scenes) == 1200 and len(records) == 28800 groups = collections.defaultdict(list) for r in records: groups[(r['scene_id'], r['render'])].append(r) image_type = pa.struct([('bytes', pa.binary()), ('path', pa.string())]) features = { 'scene_id': {'dtype': 'string', '_type': 'Value'}, 'seed': {'dtype': 'int64', '_type': 'Value'}, 'render': {'dtype': 'string', '_type': 'Value'}, 'image': {'_type': 'Image'}, 'instance_map': {'_type': 'Image'}, 'commands_json': {'dtype': 'string', '_type': 'Value'}, } schema = pa.schema([ ('scene_id', pa.string()), ('seed', pa.int64()), ('render', pa.string()), ('image', image_type), ('instance_map', image_type), ('commands_json', pa.string()) ], metadata={b'huggingface': json.dumps({'info': {'features': features}}).encode()}) for seed in SEEDS: batch = [] selected = sorted([s for s in scenes if s['seed'] == seed], key=lambda s: s['scene_id']) assert len(selected) == 240 for scene in selected: sid = scene['scene_id']; sd = ROOT / 'data/rich' / sid for render in ['flat', 'rich']: qs = groups[(sid, render)] assert len(qs) == 12 commands = [{k: q[k] for k in ['id', 'mode', 'pair_id', 'endpoint', 'query', 'target_instance_ids']} for q in qs] batch.append(dict(scene_id=sid, seed=seed, render=render, image={'bytes': (sd / (render + '.png')).read_bytes(), 'path': sid + '/' + render + '.png'}, instance_map={'bytes': (sd / 'instances.png').read_bytes(), 'path': sid + '/instances.png'}, commands_json=json.dumps(commands))) pq.write_table(pa.Table.from_pylist(batch, schema=schema), dest / 'data' / f'seed-{seed}.parquet', compression='zstd') with tarfile.open(dest / 'archives' / f'rich-seed-{seed}.tar.gz', 'w:gz', compresslevel=4) as archive: for scene in selected: sd = ROOT / 'data/rich' / scene['scene_id'] assert len(list(sd.glob('*.png'))) == 15 for p in sorted(sd.glob('*.png')): archive.add(p, arcname=p.relative_to(ROOT).as_posix(), recursive=False) print(json.dumps({'seed': seed, 'viewer_rows': len(batch), 'scenes': len(selected)}), flush=True) write_gz_jsonl(dest / 'manifests/rich_synthetic.jsonl.gz', records) write_gz_jsonl(dest / 'manifests/rich_scenes.jsonl.gz', scenes) with gzip.open(ROOT / 'results/r3/portable/natural_full_manifest.jsonl.gz', 'rt') as f: natural = [json.loads(s) for s in f] write_gz_jsonl(dest / 'manifests/natural_index.jsonl.gz', [{k: v for k, v in r.items() if k != 'query'} for r in natural]) scratch = dest / '_metric_staging' (scratch / 'results/r3/portable').mkdir(parents=True, exist_ok=True) (scratch / 'results/analysis').mkdir(parents=True, exist_ok=True) for p in (ROOT / 'results/r3/portable').glob('*_primary.csv.gz'): shutil.copy2(p, scratch / 'results/r3/portable' / p.name) for model in ['clipseg', 'groundedsam']: with gzip.open(ROOT / 'results/r3' / f'{model}_replay.jsonl.gz', 'rt') as f: rows = [json.loads(s) for s in f] write_gz_jsonl(scratch / 'results/r3' / f'{model}_replay.jsonl.gz', [{k: v for k, v in r.items() if k != 'query'} for r in rows]) for rel in ['results/r3/review_results.json', 'results/analysis/paper_results.json']: shutil.copy2(ROOT / rel, scratch / rel) with tarfile.open(dest / 'results/primary-and-review-metrics.tar.gz', 'w:gz') as archive: for p in sorted(scratch.rglob('*')): if p.is_file(): archive.add(p, arcname=p.relative_to(scratch).as_posix(), recursive=False) shutil.rmtree(scratch) report = dict(version='R3', scenes=1200, rendered_images=2400, command_image_records=28800, natural_expressions_scored=49492, viewer_rows=2400, seeds=SEEDS, renderer='deterministic Pillow raster renderer', human_validation='not conducted', natural_pixels_and_expression_text='retrieve from the pinned upstream release') report['files'] = {p.relative_to(dest).as_posix(): {'bytes': p.stat().st_size, 'sha256': digest(p)} for p in sorted(dest.rglob('*')) if p.is_file() and p.name != 'DATA_MANIFEST.json'} (dest / 'DATA_MANIFEST.json').write_text(json.dumps(report, indent=2)) print(json.dumps({'status': 'BUILT', 'files': len(report['files']), 'bytes': sum(v['bytes'] for v in report['files'].values())}), flush=True) if __name__ == '__main__': ap = argparse.ArgumentParser(); ap.add_argument('--output', type=Path, default=ROOT / 'release_public') build(ap.parse_args().output)