Skip to content

Add support for numpy 1.25+ #96

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

Merged
merged 7 commits into from
May 9, 2025
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
22 changes: 14 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pathlib import Path
from typing import TYPE_CHECKING

from docutils.nodes import Text


if TYPE_CHECKING:
from docutils.nodes import TextElement, reference
Expand Down Expand Up @@ -82,6 +84,7 @@
source_directory="docs/",
)

_np_aliases = {"bool_": "bool"}
_np_nocls = {"float64": "attr"}
_optional_types = {
"CupyArray": "cupy.ndarray",
Expand All @@ -94,30 +97,31 @@
}


def find_type_alias(name: str) -> tuple[str, str] | tuple[None, None]:
def find_type_alias(name: str) -> tuple[str, str, str | None] | tuple[None, None, None]:
"""Find a type alias."""
import numpy.typing as npt

from fast_array_utils import types, typing

if name in typing.__all__:
return "data", f"fast_array_utils.typing.{name}"
return "data", f"fast_array_utils.typing.{name}", None
if name.startswith("types.") and name[6:] in {*types.__all__, *_optional_types}:
if path := _optional_types.get(name[6:]):
return "class", path
return "data", f"fast_array_utils.{name}"
return "class", path, None
return "data", f"fast_array_utils.{name}", None
if name.startswith("np."):
return _np_nocls.get(name[3:], "class"), f"numpy.{name[3:]}"
name = _np_aliases.get(name[3:], name[3:])
return _np_nocls.get(name, "class"), f"numpy.{name}", f"np.{name}"
if name in npt.__all__:
return "data", f"numpy.typing.{name}"
return None, None
return "data", f"numpy.typing.{name}", None
return None, None, None


