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