-
Notifications
You must be signed in to change notification settings - Fork 221
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
Fix numpy warnings #1473
Fix numpy warnings #1473
Conversation
sin(pi/2) is no longer exactly 1.0 on numpy 1.25 It is still within the guaranteed tolerance
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.
Can you briefly summarise what's going on here? I have to say I don't really understand the changes.
pass | ||
if isinstance(value, str): | ||
raise TypeError # Will be dealt with below | ||
value = np.asanyarray(value).item() |
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.
Not sure I understand these changes, can you summarise?
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.
This change is a slightly more complex issue related to what I wrote in my earlier comment: in future versions, we can no longer use float(x)
as a check that x
is either a scalar number or a length-1 array with a number (and not for example a string) – it will wail on length-1 arrays.
>>> assumptions = {'sin': DEFAULT_FUNCTIONS['sin'].pyfunc, | ||
... 'pi': DEFAULT_CONSTANTS['pi'].value} | ||
>>> assumptions = {'exp': DEFAULT_FUNCTIONS['exp'].pyfunc, | ||
... 'inf': DEFAULT_CONSTANTS['inf'].value} |
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.
Any reason for change from sin to exp etc.? Not that it matters much, just curious.
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.
I briefly explain this in the commit message: our docttest relied on the fact that np.sin(np.pi/2) == 1.0
, which is reasonable but not guaranteed (results are only guaranteed to be < 4 epsilons away from the correct value). With the previous versions it matched 1.0 exactly, but with the newer, faster implementations in numpy
1.25 this is no longer the case (even with older versions, np.sin(0.0)
or np.sin(np.pi)
did not return 0.0). So I replaced it with exp(-inf)
which I think is guaranteed to be exactly zero – at least it works with both numpy 1.24 and numpy 1.25 :)
Sure. The main issue that is adressed here is that in a few places, we used code like this: int(self.variables["N"].get_value()) The result of >>> import numpy as np
>>> np.array(3).item()
3
>>> np.array([3]).item()
3
>>> np.array([3, 4]).item()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: can only convert an array of size 1 to a Python scalar |
@@ -357,7 +357,7 @@ def all_values(self): | |||
"them." | |||
) | |||
indices = self.i[:] | |||
sort_indices = np.argsort(indices) |
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.
BTW: This was always wrong (we need a stable sort here), but it only now got triggered with numpy 1.25 (and only on GitHub, not on my local machine).
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.
OK, all that makes sense - thanks!
This fixes a number of issues that will raise warnings in the forthcoming numpy version (1.25).