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

Function arg_reduction

dask/array/reductions.py:841–915  ·  view source on GitHub ↗

Generic function for argreduction. Parameters ---------- x : Array chunk : callable Partialed ``arg_chunk``. combine : callable Partialed ``arg_combine``. agg : callable Partialed ``arg_agg``. axis : int, optional split_every : int or dict, op

(
    x, chunk, combine, agg, axis=None, keepdims=False, split_every=None, out=None
)

Source from the content-addressed store, hash-verified

839
840
841def arg_reduction(
842 x, chunk, combine, agg, axis=None, keepdims=False, split_every=None, out=None
843):
844 """Generic function for argreduction.
845
846 Parameters
847 ----------
848 x : Array
849 chunk : callable
850 Partialed ``arg_chunk``.
851 combine : callable
852 Partialed ``arg_combine``.
853 agg : callable
854 Partialed ``arg_agg``.
855 axis : int, optional
856 split_every : int or dict, optional
857 """
858 if axis is None:
859 axis = tuple(range(x.ndim))
860 ravel = True
861 elif isinstance(axis, Integral):
862 axis = validate_axis(axis, x.ndim)
863 axis = (axis,)
864 ravel = x.ndim == 1
865 else:
866 raise TypeError(f"axis must be either `None` or int, got '{axis}'")
867
868 for ax in axis:
869 chunks = x.chunks[ax]
870 if len(chunks) > 1 and np.isnan(chunks).any():
871 raise ValueError(
872 "Arg-reductions do not work with arrays that have "
873 "unknown chunksizes. At some point in your computation "
874 "this array lost chunking information.\n\n"
875 "A possible solution is with \n"
876 " x.compute_chunk_sizes()"
877 )
878
879 # Map chunk across all blocks
880 name = f"arg-reduce-{tokenize(axis, x, chunk, combine, split_every)}"
881 old = x.name
882 keys = list(product(*map(range, x.numblocks)))
883 offsets = list(product(*(accumulate(operator.add, bd[:-1], 0) for bd in x.chunks)))
884 if ravel:
885 offset_info = zip(offsets, repeat(x.shape))
886 else:
887 offset_info = pluck(axis[0], offsets)
888
889 chunks = tuple((1,) * len(c) if i in axis else c for (i, c) in enumerate(x.chunks))
890 dsk = {
891 (name,) + k: (chunk, (old,) + k, axis, off)
892 for (k, off) in zip(keys, offset_info)
893 }
894
895 dtype = np.argmin(asarray_safe([1], like=meta_from_array(x)))
896 meta = None
897 if is_arraylike(dtype):
898 # This case occurs on non-NumPy types (e.g., CuPy), where the returned

Callers 4

argmaxFunction · 0.85
argminFunction · 0.85
nanargmaxFunction · 0.85
nanargminFunction · 0.85

Calls 12

validate_axisFunction · 0.90
asarray_safeFunction · 0.90
meta_from_arrayFunction · 0.90
is_arraylikeFunction · 0.90
ArrayClass · 0.90
_tree_reduceFunction · 0.90
handle_outFunction · 0.90
argminMethod · 0.80
from_collectionsMethod · 0.80
repeatFunction · 0.70
tokenizeFunction · 0.50
anyMethod · 0.45

Tested by

no test coverage detected