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

build against NPY2 #3894

Merged
merged 30 commits into from
Aug 8, 2024
Merged

build against NPY2 #3894

merged 30 commits into from
Aug 8, 2024

Conversation

njzjz
Copy link
Contributor

@njzjz njzjz commented Jun 25, 2024

Summary

Major changes:

  • fix 1: Build against NPY2 instead of NPY1. This will support both NPY1 and NPY2. See https://numpy.org/devdocs/dev/depending_on_numpy.html#numpy-2-0-specific-advice.
  • fix 2: Replace np.int_t (missing in NPY2) with np.int64_t and np.float_ with np.float64.
  • fix 3: Replace long with np.int64_t, as "The default integer type on Windows is now int64 rather than
    int32, matching the behavior on other platforms" per the change log of NPY2.

Todos

If this is work in progress, what else needs to be done?

  • feature 2: ...
  • fix 2:

Checklist

  • Google format doc strings added. Check with ruff.
  • Type annotations included. Check with mypy.
  • Tests added for new features/fixes.
  • If applicable, new classes/functions/modules have duecredit @due.dcite decorators to reference relevant papers by DOI (example)

Tip: Install pre-commit hooks to auto-check types and linting before every commit:

pip install -U pre-commit
pre-commit install

@njzjz njzjz requested review from shyuep, janosh and mkhorton as code owners June 25, 2024 20:31
@njzjz njzjz marked this pull request as draft June 25, 2024 20:36
@njzjz
Copy link
Contributor Author

njzjz commented Jun 25, 2024

Unluckily, there are still several errors. Some are related to the upstream chgnet and scipy and cannot be fixed here.

@janosh janosh force-pushed the master branch 2 times, most recently from e3fbc67 to 41e6d99 Compare August 3, 2024 19:01
@janosh
Copy link
Member

janosh commented Aug 5, 2024

@njzjz sorry for the lack of reply here! looks like this is almost finished. thanks for the effort! feel free to skip the failing chgnet tests for now. re scipy, we'll have to pin to <1.14 as the newest release dropped Python 3.9 support. but 1.13.0 already supports numpy v2

@janosh janosh added compatability Concerning pymatgen compatibility with different OS, Python versions, numpy versions, etc. pkg Package health and distribution related stuff labels Aug 5, 2024
@DanielYang59
Copy link
Contributor

DanielYang59 commented Aug 5, 2024

I really appreciate the pioneering work @njzjz! Meanwhile in case anyone missed this, ruff now has a dedicated rule NPY201 to handle API migration automatically :)

ruff check path/to/code/ --select NPY201

@njzjz njzjz marked this pull request as ready for review August 5, 2024 19:42
@njzjz
Copy link
Contributor Author

njzjz commented Aug 5, 2024

For cross reference, I've submitted the SciPy issue to scipy/scipy#21052

@njzjz
Copy link
Contributor Author

njzjz commented Aug 5, 2024

I do not know how to fix the rest of the errors and need help.

@janosh
Copy link
Member

janosh commented Aug 5, 2024

@njzjz feel free to skip the two failing chgnet tests. we'll migrate it to numpy v2 once pymatgen has. for test_delta_func, you can conditionally skip it like this:

@pytest.mark.skipif(sys.platform == "win32" and np.__version__ > "2.0.0", reason="Fails on Windows with numpy > 2.0.0, awaiting https://github.com/scipy/scipy/issues/21052 resolution")
def test_delta_func():
    x = np.array([0, 1, 2, 3, 4, 5])

not sure about TestWavecar.test_get_parchg. that might need a closer look. maybe @esoteric-ephemera has some ideas on what's going wrong?

Copy link
Member

@janosh janosh left a comment

Choose a reason for hiding this comment

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

thanks a lot @njzjz for this work and esp. for reporting multiple upstream issues! 🥇 and thanks @DanielYang59 for supporting! good team effort to get the many failing tests here sorted out.

@DanielYang59
Copy link
Contributor

The failing tests for tests/io/abinit/test_abiobjects.py looks like another randomly failing test. I cannot recreate it on my Windows PC.

