forked from abTEM/abTEM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunks.py
More file actions
408 lines (325 loc) · 11.8 KB
/
Copy pathchunks.py
File metadata and controls
408 lines (325 loc) · 11.8 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""Module for determining chunk sizes of Dask arrays."""
from __future__ import annotations
import itertools
from functools import reduce
from itertools import accumulate
from operator import mul
from typing import Generator, Optional, TypeGuard, Union
import numpy as np
from dask.utils import parse_bytes
from abtem.core import config
Chunks = Union[int, str, tuple[Union[int, str, tuple[int, ...]], ...]]
ChunksTuple = tuple[Union[int, str, tuple[int, ...]], ...]
ValidatedChunks = tuple[tuple[int, ...], ...]
def is_tuple_of_ints(x: Chunks) -> TypeGuard[tuple[int, ...]]:
return isinstance(x, tuple) and all(isinstance(c, int) for c in x)
def is_tuple_of_tuple_of_ints(x: Chunks) -> TypeGuard[tuple[tuple[int, ...], ...]]:
return isinstance(x, tuple) and all(
isinstance(x1, tuple) and all(isinstance(c, int) for c in x1) for x1 in x
)
def is_tuple_of_ints_or_tuple_of_ints(
x: Chunks,
) -> TypeGuard[tuple[tuple[int, ...], ...]]:
return isinstance(x, tuple) and all(
isinstance(x1, int)
or (isinstance(x1, tuple) and all(isinstance(c, int) for c in x1))
for x1 in x
)
def is_tuple_of_ints_or_tuple_of_tuple_of_ints(
x: Chunks,
) -> TypeGuard[tuple[int | tuple[int, ...], ...]]:
return is_tuple_of_ints(x) or is_tuple_of_tuple_of_ints(x)
def is_validated_chunks(x: Chunks) -> TypeGuard[ValidatedChunks]:
"""
Check if the input is are valid chunk sizes.
Parameters
----------
x : int or tuple of int or tuple of tuple of int or str
The chunk sizes of the Dask array.
Returns
-------
TypeGuard[ValidatedChunks]
True if the input is a valid chunk size.
"""
return is_tuple_of_tuple_of_ints(x)
def assert_chunks_match_shape(shape: tuple[int, ...], chunks: ValidatedChunks) -> None:
if not all(sum(c) == s for s, c in zip(shape, chunks)):
raise ValueError(f"chunks must match shape, got {chunks} for shape {shape}")
def chunk_ranges(chunks: ValidatedChunks) -> tuple[tuple[tuple[int, int], ...], ...]:
"""
Get the start and end indices for each chunk.
Parameters
----------
chunks : tuple of tuple of int
The chunk sizes of the Dask array.
Returns
-------
tuple of tuple of tuple of two int
The range of indices for each chunk.
"""
return tuple(
tuple((cumchunks - cc, cumchunks) for cc, cumchunks in zip(c, accumulate(c)))
for c in chunks
)
def iterate_chunk_ranges(chunks: ValidatedChunks):
"""
Iterate over the chunk ranges.
Parameters
----------
chunks : tuple of tuple of int
The chunk sizes of the Dask array.
Yields
------
block_indices : tuple of int
The indices of the current block.
slices : tuple of slice
The slices indexing the current block.
"""
chunk_shape = tuple(len(c) for c in chunks)
for block_indices, chunk_range in zip(
itertools.product(*(range(n) for n in chunk_shape)),
itertools.product(*chunk_ranges(chunks)),
):
slic = tuple(slice(*cr) for cr in chunk_range)
yield block_indices, slic
def fill_in_chunk_sizes(
shape: tuple[int, ...], chunks: tuple[int | tuple[int, ...], ...]
) -> ValidatedChunks:
validated_chunks = []
for s, c in zip(shape, chunks):
if isinstance(c, tuple):
validated_chunks.append(c)
elif isinstance(c, int):
if c == -1:
validated_chunks.append((s,))
else:
chunk_size = (c,) * (s // c)
if s % c:
chunk_size += (s % c,)
validated_chunks.append(chunk_size)
else:
raise RuntimeError("Invalid chunk type")
return tuple(validated_chunks)
def check_chunks_match_shape_length(shape: tuple[int, ...], chunks: Chunks) -> None:
if isinstance(chunks, tuple) and not len(shape) == len(chunks):
raise ValueError(f"length of shape: {shape} does not match chunks {chunks}")
def validate_chunks(
shape: tuple[int, ...],
chunks: Chunks,
max_elements: int | str = "auto",
dtype: Optional[np.dtype] = None,
device: str = "cpu",
) -> ValidatedChunks:
"""
Validate the chunks for a Dask array based on the shape and a maximum number of
elements.
Parameters
----------
shape : tuple of int
The shape of the array.
chunks : int or tuple of int or str
The chunk sizes of the Dask array. If an integer, the array will be split into
equal chunks. If a tuple, the array will be split into the specified chunks.
If "auto", the chunks will be determined automatically based on the shape and
the maximum number of elements.
max_elements : int or str
The maximum number of elements in a chunk. If "auto", the maximum number of
elements will be determined based on the maximum number of bytes per chunk and
the dtype.
dtype : np.dtype
The dtype of the array.
device : str
The device the array will be stored on.
Returns
-------
tuple of tuple of int
The chunk sizes of the Dask array.
"""
check_chunks_match_shape_length(shape, chunks)
if is_validated_chunks(chunks):
validated_chunks = chunks
elif chunks == -1:
validated_chunks = validate_chunks(shape, shape)
elif isinstance(chunks, int):
max_elements = chunks
chunks = ("auto",) * len(shape)
validated_chunks = _auto_chunks(
shape, chunks, max_elements, dtype=dtype, device=device
)
elif isinstance(chunks, str):
raise NotImplementedError()
elif any(isinstance(c, str) for c in chunks):
validated_chunks = _auto_chunks(
shape, chunks, max_elements, dtype=dtype, device=device
)
elif is_tuple_of_ints_or_tuple_of_ints(chunks):
validated_chunks = fill_in_chunk_sizes(shape, chunks)
else:
raise ValueError(
"chunks must be an integer, a tuple of integers a tuple of tuple of"
f"integers or 'auto' got {chunks}"
)
assert_chunks_match_shape(shape, validated_chunks)
return validated_chunks
def _auto_chunks(
shape: tuple[int, ...],
chunks: ChunksTuple,
max_elements: str | int = "auto",
dtype: Optional[np.dtype] = None,
device: str = "cpu",
) -> ValidatedChunks:
"""
Automatically determine the chunks for a Dask array based on the shape and a maximum
number of elements.
Parameters
----------
shape : tuple of int
The shape of the array.
chunks : tuple of int or str
The chunk sizes of the Dask array. If an integer, the array will be split into
equal chunks. If a tuple, the array will be split into the specified chunks.
If "auto", the chunks will be determined automatically based on the shape and
the maximum number of elements.
max_elements : int or str
The maximum number of elements in a chunk. If "auto", the maximum number of
elements will be determined based on the maximum number of bytes per chunk and
the dtype.
dtype : np.dtype
The dtype of the array.
device : str
The device the array will be stored on.
Returns
-------
tuple of tuple of int
The chunk sizes of the Dask array.
"""
check_chunks_match_shape_length(shape, chunks)
if max_elements == "auto":
if device == "gpu":
chunk_bytes = parse_bytes(config.get("dask.chunk-size-gpu"))
elif device == "cpu":
chunk_bytes = parse_bytes(config.get("dask.chunk-size"))
else:
raise RuntimeError(f"Unknown device: {device}")
if dtype is None:
raise ValueError("auto selecting chunk sizes requires dtype")
max_elements = int(np.floor(chunk_bytes) / np.dtype(dtype).itemsize)
elif isinstance(max_elements, str):
max_elements = int(
np.floor(parse_bytes(max_elements) / np.dtype(dtype).itemsize)
)
elif not isinstance(max_elements, int):
raise ValueError("limit must be an integer or a string")
normalized_chunks = tuple(s if c == -1 else c for s, c in zip(shape, chunks))
# minimum_chunks = tuple(
# 1 if c == "auto" else c for s, c in zip(shape, normalized_chunks)
# )
current_chunks = []
max_chunks = []
for n, c in zip(shape, normalized_chunks):
if c == "auto":
current_chunks.append(1)
max_chunks.append(n)
elif isinstance(c, int):
current_chunks.append(c)
max_chunks.append(c)
elif isinstance(c, tuple):
current_chunks.append(max(c))
max_chunks.append(max(c))
else:
raise RuntimeError()
autodims = [i for i, c in enumerate(normalized_chunks) if c == "auto"]
j = 0
while len(autodims):
# autodims = [i for i in autodims if current_chunks[i] != maximum_chunks[i]]
if len(autodims) == 0:
break
j = j % len(autodims)
current_chunks[autodims[j]] = min(
current_chunks[autodims[j]] + 1, shape[autodims[j]]
)
total = reduce(mul, current_chunks)
if total > max_elements:
current_chunks[autodims[j]] -= 1
raise RuntimeError("Object cannot be automatically chunked; consider increasing chunk-size parameter!")
break
if current_chunks == max_chunks:
break
j += 1
chunks = ()
for i, c in enumerate(normalized_chunks):
if c == "auto":
chunks += (current_chunks[i],)
else:
chunks += (c,)
chunks = validate_chunks(shape, chunks, max_elements, dtype)
return chunks
def equal_sized_chunks(
num_items: int, num_chunks: Optional[int] = None, chunk_size: Optional[int] = None
) -> tuple[int, ...]:
"""
Split an n integer into m (almost) equal integers, such that the sum of smaller
integers equals n.
Parameters
----------
num_items: int
The integer to split.
num_chunks: int
The number integers n will be split into.
chunk_size: int
The size of each chunk.
Returns
-------
tuple of int
The split integers.
"""
if num_items == 0:
return 0, 0
if num_chunks is not None and chunk_size is not None:
raise RuntimeError("specify either num_chunks or chunks, not both")
if num_chunks is None:
if chunk_size is not None:
num_chunks = (num_items + (-num_items % chunk_size)) // chunk_size
else:
raise RuntimeError("either num_chunks or chunks must be specified")
if num_items < num_chunks:
raise RuntimeError(
f"num_chunks ({num_chunks}) may not be larger than num_items ({num_items})"
)
elif num_items % num_chunks == 0:
chunks = tuple([num_items // num_chunks] * num_chunks)
else:
zp = num_chunks - (num_items % num_chunks)
pp = num_items // num_chunks
chunks = tuple(pp + 1 if i >= zp else pp for i in range(num_chunks))
assert sum(chunks) == num_items
return chunks
def generate_chunks(
num_items: int,
num_chunks: Optional[int] = None,
chunks: Optional[int] = None,
start: int = 0,
) -> Generator[tuple[int, int], None, None]:
"""
Generate start and end indices for each chunks of equal sized chunks.
Parameters
----------
num_items: int
The integer to split.
num_chunks: int
The number integers n will be split into.
chunks: int
The size of each chunk.
start: int
The starting index.
Yields
------
tuple of int
The start and end indices of the current chunk.
"""
for batch in equal_sized_chunks(num_items, num_chunks, chunks):
if num_items == 0:
break
end = start + batch
yield start, end
start = end