Datasets:
Download preview.py from SharpMeow/scrollsim-tiles: direct link, hf CLI and curl.
- Browser
- Download file 5.16 kB
-
https://huggingface.co/datasets/SharpMeow/scrollsim-tiles/resolve/main/preview.py
- Command line
-
hf download hf://datasets/SharpMeow/scrollsim-tiles/preview.py
-
curl -L -o preview.py https://huggingface.co/datasets/SharpMeow/scrollsim-tiles/resolve/main/preview.py
5.16 kB
| """Preview pictures for the ScrollSim tiles dataset (synthetic data only).""" | |
| import sys, json, numpy as np, zarr, tifffile | |
| import matplotlib; matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt | |
| from matplotlib.colors import ListedColormap | |
| from scipy import ndimage as ndi | |
| sys.path.insert(0, "/home/user/Scrolls") | |
| from kit import hardcases as hc | |
| from PIL import Image | |
| SIM, OUT = sys.argv[1], sys.argv[2] | |
| PAL = ["#4e79a7", "#f28e2b", "#e15759", "#76b7b2", "#edc948", "#b07aa1", "#ff9da7", "#9c755f", "#59a14f", "#bab0ac"] | |
| def labcolor(l): | |
| rgb = np.array([matplotlib.colors.to_rgb(c) for c in PAL]) | |
| out = np.zeros(l.shape + (4,)) | |
| m = l > 0 | |
| out[m, :3] = rgb[(l[m].astype(int) * 7) % len(PAL)] | |
| out[m, 3] = 0.55 | |
| return out | |
| # 1. Hard Cases: one tile per kind, CT above, sheet ids below | |
| want = ["plain", "spiral", "hairpin", "tight_u", "tight_bend", "accordion", "mush", "delamination", "crack", "edge"] | |
| got = {} | |
| k = 0 | |
| while len(got) < len(want) and k < 400: | |
| r = hc.tile(20261126199, k, 256); k += 1 | |
| base = r["kind"].split("+")[0].replace("_compact", "") | |
| if base in want and base not in got: | |
| got[base] = r | |
| fig, ax = plt.subplots(2, 10, figsize=(20, 4.6), dpi=110) | |
| for j, name in enumerate(want): | |
| r = got[name] | |
| ax[0, j].imshow(r["image"], cmap="gray", vmin=0, vmax=255); ax[0, j].set_title(r["kind"].replace("_", " "), fontsize=10) | |
| ax[1, j].imshow(r["image"], cmap="gray", vmin=0, vmax=255) | |
| ax[1, j].imshow(labcolor(ndi.grey_dilation(r["sheet_id"], size=3))) | |
| for a in ax[:, j]: a.set_axis_off() | |
| ax[0, 0].text(-20, 128, "CT", rotation=90, va="center", ha="right", fontsize=11) | |
| ax[1, 0].text(-20, 128, "sheet ids", rotation=90, va="center", ha="right", fontsize=11) | |
| fig.suptitle("Synthetic: Hard Cases tiles, 256 px, one per kind (seed 20261126199, a preview seed outside every split)", fontsize=12) | |
| fig.tight_layout(rect=[0, 0, 1, 0.94]); fig.savefig(OUT + "/hardcases-kinds.png"); plt.close(fig) | |
| # 2. ScrollSim v2 scroll: axial slice, winding labels, ink truth, and the flattened recto surface | |
| ct = zarr.open(SIM + "/ct.zarr/0", mode="r"); lb = zarr.open(SIM + "/truth/labels.zarr/0", mode="r") | |
| ink = zarr.open(SIM + "/truth/ink.zarr/0", mode="r") | |
| zs = np.asarray(ink[:, ::4, ::4]).sum(axis=(1, 2)); z = int(np.argmax(zs)) | |
| a, l, i3 = ct[z], lb[z], ink[z] | |
| d = SIM + "/truth/surfaces/spiral-recto" | |
| X, Y, Zs = (tifffile.imread(f"{d}/{c}.tif").astype(np.float64) for c in "xyz") | |
| inkt = tifffile.imread(d + "/ink.tif") | |
| f = 4 | |
| up = lambda g: ndi.zoom(np.nan_to_num(g, nan=-1), f, order=1) | |
| Xu, Yu, Zu = up(X), up(Y), up(Zs) | |
| bad = ndi.zoom((X < 0).astype(float), f, order=0) > 0 | |
| vol = np.asarray(ct) | |
| flat = ndi.map_coordinates(vol.astype(np.float32), [Zu, Yu, Xu], order=1, mode="nearest") | |
| flat[bad] = np.nan | |
| inku = ndi.zoom(inkt.astype(float), f, order=0) | |
| c0 = 0; c1 = min(flat.shape[1], 2600) | |
| fig = plt.figure(figsize=(16, 9.2), dpi=110) | |
| g = fig.add_gridspec(2, 3, height_ratios=[1.6, 1]) | |
| for j, (title, ov) in enumerate([("CT, axial slice z %d" % z, None), ("winding labels (exact)", "lab"), ("ink truth (exact)", "ink")]): | |
| axx = fig.add_subplot(g[0, j]); axx.imshow(a, cmap="gray", vmin=0, vmax=255) | |
| if ov == "lab": axx.imshow(labcolor(l)) | |
| if ov == "ink": | |
| m = np.zeros(a.shape + (4,)); m[i3 > 0] = matplotlib.colors.to_rgba("#e15759"); axx.imshow(m) | |
| axx.set_title(title, fontsize=11); axx.set_axis_off() | |
| axf = fig.add_subplot(g[1, :]) | |
| show = np.where(np.isnan(flat), 0, flat)[:, c0:c1] | |
| axf.imshow(show, cmap="gray", vmin=np.nanpercentile(flat, 1), vmax=np.nanpercentile(flat, 99.5), aspect="auto") | |
| cnt = np.zeros(show.shape + (4,)); edge = inku[:, c0:c1] > 0 | |
| edge = edge & ~ndi.binary_erosion(edge) | |
| cnt[edge] = matplotlib.colors.to_rgba("#f28e2b"); axf.imshow(cnt, aspect="auto") | |
| axf.set_title("CT sampled on the recto surface, flattened (inner windings left); orange outlines: ink truth. Synthetic letter-like glyphs, not text", fontsize=11) | |
| axf.set_axis_off() | |
| fig.suptitle("Synthetic: one ScrollSim v2 scroll, 256 x 512 x 512, synchrotron scan model, sheet physics and ink (seed 20261126001)", fontsize=12) | |
| fig.tight_layout(rect=[0, 0, 1, 0.96]); fig.savefig(OUT + "/scrollsim2-scroll.png"); plt.close(fig) | |
| # 3. slow GIF: CT, then labels, then ink, on the axial slice | |
| frames = [] | |
| base = np.stack([a] * 3, -1).astype(np.float32) | |
| def blend(rgba): | |
| o = base.copy(); al = rgba[..., 3:4]; o = o * (1 - al) + rgba[..., :3] * 255 * al; return Image.fromarray(o.astype(np.uint8)) | |
| frames.append(Image.fromarray(base.astype(np.uint8))) | |
| frames.append(blend(labcolor(l))) | |
| m = np.zeros(a.shape + (4,)); m[i3 > 0] = matplotlib.colors.to_rgba("#e15759"); frames.append(blend(m)) | |
| frames = [fr.resize((768, 768), Image.NEAREST) for fr in frames] | |
| frames[0].save(OUT + "/scrollsim2-slice.gif", save_all=True, append_images=frames[1:], duration=[6000, 6000, 6000], loop=0, optimize=True) | |
| json.dump({"credit": "Synthetic data made by kit.scrollsim2 and kit.hardcases (ChaseHendrick/Scrolls). No scan data.", "scroll_seed": 20261126001, "tile_seed": 20261126199, "slice_z": z, | |
| "command": "python preview.py SIM OUT"}, open(OUT + "/preview.json", "w"), indent=1) | |
| print("z", z, "ok") | |