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

Function slice_with_int_dask_array

dask/array/chunk.py:297–340  ·  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

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