v3
CodecLike
module-attribute
#
A type modelling the permissible declarations for codecs
NamedConfig #
Bases: TypedDict, Generic[TName, TConfig]
A Zarr V3 metadata object.
This class is parametrized by two type parameters: TName and TConfig.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
TName
|
The name of the metadata object. |
configuration |
NotRequired[TConfig]
|
The configuration of the metadata object. |
AnyNamedConfig #
Bases: NamedConfig[str, Mapping[str, object]]
This class models any Zarr metadata object that takes the form of a {"name": ..., "configuration": ...} dict, where the "configuration" key is not required.
NodeSpec #
Bases: StrictBase
The base class for V3 ArraySpec and GroupSpec.
Attributes:
| Name | Type | Description |
|---|---|---|
zarr_format |
Literal[3]
|
The Zarr version represented by this node. Must be 3. |
ArraySpec #
Bases: NodeSpec, Generic[TAttr]
A model of a Zarr Version 3 Array.
Attributes:
| Name | Type | Description |
|---|---|---|
node_type |
Literal['array']
|
The node type. Must be the string 'array'. |
attributes |
TAttr
|
User-defined metadata associated with this array. |
shape |
Sequence[int]
|
The shape of this array. |
data_type |
str
|
The data type of this array. |
chunk_grid |
NamedConfig
|
A |
chunk_key_encoding |
NamedConfig
|
A |
fill_value |
FillValue
|
The fill value for this array. |
codecs |
Sequence[NamedConfig]
|
The sequence of codices for this array. |
storage_transformers |
Optional[Sequence[NamedConfig]]
|
An optional sequence of |
dimension_names |
Optional[Sequence[str]]
|
An optional sequence of strings that gives names to each axis of the array. |
model_dump #
Override this method because the Zarr V3 spec requires that the dimension_names field be omitted from metadata entirely if it's set to None.
Source code in pydantic_zarr/v3.py
from_array
classmethod
#
from_array(
array,
*,
attributes="auto",
chunk_grid="auto",
chunk_key_encoding="auto",
fill_value="auto",
codecs="auto",
storage_transformers="auto",
dimension_names="auto"
)
Create an ArraySpec from a numpy array-like object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
NDArray[Any] | Array
|
object that conforms to the numpy array API.
The shape and dtype of this object will be used to construct an ArraySpec.
If the |
required |
Returns:
| Type | Description |
|---|---|
An instance of ArraySpec with properties derived from the provided array.
|
|
Source code in pydantic_zarr/v3.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
from_zarr
classmethod
#
Create an ArraySpec from a zarr.Array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
Array
|
|
required |
Returns:
| Type | Description |
|---|---|
An instance of ArraySpec with properties derived from the provided zarr
|
|
array.
|
|
Examples:
>>> import zarr
>>> from pydantic_zarr.v3 import ArraySpec
>>> x = zarr.create((10,10))
>>> ArraySpec.from_zarr(x)
ArraySpec(zarr_format=2, attributes={}, shape=(10, 10), chunks=(10, 10), dtype='<f8', fill_value=0.0, order='C', filters=None, dimension_separator='.', compressor={'id': 'blosc', 'cname': 'lz4', 'clevel': 5, 'shuffle': 1, 'blocksize': 0})
Source code in pydantic_zarr/v3.py
to_zarr #
Serialize an ArraySpec to a zarr array at a specific path in a zarr store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
instance of zarr.abc.store.Store
|
The storage backend that will manifest the array. |
required |
path
|
str
|
The location of the array inside the store. |
required |
overwrite
|
bool
|
Whether to overwrite an existing array or group at the path. If overwrite is False and an array or group already exists at the path, an exception will be raised. Defaults to False. |
False
|
config
|
ArrayConfigParams | None
|
An instance of |
= None
|
Returns:
| Type | Description |
|---|---|
A zarr array that is structurally identical to the ArraySpec.
|
|
This operation will create metadata documents in the store.
|
|
Source code in pydantic_zarr/v3.py
like #
Compare am ArraySpec to another ArraySpec or a zarr.Array, parameterized over the
fields to exclude or include in the comparison. Models are first converted to dict via the
model_dump method of pydantic.BaseModel, then compared with the == operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
ArraySpec | Array
|
The array (model or actual) to compare with. If other is a |
required |
include
|
IncEx
|
A specification of fields to include in the comparison. The default value is |
= None
|
exclude
|
IncEx
|
A specification of fields to exclude from the comparison. The default value is |
= None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
>>> import zarr
>>> from pydantic_zarr.v3 import ArraySpec
>>> x = zarr.create((10,10), zarr_format=3)
>>> x.attrs.put({'foo': 10})
>>> x_model = ArraySpec.from_zarr(x)
>>> print(x_model.like(x_model)) # it is like itself.
True
>>> print(x_model.like(x))
True
>>> y = zarr.create((10,10))
>>> y.attrs.put({'foo': 11}) # x and y are the same, other than their attrs
>>> print(x_model.like(y))
False
>>> print(x_model.like(y, exclude={'attributes'}))
True
Source code in pydantic_zarr/v3.py
GroupSpec #
Bases: NodeSpec, Generic[TAttr, TItem]
A model of a Zarr Version 3 Group.
Attributes:
| Name | Type | Description |
|---|---|---|
node_type |
Literal['group']
|
The type of this node. Must be the string "group". |
attributes |
TAttr
|
The user-defined attributes of this group. |
members |
dict[str, TItem] | None
|
The members of this group. |
from_flat
classmethod
#
Create a GroupSpec from a flat hierarchy representation.
The flattened hierarchy is a
dict with the following constraints: keys must be valid paths; values must
be ArraySpec or GroupSpec instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, ArraySpec | GroupSpec]
|
A flattened representation of a Zarr hierarchy. |
required |
Returns:
| Type | Description |
|---|---|
GroupSpec
|
A |
Examples:
from pydantic_zarr.v3 import GroupSpec, ArraySpec
import numpy as np
flat = {'': GroupSpec(attributes={'foo': 10}, members=None)}
GroupSpec.from_flat(flat)
# GroupSpec(zarr_format=3, node_type='group', attributes={'foo': 10}, members={})
flat = {
'': GroupSpec(attributes={'foo': 10}, members=None),
'/a': ArraySpec.from_array(np.arange(10))}
GroupSpec.from_flat(flat)
# GroupSpec(
# zarr_format=3,
# node_type='group',
# attributes={'foo': 10},
# members={
# 'a': ArraySpec(
# zarr_format=3,
# node_type='array',
# attributes={},
# shape=(10,),
# data_type='int64',
# chunk_grid={'name': 'regular', 'configuration': {'chunk_shape': (10,)}},
# chunk_key_encoding={'name': 'default', 'configuration': {'separator': '/'}},
# fill_value=0,
# codecs=(),
# storage_transformers=(),
# dimension_names=None)})
Source code in pydantic_zarr/v3.py
to_flat #
Flatten this GroupSpec.
This method returns a dict with string keys and values that are GroupSpec or
ArraySpec.
Then the resulting dict will contain a copy of the input with a null members attribute
under the key root_path, as well as copies of the result of calling node.to_flat on each
element of node.members, each under a key created by joining root_path with a '/`
character to the name of each member, and so on recursively for each sub-member.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_path
|
`str`
|
The root path. The keys in |
= ''
|
Returns:
| Type | Description |
|---|---|
Dict[str, ArraySpec | GroupSpec]
|
A flattened representation of the hierarchy. |
Examples:
>>> from pydantic_zarr.v3 import to_flat, GroupSpec
>>> g1 = GroupSpec(members=None, attributes={'foo': 'bar'})
>>> to_flat(g1)
{'': GroupSpec(zarr_format=3, attributes={'foo': 'bar'}, members=None)}
>>> to_flat(g1 root_path='baz')
{'baz': GroupSpec(zarr_format=3, attributes={'foo': 'bar'}, members=None)}
>>> to_flat(GroupSpec(members={'g1': g1}, attributes={'foo': 'bar'}))
{'/g1': GroupSpec(zarr_format=3, attributes={'foo': 'bar'}, members=None), '': GroupSpec(zarr_format=3, attributes={'foo': 'bar'}, members=None)}
Source code in pydantic_zarr/v3.py
from_zarr
classmethod
#
Create a GroupSpec from a zarr group. Subgroups and arrays contained in the zarr group will be converted to instances of GroupSpec and ArraySpec, respectively, and these spec instances will be stored in the .members attribute of the parent GroupSpec. This occurs recursively, so the entire zarr hierarchy below a given group can be represented as a GroupSpec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
Group
|
The Zarr group to model. |
required |
depth
|
int
|
An integer which may be no lower than -1. Determines how far into the tree to parse. The default value of -1 indicates that the entire hierarchy should be parsed. |
= -1
|
Returns:
| Type | Description |
|---|---|
An instance of GroupSpec that represents the structure of the zarr hierarchy.
|
|
Source code in pydantic_zarr/v3.py
to_zarr #
Serialize a GroupSpec to a zarr group at a specific path in a zarr store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
instance of zarr.abc.store.Store
|
The storage backend that will manifest the group and its contents. |
required |
path
|
str
|
The location of the group inside the store. |
required |
overwrite
|
bool
|
Whether to overwrite an existing array or group at the path. If overwrite is False and an array or group already exists at the path, an exception will be raised. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
A zarr group that is structurally identical to the GroupSpec.
|
|
This operation will create metadata documents in the store.
|
|
Source code in pydantic_zarr/v3.py
like #
Compare a GroupSpec to another GroupSpec or a zarr.Group.
This is parameterized over the fields to exclude or include in the comparison.
Models are first converted to dict via the model_dump method of pydantic.BaseModel,
then compared with the == operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
GroupSpec | Group
|
The group (model or actual) to compare with. If other is a |
required |
include
|
IncEx
|
A specification of fields to include in the comparison. The default is |
= None
|
exclude
|
IncEx
|
A specification of fields to exclude from the comparison. The default is |
= None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
>>> import zarr
>>> from pydantic_zarr.v3 import GroupSpec
>>> import numpy as np
>>> z1 = zarr.group(path='z1')
>>> z1a = z1.array(name='foo', data=np.arange(10))
>>> z1_model = GroupSpec.from_zarr(z1)
>>> print(z1_model.like(z1_model)) # it is like itself
True
>>> print(z1_model.like(z1))
True
>>> z2 = zarr.group(path='z2')
>>> z2a = z2.array(name='foo', data=np.arange(10))
>>> print(z1_model.like(z2))
True
>>> z2.attrs.put({'foo' : 100}) # now they have different attributes
>>> print(z1_model.like(z2))
False
>>> print(z1_model.like(z2, exclude={'attributes'}))
True
Source code in pydantic_zarr/v3.py
parse_dtype_v3 #
Todo: refactor this when the zarr python dtypes work is released
Source code in pydantic_zarr/v3.py
from_zarr #
Recursively parse a Zarr group or Zarr array into an ArraySpec or GroupSpec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
a zarr Array or zarr Group
|
|
required |
depth
|
int
|
An integer which may be no lower than -1. Determines how far into the tree to parse. The default value of -1 indicates that the entire hierarchy should be parsed. |
= -1
|
Returns:
| Type | Description |
|---|---|
An instance of GroupSpec or ArraySpec that represents the
|
|
structure of the zarr group or array.
|
|
Source code in pydantic_zarr/v3.py
to_zarr #
Serialize a GroupSpec or ArraySpec to a zarr group or array at a specific path in a zarr store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
GroupSpec or ArraySpec
|
The GroupSpec or ArraySpec that will be serialized to storage. |
required |
store
|
instance of zarr.abc.store.Store
|
The storage backend that will manifest the group or array. |
required |
path
|
str
|
The location of the group or array inside the store. |
required |
overwrite
|
bool
|
Whether to overwrite an existing array or group at the path. If overwrite is False and an array or group already exists at the path, an exception will be raised. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
A zarr Group or Array that is structurally equivalent to the spec object.
|
|
This operation will create metadata documents in the store.
|
|
Source code in pydantic_zarr/v3.py
from_flat #
Wraps from_flat_group, handling the special case where a Zarr array is defined at the root of
a hierarchy and thus is not contained by a Zarr group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, ArraySpec | GroupSpec]
|
A flat representation of a Zarr hierarchy. This is a |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec | GroupSpec
|
The |
Examples:
>>> from pydantic_zarr.v3 import from_flat, GroupSpec, ArraySpec
>>> import numpy as np
>>> tree = {'': ArraySpec.from_array(np.arange(10))}
>>> from_flat(tree) # special case of a Zarr array at the root of the hierarchy
ArraySpec(zarr_format=2, attributes={}, shape=(10,), chunks=(10,), dtype='<i8', fill_value=0, order='C', filters=None, dimension_separator='/', compressor=None)
>>> tree = {'/foo': ArraySpec.from_array(np.arange(10))}
>>> from_flat(tree) # note that an implicit Group is created
GroupSpec(zarr_format=2, attributes={}, members={'foo': ArraySpec(zarr_format=2, attributes={}, shape=(10,), chunks=(10,), dtype='<i8', fill_value=0, order='C', filters=None, dimension_separator='/', compressor=None)})
Source code in pydantic_zarr/v3.py
from_flat_group #
Generate a GroupSpec from a flat representation of a hierarchy, i.e. a dict with
string keys (paths) and ArraySpec / GroupSpec values (nodes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, ArraySpec | GroupSpec]
|
A flat representation of a Zarr hierarchy rooted at a Zarr group. |
required |
Returns:
| Type | Description |
|---|---|
GroupSpec
|
A |
Examples:
>>> from pydantic_zarr.v3 import from_flat_group, GroupSpec, ArraySpec
>>> import numpy as np
>>> tree = {'/foo': ArraySpec.from_array(np.arange(10))}
>>> from_flat_group(tree) # note that an implicit Group is created
GroupSpec(zarr_format=2, attributes={}, members={'foo': ArraySpec(zarr_format=2, attributes={}, shape=(10,), chunks=(10,), dtype='<i8', fill_value=0, order='C', filters=None, dimension_separator='/', compressor=None)})
Source code in pydantic_zarr/v3.py
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 | |
auto_codecs #
Automatically create a tuple of codecs from an arbitrary python object.
Source code in pydantic_zarr/v3.py
to_flat #
Flatten a GroupSpec or ArraySpec.
Converts a GroupSpec or ArraySpec and a string, into a dict with string keys and
values that are GroupSpec or ArraySpec.
If the input is an ArraySpec, then this function just returns the dict {root_path: node}.
If the input is a GroupSpec, then the resulting dict will contain a copy of the input
with a null members attribute under the key root_path, as well as copies of the result of
calling flatten_node on each element of node.members, each under a key created by joining
root_path with a '/` character to the name of each member, and so on recursively for each
sub-member.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
`GroupSpec` | `ArraySpec`
|
The node to flatten. |
required |
root_path
|
`str`
|
The root path. If |
= ''
|
Returns:
| Type | Description |
|---|---|
Dict[str, ArraySpec | GroupSpec]
|
A flattened representation of the hierarchy. |
Examples:
>>> from pydantic_zarr.v3 import flatten, GroupSpec
>>> g1 = GroupSpec(members=None, attributes={'foo': 'bar'})
>>> to_flat(g1)
{'': GroupSpec(zarr_format=3, attributes={'foo': 'bar'}, members=None)}
>>> to_flat(g1 root_path='baz')
{'baz': GroupSpec(zarr_format=3, attributes={'foo': 'bar'}, members=None)}
>>> to_flat(GroupSpec(members={'g1': g1}, attributes={'foo': 'bar'}))
{'/g1': GroupSpec(zarr_format=3, attributes={'foo': 'bar'}, members=None), '': GroupSpec(zarr_format=2, attributes={'foo': 'bar'}, members=None)}