Skip to content
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

Calling the ufunc in __array_wrap__ is probably a bad idea #424

Closed
eric-wieser opened this issue Aug 17, 2016 · 5 comments · Fixed by #905
Closed

Calling the ufunc in __array_wrap__ is probably a bad idea #424

eric-wieser opened this issue Aug 17, 2016 · 5 comments · Fixed by #905

Comments

@eric-wieser
Copy link
Contributor

Since by that point, the ufunc has already run once. Although putting it in __array_prepare__ may also not solve the problem, since that:

__array_prepare__ should not attempt to access the array data or resize the array, it is intended for setting the output array type, updating attributes and metadata, and performing any checks based on the input that may be desired before computation begins.

@eric-wieser eric-wieser changed the title Calling the ufunc in wrap is probably a bad idea Calling the ufunc in __array_wrap__ is probably a bad idea Aug 17, 2016
@hgrecco
Copy link
Owner

hgrecco commented Sep 13, 2016

I agree but there is no other way, as far as I now. I discuss this in the past with the numpy devs but there was no big interest to do provide a way to circunvent it. Recently the has been a discussion to add a new way to do this. It is still unstable.

@eric-wieser
Copy link
Contributor Author

eric-wieser commented Sep 13, 2016

Here's a possible workaround:

  • __array_prepare__:
    • check unit compatibility, raising exceptions if needed
    • convert arguments in args in-place to the same units (using .ito, perhaps). Store the old units and magnitudes
  • __array_wrap__
    • restore the magnitude/units of args back to what they were

Yes, this violates the "attempt[ing] to access array data" rule, but calling the ufunc within __array_wrap__ is probably not really allowed either.


Could you link me to that discussion?

@hgrecco
Copy link
Owner

hgrecco commented Nov 17, 2016

Take a look at numpy/numpy#4072

@eric-wieser
Copy link
Contributor Author

eric-wieser commented Nov 17, 2016

Thanks, although that doesn't seem all that relevant to this discussion - the issue here is that you're not able to override ufunc behaviour at all, derivative of np.array or otherwise.

Also, there's some work in the numpy development pipeline to fix this with __numpy_ufunc__/__array_ufunc__ - presumably this is the discussion you were referring to?

What are your thoughts on my proposed workaround above?

@hgrecco
Copy link
Owner

hgrecco commented Nov 18, 2016

Your workaround looks ok in theory. If you like, prototype it and we test it.

However, at the end the numpy devs need to provide a good way to do this.

By the way, have you seen #326 ?

@bors bors bot closed this as completed in 43fbae2 Dec 11, 2019
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 a pull request may close this issue.

2 participants