scrollsim-tiles / load_example.py
SharpMeow's picture
Add the card, previews, loading example, cube index and SHA256SUMS
2a8335a verified
Raw History Blame Contribute Delete
2.14 kB
"""Load the ScrollSim synthetic tiles: one Hard Cases tile, one Hard Cases volume and one 128^3 cube of a scroll.
python load_example.py # from the Hub
python load_example.py --root DIR # from a local copy (hf download ... --local-dir DIR)
Needs: numpy, pillow, zarr (3), datasets, huggingface_hub.
"""
import argparse, glob, json, os
import numpy as np, zarr
from datasets import load_dataset
from huggingface_hub import hf_hub_download, snapshot_download
REPO = "SharpMeow/scrollsim-tiles"
ap = argparse.ArgumentParser()
ap.add_argument("--root", help="a local copy of the dataset")
a = ap.parse_args()
# 1. a 2D Hard Cases tile with exact sheet ids
if a.root:
ds = load_dataset("parquet", data_files=sorted(glob.glob(f"{a.root}/hardcases-tiles/validation-*.parquet")), split="train")
else:
ds = load_dataset(REPO, "hardcases-tiles", split="validation")
row = ds[0]
img = np.array(row["image"]) # uint8 CT
ids = np.array(row["sheet_id"]) # uint16, 0 background, one id per sheet
print("tile:", row["kind"], row["plane"], img.shape, "sheets:", len(np.unique(ids)) - 1)
# 2. a 3D Hard Cases block
name = "hardcases-volumes/hairpin-20261126213.npz"
path = f"{a.root}/{name}" if a.root else hf_hub_download(REPO, name, repo_type="dataset")
v = np.load(path)
print("volume:", json.loads(str(v["layout"]))["kind"], v["ct"].shape, "sheets:", len(np.unique(v["sheet"])) - 1)
# 3. one 128^3 cube of a whole synthetic scroll: CT, winding labels, ink truth
scroll = "scrollsim2/20261126001-sync-ink"
root = a.root or snapshot_download(REPO, repo_type="dataset", allow_patterns=[f"{scroll}/*"])
sim = f"{root}/{scroll}"
cube = (slice(0, 128), slice(128, 256), slice(128, 256))
ct = zarr.open(f"{sim}/ct.zarr/0", mode="r")[cube]
winding = zarr.open(f"{sim}/truth/labels.zarr/0", mode="r")[cube].astype(int) - 1 # -1 is air
ink = zarr.open(f"{sim}/truth/ink.zarr/0", mode="r")[cube]
print("cube:", ct.shape, "windings:", np.unique(winding[winding >= 0]).size, "ink voxels:", int(ink.sum()))
assert img.dtype == np.uint8 and ids.dtype == np.uint16 and ct.dtype == np.uint8
print("ok")