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

Function common_blockdim

dask/array/core.py:4264–4329  ·  view source on GitHub ↗

Find the common block dimensions from the list of block dimensions Currently only implements the simplest possible heuristic: the common block-dimension is the only one that does not span fully span a dimension. This is a conservative choice that allows us to avoid potentially very

(blockdims)

Source from the content-addressed store, hash-verified

4262
4263
4264def common_blockdim(blockdims):
4265 """Find the common block dimensions from the list of block dimensions
4266
4267 Currently only implements the simplest possible heuristic: the common
4268 block-dimension is the only one that does not span fully span a dimension.
4269 This is a conservative choice that allows us to avoid potentially very
4270 expensive rechunking.
4271
4272 Assumes that each element of the input block dimensions has all the same
4273 sum (i.e., that they correspond to dimensions of the same size).
4274
4275 Examples
4276 --------
4277 >>> common_blockdim([(3,), (2, 1)])
4278 (2, 1)
4279 >>> common_blockdim([(1, 2), (2, 1)])
4280 (1, 1, 1)
4281 >>> common_blockdim([(2, 2), (3, 1)]) # doctest: +SKIP
4282 Traceback (most recent call last):
4283 ...
4284 ValueError: Chunks do not align
4285 """
4286 if not any(blockdims):
4287 return ()
4288 non_trivial_dims = {d for d in blockdims if len(d) > 1}
4289 if len(non_trivial_dims) == 1:
4290 return first(non_trivial_dims)
4291 if len(non_trivial_dims) == 0:
4292 return max(blockdims, key=first)
4293
4294 if np.isnan(sum(map(sum, blockdims))):
4295 raise ValueError(
4296 f"Arrays' chunk sizes ({blockdims}) are unknown.\n\n"
4297 "A possible solution:\n"
4298 " x.compute_chunk_sizes()"
4299 )
4300
4301 if len(set(map(sum, non_trivial_dims))) > 1:
4302 raise ValueError("Chunks do not add up to same value", blockdims)
4303
4304 # We have multiple non-trivial chunks on this axis
4305 # e.g. (5, 2) and (4, 3)
4306
4307 # We create a single chunk tuple with the same total length
4308 # that evenly divides both, e.g. (4, 1, 2)
4309
4310 # To accomplish this we walk down all chunk tuples together, finding the
4311 # smallest element, adding it to the output, and subtracting it from all
4312 # other elements and remove the element itself. We stop once we have
4313 # burned through all of the chunk tuples.
4314 # For efficiency's sake we reverse the lists so that we can pop off the end
4315 rchunks = [list(ntd)[::-1] for ntd in non_trivial_dims]
4316 total = sum(first(non_trivial_dims))
4317 i = 0
4318
4319 out = []
4320 while i < total:
4321 m = min(c[-1] for c in rchunks)

Callers 1

test_common_blockdimFunction · 0.90

Calls 6

anyFunction · 0.90
maxFunction · 0.90
sumFunction · 0.90
minFunction · 0.90
setClass · 0.85
popMethod · 0.80

Tested by 1

test_common_blockdimFunction · 0.72