Skip to content

Add gradient for Dask Arrays - #3434

Merged
mrocklin merged 5 commits into
dask:masterfrom
jakirkham:add_np_gradient
Apr 25, 2018
Merged

mrocklin merged 5 commits into
dask:masterfrom
jakirkham:add_np_gradient

Conversation

@jakirkham

@jakirkham jakirkham commented Apr 24, 2018 •

Copy link
Copy Markdown
Member

Fixes #2918

Adds NumPy's gradient for Dask Arrays. Basically uses map_overlap to implement it with some handling for boundaries based on the arguments passed to gradient.


  • Tests added / passed
  • Passes flake8 dask
  • Fully documented, including docs/source/changelog.rst for all changes
    and one of the docs/source/*-api.rst files for new API

@jakirkham jakirkham changed the title Add gradient fo Dask Arrays Add gradient for Dask Arrays Apr 24, 2018
@jakirkham
jakirkham force-pushed the add_np_gradient branch 2 times, most recently from 72a393e to 1a11d11 Compare April 24, 2018 04:02
@jakirkham
jakirkham requested a review from shoyer April 24, 2018 04:04

@mrocklin mrocklin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. A couple minor comments follow.

Comment thread dask/array/routines.py Outdated
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()])),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See toolz.merge?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not aware. Thanks for the tip. Have changed it.

Comment thread dask/array/routines.py


def _gradient_kernel(f, grad_varargs, grad_kwargs):
return np.gradient(f, *grad_varargs, **grad_kwargs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this necessary? Would it be possible to change map_overlap to accept *args, **kwargs?

@jakirkham jakirkham Apr 24, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jakirkham jakirkham Apr 24, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. It sounds like cleaning up this function is future work regardless of the way in which the map_overlap concludes.

@mrocklin mrocklin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small comments and questions. Generally this looks good to me.

Comment thread dask/array/routines.py
f.map_overlap(
_gradient_kernel,
dtype=f.dtype,
depth={j: 1 if j == ax else 0 for j in range(f.ndim)},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you might be able to say just depth={ax: 1} here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not sure if that was supported. Though looking at ghost, it does seem to be.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread dask/array/routines.py
f = asarray(f)

if not all([isinstance(e, Number) for e in varargs]):
raise NotImplementedError("Only numeric scalar spacings supported.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is behavior on non-integral spacings defined?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In NumPy Arrays (or non-uniform spacings) are supported. Opted not to do that here. Would rather see users request this functionality first.

@jakirkham jakirkham Apr 25, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...or maybe you were referring to non-discrete spacings, in which case the answer is yes for both (NumPy and our implementation).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add there is a test for non-discrete spacings. :)

Comment thread dask/array/routines.py Outdated
axis = kwargs.pop("axis", None)
if axis is None:
axis = tuple(range(f.ndim))
elif isinstance(axis, Number):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

integral?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.

Comment thread dask/array/routines.py

axis = tuple(ax % f.ndim for ax in axis)

if issubclass(f.dtype.type, (np.bool8, Integral)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.issubdtype ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@mrocklin

mrocklin commented Apr 25, 2018 via email

Copy link
Copy Markdown
Member

Make sure that Dask Array's `gradient` gets comparable answers to
NumPy's `gradient` even across chunks.
@jakirkham

Copy link
Copy Markdown
Member Author

Please let me know if there is anything else.

@mrocklin
mrocklin merged commit 4020cb9 into dask:master Apr 25, 2018
@mrocklin

Copy link
Copy Markdown
Member

This is in. Thanks @jakirkham !

@jakirkham

Copy link
Copy Markdown
Member Author

Thanks for the reviews. :)

@jakirkham
jakirkham deleted the add_np_gradient branch April 25, 2018 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants