Skip to content

toolkit.array.numpy

Functions:

Attributes:

ArrayLike module-attribute

ArrayLike: TypeAlias = ArrayLike | ArrayLike

B module-attribute

B = Bool[ndarray, '']

B33 module-attribute

B33 = Bool[ndarray, '3 3']

B33Like module-attribute

B33Like = Bool[ArrayLike, '3 3']

B34 module-attribute

B34 = Bool[ndarray, '3 4']

B34Like module-attribute

B34Like = Bool[ArrayLike, '3 4']

B43 module-attribute

B43 = Bool[ndarray, '4 3']

B43Like module-attribute

B43Like = Bool[ArrayLike, '4 3']

B44 module-attribute

B44 = Bool[ndarray, '4 4']

B44Like module-attribute

B44Like = Bool[ArrayLike, '4 4']

BLike module-attribute

BLike = Bool[ArrayLike, '']

BMN module-attribute

BMN = Bool[ndarray, 'M N']

BMNLike module-attribute

BMNLike = Bool[ArrayLike, 'M N']

BN module-attribute

BN = Bool[ndarray, 'N']

BN3 module-attribute

BN3 = Bool[ndarray, 'N 3']

BN3Like module-attribute

BN3Like = Bool[ArrayLike, 'N 3']

BN4 module-attribute

BN4 = Bool[ndarray, 'N 4']

BN4Like module-attribute

BN4Like = Bool[ArrayLike, 'N 4']

BNLike module-attribute

BNLike = Bool[ArrayLike, 'N']

BNN module-attribute

BNN = Bool[ndarray, 'N N']

BNNLike module-attribute

BNNLike = Bool[ArrayLike, 'N N']

F module-attribute

F = Float[ndarray, '']

F33 module-attribute

F33 = Float[ndarray, '3 3']

F33Like module-attribute

F33Like = Float[ArrayLike, '3 3']

F34 module-attribute

F34 = Float[ndarray, '3 4']

F34Like module-attribute

F34Like = Float[ArrayLike, '3 4']

F43 module-attribute

F43 = Float[ndarray, '4 3']

F43Like module-attribute

F43Like = Float[ArrayLike, '4 3']

F44 module-attribute

F44 = Float[ndarray, '4 4']

F44Like module-attribute

F44Like = Float[ArrayLike, '4 4']

FLike module-attribute

FLike = Float[ArrayLike, '']

FMN module-attribute

FMN = Float[ndarray, 'M N']

FMNLike module-attribute

FMNLike = Float[ArrayLike, 'M N']

FN module-attribute

FN = Float[ndarray, 'N']

FN3 module-attribute

FN3 = Float[ndarray, 'N 3']

FN3Like module-attribute

FN3Like = Float[ArrayLike, 'N 3']

FN4 module-attribute

FN4 = Float[ndarray, 'N 4']

FN4Like module-attribute

FN4Like = Float[ArrayLike, 'N 4']

FNLike module-attribute

FNLike = Float[ArrayLike, 'N']

FNN module-attribute

FNN = Float[ndarray, 'N N']

FNNLike module-attribute

FNNLike = Float[ArrayLike, 'N N']

I module-attribute

I = Integer[ndarray, '']

I33 module-attribute

I33 = Integer[ndarray, '3 3']

I33Like module-attribute

I33Like = Integer[ArrayLike, '3 3']

I34 module-attribute

I34 = Integer[ndarray, '3 4']

I34Like module-attribute

I34Like = Integer[ArrayLike, '3 4']

I43 module-attribute

I43 = Integer[ndarray, '4 3']

I43Like module-attribute

I43Like = Integer[ArrayLike, '4 3']

I44 module-attribute

I44 = Integer[ndarray, '4 4']

I44Like module-attribute

I44Like = Integer[ArrayLike, '4 4']

ILike module-attribute

ILike = Integer[ArrayLike, '']

IMN module-attribute

IMN = Integer[ndarray, 'M N']

IMNLike module-attribute

IMNLike = Integer[ArrayLike, 'M N']

IN module-attribute

IN = Integer[ndarray, 'N']

IN3 module-attribute

IN3 = Integer[ndarray, 'N 3']

IN3Like module-attribute

IN3Like = Integer[ArrayLike, 'N 3']

IN4 module-attribute

IN4 = Integer[ndarray, 'N 4']

IN4Like module-attribute

IN4Like = Integer[ArrayLike, 'N 4']

INLike module-attribute

INLike = Integer[ArrayLike, 'N']

INN module-attribute

INN = Integer[ndarray, 'N N']

INNLike module-attribute

INNLike = Integer[ArrayLike, 'N N']

as_dict_of_numpy

as_dict_of_numpy(obj: Mapping[str, ArrayLike] | None) -> dict[str, ndarray]
Source code in src/toolkit/array/numpy/_utils/_as_numpy.py
20
21
22
23
def as_dict_of_numpy(obj: Mapping[str, tp.ArrayLike] | None) -> dict[str, np.ndarray]:
    if obj is None:
        return {}
    return {k: tk.as_numpy(v) for k, v in obj.items()}

as_dtype

as_dtype(x: Annotated[ndarray, Numpy], dtype: Annotated[dtype, BeforeValidator(dtype)]) -> NDArray[...]
Source code in src/toolkit/array/numpy/_utils/_as_dtype.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@pydantic.validate_call
def as_dtype(
    x: Annotated[np.ndarray, tv.Numpy],
    dtype: Annotated[np.dtype, pydantic.BeforeValidator(np.dtype)],
) -> npt.NDArray[...]:
    if np.issubdtype(x.dtype, dtype):
        return x
    if np.isdtype(dtype, "bool"):
        if np.ptp(x) > 0:
            x = tk.array.numpy.scale(x)
        return x > 0.5
    if np.isdtype(dtype, "integral"):
        x = np.rint(x)
    return x.astype(dtype)

as_numpy

as_numpy(obj: Any) -> ndarray
Source code in src/toolkit/array/numpy/_utils/_as_numpy.py
12
13
14
15
16
17
def as_numpy(obj: Any) -> np.ndarray:
    if tk.is_numpy(obj):
        return obj
    if tk.is_torch(obj):
        return obj.numpy(force=True)
    return np.asarray(obj)

is_array_like

is_array_like(obj: Any) -> bool
Source code in src/toolkit/array/array_like/_utils.py
4
5
def is_array_like(obj: Any) -> bool:
    return hasattr(obj, "__len__") and not isinstance(obj, str | bytes)

is_numpy

is_numpy(obj: Any) -> TypeGuard[ndarray]
Source code in src/toolkit/array/numpy/_utils/_is.py
10
11
def is_numpy(obj: Any) -> TypeGuard[np.ndarray]:
    return tp.is_instance_named_partial(obj, "numpy.ndarray")

scale

scale(x: Annotated[ndarray, Numpy], a: float = 0, b: float = 1) -> ndarray
Source code in src/toolkit/array/numpy/_utils/_scale.py
11
12
13
14
15
@pydantic.validate_call
def scale(x: Annotated[np.ndarray, tv.Numpy], a: float = 0, b: float = 1) -> np.ndarray:
    x = (x - x.min()) / np.ptp(x)
    x = x * (b - a) + a
    return x