Repository navigation
Add gradient for Dask Arrays - #3434
Conversation
72a393e to
1a11d11
Compare
mrocklin
left a comment
There was a problem hiding this comment.
Looks good to me. A couple minor comments follow.
| depth={j: 1 if j == ax else 0 for j in range(f.ndim)}, | ||
| boundary="none", | ||
| grad_varargs=(varargs[i],), | ||
| grad_kwargs=dict(concat([kwargs.items(), {"axis": ax}.items()])), |
There was a problem hiding this comment.
Was not aware. Thanks for the tip. Have changed it.
|
|
||
|
|
||
| def _gradient_kernel(f, grad_varargs, grad_kwargs): | ||
| return np.gradient(f, *grad_varargs, **grad_kwargs) |
There was a problem hiding this comment.
Why was this necessary? Would it be possible to change map_overlap to accept *args, **kwargs?
There was a problem hiding this comment.
It wouldn't be possible. map_overlap only accepts one non-keyword argument, depth, and a couple optional arguments.
It doesn't take any variadic argument and provides no real mechanism to forward variadic arguments, which leaves us doing this.
While kwargs is taken by map_overlap, it gets fed into map_blocks and can overlap with user arguments. So it isn't that safe to rely on generally.
There was a problem hiding this comment.
Would it be possible to change map_overlap to accept
*args, **kwargs?
Not sure if this is what you were meaning, but opened issue ( #3437 ) to discuss some changes to map_blocks/map_overlap that would improve usability.
There was a problem hiding this comment.
Yeah, I'd personally be in favor of changing map_overlap to accept *args and **kwargs. The map_blocks and map_partitions (dataframes) do this and it seems to be well loved by users. It might be valuable to be consistent across API functions like this.
There was a problem hiding this comment.
So the larger questions of how map_overlap behaves I think we can save for the other issue (already some good discussion in there). Is there anything else you need on this particular point?
There was a problem hiding this comment.
Nope. It sounds like cleaning up this function is future work regardless of the way in which the map_overlap concludes.
1a11d11 to
319c821
Compare
mrocklin
left a comment
There was a problem hiding this comment.
Some small comments and questions. Generally this looks good to me.
| f.map_overlap( | ||
| _gradient_kernel, | ||
| dtype=f.dtype, | ||
| depth={j: 1 if j == ax else 0 for j in range(f.ndim)}, |
There was a problem hiding this comment.
I think that you might be able to say just depth={ax: 1} here
There was a problem hiding this comment.
Was not sure if that was supported. Though looking at ghost, it does seem to be.
There was a problem hiding this comment.
Went to do this locally and run the tests, but got test failures. Came up with an MRE to reproduce this and filed it as issue ( #3444 ). That said, agree this simplification would be nice.
| f = asarray(f) | ||
|
|
||
| if not all([isinstance(e, Number) for e in varargs]): | ||
| raise NotImplementedError("Only numeric scalar spacings supported.") |
There was a problem hiding this comment.
Is behavior on non-integral spacings defined?
There was a problem hiding this comment.
In NumPy Arrays (or non-uniform spacings) are supported. Opted not to do that here. Would rather see users request this functionality first.
There was a problem hiding this comment.
...or maybe you were referring to non-discrete spacings, in which case the answer is yes for both (NumPy and our implementation).
There was a problem hiding this comment.
Should add there is a test for non-discrete spacings. :)
| axis = kwargs.pop("axis", None) | ||
| if axis is None: | ||
| axis = tuple(range(f.ndim)) | ||
| elif isinstance(axis, Number): |
|
|
||
| axis = tuple(ax % f.ndim for ax in axis) | ||
|
|
||
| if issubclass(f.dtype.type, (np.bool8, Integral)): |
There was a problem hiding this comment.
Is there some reason to prefer that? AFAICT this works just as well.
On a different point, one still has to deal with np.bool8 differently as it isn't treated as an integer in either case.
There was a problem hiding this comment.
I think issubclass is slightly saner -- issubdtype deviates from this behavior in some weird deprecated cases.
Creates the equivalent of NumPy's `gradient` function for Dask Arrays. Simply wraps the NumPy `gradient` function using `map_overlap` to allow `gradient` to computed on each chunk with a halo. Currently only supports uniform spacings specified by concrete scalars. Though this could be relaxed in the future if there is demand.
|
sounds good to me
…On Wed, Apr 25, 2018 at 11:27 AM, Stephan Hoyer ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In dask/array/routines.py
<#3434 (comment)>:
> + elif isinstance(axis, Number):
+ drop_result_list = True
+ axis = (axis,)
+
+ for e in axis:
+ if not isinstance(e, Integral):
+ raise TypeError("%s, invalid value for axis" % repr(e))
+ if not (-f.ndim <= e < f.ndim):
+ raise ValueError("axis, %s, is out of bounds" % repr(e))
+
+ if len(axis) != len(set(axis)):
+ raise ValueError("duplicate axes not allowed")
+
+ axis = tuple(ax % f.ndim for ax in axis)
+
+ if issubclass(f.dtype.type, (np.bool8, Integral)):
I think issubclass is slightly saner -- issubdtype deviates from this
behavior in some weird deprecated cases.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3434 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszIXHgUJCtBQ121gkGzydHDA8JLbHks5tsJXygaJpZM4Tg8dt>
.
|
319c821 to
85de3ef
Compare
Make sure that Dask Array's `gradient` gets comparable answers to NumPy's `gradient` even across chunks.
85de3ef to
08c229d
Compare
|
Please let me know if there is anything else. |
|
This is in. Thanks @jakirkham ! |
|
Thanks for the reviews. :) |
Fixes #2918
Adds NumPy's
gradientfor Dask Arrays. Basically usesmap_overlapto implement it with some handling for boundaries based on the arguments passed togradient.flake8 daskdocs/source/changelog.rstfor all changesand one of the
docs/source/*-api.rstfiles for new API