-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add array slicing LinearOperator
#162
Conversation
Codecov Report
@@ Coverage Diff @@
## main #162 +/- ##
==========================================
+ Coverage 92.14% 92.20% +0.06%
==========================================
Files 48 48
Lines 3298 3350 +52
==========================================
+ Hits 3039 3089 +50
- Misses 259 261 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
|
||
|
||
class Slice(LinearOperator): | ||
"""A linear operator for slicing an array.""" |
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.
or block array, I wonder? I think you could use one of these to select a block from a block array, but probably not use one to select a block and index within the block, if I'm reading the docs correctly.
(not saying we should add the functionality, but maybe just address this in the docstring)
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 that would be worth adding. Let's discuss details.
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.
The syntax is already established (first axis indexes the block), but this is not supported due to limitations of the relevant BlockArray
code:
Lines 799 to 807 in 445412e
def __getitem__(self, idx: Union[int, Ellipsis]) -> JaxArray: | |
if isinstance(idx, slice): | |
raise TypeError(f"Slicing not supported on block index") | |
if idx == Ellipsis: | |
return reshape(self._data, self.shape) | |
if idx < 0: | |
idx = self.num_blocks + idx | |
return reshape(self._data[self.bndpos[idx] : self.bndpos[idx + 1]], self.shape[idx]) | |
As you can see, only int
or Ellipsis
indexing expressions are allowed, and there is no support for concatenated indexing of block and block content. It's not clear how difficult it would be to remove the former constraint (one complication would be that indexing could return another BlockArray
rather than a JaxArray
), but I think it would be straightforward to add support for the latter.
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.
Added support for more general indexing of BlockArray, and documented BlockArray support by Slice.
|
||
@pytest.mark.parametrize("slc", slice_examples) | ||
def test_slice_eval(slicetestobj, slc): | ||
x = slicetestobj.x |
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.
Possible alternative: remove SliceTestObj
and slicetestobj
, replace line 586 with
x = snp.zeros((4, 5, 6, 7), dtype=dtype)
, parametrize over dtype
.
My eyes glaze over when I see fixtures, and what is it gaining us here? Perhaps it caches x
?
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 feel the same way. The approach here is a direct copy of the tests for the Sum
operator. No arguments on simplifying the new tests, but it would seem to make sense to simplify the ones for Sum
at the same time?
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.
Agreed. I made an issue for fixing it, let's leave it as is for this PR.
Add a
LinearOperator
that select part of an input array via array slicing/indexing.Is
Slice
the best choice of name for this operator? PerhapsIndex
orSelect
?