@@ -50,7 +50,8 @@ class LinearAssignment:
"""

def __init__(self, costs, epsilon=1e-13):
self.orig_c = np.array(costs, dtype=np.float_, copy=False, order="C")
# https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword
Copy link
Contributor

Choose a reason for hiding this comment

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

@janosh This doesn't look like something we should keep inside the code (it's pretty easy to find)? What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

i'm generally in favor of linking relevant docs. even if easy to find, people might not think to look for them. but no strong opinion in this case

Copy link
Contributor

Choose a reason for hiding this comment

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

This one looks more like a "disposable" migration guide which we would only do once :)

@janosh
Copy link
Member

janosh commented Aug 6, 2024

The failing tests for tests/io/abinit/test_abiobjects.py looks like another randomly failing test. I cannot recreate it on my Windows PC.

probably something to report to https://github.com/Unidata/netcdf4-python, even if just to say the error message is self-contradictory...

E OSError: Error loading charset-233f44715e5aacaf3a688c2faff5ddf7.dll; The operation completed successfully.

@DanielYang59
Copy link
Contributor

The failing tests for tests/io/abinit/test_abiobjects.py looks like another randomly failing test. I cannot recreate it on my Windows PC.

probably something to report to https://github.com/Unidata/netcdf4-python, even if just to say the error message is self-contradictory...

Agreed, just for my own record, it failed in another PR randomly https://github.com/materialsproject/pymatgen/actions/runs/10268736299/job/28412461926?pr=3892

@njzjz
Copy link
Contributor Author

njzjz commented Aug 6, 2024

E OSError: Error loading charset-233f44715e5aacaf3a688c2faff5ddf7.dll; The operation completed successfully.

This part of the code is generated by delvewheel here. I think I can report there.

According to the Windows documentation, this error message means no error. So it may be incorrect to throw an error (while it is also strange that LoadLibraryExW returns nothing but gives ERROR_SUCCESS).

ERROR_SUCCESS
0 (0x0)
The operation completed successfully.

See also https://stackoverflow.com/questions/7524142/what-does-windows-error-0-error-success-mean

@njzjz
Copy link
Contributor Author

njzjz commented Aug 6, 2024

E OSError: Error loading charset-233f44715e5aacaf3a688c2faff5ddf7.dll; The operation completed successfully.

Submitted to adang1345/delvewheel#51

@janosh
Copy link
Member

janosh commented Aug 8, 2024

@njzjz can you pin pyproject to the fixed delvewheel>=1.7.4

janosh and others added 2 commits August 8, 2024 09:16
auto-merge was automatically disabled August 8, 2024 18:05

Head branch was pushed to by a user without write access

@njzjz
Copy link
Contributor Author

njzjz commented Aug 8, 2024

@njzjz can you pin pyproject to the fixed delvewheel>=1.7.4

This may not work, as delvewheel is a build-time dependency, not a runtime dependency.

@janosh
Copy link
Member

janosh commented Aug 8, 2024

This may not work, as delvewheel is a build-time dependency, not a runtime dependency.

ah, didn't realize that. so the place to pin that would be here

[build-system]
requires = [
"Cython>=0.29.23",
# Building against NPY2 will support both NPY1 and NPY2

but looks like we're in the green anyway so would be good to remove the temp install command in test.yml

@janosh
Copy link
Member

janosh commented Aug 8, 2024

thanks again @njzjz! 👍

@janosh janosh merged commit ce360f4 into materialsproject:master Aug 8, 2024
33 checks passed
@DanielYang59
Copy link
Contributor

Just recorded another failure in https://github.com/materialsproject/pymatgen/actions/runs/10319015504/job/28566565139:

OSError: Error loading libxml2-14375db763c77d38e40bbe5c028add84.dll; The operation completed successfully.

@@ -88,7 +90,7 @@ ase = ["ase>=3.23.0"]
# don't depend on tblite above 3.11 since unsupported https://github.com/tblite/tblite/issues/175
tblite = ["tblite[ase]>=0.3.0; python_version<'3.12'"]
vis = ["vtk>=6.0.0"]
abinit = ["netcdf4>=1.6.5"]
abinit = ["netcdf4>=1.7.1"]
Copy link
Contributor

Choose a reason for hiding this comment

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

@janosh Janosh can I have a quick comment on this bump of netcdf4 version here from commit 1dfc9e4?

It seems to cause CI failure (more details on #4128 (comment)) in Ubuntu CI runner for netcdf4>=1.7.1.post1 (1.7.1 is yanked), 1.6.5 seems to work though

I also tried to install delvewheel but doesn't seem to help

There seem to be a lot of discussion around this error but didn't see a solid solution yet. And the issue for me is that I cannot recreate this with my local Ubuntu machine (with exactly the same dependency versions).

Copy link
Member

Choose a reason for hiding this comment

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

netcdf needed to be bumped in #3894 to fix the following CI errors:

_____________ ERROR collecting tests/io/abinit/test_abiobjects.py _____________
tests\io\abinit\test_abiobjects.py:10: in <module>
    from pymatgen.io.abinit.abiobjects import (
src\pymatgen\io\abinit\__init__.py:5: in <module>
    from .netcdf import (
src\pymatgen\io\abinit\netcdf.py:25: in <module>
    import netCDF4
C:\Users\runneradmin\micromamba\envs\pmg\lib\site-packages\netCDF4\__init__.py:28: in <module>
    _delvewheel_patch_1_7_0()
C:\Users\runneradmin\micromamba\envs\pmg\lib\site-packages\netCDF4\__init__.py:25: in _delvewheel_patch_1_7_0
    raise OSError('Error loading {}; {}'.format(lib, ctypes.FormatError(ctypes.get_last_error())))
E   OSError: Error loading charset-[23](https://github.com/materialsproject/pymatgen/actions/runs/10265440264/job/28401647005#step:8:24)3f44715e5aacaf3a688c2faff5ddf7.dll; The operation completed successfully.

if it's causing problems again and this time downgrading helps, by all means let's do it :)

Copy link
Contributor

@DanielYang59 DanielYang59 Oct 23, 2024

Choose a reason for hiding this comment

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

Current pymatgen dependency declare has duplicate (would be fixed #4128), you must have been mislead too :)

The netcdf4 bumped is not really the one installed in CI (the one in optional section is the one actually installed in CI, surely you know):

- os: ubuntu-latest
python: "3.12"
resolution: lowest-direct
extras: ci,optional

abinit = ["netcdf4>=1.7.1"]

pymatgen/pyproject.toml

Lines 100 to 113 in 3ee17e2

optional = [
"ase>=3.23.0",
"beautifulsoup4",
# BoltzTraP2 build fails on Windows GitHub runners
"BoltzTraP2>=24.9.4 ; platform_system != 'Windows'",
"chemview>=0.6",
"chgnet>=0.3.8",
"f90nml>=1.1.2",
"galore>=0.6.1",
"h5py>=3.11.0",
"jarvis-tools>=2020.7.14",
"matgl>=1.1.3",
"matplotlib>=3.8",
"netCDF4>=1.6.5",

So it still installed: + netcdf4==1.6.5


I looked into the error log again, and turns out it's + netcdf4==1.7.1.post1 causing the following error:

 ______________ ERROR collecting tests/io/abinit/test_pseudos.py _______________
tests\io\abinit\test_pseudos.py:11: in <module>
    from pymatgen.io.abinit.pseudos import Pseudo, PseudoTable
src\pymatgen\io\abinit\__init__.py:5: in <module>
    from .netcdf import (
src\pymatgen\io\abinit\netcdf.py:25: in <module>
    import netCDF4
C:\Users\runneradmin\micromamba\envs\pmg\lib\site-packages\netCDF4\__init__.py:28: in <module>
    _delvewheel_patch_1_7_0()
C:\Users\runneradmin\micromamba\envs\pmg\lib\site-packages\netCDF4\__init__.py:25: in _delvewheel_patch_1_7_0
    raise OSError('Error loading {}; {}'.format(lib, ctypes.FormatError(ctypes.get_last_error())))
E   OSError: Error loading charset-233f44715e5aacaf3a688c2faff5ddf7.dll; The operation completed successfully.

So I might revert that pin to <1.7.1.post1 and (1.7.1.post2 also triggers error), same for latest 1.7.2

It's a huge headache because I cannot recreate these errors locally, which make things very hard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatability Concerning pymatgen compatibility with different OS, Python versions, numpy versions, etc. pkg Package health and distribution related stuff
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants