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

Function slice_with_int_dask_array_aggregate

dask/array/chunk.py:343–396  ·  view source on GitHub ↗

Final aggregation function of `slice_with_int_dask_array_on_axis`. Aggregate all chunks of x by one chunk of idx, reordering the output of `slice_with_int_dask_array`. Note that there is no combine function, as a recursive aggregation (e.g. with split_every) would not give any benef

(idx, chunk_outputs, x_chunks, axis)

Source from the content-addressed store, hash-verified

341
342
343def slice_with_int_dask_array_aggregate(idx, chunk_outputs, x_chunks, axis):
344 """Final aggregation function of `slice_with_int_dask_array_on_axis`.
345 Aggregate all chunks of x by one chunk of idx, reordering the output of
346 `slice_with_int_dask_array`.
347
348 Note that there is no combine function, as a recursive aggregation (e.g.
349 with split_every) would not give any benefit.
350
351 Parameters
352 ----------
353 idx: ndarray, ndim=1, dtype=any integer
354 j-th chunk of idx
355 chunk_outputs: ndarray
356 concatenation along axis of the outputs of `slice_with_int_dask_array`
357 for all chunks of x and the j-th chunk of idx
358 x_chunks: tuple
359 dask chunks of the x da.Array along axis, e.g. ``(3, 3, 2)``
360 axis: int
361 normalized axis to take elements from (0 <= axis < x.ndim)
362
363 Returns
364 -------
365 Selection from all chunks of x for the j-th chunk of idx, in the correct
366 order
367 """
368 # Needed when idx is unsigned
369 idx = idx.astype(np.int64)
370
371 # Normalize negative indices
372 idx = np.where(idx < 0, idx + sum(x_chunks), idx)
373
374 x_chunk_offset = 0
375 chunk_output_offset = 0
376
377 # Assemble the final index that picks from the output of the previous
378 # kernel by adding together one layer per chunk of x
379 # FIXME: this could probably be reimplemented with a faster search-based
380 # algorithm
381 idx_final = np.zeros_like(idx)
382 for x_chunk in x_chunks:
383 idx_filter = (idx >= x_chunk_offset) & (idx < x_chunk_offset + x_chunk)
384 idx_cum = np.cumsum(idx_filter)
385 idx_final += np.where(idx_filter, idx_cum - 1 + chunk_output_offset, 0)
386 x_chunk_offset += x_chunk
387 if idx_cum.size > 0:
388 chunk_output_offset += idx_cum[-1]
389
390 # np.take does not support slice indices
391 # return np.take(chunk_outputs, idx_final, axis)
392 return chunk_outputs[
393 tuple(
394 idx_final if i == axis else slice(None) for i in range(chunk_outputs.ndim)
395 )
396 ]
397
398
399def getitem(obj, index):

Callers

nothing calls this directly

Calls 4

sumFunction · 0.70
astypeMethod · 0.45
whereMethod · 0.45
cumsumMethod · 0.45

Tested by

no test coverage detected