Skip to content

Commit

Permalink
Fixes for numpy 2 (#124)
Browse files Browse the repository at this point in the history
* np.float_ -> float

* Build with numpy 2+

* Try supporting python 3.8
  • Loading branch information
ixjlyons authored Jun 17, 2024
1 parent c8ff0c0 commit d1f06e9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
8 changes: 4 additions & 4 deletions ndsplines/_npy_bspl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def find_interval(t, k, xvals, extrapolate=False, workspace=None):
Parameters
----------
t : ndarray, shape=(n+k+1,) dtype=np.float_
t : ndarray, shape=(n+k+1,) dtype=float
knots
k : int
degree of B-spline
xvals : ndarray, shape=(s,) dtype=np.float_
xvals : ndarray, shape=(s,) dtype=float
values to find the interval for
extrapolate : bool, optional
whether to return the last or the first interval if xval
Expand Down Expand Up @@ -105,14 +105,14 @@ def evaluate_spline(t, k, xvals, nu, extrapolate,

basis_workspace = basis_workspace.T
if (not isinstance(basis_workspace, np.ndarray) or
(basis_workspace.dtype != np.float_) or
(basis_workspace.dtype != float) or
(basis_workspace.shape[0] < 2*k+2) or
(basis_workspace.shape[1] < s)):
raise ValueError("basis_workspace has invalid shape or dtype")

u = basis_workspace[:k+1,:s]
w = basis_workspace[k+1:2*k+2,:s]
bounds = np.empty((2,s), dtype=np.float_)
bounds = np.empty((2,s), dtype=float)

u[0,...] = 1.0
for j in range(1, k-nu+1):
Expand Down
22 changes: 11 additions & 11 deletions ndsplines/ndsplines.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class NDSpline(object):
Parameters
----------
knots : list of ndarrays, shapes=[n_0+degrees[i]+1, ..., n_ndim+degrees[-1]+1], dtype=np.float_
knots : list of ndarrays, shapes=[n_0+degrees[i]+1, ..., n_ndim+degrees[-1]+1], dtype=float
List of knots in each dimension, :math:`[t_1, t_2, \ldots, t_N]`.
coefficients : ndarray, shape=(n_1, n_2, ..., n_xdim) + yshape, dtype=np.float_
coefficients : ndarray, shape=(n_1, n_2, ..., n_xdim) + yshape, dtype=float
N-D array of coefficients, :math:`c_{i_1, ..., i_N}`.
degrees : ndarray, shape=(xdim,), dtype=np.int_
Array of the degree of each dimension, :math:`[k_1, k_2, \ldots, k_N]`.
Expand All @@ -39,11 +39,11 @@ class NDSpline(object):
Attributes
----------
knots : list of ndarrays, shapes=[n_0+degrees[i]+1, ..., n_ndim+degrees[-1]+1], dtype=np.float_
knots : list of ndarrays, shapes=[n_0+degrees[i]+1, ..., n_ndim+degrees[-1]+1], dtype=float
List of knots in each dimension, :math:`[t_1, t_2, \ldots, t_N]`.
xdim : int
Dimension of spline input space.
coefficients : ndarray, shape=(n_1, n_2, ..., n_xdim, ydim), dtype=np.float_
coefficients : ndarray, shape=(n_1, n_2, ..., n_xdim, ydim), dtype=float
N-D array of coefficients, :math:`c_{i_1, ..., i_N}`.
ydim : int
Dimension of spline output space.
Expand Down Expand Up @@ -123,7 +123,7 @@ def allocate_workspace_arrays(self, num_points):
self.xdim,
self.current_max_num_points,
2*self.max_degree+2,
), dtype=np.float_)
), dtype=float)
self.interval_workspace = np.empty((self.xdim, self.current_max_num_points, ), dtype=np.intc)
self.coefficient_selector = np.empty((self.current_max_num_points,) + self.coefficient_shape_base, dtype=np.intc)

Expand All @@ -135,7 +135,7 @@ def compute_basis_coefficient_selector(self, x, nus=0):
Parameters
----------
x : ndarray, shape=(s, self.xdim,) dtype=np.float_
x : ndarray, shape=(s, self.xdim,) dtype=float
Points at which to evaluate the spline.
nus : int or ndarray, shape=(self.xdim,) dtype=np.int_
Order of derivatives for each dimension to evaluate. Optional,
Expand Down Expand Up @@ -185,15 +185,15 @@ def __call__(self, x, nus=0):
Parameters
----------
x : ndarray, shape=(..., self.xdim) dtype=np.float_
x : ndarray, shape=(..., self.xdim) dtype=float
Points at which to evaluate the spline.
nus : ndarray, broadcastable to shape=(self.xdim,) dtype=np.int_
Order of derivatives for each dimension to evaluate. Optional,
default is 0.
Returns
-------
y : ndarray, shape=(..., self.yshape) dtype=np.float_
y : ndarray, shape=(..., self.yshape) dtype=float
Value of N-dimensional B-spline at x.
"""
Expand All @@ -209,7 +209,7 @@ def __call__(self, x, nus=0):

x_shape, x_ndim = x.shape, x.ndim
# need to double transpose so slices of all `i`th dim coords are c-contiguous
x = np.ascontiguousarray(x.reshape((-1, self.xdim)).T, dtype=np.float_).T
x = np.ascontiguousarray(x.reshape((-1, self.xdim)).T, dtype=float).T
num_points = x.shape[0]

if isinstance(nus, np.ndarray):
Expand Down Expand Up @@ -515,7 +515,7 @@ def _not_a_knot(x, k, left=True, right=True):
Parameters
----------
x : ndarray, shape=(n,), dtype=np.float_
x : ndarray, shape=(n,), dtype=float
Knot array to perform the not-a-knot procedure on
k : int
Degree of desired spline
Expand Down Expand Up @@ -699,7 +699,7 @@ def make_interp_spline(x, y, degrees=3, bcs=(-1,0)):

# set up the LHS: the collocation matrix + derivatives at boundaries
kl = ku = k
ab = np.zeros((2*kl + ku + 1, nt), dtype=np.float_, order='F')
ab = np.zeros((2*kl + ku + 1, nt), dtype=float, order='F')
_sci_bspl._colloc(x_slice, t, k, ab, offset=nleft)
if nleft > 0:
_sci_bspl._handle_lhs_derivatives(t, k, x_slice[0], ab, kl, ku, deriv_l_ords)
Expand Down
10 changes: 7 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ classifiers=[
]
requires-python = ">=3.8"

dependencies = ["numpy", "scipy"]
dependencies = [
"numpy<2; python_version<='3.8'",
"numpy; python_version>'3.8'",
"scipy",
]

[project.optional-dependencies]
test = ["pytest", "pandas"]
Expand All @@ -34,8 +38,8 @@ requires = [
"setuptools>=45",
"setuptools_scm[toml]",
"cython",
"oldest-supported-numpy; python_version<'3.12'",
"numpy>=1.26.0; python_version>='3.12'",
"oldest-supported-numpy; python_version<='3.8'",
"numpy>=2; python_version>'3.8'",
]

[tool.setuptools]
Expand Down

0 comments on commit d1f06e9

Please sign in to comment.