-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Using oindex and vindex instead of ExplicitIndexer objects. #9273
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 all commits
3029943
ddd4cdb
96ac4b7
18c5c70
b46c320
7d55345
795daf2
f2c4659
d763c02
45ceac6
8d64f60
46a902f
62d474f
07f663e
8b591e0
8cc0d29
5884605
146daff
50791e0
014e7cf
5e22be6
7056aba
43046e8
2a7e2f2
ead4251
08dc10a
ed74afa
76b2d5a
938a846
389a62f
ee81af3
550ba28
0c86622
0f54b64
ab20531
b60accd
222c5c2
810b822
a414965
0b99aea
2ceaeac
749da0b
f58262a
dcd3ac9
2105aa0
fb24e9c
1ffe5e9
6064046
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 |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
if TYPE_CHECKING: | ||
from xarray.core.dataset import Dataset | ||
from xarray.core.types import NestedSequence | ||
from xarray.namedarray._typing import _OuterIndexerKey, _VectorizedIndexerKey | ||
|
||
T_Name = Union[Hashable, None] | ||
|
||
|
@@ -268,11 +269,35 @@ def robust_getitem(array, key, catch=Exception, max_retries=6, initial_delay=500 | |
|
||
|
||
class BackendArray(NdimSizeLenMixin, indexing.ExplicitlyIndexed): | ||
__slots__ = () | ||
|
||
def get_duck_array(self, dtype: np.typing.DTypeLike = None): | ||
key = indexing.BasicIndexer((slice(None),) * self.ndim) | ||
return self[key] # type: ignore[index] | ||
return self[key] # type: ignore [index] | ||
|
||
|
||
class NewBackendArray(NdimSizeLenMixin, indexing.ExplicitlyIndexed): | ||
__slots__ = ("indexing_support",) | ||
|
||
def get_duck_array(self, dtype: np.typing.DTypeLike = None): | ||
key = (slice(None),) * self.ndim | ||
return self[key] # type: ignore [index] | ||
|
||
def _oindex_get(self, key: _OuterIndexerKey) -> Any: | ||
raise NotImplementedError( | ||
f"{self.__class__.__name__}._oindex_get method should be overridden" | ||
) | ||
|
||
def _vindex_get(self, key: _VectorizedIndexerKey) -> Any: | ||
raise NotImplementedError( | ||
f"{self.__class__.__name__}._vindex_get method should be overridden" | ||
) | ||
|
||
@property | ||
def oindex(self) -> indexing.IndexCallable: | ||
return indexing.IndexCallable(self._oindex_get) | ||
|
||
@property | ||
def vindex(self) -> indexing.IndexCallable: | ||
return indexing.IndexCallable(self._vindex_get) | ||
Comment on lines
+294
to
+300
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as reminder the goal is to use I think this gets us nearly all the way to removing the special case for |
||
|
||
|
||
class AbstractDataStore: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,16 +44,33 @@ | |
from xarray.core.dataset import Dataset | ||
from xarray.core.datatree import DataTree | ||
from xarray.core.types import ReadBuffer | ||
from xarray.namedarray._typing import ( | ||
_BasicIndexerKey, | ||
_OuterIndexerKey, | ||
_VectorizedIndexerKey, | ||
) | ||
|
||
|
||
class H5NetCDFArrayWrapper(BaseNetCDF4Array): | ||
indexing_support = indexing.IndexingSupport.OUTER_1VECTOR | ||
|
||
def get_array(self, needs_lock=True): | ||
ds = self.datastore._acquire(needs_lock) | ||
return ds.variables[self.variable_name] | ||
|
||
def __getitem__(self, key): | ||
return indexing.explicit_indexing_adapter( | ||
key, self.shape, indexing.IndexingSupport.OUTER_1VECTOR, self._getitem | ||
def _oindex_get(self, key: _OuterIndexerKey) -> Any: | ||
return indexing.outer_indexing_adapter( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now we have three different adapters . This kind of dispatching was happening in |
||
key, self.shape, self.indexing_support, self._getitem | ||
) | ||
|
||
def _vindex_get(self, key: _VectorizedIndexerKey) -> Any: | ||
return indexing.vectorized_indexing_adapter( | ||
key, self.shape, self.indexing_support, self._getitem | ||
) | ||
|
||
def __getitem__(self, key: _BasicIndexerKey) -> Any: | ||
return indexing.basic_indexing_adapter( | ||
key, self.shape, self.indexing_support, self._getitem | ||
) | ||
|
||
def _getitem(self, key): | ||
|
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 don't think its possible to do this without creating a new
BackendArray
class for backends.