|
| 1 | +# SPDX-License-Identifier: MPL-2.0 |
| 2 | +"""Testing utilities.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import re |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from typing import Any, Generic, Literal, Protocol, SupportsFloat, TypeAlias, TypeVar |
| 14 | + |
| 15 | + from numpy.typing import ArrayLike, NDArray |
| 16 | + |
| 17 | + from fast_array_utils import types |
| 18 | + from fast_array_utils.types import CSBase |
| 19 | + |
| 20 | + _SCT_co = TypeVar("_SCT_co", covariant=True, bound=np.generic) |
| 21 | + _SCT_contra = TypeVar("_SCT_contra", contravariant=True, bound=np.generic) |
| 22 | + _SCT_float = TypeVar("_SCT_float", np.float32, np.float64) |
| 23 | + |
| 24 | + Array: TypeAlias = ( |
| 25 | + NDArray[_SCT_co] |
| 26 | + | types.CSBase[_SCT_co] |
| 27 | + | types.CupyArray[_SCT_co] |
| 28 | + | types.DaskArray |
| 29 | + | types.H5Dataset |
| 30 | + | types.ZarrArray |
| 31 | + ) |
| 32 | + |
| 33 | + class ToArray(Protocol, Generic[_SCT_contra]): |
| 34 | + """Convert to a supported array.""" |
| 35 | + |
| 36 | + def __call__( # noqa: D102 |
| 37 | + self, data: ArrayLike, /, *, dtype: _SCT_contra | None = None |
| 38 | + ) -> Array[_SCT_contra]: ... |
| 39 | + |
| 40 | + |
| 41 | +RE_ARRAY_QUAL = re.compile(r"(?P<mod>(?:\w+\.)*\w+)\.(?P<name>[^\[]+)(?:\[(?P<inner>[\w.]+)\])?") |
| 42 | + |
| 43 | + |
| 44 | +def get_array_cls(qualname: str) -> type[Array[Any]]: # noqa: PLR0911 |
| 45 | + """Get a supported array class by qualname.""" |
| 46 | + m = RE_ARRAY_QUAL.fullmatch(qualname) |
| 47 | + assert m |
| 48 | + match m["mod"], m["name"], m["inner"]: |
| 49 | + case "numpy", "ndarray", None: |
| 50 | + return np.ndarray |
| 51 | + case "scipy.sparse", ( |
| 52 | + "csr_array" | "csc_array" | "csr_matrix" | "csc_matrix" |
| 53 | + ) as cls_name, None: |
| 54 | + import scipy.sparse |
| 55 | + |
| 56 | + return getattr(scipy.sparse, cls_name) # type: ignore[no-any-return] |
| 57 | + case "cupy", "ndarray", None: |
| 58 | + import cupy as cp |
| 59 | + |
| 60 | + return cp.ndarray # type: ignore[no-any-return] |
| 61 | + case "cupyx.scipy.sparse", ("csr_matrix" | "csc_matrix") as cls_name, None: |
| 62 | + import cupyx.scipy.sparse as cu_sparse |
| 63 | + |
| 64 | + return getattr(cu_sparse, cls_name) # type: ignore[no-any-return] |
| 65 | + case "dask.array", cls_name, _: |
| 66 | + if TYPE_CHECKING: |
| 67 | + from dask.array.core import Array as DaskArray |
| 68 | + else: |
| 69 | + from dask.array import Array as DaskArray |
| 70 | + |
| 71 | + return DaskArray |
| 72 | + case "h5py", "Dataset", _: |
| 73 | + import h5py |
| 74 | + |
| 75 | + return h5py.Dataset # type: ignore[no-any-return] |
| 76 | + case "zarr", "Array", _: |
| 77 | + import zarr |
| 78 | + |
| 79 | + return zarr.Array |
| 80 | + case _: |
| 81 | + msg = f"Unknown array class: {qualname}" |
| 82 | + raise ValueError(msg) |
| 83 | + |
| 84 | + |
| 85 | +def random_mat( |
| 86 | + shape: tuple[int, int], |
| 87 | + *, |
| 88 | + density: SupportsFloat = 0.01, |
| 89 | + format: Literal["csr", "csc"] = "csr", # noqa: A002 |
| 90 | + dtype: np.dtype[_SCT_float] | type[_SCT_float] | None = None, |
| 91 | + container: Literal["array", "matrix"] = "array", |
| 92 | + gen: np.random.Generator | None = None, |
| 93 | +) -> CSBase[_SCT_float]: |
| 94 | + """Create a random matrix.""" |
| 95 | + from scipy.sparse import random as random_spmat |
| 96 | + from scipy.sparse import random_array as random_sparr |
| 97 | + |
| 98 | + m, n = shape |
| 99 | + return ( |
| 100 | + random_spmat(m, n, density=density, format=format, dtype=dtype, random_state=gen) |
| 101 | + if container == "matrix" |
| 102 | + else random_sparr(shape, density=density, format=format, dtype=dtype, random_state=gen) |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +def random_array( |
| 107 | + qualname: str, |
| 108 | + shape: tuple[int, int], |
| 109 | + *, |
| 110 | + dtype: np.dtype[_SCT_float] | type[_SCT_float] | None, |
| 111 | + gen: np.random.Generator | None = None, |
| 112 | +) -> Array[_SCT_float]: |
| 113 | + """Create a random array.""" |
| 114 | + gen = np.random.default_rng(gen) |
| 115 | + |
| 116 | + m = RE_ARRAY_QUAL.fullmatch(qualname) |
| 117 | + assert m |
| 118 | + match m["mod"], m["name"], m["inner"]: |
| 119 | + case "numpy", "ndarray", None: |
| 120 | + return gen.random(shape, dtype=dtype or np.float64) |
| 121 | + case "scipy.sparse", ( |
| 122 | + "csr_array" | "csc_array" | "csr_matrix" | "csc_matrix" |
| 123 | + ) as cls_name, None: |
| 124 | + fmt, container = cls_name.split("_") |
| 125 | + return random_mat(shape, format=fmt, container=container, dtype=dtype) # type: ignore[arg-type] |
| 126 | + case "cupy", "ndarray", None: |
| 127 | + raise NotImplementedError |
| 128 | + case "cupyx.scipy.sparse", ("csr_matrix" | "csc_matrix") as cls_name, None: |
| 129 | + raise NotImplementedError |
| 130 | + case "dask.array", cls_name, _: |
| 131 | + raise NotImplementedError |
| 132 | + case "h5py", "Dataset", _: |
| 133 | + raise NotImplementedError |
| 134 | + case "zarr", "Array", _: |
| 135 | + raise NotImplementedError |
| 136 | + case _: |
| 137 | + msg = f"Unknown array class: {qualname}" |
| 138 | + raise ValueError(msg) |
0 commit comments