-
-
Notifications
You must be signed in to change notification settings - Fork 11.9k
API: Allow SciPy to get away with assuming trapz is a Python function
#23034
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
Conversation
7ba4883 to
3dcb7a5
Compare
numpy/lib/function_base.py
Outdated
| @functools.wraps(_trapz_orig) | ||
| def trapz(y, x=None, dx=1.0, axis=-1): | ||
| # Hack so that SciPy has some python code it can steal | ||
| return _trapz_orig(y, x=x, dx=dx, axis=axis) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this means we always pass defaults in __array_function__ (which is probably OK). We can also go back to *args, **kwargs. Or, I can just override all the attributes used by SciPy (__code__, and a couple of others).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finding @dhomeier comment at astropy:
E astropy.utils.exceptions.AstropyWarning: function 'trapz' is not known to astropy's Quantity. Will run it anyway, hoping it will treat ndarray subclasses correctly. Please raise an issue at https://github.com/astropy/astropy/issues.The issue seems to be that with the proposed fix np.trapz identifies as _trapz_orig, which is not in function_helpers.SUBCLASS_SAFE_FUNCTIONS.
Wrapping things up seems to confuse astropy a bit (the function is just an entry-point).
So I will fix this up to just steal all the attributes so that pretending that this is a Python function works (what SciPy does) to allow creating a new Python function from the non-python version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @seberg, this version (fbb915f) resolves astropy/astropy#14287 out of the box!
mattip
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. NIT: Should there be a test somewhere? Maybe not since this is a SciPy support shim that should go away in a few years.
Dunno, if its worthwhile, but seemed easier to add one than ponder whether or not :) |
fbb915f to
eff8a9b
Compare
This wraps `trapz` into a proper python function and then copies all attributes expected on a Python function over from the "fake" version to the real one. This allows SciPy to pretend `trapz` is a Python function to create their own version.
rgommers
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works like a charm, so LGTM too. Thanks @seberg!
|
thank you @seberg ! |
This wraps
trapzinto a proper python function, I could also just copy over all the relevant attributes. In case we change things so thattrapzis a Python function anyway, the assert would fail.EDIT: Changed the approach to just copy over the stuff that SciPy needs to "clone" the function.