Skip to content

BUG: various fixes to np.gradient #9408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 21, 2017
Merged

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Jul 13, 2017

Follows on from @apbard's #8446.

This fixes #8292, and provides a clearer error for #9401, which looks invalid

This allowed some things to happen that shouldn't, and forbid some things from happening that should.

elif n == len_axes or (n == 1 and np.isscalar(varargs[0])):
dx = list(varargs)
dx = [asanyarray(d) for d in varargs]
Copy link
Member Author

Choose a reason for hiding this comment

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

This is fine WRT to array-likes, because np.diff calls asanyarray anyway

2d arrays would work, but in unpredictable and undocumented ways.

This at least makes numpygh-9401 give a better error message.
@@ -1674,6 +1674,7 @@ def gradient(f, *varargs, **kwargs):
S0025-5718-1988-0935077-0/S0025-5718-1988-0935077-0.pdf>`_.
"""
f = np.asanyarray(f)
varargs = [asanyarray(d) for d in varargs]
Copy link
Member Author

Choose a reason for hiding this comment

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

This is fine WRT to array-likes, because np.diff calls asanyarray anyway

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, this doesn't work for scalar duck types - now fixed

continue
elif np.ndim(distances) != 1:
raise ValueError("distances must be either scalars or 1d")
Copy link
Contributor

Choose a reason for hiding this comment

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

right, didn't thought about explicit error message for not 1D arrays

Copy link
Member Author

Choose a reason for hiding this comment

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

I suspect we can do something much more powerful for nd arrays if we think about it (perhaps the grad operator from vector calculus?), but short-term we just want to disable the currently-broken behaviour

Copy link
Contributor

@apbard apbard Jul 13, 2017

Choose a reason for hiding this comment

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

Well, that behaviour was already disabled and the docstring correctly says what is the expected input.
When I firstly implemented this I thought about allowing a list of coordinates instead of individual parameters, but I discarded that because I think it is quite error prone and error checking is more complex. Anyway, if I am not wrong, you cannot pass a "real"-2d matrix unless in the special case where your data have the same dimension along each axis.
So, IMHO, disabling this is not a short term patch but rather the most convenient thing to do.

Copy link
Member Author

Choose a reason for hiding this comment

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

unless in the special case where your data have the same dimension along each axis.

Correct, but this is exactly the case that all our tests use!

Copy link
Contributor

Choose a reason for hiding this comment

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

Allowing instead to compute the gradient with an arbitrary 2d list of coordinates would require a quite different approach and implementation. Anyway, even in this case you would require at maximum a 2d array with shape (num_points, num_dimensions)

Copy link
Contributor

@apbard apbard Jul 13, 2017

Choose a reason for hiding this comment

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

unless in the special case where your data have the same dimension along each axis.

Correct, but this is exactly the case that all our tests use!

I agree that we can add a more general test to check ND-"rectangular" data as well as "squared" ones, but this is quite unrelated to the dimension/type of input coordinates , is it?
What I meant that even in the case we decide to implement this it should not be a 2d-array but a list of 1d-arrays because each would require to have a potentially different size.

Copy link
Member Author

@eric-wieser eric-wieser Jul 13, 2017

Choose a reason for hiding this comment

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

Let's discuss that type of extension elsewhere: #9409

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Jul 13, 2017
@charris
Copy link
Member

charris commented Jul 13, 2017

Added backport-candidate tag in case this fixes some regressions/changed behavior.

@eric-wieser
Copy link
Member Author

eric-wieser commented Jul 13, 2017

@charris: this is another one of those PRs that I rebased, for easy backport via the github ui. Since this fixes a new 1.13 feature, and a regression in 1.12, I think it is a candidate for 1.13.2

@charris charris added this to the 1.13.2 release milestone Jul 14, 2017
@charris
Copy link
Member

charris commented Jul 21, 2017

This was broken by the commit of #9411.

@charris
Copy link
Member

charris commented Jul 21, 2017

Guess that #9411 should be backported if this one is.

@eric-wieser
Copy link
Member Author

Ugh, this was not supposed to conflict with #9411

@eric-wieser
Copy link
Member Author

Only the last (MAINT) commit conflicted, so I've just rolled that back - I can make a separate PR for that after this is backported.

@charris charris merged commit 92d08db into numpy:master Sep 21, 2017
@charris
Copy link
Member

charris commented Sep 21, 2017

Thanks Eric. Want to do the backport?

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Sep 21, 2017
@charris charris removed this from the 1.13.2 release milestone Sep 21, 2017
@eric-wieser
Copy link
Member Author

Guess I need to dig out that MAINT commit from my local history and submit that now too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gradient() no longer accepts scalar-like wrappers for dx
3 participants