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

Support NumPy 2 #977

Merged
merged 12 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,17 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
arch: [auto, aarch64, universal2]
os: [ubuntu-latest, windows-latest, macos-13, macos-14]
Copy link
Member Author

Choose a reason for hiding this comment

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

MacOS 14 is the new ARM runner. So this builds native wheels on both (the ARM part can now be tested! Except for CPython 3.8, which is wonky)

arch: [auto, aarch64]
py: [cp38, cp39, cp310, cp311, cp312]
exclude:
- os: windows-latest
arch: aarch64
- os: windows-latest
arch: universal2
- os: macos-latest
- os: macos-13
arch: aarch64
- os: macos-14
arch: aarch64
- os: macos-latest
arch: auto
- os: ubuntu-latest
arch: universal2

env:
CIBW_BUILD: ${{ matrix.py }}-*
CIBW_ARCHS_LINUX: ${{ matrix.arch }}

steps:
- uses: actions/checkout@v4
with:
Expand All @@ -63,7 +56,7 @@ jobs:
- if: ${{ matrix.arch == 'aarch64' }}
uses: docker/setup-qemu-action@v3

- uses: pypa/cibuildwheel@v2.16.5
- uses: pypa/cibuildwheel@v2.17.0
env:
CIBW_BUILD: ${{ matrix.py }}-*
CIBW_ARCHS: ${{ matrix.arch }}
Expand Down
10 changes: 7 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ jobs:

strategy:
matrix:
os: [windows-latest, macos-latest, ubuntu-latest]
include:
# version number must be string, otherwise 3.10 becomes 3.1
- os: windows-latest
python-version: "3.11"
installs: "numpy>=2.0.0rc1"
- os: macos-latest
python-version: "3.8"
installs: "'numpy==1.21.0'"
- os: ubuntu-latest
python-version: "pypy-3.8"
- os: ubuntu-latest
python-version: "3.12"
installs: "'numpy>=2.0.0rc1' scipy matplotlib"
fail-fast: false
steps:
- uses: actions/checkout@v4
Expand All @@ -41,8 +45,8 @@ jobs:
python-version: ${{ matrix.python-version }}
- run: python -m pip install --upgrade pip wheel
# python -m pip install .[test] is not used here to test minimum (faster),
# the cov workflow runs all tests
- run: python -m pip install -v . pytest
# the cov workflow runs all tests.
- run: python -m pip install -v . pytest ${{ matrix.installs }}
HDembinski marked this conversation as resolved.
Show resolved Hide resolved
- run: python -m pytest

# aarch64:
Expand Down
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ repos:
additional_dependencies: [numpy]
args: [src]
pass_filenames: false

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.0
hooks:
- id: check-readthedocs
- id: check-github-workflows

