MCPcopy Create free account
hub / github.com/dask/dask / compute_chunk_sizes

Method compute_chunk_sizes

dask/array/core.py:1460–1507  ·  view source on GitHub ↗

Compute the chunk sizes for a Dask array. This is especially useful when the chunk sizes are unknown (e.g., when indexing one Dask array with another). Notes ----- This function modifies the Dask array in-place. Examples --------

(self)

Source from the content-addressed store, hash-verified

1458 return reduce(mul, self.numblocks, 1)
1459
1460 def compute_chunk_sizes(self):
1461 """
1462 Compute the chunk sizes for a Dask array. This is especially useful
1463 when the chunk sizes are unknown (e.g., when indexing one Dask array
1464 with another).
1465
1466 Notes
1467 -----
1468 This function modifies the Dask array in-place.
1469
1470 Examples
1471 --------
1472 >>> import dask.array as da
1473 >>> import numpy as np
1474 >>> x = da.from_array([-2, -1, 0, 1, 2], chunks=2)
1475 >>> x.chunks
1476 ((2, 2, 1),)
1477 >>> y = x[x <= 0]
1478 >>> y.chunks
1479 ((nan, nan, nan),)
1480 >>> y.compute_chunk_sizes() # in-place computation
1481 dask.array<getitem, shape=(3,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>
1482 >>> y.chunks
1483 ((2, 1, 0),)
1484
1485 """
1486 x = self
1487 chunk_shapes = x.map_blocks(
1488 _get_chunk_shape,
1489 dtype=int,
1490 chunks=tuple(len(c) * (1,) for c in x.chunks) + ((x.ndim,),),
1491 new_axis=x.ndim,
1492 )
1493
1494 c = []
1495 for i in range(x.ndim):
1496 s = x.ndim * [0] + [i]
1497 s[i] = slice(None)
1498 s = tuple(s)
1499
1500 c.append(tuple(chunk_shapes[s]))
1501
1502 # `map_blocks` assigns numpy dtypes
1503 # cast chunk dimensions back to python int before returning
1504 x._chunks = tuple(
1505 tuple(int(chunk) for chunk in chunks) for chunks in compute(tuple(c))[0]
1506 )
1507 return x
1508
1509 @cached_property
1510 def shape(self) -> tuple[T_IntOrNaN, ...]:

Calls 2

computeFunction · 0.90
map_blocksMethod · 0.45