vesuvius-snapped-labels / load_example.py
SharpMeow's picture
1.0.1: abstain.tif code 2 marks vertices moved by the neighbour fill
95432ec verified
Raw History Blame Contribute Delete
2.71 kB
"""Load one snapped segment of SharpMeow/vesuvius-snapped-labels and compare it with its published mesh.
pip install huggingface_hub numpy tifffile imagecodecs
python load_example.py # PHerc0841 w00, from the Hub
python load_example.py --root snapped-labels # a local copy made with `hf download`
python load_example.py --segment PHerc0139/20250108000000-w025_2025010863 # any path from index.json
"""
import argparse
import io
import json
import urllib.request
from pathlib import Path
import numpy as np
import tifffile
REPO = "SharpMeow/vesuvius-snapped-labels"
def opener(root):
"""read(path) -> bytes, from a local copy or from the Hub (cached by huggingface_hub)."""
if root:
return lambda p: (Path(root) / p).read_bytes()
from huggingface_hub import hf_hub_download
return lambda p: Path(hf_hub_download(REPO, p, repo_type="dataset")).read_bytes()
def tif(b):
return tifffile.imread(io.BytesIO(b))
def main():
a = argparse.ArgumentParser(description=__doc__.splitlines()[0])
a.add_argument("--root", help="a local copy of the dataset; default: read from the Hub")
a.add_argument("--segment", default="PHerc0841/20260220213127-w00", help="a segment path from index.json")
args = a.parse_args()
read = opener(args.root)
rec = json.loads(read(f"{args.segment}/release.json"))
snapped = np.stack([tif(read(f"{args.segment}/snapped.tifxyz/{c}.tif")) for c in "xyz"], -1)
mask = tif(read(f"{args.segment}/release_mask.tif")) # 0 released, 1 held back, 255 no vertex
abstain = tif(read(f"{args.segment}/abstain.tif")) # 0 snapped, 1 not measured (left in place), 2 filled
# the published mesh, read live from the open data (the URL and its checksums are in release.json)
src = rec["source_mesh"].rstrip("/")
published = np.stack([tif(urllib.request.urlopen(f"{src}/{c}.tif").read()) for c in "xyz"], -1)
out = snapped[..., 0] != -1 # released vertices; -1 where none or held back
d = np.linalg.norm(snapped[out] - published[out], axis=1) # 0 where nothing moved the vertex
print(rec["scroll"], rec["segment"], rec["method"], "grid", snapped.shape[:2])
print("released", int((mask == 0).sum()), "held back", int((mask == 1).sum()), "snapped", int(((abstain == 0) & out).sum()),
"filled", int((abstain == 2).sum()), "left in place", int((abstain == 1).sum()))
print("move, voxels, over released vertices: median %.2f, 90th percentile %.2f" % (np.median(d), np.percentile(d, 90)))
print("matches release.json:", round(float(np.median(d)), 3) == rec["summary"]["move_voxels"]["median"])
if __name__ == "__main__":
main()