Skip to content

toolkit.array

Modules:

Functions:

Attributes:

ArrayLike module-attribute

ArrayLike: TypeAlias = ArrayLike | ArrayLike

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_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)

as_scalar

as_scalar(x: Any) -> float
Source code in src/toolkit/array/python/_cast.py
 6
 7
 8
 9
10
11
12
13
def as_scalar(x: Any) -> float:
    if tk.is_jax(x):
        return x.item()
    if tk.is_numpy(x):
        return x.item()
    if tk.is_torch(x):
        return x.item()
    return x

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_jax

is_jax(obj: Any) -> TypeGuard[Array]
Source code in src/toolkit/array/jax/_utils.py
11
12
def is_jax(obj: Any) -> TypeGuard[jax.Array]:
    return tp.is_instance_named_partial(obj, "jax.Array")

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")

is_torch

is_torch(obj: Any) -> TypeGuard[Tensor]
Source code in src/toolkit/array/torch/_utils.py
11
12
def is_torch(obj: Any) -> TypeGuard[torch.Tensor]:
    return tp.is_instance_named_partial(obj, "torch.Tensor")