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

Function slice_with_int_dask_array

dask/array/chunk.py:306–349  ·  view source on GitHub ↗

Chunk function of `slice_with_int_dask_array_on_axis`. Slice one chunk of x by one chunk of idx. Parameters ---------- x: ndarray, any dtype, any shape i-th chunk of x idx: ndarray, ndim=1, dtype=any integer j-th chunk of idx (cartesian product with the chunks of

(x, idx, offset, x_size, axis)

Source from the content-addressed store, hash-verified

304
305
306def slice_with_int_dask_array(x, idx, offset, x_size, axis):
307 """Chunk function of `slice_with_int_dask_array_on_axis`.
308 Slice one chunk of x by one chunk of idx.
309
310 Parameters
311 ----------
312 x: ndarray, any dtype, any shape
313 i-th chunk of x
314 idx: ndarray, ndim=1, dtype=any integer
315 j-th chunk of idx (cartesian product with the chunks of x)
316 offset: ndarray, shape=(1, ), dtype=int64
317 Index of the first element along axis of the current chunk of x
318 x_size: int
319 Total size of the x da.Array along axis
320 axis: int
321 normalized axis to take elements from (0 <= axis < x.ndim)
322
323 Returns
324 -------
325 x sliced along axis, using only the elements of idx that fall inside the
326 current chunk.
327 """
328 from dask.array.utils import asarray_safe, meta_from_array
329
330 idx = asarray_safe(idx, like=meta_from_array(x))
331
332 # Needed when idx is unsigned
333 idx = idx.astype(np.int64)
334
335 # Normalize negative indices
336 idx = np.where(idx < 0, idx + x_size, idx)
337
338 # A chunk of the offset dask Array is a numpy array with shape (1, ).
339 # It indicates the index of the first element along axis of the current
340 # chunk of x.
341 idx = idx - offset
342
343 # Drop elements of idx that do not fall inside the current chunk of x
344 idx_filter = (idx >= 0) & (idx < x.shape[axis])
345 idx = idx[idx_filter]
346
347 # np.take does not support slice indices
348 # return np.take(x, idx, axis)
349 return x[tuple(idx if i == axis else slice(None) for i in range(x.ndim))]
350
351
352def slice_with_int_dask_array_aggregate(idx, chunk_outputs, x_chunks, axis):

Callers

nothing calls this directly

Calls 4

asarray_safeFunction · 0.90
meta_from_arrayFunction · 0.90
astypeMethod · 0.45
whereMethod · 0.45

Tested by

no test coverage detected