|
2 | 2 | Private utilities. |
3 | 3 | """ |
4 | 4 |
|
| 5 | +from __future__ import annotations |
| 6 | + |
5 | 7 | from collections import Counter |
6 | | -from collections.abc import Hashable, Iterable |
7 | 8 | from dataclasses import MISSING, fields, is_dataclass |
8 | | -from typing import TypeVar |
| 9 | +from typing import TYPE_CHECKING, Any, TypeVar |
9 | 10 |
|
10 | 11 | import pydantic |
| 12 | +import pydantic_zarr.v2 |
| 13 | +import pydantic_zarr.v3 |
11 | 14 | from pydantic import create_model |
12 | | -from zarr.abc.store import Store |
13 | 15 |
|
14 | | -T = TypeVar("T") |
| 16 | +from ome_zarr_models.base import BaseAttrsv2, BaseAttrsv3 |
| 17 | +from ome_zarr_models.common.validation import ( |
| 18 | + check_array_path, |
| 19 | + check_group_path, |
| 20 | +) |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from collections.abc import Hashable, Iterable |
| 24 | + |
| 25 | + import zarr |
| 26 | + from zarr.abc.store import Store |
| 27 | + |
| 28 | + from ome_zarr_models._v06.base import BaseGroupv06 |
| 29 | + from ome_zarr_models.v04.base import BaseGroupv04 |
| 30 | + from ome_zarr_models.v05.base import BaseGroupv05 |
| 31 | + |
| 32 | +TBaseGroupv2 = TypeVar("TBaseGroupv2", bound="BaseGroupv04[Any]") |
| 33 | +TAttrsv2 = TypeVar("TAttrsv2", bound=BaseAttrsv2) |
| 34 | + |
| 35 | + |
| 36 | +def _from_zarr_v2( |
| 37 | + group: zarr.Group, |
| 38 | + group_cls: type[TBaseGroupv2], |
| 39 | + attrs_cls: type[TAttrsv2], |
| 40 | +) -> TBaseGroupv2: |
| 41 | + """ |
| 42 | + Create a GroupSpec from a potentially unlistable Zarr group. |
| 43 | +
|
| 44 | + This uses methods on the attribute class to get required and optional |
| 45 | + paths to ararys and groups, and then manually constructs the GroupSpec |
| 46 | + from those paths. |
| 47 | +
|
| 48 | + Parameters |
| 49 | + ---------- |
| 50 | + group : |
| 51 | + Zarr group to create GroupSpec from. |
| 52 | + group_cls : |
| 53 | + Class of the Group to return. |
| 54 | + attrs_cls : |
| 55 | + Attributes class. |
| 56 | + """ |
| 57 | + # on unlistable storage backends, the members of this group will be {} |
| 58 | + group_spec_in: pydantic_zarr.v2.AnyGroupSpec |
| 59 | + group_spec_in = pydantic_zarr.v2.GroupSpec.from_zarr(group, depth=0) |
| 60 | + attributes = attrs_cls.model_validate(group_spec_in.attributes) |
| 61 | + |
| 62 | + members_tree_flat: dict[ |
| 63 | + str, pydantic_zarr.v2.AnyGroupSpec | pydantic_zarr.v2.AnyArraySpec |
| 64 | + ] = {} |
| 65 | + |
| 66 | + # Required array paths |
| 67 | + for array_path in attrs_cls.get_array_paths(attributes): |
| 68 | + array_spec = check_array_path(group, array_path, expected_zarr_version=2) |
| 69 | + members_tree_flat["/" + array_path] = array_spec |
| 70 | + |
| 71 | + # Optional array paths |
| 72 | + for array_path in attrs_cls.get_optional_array_paths(attributes): |
| 73 | + try: |
| 74 | + array_spec = check_array_path(group, array_path, expected_zarr_version=2) |
| 75 | + except ValueError: |
| 76 | + continue |
| 77 | + members_tree_flat["/" + array_path] = array_spec |
| 78 | + |
| 79 | + # Required group paths |
| 80 | + required_groups = attrs_cls.get_group_paths(attributes) |
| 81 | + for group_path in required_groups: |
| 82 | + check_group_path(group, group_path, expected_zarr_version=2) |
| 83 | + group_flat = required_groups[group_path].from_zarr(group[group_path]).to_flat() # type: ignore[arg-type] |
| 84 | + for path in group_flat: |
| 85 | + members_tree_flat["/" + group_path + path] = group_flat[path] |
| 86 | + |
| 87 | + # Optional group paths |
| 88 | + optional_groups = attrs_cls.get_optional_group_paths(attributes) |
| 89 | + for group_path in optional_groups: |
| 90 | + try: |
| 91 | + check_group_path(group, group_path, expected_zarr_version=2) |
| 92 | + except FileNotFoundError: |
| 93 | + continue |
| 94 | + group_flat = optional_groups[group_path].from_zarr(group[group_path]).to_flat() # type: ignore[arg-type] |
| 95 | + for path in group_flat: |
| 96 | + members_tree_flat["/" + group_path + path] = group_flat[path] |
| 97 | + |
| 98 | + members_normalized: pydantic_zarr.v2.AnyGroupSpec = ( |
| 99 | + pydantic_zarr.v2.GroupSpec.from_flat(members_tree_flat) |
| 100 | + ) |
| 101 | + return group_cls(members=members_normalized.members, attributes=attributes) |
| 102 | + |
| 103 | + |
| 104 | +TBaseGroupv3 = TypeVar("TBaseGroupv3", bound="BaseGroupv05[Any] | BaseGroupv06[Any]") |
| 105 | +TAttrsv3 = TypeVar("TAttrsv3", bound=BaseAttrsv3) |
| 106 | + |
| 107 | + |
| 108 | +def _from_zarr_v3( |
| 109 | + group: zarr.Group, |
| 110 | + group_cls: type[TBaseGroupv3], |
| 111 | + attrs_cls: type[TAttrsv3], |
| 112 | +) -> TBaseGroupv3: |
| 113 | + """ |
| 114 | + Create a GroupSpec from a potentially unlistable Zarr group. |
| 115 | +
|
| 116 | + This uses methods on the attribute class to get required and optional |
| 117 | + paths to ararys and groups, and then manually constructs the GroupSpec |
| 118 | + from those paths. |
| 119 | +
|
| 120 | + Parameters |
| 121 | + ---------- |
| 122 | + group : |
| 123 | + Zarr group to create GroupSpec from. |
| 124 | + group_cls : |
| 125 | + Class of the Group to return. |
| 126 | + attrs_cls : |
| 127 | + Attributes class. |
| 128 | + """ |
| 129 | + # on unlistable storage backends, the members of this group will be {} |
| 130 | + group_spec_in: pydantic_zarr.v3.AnyGroupSpec |
| 131 | + group_spec_in = pydantic_zarr.v3.GroupSpec.from_zarr(group, depth=0) |
| 132 | + ome_attributes = attrs_cls.model_validate(group.attrs.asdict()["ome"]) |
| 133 | + |
| 134 | + members_tree_flat: dict[ |
| 135 | + str, pydantic_zarr.v3.AnyGroupSpec | pydantic_zarr.v3.AnyArraySpec |
| 136 | + ] = {} |
| 137 | + |
| 138 | + # Required array paths |
| 139 | + for array_path in attrs_cls.get_array_paths(ome_attributes): |
| 140 | + array_spec = check_array_path(group, array_path, expected_zarr_version=3) |
| 141 | + members_tree_flat["/" + array_path] = array_spec |
| 142 | + |
| 143 | + # Optional array paths |
| 144 | + for array_path in attrs_cls.get_optional_array_paths(ome_attributes): |
| 145 | + try: |
| 146 | + array_spec = check_array_path(group, array_path, expected_zarr_version=3) |
| 147 | + except ValueError: |
| 148 | + continue |
| 149 | + members_tree_flat["/" + array_path] = array_spec |
| 150 | + |
| 151 | + # Required group paths |
| 152 | + required_groups = attrs_cls.get_group_paths(ome_attributes) |
| 153 | + for group_path in required_groups: |
| 154 | + check_group_path(group, group_path, expected_zarr_version=3) |
| 155 | + group_flat = required_groups[group_path].from_zarr(group[group_path]).to_flat() # type: ignore[arg-type] |
| 156 | + for path in group_flat: |
| 157 | + members_tree_flat["/" + group_path + path] = group_flat[path] |
| 158 | + |
| 159 | + # Optional group paths |
| 160 | + optional_groups = attrs_cls.get_optional_group_paths(ome_attributes) |
| 161 | + for group_path in optional_groups: |
| 162 | + try: |
| 163 | + check_group_path(group, group_path, expected_zarr_version=3) |
| 164 | + except FileNotFoundError: |
| 165 | + continue |
| 166 | + group_flat = optional_groups[group_path].from_zarr(group[group_path]).to_flat() # type: ignore[arg-type] |
| 167 | + for path in group_flat: |
| 168 | + members_tree_flat["/" + group_path + path] = group_flat[path] |
| 169 | + |
| 170 | + members_normalized: pydantic_zarr.v3.AnyGroupSpec = ( |
| 171 | + pydantic_zarr.v3.GroupSpec.from_flat(members_tree_flat) |
| 172 | + ) |
| 173 | + return group_cls( # type: ignore[return-value] |
| 174 | + members=members_normalized.members, attributes=group_spec_in.attributes |
| 175 | + ) |
15 | 176 |
|
16 | 177 |
|
17 | 178 | def get_store_path(store: Store) -> str: |
|
0 commit comments