MCPcopy
hub / github.com/dask/dask / gradient

Function gradient

dask/array/routines.py:661–735  ·  view source on GitHub ↗
(f, *varargs, axis=None, **kwargs)

Source from the content-addressed store, hash-verified

659
660@derived_from(np)
661def gradient(f, *varargs, axis=None, **kwargs):
662 f = asarray(f)
663
664 kwargs["edge_order"] = math.ceil(kwargs.get("edge_order", 1))
665 if kwargs["edge_order"] > 2:
666 raise ValueError("edge_order must be less than or equal to 2.")
667
668 drop_result_list = False
669 if axis is None:
670 axis = tuple(range(f.ndim))
671 elif isinstance(axis, Integral):
672 drop_result_list = True
673 axis = (axis,)
674
675 axis = validate_axis(axis, f.ndim)
676
677 if len(axis) != len(set(axis)):
678 raise ValueError("duplicate axes not allowed")
679
680 axis = tuple(ax % f.ndim for ax in axis)
681
682 if varargs == ():
683 varargs = (1,)
684 if len(varargs) == 1:
685 varargs = len(axis) * varargs
686 if len(varargs) != len(axis):
687 raise TypeError(
688 "Spacing must either be a single scalar, or a scalar / 1d-array per axis"
689 )
690
691 if issubclass(f.dtype.type, (np.bool_, Integral)):
692 f = f.astype(float)
693 elif issubclass(f.dtype.type, Real) and f.dtype.itemsize < 4:
694 f = f.astype(float)
695
696 results = []
697 for i, ax in enumerate(axis):
698 for c in f.chunks[ax]:
699 if np.min(c) < kwargs["edge_order"] + 1:
700 raise ValueError(
701 "Chunk size must be larger than edge_order + 1. "
702 f"Minimum chunk for axis {ax} is {np.min(c)}. Rechunk to "
703 "proceed."
704 )
705
706 if np.isscalar(varargs[i]):
707 array_locs = None
708 else:
709 if isinstance(varargs[i], Array):
710 raise NotImplementedError("dask array coordinated is not supported.")
711 # coordinate position for each block taking overlap into account
712 chunk = np.array(f.chunks[ax])
713 array_loc_stop = np.cumsum(chunk) + 1
714 array_loc_start = array_loc_stop - chunk - 2
715 array_loc_stop[-1] -= 1
716 array_loc_start[0] = 0
717 array_locs = (array_loc_start, array_loc_stop)
718

Callers

nothing calls this directly

Calls 8

asarrayFunction · 0.90
validate_axisFunction · 0.90
setClass · 0.85
getMethod · 0.45
astypeMethod · 0.45
minMethod · 0.45
cumsumMethod · 0.45
map_overlapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…