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