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.
AllowedExtraField #
Bases: TypedDict
The type of additional fields that may be added to Zarr V3 Array or Group metadata documents.
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. |
validate_extra_fields #
Validate that extra fields conform to the Zarr V3 spec.
Extra fields must be dicts with a 'must_understand' key set to False.
Source code in pydantic_zarr/experimental/v3.py
ArraySpec #
Bases: NodeSpec
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 |
BaseAttributes
|
JSON-serializable 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 empty.
Source code in pydantic_zarr/experimental/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/experimental/v3.py
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 326 327 328 329 330 331 332 333 | |
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/experimental/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/experimental/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/experimental/v3.py
with_attributes #
Return a copy of this model with a new attributes field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attributes
|
BaseAttributes
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
with_shape #
Return a copy of this model with a new shape field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, ...]
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
with_data_type #
Return a copy of this model with a new data_type field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_type
|
DTypeLike
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
with_chunk_grid #
Return a copy of this model with a new chunk_grid field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_grid
|
RegularChunking
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
with_chunk_key_encoding #
Return a copy of this model with a new chunk_key_encoding field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_key_encoding
|
DefaultChunkKeyEncoding
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
with_fill_value #
Return a copy of this model with a new fill_value field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fill_value
|
FillValue
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
with_codecs #
Return a copy of this model with a new codecs field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
codecs
|
CodecTuple
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
with_storage_transformers #
Return a copy of this model with a new storage_transformers field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage_transformers
|
tuple[AnyNamedConfig, ...]
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
with_dimension_names #
Return a copy of this model with a new dimension_names field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension_names
|
tuple[str | None, ...] | None
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
BaseGroupSpec #
Bases: NodeSpec
A base GroupSpec class that only has core Zarr V3 group attributes
with_attributes #
Return a copy of this model with a new attributes field.
The new model will be validated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attributes
|
BaseAttributes
|
The new |
required |
Returns:
| Type | Description |
|---|---|
ArraySpec
|
A copy of this model with a new |
Source code in pydantic_zarr/experimental/v3.py
GroupSpec #
Bases: BaseGroupSpec
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 |
BaseAttributes
|
The user-defined attributes of this group. |
members |
dict[str, ArraySpec | GroupSpec | BaseGroupSpec]
|
The members of this group. This is a dict with string keys and values that must be ArraySpec, GroupSpec, or BaseGroupSpec instances. |
with_members #
Return a copy of this model with a new members field.
The new model will be validated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
members
|
Mapping[str, ArraySpec | GroupSpec]
|
The new |
required |
Returns:
| Type | Description |
|---|---|
A copy of this model with a new `members` field.
|
|
Source code in pydantic_zarr/experimental/v3.py
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.experimental.v3 import GroupSpec, ArraySpec, BaseGroupSpec
import numpy as np
flat = {'' : BaseGroupSpec(attributes={'foo': 10})}
GroupSpec.from_flat(flat)
# GroupSpec(zarr_format=3, node_type='group', attributes={'foo': 10}, members={})
flat = {
'': BaseGroupSpec(attributes={'foo': 10}),
'/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/experimental/v3.py
to_flat #
Flatten this GroupSpec.
This method returns a dict with string keys and values that are BaseGroupSpec 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, BaseGroupSpec
>>> g1 = GroupSpec(members={}, attributes={'foo': 'bar'})
>>> to_flat(g1)
{'': BaseGroupSpec(zarr_format=3, attributes={'foo': 'bar'})}
>>> to_flat(g1, root_path='baz')
{'baz': BaseGroupSpec(zarr_format=3, attributes={'foo': 'bar'})}
>>> to_flat(GroupSpec(members={'g1': g1}, attributes={'foo': 'bar'}))
{'/g1': BaseGroupSpec(zarr_format=3, attributes={'foo': 'bar'}), '': BaseGroupSpec(zarr_format=3, attributes={'foo': 'bar'})}
Source code in pydantic_zarr/experimental/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/experimental/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/experimental/v3.py
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 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 | |
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/experimental/v3.py
parse_dtype_v3 #
Todo: refactor this when the zarr python dtypes work is released
Source code in pydantic_zarr/experimental/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/experimental/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/experimental/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/experimental/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 | BaseGroupSpec]
|
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/experimental/v3.py
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 | |
auto_codecs #
Automatically create a tuple of codecs from an arbitrary python object.
Source code in pydantic_zarr/experimental/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 to_flat, GroupSpec, BaseGroupSpec
>>> g1 = GroupSpec(members={}, attributes={'foo': 'bar'})
>>> to_flat(g1)
{'': BaseGroupSpec(zarr_format=3, attributes={'foo': 'bar'})}
>>> to_flat(g1, root_path='baz')
{'baz': BaseGroupSpec(zarr_format=3, attributes={'foo': 'bar'})}
>>> to_flat(GroupSpec(members={'g1': g1}, attributes={'foo': 'bar'}))
{'/g1': BaseGroupSpec(zarr_format=3, attributes={'foo': 'bar'}), '': BaseGroupSpec(zarr_format=3, attributes={'foo': 'bar'})}