|
| 1 | +# adapted from https://raw.githubusercontent.com/ome-zarr-models/ome-zarr-models-py/refs/heads/rfc5/scripts/generate_transform_graphs.py |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import zarr |
| 5 | + |
| 6 | +from ome_zarr_models._v06.collection import Collection |
| 7 | +from ome_zarr_models._v06.image import Image |
| 8 | + |
| 9 | +EXAMPLE_PATH = ( |
| 10 | + Path(__file__).parent.parent |
| 11 | + / "data" |
| 12 | + / "ngff-rfc5-coordinate-transformation-examples" |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def get_all_zarrs(directory: Path) -> list[Path]: |
| 17 | + """ |
| 18 | + Get all Zarr sub-directories. |
| 19 | + """ |
| 20 | + zarrs: list[Path] = [] |
| 21 | + for f in directory.glob("*"): |
| 22 | + if f.is_dir(): |
| 23 | + if f.suffix == ".zarr": |
| 24 | + # Found a Zarr group |
| 25 | + zarrs.append(f) |
| 26 | + else: |
| 27 | + # Recurse |
| 28 | + zarrs += get_all_zarrs(f) |
| 29 | + |
| 30 | + return sorted(zarrs) |
| 31 | + |
| 32 | + |
| 33 | +for zarr_path in get_all_zarrs(EXAMPLE_PATH): |
| 34 | + relative_path = zarr_path.relative_to(EXAMPLE_PATH) |
| 35 | + |
| 36 | + group: Collection | Image |
| 37 | + try: |
| 38 | + if relative_path.parts[0] == "user_stories": |
| 39 | + group = Collection.from_zarr(zarr.open_group(zarr_path, mode="r")) |
| 40 | + else: |
| 41 | + group = Image.from_zarr(zarr.open_group(zarr_path, mode="r")) |
| 42 | + except Exception: |
| 43 | + print(f"😢 Failed to load group at {zarr_path.relative_to(EXAMPLE_PATH)}") |
| 44 | + continue |
| 45 | + |
| 46 | + print(f"📈 Rendering transform graph for {zarr_path.relative_to(EXAMPLE_PATH)}") |
| 47 | + graph = group.transform_graph() |
| 48 | + graphviz_graph = graph.to_graphviz() |
| 49 | + from PIL import Image as PILImage |
| 50 | + import io |
| 51 | + |
| 52 | + png_bytes = graphviz_graph.pipe(format="png") |
| 53 | + |
| 54 | + PILImage.open(io.BytesIO(png_bytes)).show() # uses default image viewer |
| 55 | + |
| 56 | + pass |
| 57 | + |
| 58 | + |
| 59 | + |
0 commit comments