-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor indexing.py
: introduce .oindex
for Explicitly Indexed Arrays
#8750
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
064e2de
first stab at decoupling indexing.py and variable.py
andersy005 bc73e61
Merge branch 'main' into refactor-indexing
andersy005 26f03a0
focus on `oindex()` first
andersy005 bc61343
Merge branch 'main' into refactor-indexing
andersy005 f7230bb
add support for `.oindex()` in `_getitem_with_mask()`
andersy005 ba66e45
add oindex method to LazilyIndexedArray, CopyOnWriteArray, and Memory…
andersy005 cc41d48
Merge branch 'main' into refactor-indexing
andersy005 9c2610f
provide getitem sytnax for `oindex`
andersy005 5ba6d9b
Add ABC and abstractmethod to ExplicitlyIndexedNDArrayMixin
andersy005 6c5fa96
remove ABC/abstract method
andersy005 d8d9866
Fix indexing bug in xarray/core/indexing.py
andersy005 14c1a38
Merge branch 'main' into refactor-indexing
andersy005 c9b6fd7
Merge branch 'main' into refactor-indexing
andersy005 85483b2
update whats-new
andersy005 51d6fe5
Merge branch 'main' into refactor-indexing
andersy005 e6e6f41
replace `_oindex` with `_oindex_get`
andersy005 a94ff50
Update xarray/core/indexing.py
andersy005 56d165d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c38c900
Fix __setitem__ method in IndexCallable class
andersy005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,6 +325,18 @@ def as_integer_slice(value): | |
return slice(start, stop, step) | ||
|
||
|
||
class IndexCallable: | ||
"""Provide getitem syntax for a callable object.""" | ||
|
||
__slots__ = ("func",) | ||
|
||
def __init__(self, func): | ||
self.func = func | ||
|
||
def __getitem__(self, key): | ||
return self.func(key) | ||
|
||
|
||
class BasicIndexer(ExplicitIndexer): | ||
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. drive-by comment: Our TODO list should include migrating these indexer objects to |
||
"""Tuple for basic indexing. | ||
|
||
|
@@ -470,6 +482,13 @@ def __array__(self, dtype: np.typing.DTypeLike = None) -> np.ndarray: | |
# Note this is the base class for all lazy indexing classes | ||
return np.asarray(self.get_duck_array(), dtype=dtype) | ||
|
||
def _oindex(self, key): | ||
andersy005 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise NotImplementedError("This method should be overridden") | ||
|
||
@property | ||
def oindex(self): | ||
return IndexCallable(self._oindex) | ||
|
||
|
||
class ImplicitToExplicitIndexingAdapter(NDArrayMixin): | ||
"""Wrap an array, converting tuples into the indicated explicit indexer.""" | ||
|
@@ -560,6 +579,9 @@ def get_duck_array(self): | |
def transpose(self, order): | ||
return LazilyVectorizedIndexedArray(self.array, self.key).transpose(order) | ||
|
||
def _oindex(self, indexer): | ||
return type(self)(self.array, self._updated_key(indexer)) | ||
|
||
def __getitem__(self, indexer): | ||
if isinstance(indexer, VectorizedIndexer): | ||
array = LazilyVectorizedIndexedArray(self.array, self.key) | ||
|
@@ -663,6 +685,9 @@ def _ensure_copied(self): | |
def get_duck_array(self): | ||
return self.array.get_duck_array() | ||
|
||
def _oindex(self, key): | ||
return type(self)(_wrap_numpy_scalars(self.array[key])) | ||
|
||
def __getitem__(self, key): | ||
return type(self)(_wrap_numpy_scalars(self.array[key])) | ||
|
||
|
@@ -696,6 +721,9 @@ def get_duck_array(self): | |
self._ensure_cached() | ||
return self.array.get_duck_array() | ||
|
||
def _oindex(self, key): | ||
return type(self)(_wrap_numpy_scalars(self.array[key])) | ||
|
||
def __getitem__(self, key): | ||
return type(self)(_wrap_numpy_scalars(self.array[key])) | ||
|
||
|
@@ -1332,6 +1360,10 @@ def _indexing_array_and_key(self, key): | |
def transpose(self, order): | ||
return self.array.transpose(order) | ||
|
||
def _oindex(self, key): | ||
array, key = self._indexing_array_and_key(key) | ||
return array[key] | ||
|
||
def __getitem__(self, key): | ||
array, key = self._indexing_array_and_key(key) | ||
return array[key] | ||
|
@@ -1376,16 +1408,19 @@ def __init__(self, array): | |
) | ||
self.array = array | ||
|
||
def _oindex(self, key): | ||
# manual orthogonal indexing (implemented like DaskIndexingAdapter) | ||
key = key.tuple | ||
value = self.array | ||
for axis, subkey in reversed(list(enumerate(key))): | ||
value = value[(slice(None),) * axis + (subkey, Ellipsis)] | ||
return value | ||
|
||
def __getitem__(self, key): | ||
if isinstance(key, BasicIndexer): | ||
return self.array[key.tuple] | ||
elif isinstance(key, OuterIndexer): | ||
# manual orthogonal indexing (implemented like DaskIndexingAdapter) | ||
key = key.tuple | ||
value = self.array | ||
for axis, subkey in reversed(list(enumerate(key))): | ||
value = value[(slice(None),) * axis + (subkey, Ellipsis)] | ||
return value | ||
return self.oindex[key] | ||
else: | ||
if isinstance(key, VectorizedIndexer): | ||
raise TypeError("Vectorized indexing is not supported") | ||
|
@@ -1395,11 +1430,10 @@ def __getitem__(self, key): | |
def __setitem__(self, key, value): | ||
if isinstance(key, (BasicIndexer, OuterIndexer)): | ||
self.array[key.tuple] = value | ||
elif isinstance(key, VectorizedIndexer): | ||
raise TypeError("Vectorized indexing is not supported") | ||
else: | ||
if isinstance(key, VectorizedIndexer): | ||
raise TypeError("Vectorized indexing is not supported") | ||
else: | ||
raise TypeError(f"Unrecognized indexer: {key}") | ||
raise TypeError(f"Unrecognized indexer: {key}") | ||
|
||
def transpose(self, order): | ||
xp = self.array.__array_namespace__() | ||
|
@@ -1417,24 +1451,25 @@ def __init__(self, array): | |
""" | ||
self.array = array | ||
|
||
def __getitem__(self, key): | ||
def _oindex(self, key): | ||
key = key.tuple | ||
try: | ||
return self.array[key] | ||
except NotImplementedError: | ||
# manual orthogonal indexing | ||
value = self.array | ||
for axis, subkey in reversed(list(enumerate(key))): | ||
value = value[(slice(None),) * axis + (subkey,)] | ||
return value | ||
|
||
def __getitem__(self, key): | ||
if isinstance(key, BasicIndexer): | ||
return self.array[key.tuple] | ||
elif isinstance(key, VectorizedIndexer): | ||
return self.array.vindex[key.tuple] | ||
else: | ||
assert isinstance(key, OuterIndexer) | ||
key = key.tuple | ||
try: | ||
return self.array[key] | ||
except NotImplementedError: | ||
# manual orthogonal indexing. | ||
# TODO: port this upstream into dask in a saner way. | ||
value = self.array | ||
for axis, subkey in reversed(list(enumerate(key))): | ||
value = value[(slice(None),) * axis + (subkey,)] | ||
return value | ||
return self.oindex[key] | ||
|
||
def __setitem__(self, key, value): | ||
if isinstance(key, BasicIndexer): | ||
|
@@ -1510,6 +1545,9 @@ def _convert_scalar(self, item): | |
# a NumPy array. | ||
return to_0d_array(item) | ||
|
||
def _oindex(self, key): | ||
return self.__getitem__(key) | ||
|
||
def __getitem__( | ||
self, indexer | ||
) -> ( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.