"""Unpack PixCell-training-data Parquet shards into the folder layout used by the PixCell training code. python to_pixcell_layout.py --hf_dir --out [--sources tcga_diagnostic gtex ...] Writes /patches///single_1024//_.jpeg (original JPEG bytes) and /patches/metadata/patch_names_all.hdf5 with one key per source (e.g. "tcga_diagnostic_1024"). Note: PTRC-HGSOC lives under "others/" (key "others_1024"), matching the original layout. """ import argparse from collections import defaultdict from pathlib import Path import h5py import pyarrow.parquet as pq from tqdm import tqdm p = argparse.ArgumentParser() p.add_argument("--hf_dir", type=Path, required=True) p.add_argument("--out", type=Path, required=True) p.add_argument("--sources", nargs="*", default=None) args = p.parse_args() patches = args.out / "patches" names = defaultdict(list) sources = args.sources or sorted(d.name for d in (args.hf_dir / "data").iterdir()) for source in sources: for f in tqdm(sorted((args.hf_dir / "data" / source).glob("*.parquet")), desc=source): for batch in pq.ParquetFile(f).iter_batches(columns=["image"], batch_size=256): for img in batch.column("image").to_pylist(): dst = patches / img["path"] dst.parent.mkdir(parents=True, exist_ok=True) dst.write_bytes(img["bytes"]) names[img["path"].split("/")[0] + "_1024"].append(img["path"]) (patches / "metadata").mkdir(parents=True, exist_ok=True) with h5py.File(patches / "metadata/patch_names_all.hdf5", "a") as h5: for key, paths in names.items(): if key in h5: del h5[key] h5.create_dataset(key, data=paths, dtype=h5py.string_dtype()) print({k: len(v) for k, v in names.items()})