forked from zarr-developers/zarr-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk_grids.py
More file actions
253 lines (203 loc) · 8.05 KB
/
chunk_grids.py
File metadata and controls
253 lines (203 loc) · 8.05 KB
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
from __future__ import annotations
import itertools
import math
import numbers
import operator
import warnings
from abc import abstractmethod
from dataclasses import dataclass
from functools import reduce
from typing import TYPE_CHECKING, Any, Literal
import numpy as np
from zarr.abc.metadata import Metadata
from zarr.core.common import (
JSON,
ChunkCoords,
ChunkCoordsLike,
ShapeLike,
ceildiv,
parse_named_configuration,
parse_shapelike,
)
if TYPE_CHECKING:
from collections.abc import Iterator
from typing import Self
from zarr.core.array import ShardsLike
def _guess_chunks(
shape: ShapeLike,
typesize: int,
*,
increment_bytes: int = 256 * 1024,
min_bytes: int = 128 * 1024,
max_bytes: int = 64 * 1024 * 1024,
) -> ChunkCoords:
"""
Iteratively guess an appropriate chunk layout for an array, given its shape and
the size of each element in bytes, and size constraints expressed in bytes. This logic is
adapted from h5py.
Parameters
----------
shape : ChunkCoords
The chunk shape.
typesize : int
The size, in bytes, of each element of the chunk.
increment_bytes : int = 256 * 1024
The number of bytes used to increment or decrement the target chunk size in bytes.
min_bytes : int = 128 * 1024
The soft lower bound on the final chunk size in bytes.
max_bytes : int = 64 * 1024 * 1024
The hard upper bound on the final chunk size in bytes.
Returns
-------
ChunkCoords
"""
if isinstance(shape, int):
shape = (shape,)
if typesize == 0:
return shape
ndims = len(shape)
# require chunks to have non-zero length for all dimensions
chunks = np.maximum(np.array(shape, dtype="=f8"), 1)
# Determine the optimal chunk size in bytes using a PyTables expression.
# This is kept as a float.
dset_size = np.prod(chunks) * typesize
target_size = increment_bytes * (2 ** np.log10(dset_size / (1024.0 * 1024)))
if target_size > max_bytes:
target_size = max_bytes
elif target_size < min_bytes:
target_size = min_bytes
idx = 0
while True:
# Repeatedly loop over the axes, dividing them by 2. Stop when:
# 1a. We're smaller than the target chunk size, OR
# 1b. We're within 50% of the target chunk size, AND
# 2. The chunk is smaller than the maximum chunk size
chunk_bytes = np.prod(chunks) * typesize
if (
chunk_bytes < target_size or abs(chunk_bytes - target_size) / target_size < 0.5
) and chunk_bytes < max_bytes:
break
if np.prod(chunks) == 1:
break # Element size larger than max_bytes
chunks[idx % ndims] = math.ceil(chunks[idx % ndims] / 2.0)
idx += 1
return tuple(int(x) for x in chunks)
def normalize_chunks(chunks: Any, shape: tuple[int, ...], typesize: int) -> tuple[int, ...]:
"""Convenience function to normalize the `chunks` argument for an array
with the given `shape`."""
# N.B., expect shape already normalized
# handle auto-chunking
if chunks is None or chunks is True:
return _guess_chunks(shape, typesize)
# handle no chunking
if chunks is False:
return shape
# handle 1D convenience form
if isinstance(chunks, numbers.Integral):
chunks = tuple(int(chunks) for _ in shape)
# handle dask-style chunks (iterable of iterables)
if all(isinstance(c, (tuple | list)) for c in chunks):
# take first chunk size for each dimension
chunks = tuple(
c[0] for c in chunks
) # TODO: check/error/warn for irregular chunks (e.g. if c[0] != c[1:-1])
# handle bad dimensionality
if len(chunks) > len(shape):
raise ValueError("too many dimensions in chunks")
# handle underspecified chunks
if len(chunks) < len(shape):
# assume chunks across remaining dimensions
chunks += shape[len(chunks) :]
# handle None or -1 in chunks
if -1 in chunks or None in chunks:
chunks = tuple(
s if c == -1 or c is None else int(c) for s, c in zip(shape, chunks, strict=False)
)
if not all(isinstance(c, numbers.Integral) for c in chunks):
raise TypeError("non integer value in chunks")
return tuple(int(c) for c in chunks)
@dataclass(frozen=True)
class ChunkGrid(Metadata):
@classmethod
def from_dict(cls, data: dict[str, JSON] | ChunkGrid) -> ChunkGrid:
if isinstance(data, ChunkGrid):
return data
name_parsed, _ = parse_named_configuration(data)
if name_parsed == "regular":
return RegularChunkGrid._from_dict(data)
raise ValueError(f"Unknown chunk grid. Got {name_parsed}.")
@abstractmethod
def all_chunk_coords(self, array_shape: ChunkCoords) -> Iterator[ChunkCoords]:
pass
@abstractmethod
def get_nchunks(self, array_shape: ChunkCoords) -> int:
pass
@dataclass(frozen=True)
class RegularChunkGrid(ChunkGrid):
chunk_shape: ChunkCoords
def __init__(self, *, chunk_shape: ChunkCoordsLike) -> None:
chunk_shape_parsed = parse_shapelike(chunk_shape)
object.__setattr__(self, "chunk_shape", chunk_shape_parsed)
@classmethod
def _from_dict(cls, data: dict[str, JSON]) -> Self:
_, configuration_parsed = parse_named_configuration(data, "regular")
return cls(**configuration_parsed) # type: ignore[arg-type]
def to_dict(self) -> dict[str, JSON]:
return {"name": "regular", "configuration": {"chunk_shape": tuple(self.chunk_shape)}}
def all_chunk_coords(self, array_shape: ChunkCoords) -> Iterator[ChunkCoords]:
return itertools.product(
*(range(ceildiv(s, c)) for s, c in zip(array_shape, self.chunk_shape, strict=False))
)
def get_nchunks(self, array_shape: ChunkCoords) -> int:
return reduce(
operator.mul,
itertools.starmap(ceildiv, zip(array_shape, self.chunk_shape, strict=True)),
1,
)
def _auto_partition(
*,
array_shape: tuple[int, ...],
chunk_shape: tuple[int, ...] | Literal["auto"],
shard_shape: ShardsLike | None,
item_size: int,
) -> tuple[tuple[int, ...] | None, tuple[int, ...]]:
"""
Automatically determine the shard shape and chunk shape for an array, given the shape and dtype of the array.
If `shard_shape` is `None` and the chunk_shape is "auto", the chunks will be set heuristically based
on the dtype and shape of the array.
If `shard_shape` is "auto", then the shard shape will be set heuristically from the dtype and shape
of the array; if the `chunk_shape` is also "auto", then the chunks will be set heuristically as well,
given the dtype and shard shape. Otherwise, the chunks will be returned as-is.
"""
if shard_shape is None:
_shards_out: None | tuple[int, ...] = None
if chunk_shape == "auto":
_chunks_out = _guess_chunks(array_shape, item_size)
else:
_chunks_out = chunk_shape
else:
if chunk_shape == "auto":
# aim for a 1MiB chunk
_chunks_out = _guess_chunks(array_shape, item_size, max_bytes=1024)
else:
_chunks_out = chunk_shape
if shard_shape == "auto":
warnings.warn(
"Automatic shard shape inference is experimental and may change without notice.",
UserWarning,
stacklevel=2,
)
_shards_out = ()
for a_shape, c_shape in zip(array_shape, _chunks_out, strict=True):
# TODO: make a better heuristic than this.
# for each axis, if there are more than 8 chunks along that axis, then put
# 2 chunks in each shard for that axis.
if a_shape // c_shape > 8:
_shards_out += (c_shape * 2,)
else:
_shards_out += (c_shape,)
elif isinstance(shard_shape, dict):
_shards_out = tuple(shard_shape["shape"])
else:
_shards_out = shard_shape
return _shards_out, _chunks_out