-
Notifications
You must be signed in to change notification settings - Fork 3
/
cubedmanager.py
216 lines (180 loc) · 6.27 KB
/
cubedmanager.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from __future__ import annotations
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, Callable, Union
import numpy as np
from tlz import partition
from xarray.namedarray.parallelcompat import ChunkManagerEntrypoint
if TYPE_CHECKING:
from xarray.core.types import T_Chunks, T_NormalizedChunks
from cubed import Array as CubedArray
class CubedManager(ChunkManagerEntrypoint["CubedArray"]):
array_cls: type["CubedArray"]
def __init__(self) -> None:
from cubed import Array
self.array_cls = Array
def chunks(self, data: "CubedArray") -> T_NormalizedChunks:
return data.chunks
def normalize_chunks(
self,
chunks: T_Chunks | T_NormalizedChunks,
shape: tuple[int, ...] | None = None,
limit: int | None = None,
dtype: np.dtype | None = None,
previous_chunks: T_NormalizedChunks | None = None,
) -> T_NormalizedChunks:
from cubed.vendor.dask.array.core import normalize_chunks
return normalize_chunks(
chunks,
shape=shape,
limit=limit,
dtype=dtype,
previous_chunks=previous_chunks,
)
def from_array(self, data: np.ndarray, chunks, **kwargs) -> "CubedArray":
from cubed import from_array
# Extract cubed-specific kwargs.
# Also ignores dask-specific kwargs that are passed in.
# The passing of dask-specific kwargs to cubed should be eventually removed by deprecating them
# as explicit arguments to xarray methods
spec = kwargs.pop("spec", None)
return from_array(
data,
chunks,
spec=spec,
)
def compute(self, *data: "CubedArray", **kwargs) -> tuple[np.ndarray, ...]:
from cubed import compute
return compute(*data, **kwargs)
@property
def array_api(self) -> Any:
from cubed import array_api
return array_api
def reduction(
self,
arr: "CubedArray",
func: Callable,
combine_func: Callable | None = None,
aggregate_func: Callable | None = None,
axis: int | Sequence[int] | None = None,
dtype: np.dtype | None = None,
keepdims: bool = False,
) -> "CubedArray":
from cubed.core.ops import reduction
return reduction(
arr,
func=func,
combine_func=combine_func,
aggegrate_func=aggregate_func, # TODO fix the typo in argument name in cubed
axis=axis,
dtype=dtype,
keepdims=keepdims,
)
def map_blocks(
self,
func: Callable,
*args: Any,
dtype: np.typing.DTypeLike | None = None,
chunks: tuple[int, ...] | None = None,
drop_axis: int | Sequence[int] | None = None,
new_axis: int | Sequence[int] | None = None,
**kwargs,
):
from cubed.core.ops import map_blocks
if drop_axis is None:
# TODO should fix this upstream in cubed to match dask
# see https://github.com/pydata/xarray/pull/7019#discussion_r1196729489
drop_axis = []
return map_blocks(
func,
*args,
dtype=dtype,
chunks=chunks,
drop_axis=drop_axis,
new_axis=new_axis,
**kwargs,
)
def blockwise(
self,
func: Callable,
out_ind: Iterable,
*args: Any,
# can't type this as mypy assumes args are all same type, but blockwise args alternate types
dtype: np.dtype | None = None,
adjust_chunks: dict[Any, Callable] | None = None,
new_axes: dict[Any, int] | None = None,
align_arrays: bool = True,
target_store=None,
**kwargs,
):
from cubed.core.ops import blockwise
# TODO where to get the target_store kwarg from? Filter down from a blockwise call? Set as attribute on CubedManager?
return blockwise(
func,
out_ind,
*args,
dtype=dtype,
adjust_chunks=adjust_chunks,
new_axes=new_axes,
align_arrays=align_arrays,
target_store=target_store,
**kwargs,
)
def apply_gufunc(
self,
func: Callable,
signature: str,
*args: Any,
axes: Sequence[tuple[int, ...]] | None = None,
axis: int | None = None,
keepdims: bool = False,
output_dtypes: Sequence[np.typing.DTypeLike] | None = None,
output_sizes: dict[str, int] | None = None,
vectorize: bool | None = None,
allow_rechunk: bool = False,
**kwargs,
):
if allow_rechunk:
raise NotImplementedError(
"cubed.apply_gufunc doesn't support allow_rechunk"
)
if keepdims:
raise NotImplementedError("cubed.apply_gufunc doesn't support keepdims")
from cubed import apply_gufunc
return apply_gufunc(
func,
signature,
*args,
axes=axes,
axis=axis,
output_dtypes=output_dtypes,
output_sizes=output_sizes,
vectorize=vectorize,
**kwargs,
)
def unify_chunks(
self,
*args: Any, # can't type this as mypy assumes args are all same type, but dask unify_chunks args alternate types
**kwargs,
) -> tuple[dict[str, T_NormalizedChunks], list["CubedArray"]]:
from cubed.array_api import asarray
from cubed.core import unify_chunks
# Ensure that args are Cubed arrays. Note that we do this here and not in Cubed, following
# https://numpy.org/neps/nep-0047-array-api-standard.html#the-asarray-asanyarray-pattern
arginds = [
(asarray(a) if ind is not None else a, ind) for a, ind in partition(2, args)
]
array_args = [item for pair in arginds for item in pair]
return unify_chunks(*array_args, **kwargs)
def store(
self,
sources: Union["CubedArray", Sequence["CubedArray"]],
targets: Any,
**kwargs,
):
"""Used when writing to any backend."""
from cubed.core.ops import store
return store(
sources,
targets,
**kwargs,
)