-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Avoid coercing to numpy in as_shared_dtypes
#8714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
c6f4e3a
1467c4c
d9931ef
c067f7d
5092aaa
a884ba8
86e6bf8
630629c
45808d8
e625c67
6833d66
bcd02bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -214,26 +214,32 @@ def astype(data, dtype, **kwargs): | |||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def asarray(data, xp=np): | ||||||||||||||||||||||||||||
print(data) | ||||||||||||||||||||||||||||
print(type(data)) | ||||||||||||||||||||||||||||
return data if is_duck_array(data) else xp.asarray(data) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def as_shared_dtype(scalars_or_arrays, xp=np): | ||||||||||||||||||||||||||||
"""Cast a arrays to a shared dtype using xarray's type promotion rules.""" | ||||||||||||||||||||||||||||
array_type_cupy = array_type("cupy") | ||||||||||||||||||||||||||||
if array_type_cupy and any( | ||||||||||||||||||||||||||||
isinstance(x, array_type_cupy) for x in scalars_or_arrays | ||||||||||||||||||||||||||||
): | ||||||||||||||||||||||||||||
import cupy as cp | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
arrays = [asarray(x, xp=cp) for x in scalars_or_arrays] | ||||||||||||||||||||||||||||
def as_duck_array(data, xp=np): | ||||||||||||||||||||||||||||
if is_duck_array(data): | ||||||||||||||||||||||||||||
return data | ||||||||||||||||||||||||||||
elif hasattr(data, "get_duck_array"): | ||||||||||||||||||||||||||||
# must be a lazy indexing class wrapping a duck array | ||||||||||||||||||||||||||||
return data.get_duck_array() | ||||||||||||||||||||||||||||
|
class _ElementwiseFunctionArray(indexing.ExplicitlyIndexedNDArrayMixin): | |
"""Lazily computed array holding values of elemwise-function. | |
Do not construct this object directly: call lazy_elemwise_func instead. | |
Values are computed upon indexing or coercion to a NumPy array. | |
""" | |
def __init__(self, array, func: Callable, dtype: np.typing.DTypeLike): | |
assert not is_chunked_array(array) | |
self.array = indexing.as_indexable(array) | |
self.func = func | |
self._dtype = dtype |
so you should be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm getting confused as to how this all works now... Don't I want to be computing as_shared_dtype
using the dtype of the outermost wrapped class? Whereas this will step through all the way to the innermost duckarray, which may have a different dtype?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As of now, as_shared_dtype
is expected to return pure duck arrays for stack, concatenate, and where.
So that means we need to read from disk, which you do with to_duck_array
and all these wrapper layers will be resolved.
It will get more complicated when we do lazy concatenation in Xarray, then we'd need to lazily infer dtypes and apply a lazy astype.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously this asarray
call would coerce to numpy unnecessarily, when all we really wanted was an array type that we could examine the .dtype
attribute of.
Uh oh!
There was an error while loading. Please reload this page.