def resolve_type_aliases(app: Sphinx, env: BuildEnvironment, node: pending_xref, contnode: TextElement) -> reference | None:
"""Resolve :class: references to our type aliases as :attr: instead."""
if (node["refdomain"], node["reftype"]) != ("py", "class"):
return None
typ, target = find_type_alias(node["reftarget"])
typ, target, name = find_type_alias(node["reftarget"])
if typ is None or target is None:
return None
if target.startswith("fast_array_utils."):
Expand All @@ -131,6 +135,8 @@ def resolve_type_aliases(app: Sphinx, env: BuildEnvironment, node: pending_xref,
if ref is None:
msg = f"Could not resolve {typ} {target} (from {node['reftarget']})"
raise AssertionError(msg)
if name:
ref.children[:] = [Text(name)]
return ref


Expand Down
23 changes: 20 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[build-system]
build-backend = "hatchling.build"
requires = [ "hatch-docstring-description>=1.1.1", "hatch-fancy-pypi-readme", "hatch-vcs", "hatchling" ]
requires = [
"hatch-docstring-description>=1.1.1",
"hatch-fancy-pypi-readme",
"hatch-min-requirements",
"hatch-vcs",
"hatchling",
]

[project]
name = "fast-array-utils"
Expand All @@ -18,7 +24,7 @@ classifiers = [
"Programming Language :: Python :: 3.13",
]
dynamic = [ "description", "readme", "version" ]
dependencies = [ "numpy" ]
dependencies = [ "numpy>=1.25.2" ]
optional-dependencies.accel = [ "numba" ]
optional-dependencies.doc = [
"furo",
Expand All @@ -29,7 +35,7 @@ optional-dependencies.doc = [
"sphinx-autofixture",
]
optional-dependencies.full = [ "dask", "fast-array-utils[accel,sparse]", "h5py", "zarr" ]
optional-dependencies.sparse = [ "scipy>=1.8" ]
optional-dependencies.sparse = [ "scipy>=1.11" ]
optional-dependencies.test = [
"anndata",
"fast-array-utils[accel,test-min]",
Expand Down Expand Up @@ -61,6 +67,7 @@ path = "README.rst"
start-after = ".. begin"

[tool.hatch.metadata.hooks.docstring-description]
[tool.hatch.metadata.hooks.min_requirements]

[tool.hatch.build.targets.wheel]
packages = [ "src/testing", "src/fast_array_utils" ]
Expand All @@ -87,11 +94,19 @@ overrides.matrix.extras.dependencies = [
{ if = [ "full" ], value = "scipy-stubs" },
{ if = [ "full" ], value = "scikit-learn" },
]
overrides.matrix.resolution.features = [
{ if = [ "lowest" ], value = "min-reqs" }, # feature added by hatch-min-requirements
]

[[tool.hatch.envs.hatch-test.matrix]]
python = [ "3.13", "3.11" ]
extras = [ "full", "min" ]

[[tool.hatch.envs.hatch-test.matrix]]
python = [ "3.11" ]
extras = [ "full" ]
resolution = [ "lowest" ]

[tool.ruff]
line-length = 160
namespace-packages = [ "src/testing" ]
Expand Down Expand Up @@ -124,6 +139,7 @@ lint.per-file-ignores."typings/**/*.pyi" = [ "A002", "F403", "F405", "N801" ] #
lint.allowed-confusables = [ "×", "’" ]
lint.flake8-bugbear.extend-immutable-calls = [ "testing.fast_array_utils.Flags" ]
lint.flake8-copyright.notice-rgx = "SPDX-License-Identifier: MPL-2\\.0"
lint.flake8-tidy-imports.banned-api."numpy.bool".msg = "Use `np.bool_` instead for numpy>=1.24<2 compatibility"
lint.flake8-type-checking.exempt-modules = [ ]
lint.flake8-type-checking.strict = true
lint.isort.known-first-party = [ "fast_array_utils" ]
Expand All @@ -133,6 +149,7 @@ lint.pydocstyle.convention = "numpy"

[tool.pytest.ini_options]
addopts = [
"-ptesting.fast_array_utils._private",
"--import-mode=importlib",
"--strict-markers",
"--doctest-modules",
Expand Down
10 changes: 5 additions & 5 deletions src/fast_array_utils/stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@overload
def is_constant(x: NDArray[Any] | types.CSBase | types.CupyArray, /, *, axis: None = None) -> bool: ...
@overload
def is_constant(x: NDArray[Any] | types.CSBase, /, *, axis: Literal[0, 1]) -> NDArray[np.bool]: ...
def is_constant(x: NDArray[Any] | types.CSBase, /, *, axis: Literal[0, 1]) -> NDArray[np.bool_]: ...
@overload
def is_constant(x: types.CupyArray, /, *, axis: Literal[0, 1]) -> types.CupyArray: ...
@overload
Expand All @@ -41,7 +41,7 @@ def is_constant(
/,
*,
axis: Literal[0, 1, None] = None,
) -> bool | NDArray[np.bool] | types.CupyArray | types.DaskArray:
) -> bool | NDArray[np.bool_] | types.CupyArray | types.DaskArray:
"""Check whether values in array are constant.

Parameters
Expand Down Expand Up @@ -118,7 +118,7 @@ def mean(
... [0, 0, 0],
... ])
>>> mean(x)
np.float64(0.5)
0.5
>>> mean(x, axis=0)
array([0. , 0.5, 1. ])
>>> mean(x, axis=1)
Expand Down Expand Up @@ -184,7 +184,7 @@ def mean_var(
... [0, 0, 0],
... ])
>>> mean_var(x) # doctest: +FLOAT_CMP
(np.float64(0.5), np.float64(0.5833333333333334))
(0.5, 0.5833333333333334)
>>> mean_var(x, axis=0)
(array([0. , 0.5, 1. ]), array([0. , 0.25, 1. ]))
>>> mean_var(x, axis=1)
Expand Down Expand Up @@ -251,7 +251,7 @@ def sum(
... [0, 0, 0],
... ])
>>> sum(x)
np.int64(3)
3
>>> sum(x, axis=0)
array([0, 1, 2])
>>> sum(x, axis=1)
Expand Down
16 changes: 8 additions & 8 deletions src/fast_array_utils/stats/_is_constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def is_constant_(
/,
*,
axis: Literal[0, 1, None] = None,
) -> bool | NDArray[np.bool] | types.CupyArray | types.DaskArray: # pragma: no cover
) -> bool | NDArray[np.bool_] | types.CupyArray | types.DaskArray: # pragma: no cover
raise NotImplementedError


@is_constant_.register(np.ndarray | types.CupyArray) # type: ignore[call-overload,misc]
def _is_constant_ndarray(a: NDArray[Any] | types.CupyArray, /, *, axis: Literal[0, 1, None] = None) -> bool | NDArray[np.bool] | types.CupyArray:
def _is_constant_ndarray(a: NDArray[Any] | types.CupyArray, /, *, axis: Literal[0, 1, None] = None) -> bool | NDArray[np.bool_] | types.CupyArray:
# Should eventually support nd, not now.
match axis:
case None:
Expand All @@ -41,13 +41,13 @@ def _is_constant_ndarray(a: NDArray[Any] | types.CupyArray, /, *, axis: Literal[
return _is_constant_rows(a)


def _is_constant_rows(a: NDArray[Any] | types.CupyArray) -> NDArray[np.bool] | types.CupyArray:
def _is_constant_rows(a: NDArray[Any] | types.CupyArray) -> NDArray[np.bool_] | types.CupyArray:
b = np.broadcast_to(a[:, 0][:, np.newaxis], a.shape)
return cast("NDArray[np.bool]", (a == b).all(axis=1))
return cast("NDArray[np.bool_]", (a == b).all(axis=1))


@is_constant_.register(types.CSBase) # type: ignore[call-overload,misc]
def _is_constant_cs(a: types.CSBase, /, *, axis: Literal[0, 1, None] = None) -> bool | NDArray[np.bool]:
def _is_constant_cs(a: types.CSBase, /, *, axis: Literal[0, 1, None] = None) -> bool | NDArray[np.bool_]:
from . import is_constant

if len(a.shape) == 1: # pragma: no cover
Expand All @@ -68,9 +68,9 @@ def _is_constant_cs(a: types.CSBase, /, *, axis: Literal[0, 1, None] = None) ->


@numba.njit(cache=True)
def _is_constant_cs_major(a: types.CSBase, shape: tuple[int, int]) -> NDArray[np.bool]:
def _is_constant_cs_major(a: types.CSBase, shape: tuple[int, int]) -> NDArray[np.bool_]:
n = len(a.indptr) - 1
result = np.ones(n, dtype=np.bool)
result = np.ones(n, dtype=np.bool_)
for i in numba.prange(n):
start = a.indptr[i]
stop = a.indptr[i + 1]
Expand All @@ -89,7 +89,7 @@ def _is_constant_dask(a: types.DaskArray, /, *, axis: Literal[0, 1, None] = None
from . import is_constant

if axis is not None:
return da.map_blocks(partial(is_constant, axis=axis), a, drop_axis=axis, meta=np.array([], dtype=np.bool))
return da.map_blocks(partial(is_constant, axis=axis), a, drop_axis=axis, meta=np.array([], dtype=np.bool_))

rv = (
(a == a[0, 0].compute()).all()
Expand Down
11 changes: 11 additions & 0 deletions src/testing/fast_array_utils/_private.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: MPL-2.0
from __future__ import annotations

import numpy as np
import pytest


@pytest.fixture(autouse=True)
def _set_numpy_print() -> None: # TODO(flying-sheep): #97 remove once we depend on numpy >=2
if int(np.__version__.split(".", 1)[0]) > 1:
np.set_printoptions(legacy="1.25")
8 changes: 4 additions & 4 deletions tests/test_numpy_scipy_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def test_copy(
assert mat.indptr.ctypes.data != copied.indptr.ctypes.data
# check that the array contents and dtypes are the same
assert mat.shape == copied.shape
np.testing.assert_equal(copied.toarray(), mat.toarray(), strict=True)
np.testing.assert_equal(copied.data, mat.data, strict=True)
np.testing.assert_equal(copied.indices, mat.indices, strict=not downcasts_idx(mat))
np.testing.assert_equal(copied.indptr, mat.indptr, strict=not downcasts_idx(mat))
np.testing.assert_array_equal(copied.toarray(), mat.toarray(), strict=True)
np.testing.assert_array_equal(copied.data, mat.data, strict=True)
np.testing.assert_array_equal(copied.indices, mat.indices, strict=not downcasts_idx(mat))
np.testing.assert_array_equal(copied.indptr, mat.indptr, strict=not downcasts_idx(mat))


def downcasts_idx(mat: types.CSBase) -> bool:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

Array: TypeAlias = CpuArray | GpuArray | DiskArray | types.CSDataset | types.DaskArray

DTypeIn = np.float32 | np.float64 | np.int32 | np.bool
DTypeIn = np.float32 | np.float64 | np.int32 | np.bool_
DTypeOut = np.float32 | np.float64 | np.int64

NdAndAx: TypeAlias = tuple[Literal[1], Literal[None]] | tuple[Literal[2], Literal[0, 1, None]]
Expand Down Expand Up @@ -81,7 +81,7 @@ def axis(ndim_and_axis: NdAndAx) -> Literal[0, 1, None]:
return ndim_and_axis[1]


@pytest.fixture(params=[np.float32, np.float64, np.int32, np.bool])
@pytest.fixture(params=[np.float32, np.float64, np.int32, np.bool_])
def dtype_in(request: pytest.FixtureRequest, array_type: ArrayType) -> type[DTypeIn]:
dtype = cast("type[DTypeIn]", request.param)
inner_cls = array_type.inner.cls if array_type.inner else array_type.cls
Expand Down Expand Up @@ -152,7 +152,7 @@ def test_sum(

if dtype_arg is not None:
assert sum_.dtype == dtype_arg, (sum_.dtype, dtype_arg)
elif dtype_in in {np.bool, np.int32}:
elif dtype_in in {np.bool_, np.int32}:
assert sum_.dtype == np.int64
else:
assert sum_.dtype == dtype_in
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_mean_var(
mean, var = mean.get(), var.get()

mean_expected = np.mean(np_arr, axis=axis) # type: ignore[arg-type]
var_expected = np.var(np_arr, axis=axis, correction=1) # type: ignore[arg-type]
var_expected = np.var(np_arr, axis=axis, ddof=1) # type: ignore[arg-type]
np.testing.assert_array_equal(mean, mean_expected)
np.testing.assert_array_almost_equal(var, var_expected) # type: ignore[arg-type]

Expand Down Expand Up @@ -276,7 +276,7 @@ def test_is_constant(
x = array_type(x_data, dtype=np.float64)
result = stats.is_constant(x, axis=axis)
if isinstance(result, types.DaskArray):
result = cast("NDArray[np.bool] | bool", result.compute())
result = cast("NDArray[np.bool_] | bool", result.compute())
if isinstance(result, types.CupyArray | types.CupyCSMatrix):
result = result.get()
if isinstance(expected, list):
Expand Down
2 changes: 1 addition & 1 deletion typings/cupy/_core/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ndarray:
@property
def T(self) -> Self: ... # noqa: N802
@overload
def all(self, axis: None = None) -> np.bool: ...
def all(self, axis: None = None) -> np.bool_: ...
@overload
def all(self, axis: int) -> ndarray: ...
def reshape(self, shape: tuple[int, ...] | int) -> ndarray: ...
Expand Down
Loading