forked from zarr-developers/zarr-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3.py
296 lines (210 loc) · 7.54 KB
/
v3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
"""
Models for objects described in zarr version 3
"""
from dataclasses import dataclass
import json
from typing import (
Any,
Dict,
Literal,
Optional,
Protocol,
Tuple,
TypedDict,
Union,
runtime_checkable,
)
import numpy as np
import attr
from zarr.v3.types import Attributes
class NamedConfigDict(TypedDict):
name: str
configuration: Attributes
# not clear how useful these protocols are, but lets try it
@runtime_checkable
class NamedConfig(Protocol):
name: str
configuration: Any
@runtime_checkable
class CodecMetadata(Protocol):
name: str
class RegularChunkGridConfigDict(TypedDict):
chunk_shape: tuple[int, ...]
class RegularChunkGridConfig:
chunk_shape: Tuple[int, ...]
def __init__(self, chunk_shape) -> None:
self.chunk_shape = chunk_shape
def to_dict(self) -> RegularChunkGridConfigDict:
return {"chunk_shape": self.chunk_shape}
class RegularChunkGridDict(TypedDict):
configuration: RegularChunkGridConfigDict
name: str
class RegularChunkGrid(NamedConfig):
configuration: RegularChunkGridConfig
name: Literal["regular"] = "regular"
def __init__(self, configuration: RegularChunkGridConfig) -> None:
self.configuration = configuration
self.name = "regular"
def to_dict(self) -> RegularChunkGridDict:
return {"configuration": self.configuration.to_dict(), "name": self.name}
class DefaultChunkKeyConfigDict(TypedDict):
separator: Literal[".", "/"]
class DefaultChunkKeyConfig:
separator: Literal[".", "/"]
def __init__(self, *, separator: Literal[".", "/"] = "/") -> None:
self.separator = parse_dimension_separator
def to_dict(self) -> DefaultChunkKeyConfigDict:
return {"separator": self.separator}
def parse_dimension_separator(separator: Any) -> Literal[".", "/"]:
if separator not in (".", "/"):
raise ValueError
return separator
class DefaultChunkKeyEncodingDict(TypedDict):
configuration: DefaultChunkKeyConfigDict
name: Literal["default", "v2"]
class DefaultChunkKeyEncoding(NamedConfig):
configuration: DefaultChunkKeyConfig
name: Literal["default", "V2"]
def __init__(self, *, configuration=DefaultChunkKeyConfig(), name="default") -> None:
self.configuration = configuration
self.name = name
def to_dict(self) -> DefaultChunkKeyEncodingDict:
return {"configuration": self.configuration.to_dict(), "name": self.name}
class V2ChunkKeyEncodingDict(TypedDict):
configuration: DefaultChunkKeyConfigDict
name: Literal["V2"]
class V2ChunkKeyEncoding(NamedConfig):
configuration: DefaultChunkKeyConfig = DefaultChunkKeyConfig()
name: Literal["V2"] = "V2"
def __init__(self, configuration: DefaultChunkKeyConfig) -> None:
self.configuration = configuration
self.name = "V2"
def to_dict(self) -> V2ChunkKeyEncodingDict:
return {"configuration": self.configuration.to_dict(), "name": self.name}
ChunkKeyEncoding = Union[DefaultChunkKeyEncoding, V2ChunkKeyEncoding]
class _ArrayMetadataDictBase(TypedDict):
"""
This is a private base class with all the required attributes.
Because `dimension_names` is an optional attribute, we need a subclass to express this.
See https://peps.python.org/pep-0655/ for a cleaner way
"""
shape: Tuple[int, ...]
data_type: str
chunk_grid: RegularChunkGridDict
chunk_key_encoding: Union[DefaultChunkKeyConfigDict, V2ChunkKeyEncodingDict]
fill_value: Any
codecs: list[NamedConfigDict]
zarr_format: Literal["3"]
node_type: Literal["array"]
class ArrayMetadataDict(_ArrayMetadataDictBase, total=False):
"""
This inherits from a private base class with all the required attributes.
Because `dimension_names` is an optional attribute, we need a subclass to express this.
See https://peps.python.org/pep-0655/ for a cleaner way
"""
dimension_names: list[str]
class ArrayMetadata:
"""
A representation of v3 array metadata with no behavior besides
input validation and to / from JSON serialization
"""
shape: Tuple[int, ...]
data_type: np.dtype
chunk_grid: RegularChunkGrid
chunk_key_encoding: Union[DefaultChunkKeyEncoding, V2ChunkKeyEncoding]
fill_value: Any
codecs: list[CodecMetadata]
dimension_names: Optional[Tuple[str, ...]]
zarr_format: Literal[3] = 3
node_type: Literal["array"] = "array"
def __init__(
self,
*,
shape,
data_type,
chunk_grid,
chunk_key_encoding,
fill_value,
codecs,
dimension_names: Optional[Tuple[str]] = None,
):
"""
The only thing we need to do here is validate inputs.
"""
self.shape = parse_shape(shape)
self.data_type = parse_data_type(data_type)
self.chunk_grid = parse_chunk_grid(chunk_grid)
self.chunk_key_encoding = parse_chunk_key_encoding(chunk_key_encoding)
self.fill_value = parse_fill_value(fill_value)
self.codecs = parse_codecs(codecs)
self.dimension_names = parse_dimension_names(dimension_names)
self = parse_array_metadata(self)
def to_dict(self) -> ArrayMetadataDict:
self_dict: ArrayMetadataDict = {
"shape": self.shape,
"data_type": self.data_type.str,
"chunk_grid": self.chunk_grid.to_dict(),
"fill_value": self.fill_value,
"chunk_key_encoding": self.chunk_grid.to_dict(),
"codecs": [codec.to_dict() for codec in self.codecs],
"node_type": "array",
"zarr_format": 3,
}
if self.dimension_names is not None:
# dimension names cannot by Null in JSON
self_dict["dimension_names"] = self.dimension_names
def to_json(self) -> bytes:
return json.dumps(self.to_dict()).encode()
@classmethod
def from_json(cls, json: bytes) -> "ArrayMetadata":
...
class GroupMetadata:
@classmethod
def from_json(cls, json: bytes) -> "GroupMetadata":
...
def to_json(self) -> bytes:
...
def from_json(blob: bytes) -> Union[ArrayMetadata, GroupMetadata]:
"""The class methods can very lightly wrap this function"""
...
def to_json(obj: Union[ArrayMetadata, GroupMetadata]) -> bytes:
"""The class methods can very lightly wrap this function"""
...
def parse_shape(shape: Any) -> Tuple[int, ...]:
return shape
def parse_data_type(data_type: Any) -> Any:
return data_type
def parse_chunk_grid(chunk_grid: Any) -> RegularChunkGrid:
return chunk_grid
def parse_chunk_key_encoding(chunk_key_encoding: Any) -> DefaultChunkKeyEncoding:
return chunk_key_encoding
def parse_fill_value(fill_value: Any) -> Any:
return fill_value
def parse_codecs(codecs: Any) -> list[CodecMetadata]:
return codecs
def parse_dimension_names(dimension_names: Optional[tuple[str, ...]]):
return dimension_names
def parse_array_metadata(metadata: ArrayMetadata):
"""
Check that all properties are consistent
"""
# todo: check that dimensional attributes like shape and dimension_names are consistent
return metadata
dtype_to_data_type = {
"|b1": "bool",
"bool": "bool",
"|i1": "int8",
"<i2": "int16",
"<i4": "int32",
"<i8": "int64",
"|u1": "uint8",
"<u2": "uint16",
"<u4": "uint32",
"<u8": "uint64",
"<f4": "float32",
"<f8": "float64",
}
def byte_count(dtype: np.dtype) -> int:
return dtype.itemsize
def to_numpy_shortname(dtype: np.dtype) -> str:
return dtype.str.lstrip("|").lstrip("^").lstrip("<").lstrip(">")