You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`zarr-python` supports creating Zarr arrays or groups deep in the
197
+
hierarchy without explicitly creating the intermediate groups first.
198
+
`from_flat` models this behavior. For example, `{'/a/b/c': ArraySpec(...)}` implicitly defines the existence of a groups named `a` and `b` (which is contained in `a`). `from_flat` will create the expected `GroupSpec` object from such `dict` instances.
199
+
200
+
```python
201
+
from pydantic_zarr.v2 import GroupSpec, ArraySpec
202
+
tree = {'/a/b/c': ArraySpec(shape=(1,), dtype='uint8', chunks=(1,))}
203
+
print(GroupSpec.from_flat(tree).model_dump())
204
+
"""
205
+
{
206
+
'zarr_version': 2,
207
+
'attributes': {},
208
+
'members': {
209
+
'a': {
210
+
'zarr_version': 2,
211
+
'attributes': {},
212
+
'members': {
213
+
'b': {
214
+
'zarr_version': 2,
215
+
'attributes': {},
216
+
'members': {
217
+
'c': {
218
+
'zarr_version': 2,
219
+
'attributes': {},
220
+
'shape': (1,),
221
+
'chunks': (1,),
222
+
'dtype': '|u1',
223
+
'fill_value': 0,
224
+
'order': 'C',
225
+
'filters': None,
226
+
'dimension_separator': '/',
227
+
'compressor': None,
228
+
}
229
+
},
230
+
}
231
+
},
232
+
}
233
+
},
234
+
}
235
+
"""
236
+
```
237
+
238
+
## Comparing `GroupSpec` and `ArraySpec` models
239
+
240
+
`GroupSpec` and `ArraySpec` both have `like` methods that take another `GroupSpec` or `ArraySpec` as an argument and return `True` (the models are like each other) or `False` (the models are not like each other).
241
+
242
+
The `like` method works by converting both input models to `dict` via `pydantic.BaseModel.model_dump`, and comparing the `dict` representation of the models. This means that instances of two different subclasses of `GroupSpec`, which would not be considered equal according to the `==` operator, will be considered `like` if and only if they serialize to identical `dict` instances.
243
+
244
+
The `like` method also takes keyword arguments `include` and `exclude`, which results in attributes being explicitly included or excluded from the model comparison. So it's possible to use `like` to check if two `ArraySpec` instances have the same `shape` and `dtype` by calling `array_a.like(array_b, include={'shape', 'dtype'})`. This is useful if you don't care about the compressor or filters and just want to ensure that you can safely write an in-memory array to a Zarr array.
print(g_a.like(g_b)) # False, because of mismatched attributes
279
+
#> False
280
+
281
+
print(g_a.like(g_b, exclude={'attributes'})) # True, because we ignore attributes
282
+
#> True
283
+
284
+
print(g_a.like(g_a.to_zarr(store, path='g_a'))) # g_a is like its zarr.Group counterpart
285
+
#> True
286
+
```
103
287
104
288
## Using generic types
105
289
@@ -132,7 +316,7 @@ except ValidationError as exc:
132
316
1 validation error for GroupSpec[GroupAttrs, ~TItem]
133
317
attributes.b
134
318
Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='foo', input_type=str]
135
-
For further information visit https://errors.pydantic.dev/2.4/v/int_parsing
319
+
For further information visit https://errors.pydantic.dev/2.6/v/int_parsing
136
320
"""
137
321
138
322
# this passes validation
@@ -151,7 +335,7 @@ except ValidationError as exc:
151
335
1 validation error for GroupSpec[~TAttr, ArraySpec]
152
336
members.foo
153
337
Input should be a valid dictionary or instance of ArraySpec [type=model_type, input_value=GroupSpec(zarr_version=2,...tributes={}, members={}), input_type=GroupSpec]
154
-
For further information visit https://errors.pydantic.dev/2.4/v/model_type
338
+
For further information visit https://errors.pydantic.dev/2.6/v/model_type
0 commit comments