(
data: duckarray[Any, Any], **kwargs: dict[str, Any]
)
| 97 | |
| 98 | |
| 99 | def to_numpy( |
| 100 | data: duckarray[Any, Any], **kwargs: dict[str, Any] |
| 101 | ) -> np.ndarray[Any, np.dtype[Any]]: |
| 102 | from xarray.core.indexing import ExplicitlyIndexed |
| 103 | from xarray.namedarray.parallelcompat import get_chunked_array_type |
| 104 | |
| 105 | try: |
| 106 | # for tests only at the moment |
| 107 | return data.to_numpy() # type: ignore[no-any-return,union-attr] |
| 108 | except AttributeError: |
| 109 | pass |
| 110 | |
| 111 | if isinstance(data, ExplicitlyIndexed): |
| 112 | data = data.get_duck_array() # type: ignore[no-untyped-call] |
| 113 | |
| 114 | # TODO first attempt to call .to_numpy() once some libraries implement it |
| 115 | if is_chunked_array(data): |
| 116 | chunkmanager = get_chunked_array_type(data) |
| 117 | data, *_ = chunkmanager.compute(data, **kwargs) |
| 118 | if isinstance(data, array_type("cupy")): |
| 119 | data = data.get() |
| 120 | # pint has to be imported dynamically as pint imports xarray |
| 121 | if isinstance(data, array_type("pint")): |
| 122 | data = data.magnitude |
| 123 | if isinstance(data, array_type("sparse")): |
| 124 | data = data.todense() |
| 125 | data = np.asarray(data) |
| 126 | |
| 127 | return data |
| 128 | |
| 129 | |
| 130 | def to_duck_array(data: Any, **kwargs: dict[str, Any]) -> duckarray[_ShapeType, _DType]: |
no test coverage detected
searching dependent graphs…