|
| 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 pytest |
| 5 | +import zarr |
| 6 | +from ome_zarr_models._v06.collection import Collection |
| 7 | +from ome_zarr_models._v06.coordinate_transforms import Sequence |
| 8 | +from ome_zarr_models._v06.image import Image |
| 9 | + |
| 10 | +from ngff_transformations.graph import ( |
| 11 | + create_sequence_transformation_from_path, |
| 12 | + get_relative_path, |
| 13 | + transform_graph_to_networkx, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +EXAMPLE_PATH = ( |
| 18 | + Path(__file__).parent.parent |
| 19 | + / "data" |
| 20 | + / "ngff-rfc5-coordinate-transformation-examples" |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def get_test_zarr_paths( |
| 25 | + data_dir: Path = EXAMPLE_PATH |
| 26 | +) -> list[Path]: |
| 27 | + """ |
| 28 | + Get all valid test Zarr paths recursively, excluding paths matching the pattern. |
| 29 | + """ |
| 30 | + |
| 31 | + exclude_patterns = [ |
| 32 | + "byDimension", |
| 33 | + "scaleParams", |
| 34 | + "translationParams", |
| 35 | + "affineParams", |
| 36 | + "rotationParams", |
| 37 | + ] |
| 38 | + |
| 39 | + zarrs: list[Path] = [] |
| 40 | + for item in data_dir.glob("*"): |
| 41 | + if item.is_dir(): |
| 42 | + if item.suffix == ".zarr": |
| 43 | + # Found a Zarr group - add if it doesn't match exclusion pattern |
| 44 | + if not any(pattern in str(item) for pattern in exclude_patterns): |
| 45 | + zarrs.append(item) |
| 46 | + else: |
| 47 | + # Recurse into subdirectories |
| 48 | + zarrs.extend(get_test_zarr_paths(item)) |
| 49 | + return sorted(zarrs) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("zarr_path", get_test_zarr_paths()) |
| 53 | +def test_graph(zarr_path: Path): |
| 54 | + """ |
| 55 | + Test transformation graph creation and functionality |
| 56 | + """ |
| 57 | + relative_path = zarr_path.relative_to(EXAMPLE_PATH) |
| 58 | + |
| 59 | + # Load appropriate group type based on location |
| 60 | + group: Collection | Image |
| 61 | + if relative_path.parts[0] == "user_stories": |
| 62 | + group = Collection.from_zarr(zarr.open_group(zarr_path, mode="r")) |
| 63 | + else: |
| 64 | + group = Image.from_zarr(zarr.open_group(zarr_path, mode="r")) |
| 65 | + |
| 66 | + # Create and validate graph |
| 67 | + graph = group.transform_graph() |
| 68 | + nx_graph = transform_graph_to_networkx(graph) |
| 69 | + |
| 70 | + assert len(nx_graph.nodes) > 0 |
| 71 | + |
| 72 | + # Test path finding and sequence creation |
| 73 | + # For now perform path finding between an example edge's nodes |
| 74 | + |
| 75 | + example_edge = list(nx_graph.edges)[0] |
| 76 | + path = get_relative_path(nx_graph, example_edge[0], example_edge[1]) |
| 77 | + sequence_transformation = create_sequence_transformation_from_path(nx_graph, path) |
| 78 | + |
| 79 | + assert isinstance(sequence_transformation, Sequence) |
0 commit comments