-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_asarray.py
More file actions
74 lines (49 loc) · 1.58 KB
/
_asarray.py
File metadata and controls
74 lines (49 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# SPDX-License-Identifier: MPL-2.0
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
from .._import import lazy_singledispatch
from ..types import OutOfCoreDataset
if TYPE_CHECKING:
from typing import Any
from numpy.typing import ArrayLike, NDArray
from .. import types
__all__ = ["asarray"]
# fallback’s arg0 type has to include types of registered functions
@lazy_singledispatch
def asarray(
x: ArrayLike
| types.CSBase
| types.DaskArray
| types.OutOfCoreDataset[Any]
| types.H5Dataset
| types.ZarrArray
| types.CupyArray
| types.CupySparseMatrix,
) -> NDArray[Any]:
"""Convert x to a numpy array.
Parameters
----------
x
Input object to be converted.
Returns
-------
Numpy array form of ``x``
"""
return np.asarray(x)
@asarray.register("fast_array_utils.types:CSBase", "scipy.sparse")
def _(x: types.CSBase) -> NDArray[Any]:
from .scipy import to_dense
return to_dense(x)
@asarray.register("dask.array:Array")
def _(x: types.DaskArray) -> NDArray[Any]:
return asarray(x.compute()) # type: ignore[no-untyped-call]
@asarray.register(OutOfCoreDataset)
def _(x: types.OutOfCoreDataset[types.CSBase | NDArray[Any]]) -> NDArray[Any]:
return asarray(x.to_memory())
@asarray.register("cupy:ndarray")
def _(x: types.CupyArray) -> NDArray[Any]:
return x.get() # type: ignore[no-any-return]
@asarray.register("cupyx.scipy.sparse:spmatrix")
def _(x: types.CupySparseMatrix) -> NDArray[Any]:
return x.toarray().get() # type: ignore[no-any-return]