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

Updates to be compatible with numpy 2.0 #1297

Merged
merged 7 commits into from
Jun 6, 2024
Merged

Updates to be compatible with numpy 2.0 #1297

merged 7 commits into from
Jun 6, 2024

Conversation

rmjarvis
Copy link
Member

@rmjarvis rmjarvis commented Jun 5, 2024

Numpy made a few changes to their API with version 2.0. This PR addresses the places where they impact GalSim code.

  1. np.array(a, copy=False) is now an error if a copy is required for the dtype. Using copy=None now does the equivalent to what copy=False used to do. But in most cases, it's better to just use np.asarray(a), which is equivalent in both versions.
  2. There is one case where we have a more complicated decision about whether to copy, so in that case we need to use an explicit version check to get the right kwarg.
  3. They changed the implicit dtype conversion for values in e.g. += and similar operations. For GalSim purposes, this mostly just required changing test expectations. E.g. using Image(..., dtype=float) when we want to test accuracy at double precision.
  4. One other gotcha is that Python int cannot be implicitly assigned to np.uint types. This feels like a bug to me, since np.int types do work. Just not a regular Python int. cf. numpy issue BUG: Array with uint dtype cannot be multplied by negative Python int. numpy/numpy#26483 However, it is likely that in most cases where this would show up in user code, it is likely some kind of user error. So I think it's best to not work around it, but rather just let the exception be raised. We explicitly avoid it by using np.int types in tests where it would otherwise be a problem.
  5. Related to the above, Image.__neg__ now uses np.int64(-1), rather than just -1, so it will continue to work the same way it used to for unsigned dtypes.
  6. Finally, np.trapz is deprecated. The new name is np.trapezoid. We mostly don't use it, since our own implementation is faster, but there were a few places in the tests that needed to be updated.

@rmjarvis rmjarvis added this to the v2.5 milestone Jun 5, 2024
@rmjarvis rmjarvis added build Related to compiling, building, installing exceptions/warnings Related to exceptions or warnings labels Jun 5, 2024
@rmjarvis rmjarvis requested review from beckermr June 6, 2024 02:53
Copy link
Contributor

@beckermr beckermr left a comment

Choose a reason for hiding this comment

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

Thanks for the hard work!

@rmjarvis rmjarvis merged commit 733d808 into releases/2.5 Jun 6, 2024
10 checks passed
@rmjarvis rmjarvis deleted the numpy2 branch June 6, 2024 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Related to compiling, building, installing exceptions/warnings Related to exceptions or warnings
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants