Skip to content

Commit

Permalink
NumPy 2.0 also moved function_base
Browse files Browse the repository at this point in the history
Handle warning in test_bounds_gauss2d_lsq
that is flaky and we do not really care for, so just ignore it.
  • Loading branch information
pllim committed Aug 29, 2023
1 parent 05e9be7 commit b6deb07
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
21 changes: 6 additions & 15 deletions astropy/modeling/tests/test_constraints.py
Expand Up @@ -15,7 +15,6 @@
from astropy.modeling.core import Fittable1DModel
from astropy.modeling.parameters import Parameter
from astropy.utils import minversion
from astropy.utils.compat.numpycompat import NUMPY_LT_2_0
from astropy.utils.compat.optional_deps import HAS_SCIPY
from astropy.utils.exceptions import AstropyUserWarning

Expand Down Expand Up @@ -184,6 +183,7 @@ def test_bounds_slsqp(self):
assert intercept + 10**-5 >= bounds["intercept"][0]
assert intercept - 10**-5 <= bounds["intercept"][1]

@pytest.mark.filterwarnings("ignore:The fit may be unsuccessful")
@pytest.mark.parametrize("fitter", fitters)
def test_bounds_gauss2d_lsq(self, fitter):
fitter = fitter()
Expand All @@ -204,21 +204,12 @@ def test_bounds_gauss2d_lsq(self, fitter):
theta=0.5,
bounds=bounds,
)
if isinstance(fitter, (fitting.LevMarLSQFitter, fitting.DogBoxLSQFitter)):
with pytest.warns(AstropyUserWarning, match="The fit may be unsuccessful"):
model = fitter(gauss, X, Y, self.data)
if isinstance(fitter, fitting.TRFLSQFitter):
ctx = np.errstate(invalid="ignore", divide="ignore")
else:
ctx2 = nullcontext()
if isinstance(fitter, fitting.TRFLSQFitter):
ctx = np.errstate(invalid="ignore", divide="ignore")
if not NUMPY_LT_2_0 or not SCIPY_LT_1_11_2:
ctx2 = pytest.warns(
AstropyUserWarning, match="The fit may be unsuccessful"
)
else:
ctx = nullcontext()
with ctx, ctx2:
model = fitter(gauss, X, Y, self.data)
ctx = nullcontext()
with ctx:
model = fitter(gauss, X, Y, self.data)
x_mean = model.x_mean.value
y_mean = model.y_mean.value
x_stddev = model.x_stddev.value
Expand Down
2 changes: 1 addition & 1 deletion astropy/units/quantity_helper/function_helpers.py
Expand Up @@ -442,7 +442,7 @@ def select(condlist, choicelist, default=0):
def piecewise(x, condlist, funclist, *args, **kw):
from astropy.units import Quantity

# Copied implementation from numpy.lib.function_base.piecewise,
# Copied implementation from numpy.lib._function_base_impl.piecewise,
# taking care of units of function outputs.
n2 = len(funclist)
# undocumented: single condition is promoted to a list of one condition
Expand Down
15 changes: 12 additions & 3 deletions astropy/utils/masked/core.py
Expand Up @@ -19,6 +19,7 @@

import numpy as np

from astropy.utils.compat import NUMPY_LT_2_0
from astropy.utils.data_info import ParentDtypeInfo
from astropy.utils.shapes import NDArrayShapeMethods