- repo: https://github.com/henryiii/validate-pyproject-schema-store
rev: 2024.03.25
hooks:
- id: validate-pyproject
2 changes: 1 addition & 1 deletion doc/notebooks/hesse_and_minos.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"cells": [
"cells": [
{
"attachments": {},
"cell_type": "markdown",
Expand Down
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["scikit-build-core[pyproject]>=0.3", "pybind11"]
requires = ["scikit-build-core[pyproject]>=0.5", "pybind11>=2.12"]
build-backend = "scikit_build_core.build"

[project]
Expand Down Expand Up @@ -33,7 +33,7 @@ classifiers = [
"Operating System :: Unix",
"Operating System :: MacOS",
]
dependencies = ["numpy >= 1.21", "typing_extensions;python_version < '3.9'"]
dependencies = ["numpy >=1.21", "typing_extensions >=3.7.4; python_version<'3.9'"]

[project.urls]
repository = "http://github.com/scikit-hep/iminuit"
Expand Down Expand Up @@ -75,7 +75,7 @@ doc = [
]

[tool.scikit-build]
minimum-version = "0.3"
minimum-version = "0.5"
build-dir = "build/{wheel_tag}"
sdist.exclude = ["extern/root"]
sdist.include = ["extern/root/math/minuit2/inc", "extern/root/math/minuit2/src"]
Expand All @@ -87,7 +87,7 @@ log_cli_level = "INFO"
testpaths = ["tests"]
xfail_strict = true
filterwarnings = [
"error::numpy.VisibleDeprecationWarning",
"error::iminuit._exceptions.VisibleDeprecationWarning",
"error::PendingDeprecationWarning",
"error::DeprecationWarning",
"error::FutureWarning",
Expand Down Expand Up @@ -126,7 +126,7 @@ no_implicit_optional = false

[tool.cibuildwheel]
# update skip when numpy wheels become available
skip = ["*-musllinux_*", "cp31?-win32", "cp31?-manylinux_i686"]
skip = ["*-musllinux_*", "cp31?-manylinux_i686"]
test-requires = "pytest"
test-command = "python -m pytest {package}/tests"
test-skip = ["*universal2:arm64"]
Expand Down
2 changes: 1 addition & 1 deletion src/iminuit/_deprecated.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import warnings
from numpy import VisibleDeprecationWarning
from ._exceptions import VisibleDeprecationWarning


class deprecated:
Expand Down
7 changes: 7 additions & 0 deletions src/iminuit/_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# NumPy 2 and recent 1.x place this in exceptions
try:
from numpy.exceptions import VisibleDeprecationWarning
except ImportError:
from numpy import VisibleDeprecationWarning

__all__ = ["VisibleDeprecationWarning"]
15 changes: 10 additions & 5 deletions src/iminuit/cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class documentation for details.
is_positive_definite,
)
from .typing import Model, ModelGradient, LossFunction
from ._exceptions import VisibleDeprecationWarning
import numpy as np
from numpy.typing import NDArray, ArrayLike
from collections.abc import Sequence as ABCSequence
Expand Down Expand Up @@ -1521,7 +1522,7 @@ def __init__(self, n, xe, model, verbose, grad, use_pdf):
self._model_xm = self._model_xe[:-1] + 0.5 * dx
else:
self._xe_shape = tuple(len(xei) for xei in self.xe)
self._model_xe = np.row_stack(
self._model_xe = np.vstack(
HDembinski marked this conversation as resolved.
Show resolved Hide resolved
[x.flatten() for x in np.meshgrid(*self.xe, indexing="ij")]
)
if use_pdf == "approximate":
Expand Down Expand Up @@ -1754,7 +1755,7 @@ def __init__(
if method == "hpd":
warnings.warn(
"key 'hpd' is deprecated, please use 'da' instead",
category=np.VisibleDeprecationWarning,
category=VisibleDeprecationWarning,
stacklevel=2,
)

Expand All @@ -1765,7 +1766,7 @@ def __init__(
self._model_xe = _norm(self.xe)
else:
self._xe_shape = tuple(len(xei) for xei in self.xe)
self._model_xe = np.row_stack(
self._model_xe = np.vstack(
[x.flatten() for x in np.meshgrid(*self.xe, indexing="ij")]
)
self._model_len = np.prod(self._xe_shape)
Expand Down Expand Up @@ -2473,7 +2474,11 @@ def _normalize_output(x, kind, *shape, msg=None):
if x.ndim < len(shape):
return x.reshape(*shape)
elif x.shape != shape:
msg = f"output of {kind} has shape {x.shape!r}, but {shape!r} is required"
# NumPy 2 uses a numpy int here
pretty_shape = tuple(int(i) for i in shape)
msg = (
f"output of {kind} has shape {x.shape!r}, but {pretty_shape!r} is required"
)
raise ValueError(msg)
return x

Expand Down Expand Up @@ -2504,7 +2509,7 @@ def __getattr__(name: str) -> Any:
new_name, obj = _deprecated_content[name]
warnings.warn(
f"{name} was renamed to {new_name}, please import {new_name} instead",
np.VisibleDeprecationWarning,
VisibleDeprecationWarning,
stacklevel=2,
)
return obj
Expand Down
15 changes: 7 additions & 8 deletions tests/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)
from iminuit.util import describe
from iminuit.typing import Annotated, Gt, Lt
from iminuit._exceptions import VisibleDeprecationWarning
from typing import Sequence
import pickle

Expand Down Expand Up @@ -320,9 +321,7 @@ def test_UnbinnedNLL_visualize(log):
# manual spacing
c.visualize((1, 2), model_points=np.linspace(1, 1000))

with pytest.warns(
np.VisibleDeprecationWarning, match="keyword 'nbins' is deprecated"
):
with pytest.warns(VisibleDeprecationWarning, match="keyword 'nbins' is deprecated"):
c.visualize((1, 2), nbins=20)


Expand Down Expand Up @@ -1909,23 +1908,23 @@ def test_Template_pulls():
def test_deprecated():
from iminuit import cost

with pytest.warns(np.VisibleDeprecationWarning):
with pytest.warns(VisibleDeprecationWarning):
from iminuit.cost import BarlowBeestonLite
assert BarlowBeestonLite is cost.Template

with pytest.warns(np.VisibleDeprecationWarning):
with pytest.warns(VisibleDeprecationWarning):
from iminuit.cost import barlow_beeston_lite_chi2_jsc
assert barlow_beeston_lite_chi2_jsc is cost.template_chi2_jsc

with pytest.warns(np.VisibleDeprecationWarning):
with pytest.warns(VisibleDeprecationWarning):
from iminuit.cost import barlow_beeston_lite_chi2_hpd
assert barlow_beeston_lite_chi2_hpd is cost.template_chi2_da


def test_deprecated_Template_method():
from iminuit import cost

with pytest.warns(np.VisibleDeprecationWarning):
with pytest.warns(VisibleDeprecationWarning):
t = Template([1], [2, 3], [[1], [2]], method="hpd")
t._impl is cost.template_chi2_da

Expand Down Expand Up @@ -1967,7 +1966,7 @@ def cdf(xye, a):
def test_BohmZechTransform():
from iminuit.cost import BohmZechTransform

with pytest.warns(np.VisibleDeprecationWarning):
with pytest.warns(VisibleDeprecationWarning):
val = np.array([1.0, 2.0])
var = np.array([3.0, 4.0])
tr = BohmZechTransform(val, var)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_deprecated.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from iminuit._deprecated import deprecated, deprecated_parameter
from iminuit._exceptions import VisibleDeprecationWarning
import pytest
import numpy as np


def test_deprecated_func():
@deprecated("bla")
def func(x):
pass

with pytest.warns(np.VisibleDeprecationWarning, match="func is deprecated: bla"):
with pytest.warns(VisibleDeprecationWarning, match="func is deprecated: bla"):
func(1)


Expand All @@ -20,7 +20,7 @@ def some_function(x, y, foo):
some_function(1, 2, foo=3)

with pytest.warns(
np.VisibleDeprecationWarning,
VisibleDeprecationWarning,
match="keyword 'bar' is deprecated, please use 'foo'",
):
some_function(1, 2, bar=3)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_describe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from iminuit.util import describe, make_func_code
from iminuit.typing import Annotated, Gt, Lt, Ge, Le, Interval
from iminuit._exceptions import VisibleDeprecationWarning
from math import ldexp
import platform
from functools import wraps
Expand Down Expand Up @@ -94,7 +95,7 @@ def __init__(self):
def __call__(self, *args):
pass

with pytest.warns(np.VisibleDeprecationWarning):
with pytest.warns(VisibleDeprecationWarning):
assert describe(A()) == ["x", "y"]


Expand Down
3 changes: 2 additions & 1 deletion tests/test_minuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from iminuit.util import Param, make_func_code
from iminuit.warnings import IMinuitWarning, ErrordefAlreadySetWarning
from iminuit.typing import Annotated
from iminuit._exceptions import VisibleDeprecationWarning
from pytest import approx
from argparse import Namespace

Expand Down Expand Up @@ -193,7 +194,7 @@ def test_Func1():


def test_Func2():
with pytest.warns(np.VisibleDeprecationWarning):
with pytest.warns(VisibleDeprecationWarning):
func_test_helper(Func2())


Expand Down
Loading