-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Hide numpy warnings for initial operations on unscaled quantity values #5153
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
|
Because of the hacky way I use the |
|
I should also say that an alternate to this is to add an issue to Known Issues and wait for numpy 1.12 |
|
This look OK, though I'm slightly wary about about adding execution time. Could you do a few quick timing checks (on single values or small arrays, where it would matter most, and for simple ufuncs like addition). If speed is an issue, we could make a specific list of |
|
A propos ensuring it only gets done when needed: the very simplest optimization is to turn on the warnings filter only inside the |
|
@mhvk For this type of warning, I think this can only happen in inverse trig functions: arcsin, arccos, arccosh, arctanh -- so how about: if any(converters):
...
if function in [np.arcsin, np.arccos, np.arcosh, np.arctanh]:
# filter warning |
|
Yes, I like the explicitness. In order to make it even faster it would be good to define the list of functions at the top as a |
c2bdeaf to
95bf14e
Compare
|
@mhvk OK, good idea! |
astropy/units/quantity.py
Outdated
| return result | ||
|
|
||
| def __array_wrap__(self, obj, context=None): | ||
| if hasattr(self, '_catch_warnings'): |
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.
Should have seen this before: just so the no-conversion case doesn't get slowed down, can you move this to l.504 (i.e., within the if hasattr(obj, '_converters') clause)...
|
This looks good modulo the small movement requested above. Also, a changelog entry... I think this is properly classified as a bug, so it would be in the 1.2.2 section. And we might as well have a test case to ensure this doesn't come back: maybe simply the one from #3240 (maybe after |
|
@mhvk Thanks for the comments! |
|
Looks good, merging! Thanks, @adrn! |
Hide numpy warnings for initial operations on unscaled quantity values
This is an attempt to fix #3240.
Summarizing my understanding of the problem: numpy ufuncs will first operate directly on a quantity's value before it is scaled properly. This can cause a warning for things that are valid operations. For example, arcsin of the quantity
15 pc / kpc-- arcsin(15) will throw a warning, but after scaling, arcsin(0.015) is fine. This PR silences warnings like this by utilizing thewarnings.catch_warningscontext manager to silence numpy Runtime warnings in between__array_prepare__and__array_wrap__. (I use the context manager to do this because it seems that temporarily catching a warning and then returning to the previous state is actually a nontrivial task, e.g., http://stackoverflow.com/questions/2390766/how-do-i-disable-and-then-re-enable-a-warning)cc @mhvk
Closes #3240