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

add numpy 2 support #340

Merged
merged 36 commits into from
Apr 16, 2024
Merged

add numpy 2 support #340

merged 36 commits into from
Apr 16, 2024

Conversation

MuellerSeb
Copy link
Member

@MuellerSeb MuellerSeb commented Mar 14, 2024

Closes: #337 #345

Numpy 2.0.0rc1 was released: https://pypi.org/project/numpy/2.0.0c1 and is officially usable to build extensions.

  • numpy 2.0.0rc1 for building extensions (for Python 3.9 and above)
  • check multiple numpy and scipy versions in CI
  • fixed minimal versions for numpy
  • use np.asarray everywhere with np.atleast_(n)d
  • fix long/longlong integer issue in cython on windows by always using 64bit integers

Additional doc fixes

  • build docs with latest sphinx version
  • require pyvista 0.40 at least

32bit builds were removed with this PR. I think we can happily drop that.

@MuellerSeb
Copy link
Member Author

MuellerSeb commented Apr 4, 2024

  • Building with numpy 2 works 🎉
  • Running tests with numpy 2 fails 😭

A lot of the tests fail because of emcee and this issue: dfm/emcee#509

Other errors have a problem with these lines in estimator.pyx:

    cdef long[:, :] counts = np.zeros((d_max, len(bin_edges)-1), dtype=long)
    cdef long[:] counts = np.zeros(len(bin_edges)-1, dtype=long)
    cdef long[:] counts = np.zeros(k_max, dtype=long)
    cdef long[:] counts = np.zeros(k_max, dtype=long)

and raise

E   ValueError: Buffer dtype mismatch, expected 'long' but got 'long long'

This only occurs on windows and is related to this: https://numpy.org/devdocs/numpy_2_0_migration_guide.html#windows-default-integer

One other category of errors is:

If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).

np.array(..., copy=False) is used in krige/base.py, 3 times in variogram/variogram.py and 3 times in tools/geometric.py

In order to mimic the ndmin=... feature, we could replace these with e.g.:

pos = np.atleast_2d(np.asarray(pos, dtype=np.double))

@MuellerSeb
Copy link
Member Author

All issues fixed. Only remaining issues should be solved upstream in emcee with: dfm/emcee#510

@MuellerSeb MuellerSeb changed the title [WIP] Try out numpy 2 beta release [WIP] Try numpy 2 Apr 5, 2024
@MuellerSeb MuellerSeb changed the title [WIP] Try numpy 2 add numpy 2 support Apr 8, 2024
@MuellerSeb MuellerSeb self-assigned this Apr 8, 2024
@MuellerSeb MuellerSeb added enhancement New feature or request Refactoring Code-Refactoring needed here Documentation labels Apr 8, 2024
@MuellerSeb MuellerSeb added this to the 1.5.2 milestone Apr 8, 2024
@MuellerSeb MuellerSeb linked an issue Apr 8, 2024 that may be closed by this pull request
Copy link
Member

@LSchueler LSchueler left a comment

Choose a reason for hiding this comment

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

Awesome, thanks for this! I guess the most annoying work was the CI stuff?

I have just added a few questions / comments to the code.

Comment on lines -85 to +96
name: sdist on ${{ matrix.os }} with py ${{ matrix.python-version }}
name: sdist on ${{ matrix.os }} with py ${{ matrix.ver.py }} numpy${{ matrix.ver.np }} scipy${{ matrix.ver.sp }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

# https://github.com/scipy/oldest-supported-numpy/blob/main/setup.cfg
ver:
- {py: '3.8', np: '==1.20.0', sp: '==1.5.4'}
- {py: '3.9', np: '==1.20.0', sp: '==1.5.4'}
- {py: '3.10', np: '==1.21.6', sp: '==1.7.2'}
- {py: '3.11', np: '==1.23.2', sp: '==1.9.2'}
- {py: '3.12', np: '==1.26.2', sp: '==1.11.2'}
- {py: '3.12', np: '>=2.0.0rc1', sp: '>=1.13.0'}
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 awful! But I guess there is no other way until py 3.11 is deprecated?

Copy link
Member Author

Choose a reason for hiding this comment

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

What exactly is awful? I think it is pretty neat to test multiple numpy/scipy versions to fix stuff like #246 faster in the future.

Copy link
Member

Choose a reason for hiding this comment

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

It's just a lot of manual (and typo prone?) work. Just imagine we have to add another few packages to that version matrix.

And it also makes it more work to locally reproduce specific setups of users.

Copy link
Member Author

Choose a reason for hiding this comment

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

If a new Python version is release we have to add it by hand either way. And then I would just add the first version of numpy and scipy that support that new python version. That is how I selected the numpy and scipy versions for the python version together with a bleeding edge job for the latest versions.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would like to keep it like this for now. If we encounter version incompatibilities in the future, we can rethink this.

pyproject.toml Outdated Show resolved Hide resolved
src/gstools/variogram/estimator.pyx Outdated Show resolved Hide resolved
Copy link
Member

@LSchueler LSchueler left a comment

Choose a reason for hiding this comment

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

It'd be funny to see the number of changed files across open source Python projects after changes in the black formats :-)

LGTM!

@MuellerSeb MuellerSeb merged commit 2db5c0c into main Apr 16, 2024
27 checks passed
@MuellerSeb MuellerSeb deleted the test_numpy2 branch April 16, 2024 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation enhancement New feature or request Refactoring Code-Refactoring needed here
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Numpy 2 compatibility Examples failing with AttributeError
2 participants