Skip to content

Commit

Permalink
build against NPY2 (#3894)
Browse files Browse the repository at this point in the history
* build against NPY2

See https://numpy.org/devdocs/dev/depending_on_numpy.html

* replace np.int_t with np.int64_t

* replace `np.float_` with `np.float64`

* replace simps with simpson

* replace long with np.int64_t

* fix copy=False

* fix simpson

* asarray

Signed-off-by: Jinzhe Zeng <[email protected]>

* RankWarning

Signed-off-by: Jinzhe Zeng <[email protected]>

* pre-commit auto-fixes

* ImportError

Signed-off-by: Jinzhe Zeng <[email protected]>

* replace np.lib.pad with np.pad

Signed-off-by: Jinzhe Zeng <[email protected]>

* enable NPY201

Signed-off-by: Jinzhe Zeng <[email protected]>

* skip several tests related to scipy, chgnet, and phonopy

* pre-commit auto-fixes

* skip more tests

* make warning assertion more robust

Signed-off-by: Jinzhe Zeng <[email protected]>

* pre-commit auto-fixes

* fix test_properties

Signed-off-by: Jinzhe Zeng <[email protected]>

* pre-commit auto-fixes

* Revert "enable NPY201"

This reverts commit dc1d719.

* rename vars for readability

* fix test_get_parchg with comment to explain assert inversion

* don't depend on numpy RC in build-system.requires

* disable assert altogether

* bump optional dep pin abinit = ["netcdf4>=1.7.1"]

temp install delvewheel>=1.7.4 in CI

* merge

* remove delvewheel

---------

Signed-off-by: Jinzhe Zeng <[email protected]>
Co-authored-by: Janosh Riebesell <[email protected]>
  • Loading branch information
njzjz and janosh authored Aug 8, 2024
1 parent a485c55 commit ce360f4
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 143 deletions.
12 changes: 7 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[build-system]
requires = [
"Cython>=0.29.23",
# pin NumPy version used in the build
"oldest-supported-numpy",
# Building against NPY2 will support both NPY1 and NPY2
# https://numpy.org/devdocs/dev/depending_on_numpy.html#build-time-dependency
"numpy>=2.0.1",
"setuptools>=65.0.0",
]
build-backend = "setuptools.build_meta"
Expand Down Expand Up @@ -59,8 +60,8 @@ dependencies = [
"matplotlib>=3.8",
"monty>=2024.7.29",
"networkx>=2.2",
"numpy>=1.25.0 ; platform_system != 'Windows'",
"numpy>=1.25.0,<2.0 ; platform_system == 'Windows'",
# NumPy documentation suggests pinning the current major version as the C API is used
# https://numpy.org/devdocs/dev/depending_on_numpy.html#runtime-dependency-version-ranges
"palettable>=3.3.3",
"pandas>=2",
"plotly>=4.5.0",
Expand All @@ -73,6 +74,7 @@ dependencies = [
"tabulate>=0.9",
"tqdm>=4.60",
"uncertainties>=3.1.4",
'numpy>=1.25.0,<3.0',
]
version = "2024.7.18"

Expand All @@ -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"]
mlp = ["chgnet>=0.3.8", "matgl>=1.1.1"]
electronic_structure = ["fdint>=2.0.2"]
ci = ["pytest-cov>=4", "pytest-split>=0.8", "pytest>=8"]
Expand Down
9 changes: 7 additions & 2 deletions src/pymatgen/analysis/eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import numpy as np
from scipy.optimize import leastsq, minimize

try:
from numpy.exceptions import RankWarning # NPY2
except ImportError:
from numpy import RankWarning # NPY1

from pymatgen.core.units import FloatWithUnit
from pymatgen.util.plotting import add_fig_kwargs, get_ax_fig, pretty_plot

Expand Down Expand Up @@ -420,7 +425,7 @@ def fit(self, min_ndata_factor=3, max_poly_order_factor=5, min_poly_order=2):
min_poly_order (int): minimum order of the polynomial to be
considered for fitting.
"""
warnings.simplefilter("ignore", np.RankWarning)
warnings.simplefilter("ignore", RankWarning)

def get_rms(x, y):
return np.sqrt(np.sum((np.array(x) - np.array(y)) ** 2) / len(x))
Expand Down Expand Up @@ -490,7 +495,7 @@ def get_rms(x, y):
norm += weight
coeffs = np.array(val[0])
# pad the coefficient array with zeros
coeffs = np.lib.pad(coeffs, (0, max(fit_poly_order - len(coeffs), 0)), "constant")
coeffs = np.pad(coeffs, (0, max(fit_poly_order - len(coeffs), 0)), "constant")
weighted_avg_coeffs += weight * coeffs

# normalization
Expand Down
13 changes: 7 additions & 6 deletions src/pymatgen/optimization/linear_assignment.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
self.orig_c = np.asarray(costs, dtype=np.float64, order="C")
self.nx, self.ny = self.orig_c.shape
self.n = self.ny

Expand All @@ -63,7 +64,7 @@ class LinearAssignment:
if self.nx == self.ny:
self.c = self.orig_c
else:
self.c = np.zeros((self.n, self.n), dtype=np.float_)
self.c = np.zeros((self.n, self.n), dtype=np.float64)
self.c[:self.nx] = self.orig_c

# initialize solution vectors
Expand All @@ -76,15 +77,15 @@ class LinearAssignment:

@cython.boundscheck(False)
@cython.wraparound(False)
cdef np.float_t compute(int size, np.float_t[:, :] c, np.int_t[:] x, np.int_t[:] y, np.float_t eps) nogil:
cdef np.float_t compute(int size, np.float_t[:, :] c, np.int64_t[:] x, np.int64_t[:] y, np.float_t eps) nogil:

# augment
cdef int i, j, k, i1, j1, f, f0, cnt, low, up, z, last, nrr
cdef int n = size
cdef bint b
cdef np.int_t * col = <np.int_t *> malloc(n * sizeof(np.int_t))
cdef np.int_t * fre = <np.int_t *> malloc(n * sizeof(np.int_t))
cdef np.int_t * pred = <np.int_t *> malloc(n * sizeof(np.int_t))
cdef np.int64_t * col = <np.int64_t *> malloc(n * sizeof(np.int64_t))
cdef np.int64_t * fre = <np.int64_t *> malloc(n * sizeof(np.int64_t))
cdef np.int64_t * pred = <np.int64_t *> malloc(n * sizeof(np.int64_t))
cdef np.float_t * v = <np.float_t *> malloc(n * sizeof(np.float_t))
cdef np.float_t * d = <np.float_t *> malloc(n * sizeof(np.float_t))
cdef np.float_t h, m, u1, u2, cost
Expand Down
Loading

0 comments on commit ce360f4

Please sign in to comment.