Repository navigation
Consistent slicing names - #2601
Conversation
This helps to make slicing names more consistent. This would also allow us to remove some logic from the slicing code itself in the future if we wish.
e823c5e to
97a90ab
Compare
|
@jcrist this could use your review if you have the time |
| start += dim | ||
| if stop is not None: | ||
| while stop < 0: | ||
| stop += dim |
There was a problem hiding this comment.
I don't think this is correct. This seems to imply that given dim 10, stop -11 should be equal to 9, which isn't true. I think you want to just add dim once, and zero if still negative.
In [7]: import numpy as np
In [8]: x = np.arange(10)
In [9]: x[:-1]
Out[9]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])
In [10]: x[:-11]
Out[10]: array([], dtype=int64)
In [11]: x[:9]
Out[11]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])There was a problem hiding this comment.
I think this does the right thing (untested):
def normalize_slice(idx, dim):
if isinstance(idx, slice):
start, stop, step = idx.start, idx.stop, idx.step
if start is not None:
if start < 0:
start = max(0, start + dim)
elif start > dim:
start = dim
if stop is not None:
if stop < 0:
stop = max(0, stop + dim)
elif stop > dim:
stop = dim
if start == 0:
start = None
if stop == dim:
stop = None
if step == 1:
step = None
return slice(start, stop, step)
return idxThere was a problem hiding this comment.
Thanks. I've stolen your code here and added the appropriate tests
| 4. Replaces numpy arrays with lists | ||
| 5. Posify's integers and lists | ||
| 6. Normalizes slices to canonical form | ||
| """ |
There was a problem hiding this comment.
Much of this normalization is repeated in the actual slicing code (e.g. replace_ellipsis is called in slice_array). Should probably clean up the repeated work?
There was a problem hiding this comment.
The challenge here is that all of the current accuracy checks are on the internal functions, not on the public API, so we'll have to rewrite a fair amount of the test suite. I'll look into how expensive this will be.
There was a problem hiding this comment.
I've removed checks elsewhere and moved testing logic outside
| assert same_keys(x[0:], x[:10]) | ||
| assert same_keys(x[0:], x[0:10]) | ||
| assert same_keys(x[0:], x[0:10:1]) | ||
| assert same_keys(x[:], x[0:10:1]) |
There was a problem hiding this comment.
Should probably check with negative/out-of-bounds slices too:
assert same_keys(x[-11:11], x[:])
assert same_keys(x[-11:-9], x[:1])There was a problem hiding this comment.
I meant to check if the keys were the same (since the oob slices normalize to the boundaries). Not a big deal though.
|
Merging soon if there are no further comments. |
jcrist
left a comment
There was a problem hiding this comment.
Overall this seems good to me.
| assert same_keys(x[0:], x[:10]) | ||
| assert same_keys(x[0:], x[0:10]) | ||
| assert same_keys(x[0:], x[0:10:1]) | ||
| assert same_keys(x[:], x[0:10:1]) |
There was a problem hiding this comment.
I meant to check if the keys were the same (since the oob slices normalize to the boundaries). Not a big deal though.
No description provided.