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 for NumPy 2 and Matplotlib 3.9 #382

Merged
merged 2 commits into from
May 28, 2024

Conversation

MikhailRyazanov
Copy link
Collaborator

NumPy is planning to release v2.0 soon (conda already wanted to rebuild for it, see conda-forge/pyabel-feedstock#27). I've tested locally with numpy 2.0.0rc2 (from PyPI) and found that small modifications are needed to ensure compatibility with it:

  • numpy.trapz() was renamed to numpy.trapezoid(), so we'll need to use the one that is available (numpy.trapezoid() isn't available in current versions, so we can't just switch to it; scipy.integrate.trapezoid() wasn't present in scipy 1.2 — the last available for Python 2).
  • numpy.NaN (used only in one documentation figure) was removed, so numpy.nan should be used instead.

The newest Matplotlib (v3.6.0) also complains about plt.colorbar() called from wrong axes (in another documentation figure), so this is also corrected here.

The rest still looks fine.

I suspect that when numpy 2.0.0 is released, we'll also have to make a minor maintenance release...

@DanHickstein
Copy link
Member

Thanks Mikhail!

I suppose one option would be to switch to scipy.itegrate.simpson and avoid the trapezoidal rule issue altogether. Probably it gives a slightly better result and avoids the extra lines of code?

@MikhailRyazanov
Copy link
Collaborator Author

scipy.integrate.simpson has the same problem with Python 2 (it was called scipy.integrate.simps in SciPy 1.2).

But it also seems to me that the direct method was really designed assuming trapezoidal integration (for the “correction” and the fact that the trapezoidal rule is hard-coded in the Cython implementation). Maybe @rth can tell more about this.

@DanHickstein
Copy link
Member

Oh, yeah, good point. We shouldn't mess with the integration. It's annoying that numpy changed the name somewhat suddenly, requiring that we include extra code to check the numpy version. But, what you have seems like the best solution.

Copy link
Collaborator

@stggh stggh left a comment

Choose a reason for hiding this comment

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

All good for me. Thanks @MikhailRyazanov

BTW, as a separate issue, on my Ubuntu 24.04 (Dell OptiPlex 7070) direct_C quietly does not build. I am not sure how to force the compile.

@@ -5,6 +5,10 @@
from __future__ import unicode_literals

import numpy as np
if hasattr(np, 'trapezoid'): # numpy >= 2
Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with this code. However, IMO support for python2 should be dropped.

Copy link
Member

Choose a reason for hiding this comment

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

This is the numpy version, not python version. In this case, v2 is the upcoming version. (But otherwise I agree with you. No need to support Py2 anymore. It, and my beloved print statement without parentheses, are dead.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Opps! I must be getting old.

beloved print statement without parentheses

It tried to make a come back:
LWN: The (non-)return of the Python print statement

Copy link
Member

Choose a reason for hiding this comment

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

OMG! Guido has returned to bring us back our print statement :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Dropping Py2 is planned (#221), but not for right now. Basically, it will allow simplifying the code (and adding type annotations), but I don't think that just declaring that we don't support Py2 anymore without actually doing any improvements is a good thing. :–)

Copy link
Member

Choose a reason for hiding this comment

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

Agreed!

@@ -5,6 +5,10 @@
from __future__ import unicode_literals

import numpy as np
if hasattr(np, 'trapezoid'): # numpy >= 2
trapezoid = np.trapezoid
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function name change is similar to what occurred for scipy.integrate.simp to simpson. Makes more sense to use the full name.

@MikhailRyazanov MikhailRyazanov merged commit 3799cc7 into PyAbel:master May 28, 2024
2 checks passed
@rth
Copy link
Collaborator

rth commented May 29, 2024

Maybe rth can tell more about this.

Sorry, I have no recollection of any of this anymore.

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 this pull request may close these issues.

None yet

4 participants