Expand Down Expand Up @@ -759,9 +760,17 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
else:
# Parse signature with private numpy function. Note it
# cannot handle spaces in tuples, so remove those.
in_sig, out_sig = np.lib.function_base._parse_gufunc_signature(
ufunc.signature.replace(" ", "")
)
if NUMPY_LT_2_0:
in_sig, out_sig = np.lib.function_base._parse_gufunc_signature(
ufunc.signature.replace(" ", "")
)
else:
(

Check warning on line 768 in astropy/utils/masked/core.py

View check run for this annotation

Codecov / codecov/patch

astropy/utils/masked/core.py#L768

Added line #L768 was not covered by tests
in_sig,
out_sig,
) = np.lib._function_base_impl._parse_gufunc_signature(
ufunc.signature.replace(" ", "")
)
axis = kwargs.get("axis", -1)
keepdims = kwargs.get("keepdims", False)
in_masks = []
Expand Down
33 changes: 24 additions & 9 deletions astropy/utils/masked/function_helpers.py
Expand Up @@ -93,7 +93,7 @@
np.isclose, np.allclose, np.flatnonzero, np.argwhere,
# np.core.shape_base
np.atleast_1d, np.atleast_2d, np.atleast_3d, np.stack, np.hstack, np.vstack,
# np.lib.function_base
# np.lib._function_base_impl
np.average, np.diff, np.extract, np.meshgrid, np.trapz, np.gradient,
# np.lib.index_tricks
np.diag_indices_from, np.triu_indices_from, np.tril_indices_from,
Expand All @@ -108,7 +108,7 @@
np.real_if_close, np.common_type,
# np.lib.ufunclike
np.fix, np.isneginf, np.isposinf,
# np.lib.function_base
# np.lib._function_base_impl
np.angle, np.i0,
} # fmt: skip
IGNORED_FUNCTIONS = {
Expand All @@ -121,7 +121,7 @@
IGNORED_FUNCTIONS |= {
np.pad, np.searchsorted, np.digitize,
np.is_busday, np.busday_count, np.busday_offset,
# numpy.lib.function_base
# numpy.lib._function_base_impl
np.cov, np.corrcoef, np.trim_zeros,
# numpy.core.numeric
np.correlate, np.convolve,
Expand Down Expand Up @@ -439,7 +439,7 @@ def msort(a):

@dispatched_function
def sort_complex(a):
# Just a copy of function_base.sort_complex, to avoid the asarray.
# Just a copy of np.lib._function_base_impl.sort_complex, to avoid the asarray.
b = a.copy()
b.sort()
if not issubclass(b.dtype.type, np.complexfloating): # pragma: no cover
Expand Down Expand Up @@ -595,11 +595,15 @@ def median(a, axis=None, out=None, **kwargs):
)
return (r.reshape(k) if keepdims else r) if out is None else out

else:
elif NUMPY_LT_2_0:
return np.lib.function_base._ureduce(
a, func=_masked_median, axis=axis, out=out, **kwargs
)

return np.lib._function_base_impl._ureduce(

Check warning on line 603 in astropy/utils/masked/function_helpers.py

View check run for this annotation

Codecov / codecov/patch

astropy/utils/masked/function_helpers.py#L603

Added line #L603 was not covered by tests
a, func=_masked_median, axis=axis, out=out, **kwargs
)


def _masked_quantile_1d(a, q, **kwargs):
"""
Expand All @@ -608,7 +612,12 @@ def _masked_quantile_1d(a, q, **kwargs):
"""
unmasked = a.unmasked[~a.mask]
if unmasked.size:
result = np.lib.function_base._quantile_unchecked(unmasked, q, **kwargs)
if NUMPY_LT_2_0:
result = np.lib.function_base._quantile_unchecked(unmasked, q, **kwargs)
else:
result = np.lib._function_base_impl._quantile_unchecked(

Check warning on line 618 in astropy/utils/masked/function_helpers.py

View check run for this annotation

Codecov / codecov/patch

astropy/utils/masked/function_helpers.py#L618

Added line #L618 was not covered by tests
unmasked, q, **kwargs
)
return a.from_unmasked(result)
else:
return a.from_unmasked(np.zeros_like(a.unmasked, shape=q.shape), True)
Expand Down Expand Up @@ -641,7 +650,9 @@ def quantile(a, q, axis=None, out=None, **kwargs):

a = Masked(a)
q = np.asanyarray(q)
if not np.lib.function_base._quantile_is_valid(q):
if (NUMPY_LT_2_0 and not np.lib.function_base._quantile_is_valid(q)) or (
not NUMPY_LT_2_0 and not np.lib._function_base_impl._quantile_is_valid(q)
):
raise ValueError("Quantiles must be in the range [0, 1]")

if NUMPY_LT_1_24:
Expand All @@ -650,11 +661,15 @@ def quantile(a, q, axis=None, out=None, **kwargs):
a, func=_masked_quantile, q=q, axis=axis, out=out, **kwargs
)
return (r.reshape(q.shape + k) if keepdims else r) if out is None else out
else:
elif NUMPY_LT_2_0:
return np.lib.function_base._ureduce(
a, func=_masked_quantile, q=q, axis=axis, out=out, **kwargs
)

return np.lib._function_base_impl._ureduce(

Check warning on line 669 in astropy/utils/masked/function_helpers.py

View check run for this annotation

Codecov / codecov/patch

astropy/utils/masked/function_helpers.py#L669

Added line #L669 was not covered by tests
a, func=_masked_quantile, q=q, axis=axis, out=out, **kwargs
)


@dispatched_function
def percentile(a, q, *args, **kwargs):
Expand Down Expand Up @@ -761,7 +776,7 @@ def piecewise(x, condlist, funclist, *args, **kw):
Any masks in ``condlist`` are ignored.
"""
# Copied implementation from numpy.lib.function_base.piecewise,
# Copied implementation from numpy.lib._function_base_impl.piecewise,
# just to ensure output is Masked.
n2 = len(funclist)
# undocumented: single condition is promoted to a list of one condition
Expand Down

0 comments on commit b6deb07

Please sign in to comment.