|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +import zarr |
| 5 | + |
| 6 | + |
| 7 | +def find_argolid_root(out_dir: Path) -> Path: |
| 8 | + """ |
| 9 | + Find the Argolid output root directory under out_dir. |
| 10 | + The root is identified by: |
| 11 | + - directory ending with .zarr or .ome.zarr |
| 12 | + - containing METADATA.ome.xml |
| 13 | + - containing data.zarr/ |
| 14 | + """ |
| 15 | + out_dir = out_dir.resolve() |
| 16 | + |
| 17 | + if not out_dir.exists(): |
| 18 | + raise FileNotFoundError(f"Output directory does not exist: {out_dir}") |
| 19 | + |
| 20 | + # If caller passed the root itself |
| 21 | + if ( |
| 22 | + out_dir.is_dir() |
| 23 | + and out_dir.suffix == ".zarr" |
| 24 | + and (out_dir / "METADATA.ome.xml").exists() |
| 25 | + and (out_dir / "data.zarr").is_dir() |
| 26 | + ): |
| 27 | + return out_dir |
| 28 | + |
| 29 | + # Otherwise search one level down |
| 30 | + candidates = [] |
| 31 | + for p in out_dir.iterdir(): |
| 32 | + if not p.is_dir(): |
| 33 | + continue |
| 34 | + if p.suffix != ".zarr": |
| 35 | + continue |
| 36 | + if not (p / "METADATA.ome.xml").exists(): |
| 37 | + continue |
| 38 | + if not (p / "data.zarr").is_dir(): |
| 39 | + continue |
| 40 | + candidates.append(p) |
| 41 | + |
| 42 | + if len(candidates) == 1: |
| 43 | + return candidates[0] |
| 44 | + |
| 45 | + if len(candidates) > 1: |
| 46 | + # deterministic choice, newest wins |
| 47 | + return max(candidates, key=lambda p: p.stat().st_mtime) |
| 48 | + |
| 49 | + raise FileNotFoundError( |
| 50 | + f"No Argolid zarr output found under {out_dir}. " |
| 51 | + f"Expected a *.zarr directory containing METADATA.ome.xml and data.zarr/" |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def open_argolid_data_group(argolid_root: Path) -> zarr.Group: |
| 56 | + """ |
| 57 | + Open the data.zarr group inside Argolid output. |
| 58 | + """ |
| 59 | + data_path = argolid_root / "data.zarr" |
| 60 | + if not data_path.exists(): |
| 61 | + raise FileNotFoundError(f"Missing data.zarr at {data_path}") |
| 62 | + return zarr.open_group(str(data_path), mode="r") |
| 63 | + |
| 64 | + |
| 65 | +def assert_is_argolid_omexml_zarr_pyramid(out_dir: Path, expect_levels: int | None = None) -> list[tuple[int, ...]]: |
| 66 | + """ |
| 67 | + Validate Argolid-style output: |
| 68 | + <name>.ome.zarr/METADATA.ome.xml |
| 69 | + <name>.ome.zarr/data.zarr/0/<level>/... |
| 70 | +
|
| 71 | + Returns: |
| 72 | + Shapes for each pyramid level found under data.zarr/0/ |
| 73 | + """ |
| 74 | + root = find_argolid_root(out_dir) |
| 75 | + |
| 76 | + # METADATA.ome.xml exists and non-empty |
| 77 | + ome_xml = root / "METADATA.ome.xml" |
| 78 | + assert ome_xml.exists(), f"Missing {ome_xml}" |
| 79 | + assert ome_xml.stat().st_size > 0, f"Empty metadata file: {ome_xml}" |
| 80 | + |
| 81 | + data = open_argolid_data_group(root) |
| 82 | + |
| 83 | + # Argolid stores pyramids under "0/<level>" |
| 84 | + assert "0" in data, f"Expected '0' in data.zarr. keys={list(data.keys())}" |
| 85 | + series0 = data["0"] |
| 86 | + assert isinstance(series0, zarr.Group) |
| 87 | + |
| 88 | + # levels can be arrays or groups, depending on how the writer stored them |
| 89 | + level_names = sorted( |
| 90 | + [k for k in series0.keys() if str(k).isdigit()], |
| 91 | + key=lambda s: int(s), |
| 92 | + ) |
| 93 | + |
| 94 | + assert level_names, ( |
| 95 | + f"No pyramid levels found under data.zarr/0. " |
| 96 | + f"keys={list(series0.keys())}, groups={list(series0.group_keys())}, arrays={list(series0.array_keys())}" |
| 97 | + ) |
| 98 | + |
| 99 | + level_strs = {str(k) for k in level_names} |
| 100 | + assert "0" in level_strs, f"Missing level 0. Levels found: {level_names}" |
| 101 | + |
| 102 | + |
| 103 | + if expect_levels is not None: |
| 104 | + assert len(level_names) >= expect_levels, f"Expected at least {expect_levels} level(s), found {len(level_names)}: {level_names}" |
| 105 | + |
| 106 | + shapes: list[tuple[int, ...]] = [] |
| 107 | + for lvl in level_names: |
| 108 | + node = series0[str(lvl)] |
| 109 | + |
| 110 | + # Level can be a direct array: data.zarr/0/<lvl> |
| 111 | + if isinstance(node, zarr.Array): |
| 112 | + shapes.append(node.shape) |
| 113 | + continue |
| 114 | + |
| 115 | + # Or a group containing one or more arrays: data.zarr/0/<lvl>/<array> |
| 116 | + if isinstance(node, zarr.Group): |
| 117 | + array_keys = list(node.array_keys()) |
| 118 | + assert array_keys, f"No arrays found in level group {lvl}. keys={list(node.keys())}" |
| 119 | + arr = node[array_keys[0]] |
| 120 | + shapes.append(arr.shape) |
| 121 | + continue |
| 122 | + |
| 123 | + raise AssertionError(f"Unexpected type at level {lvl}: {type(node)}") |
| 124 | + |
| 125 | + # after shapes computed: |
| 126 | + assert shapes, "No shapes collected from pyramid levels" |
| 127 | + y0, x0 = shapes[0][-2], shapes[0][-1] |
| 128 | + assert y0 > 0 and x0 > 0, "Each array must have non-zero shape" |
| 129 | + |
| 130 | + # pyramid monotonicity: dims should not increase |
| 131 | + for prev, nxt in zip(shapes, shapes[1:]): |
| 132 | + assert len(prev) == len(nxt), f"Rank changed: {prev} -> {nxt}" |
| 133 | + assert all(n <= p for p, n in zip(prev, nxt)), f"Not a pyramid: {prev} -> {nxt}" |
| 134 | + |
| 135 | + return shapes |
0 commit comments