Skip to content

Consistent slicing names - #2601

Merged
mrocklin merged 4 commits into
dask:masterfrom
mrocklin:slicing-names
Aug 29, 2017
Merged

mrocklin merged 4 commits into
dask:masterfrom
mrocklin:slicing-names

Conversation

@mrocklin

Copy link
Copy Markdown
Member

No description provided.

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.
@mrocklin mrocklin changed the title WIP - Consistent slicing names Consistent slicing names Aug 24, 2017
@mrocklin

Copy link
Copy Markdown
Member Author

@jcrist this could use your review if you have the time

Comment thread dask/array/slicing.py Outdated
start += dim
if stop is not None:
while stop < 0:
stop += dim

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 idx

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've stolen your code here and added the appropriate tests

Comment thread dask/array/slicing.py
4. Replaces numpy arrays with lists
5. Posify's integers and lists
6. Normalizes slices to canonical form
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to check if the keys were the same (since the oob slices normalize to the boundaries). Not a big deal though.

@mrocklin

Copy link
Copy Markdown
Member Author

Merging soon if there are no further comments.

@jcrist jcrist left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to check if the keys were the same (since the oob slices normalize to the boundaries). Not a big deal though.

@mrocklin
mrocklin merged commit 55067b2 into dask:master Aug 29, 2017
@mrocklin
mrocklin deleted the slicing-names branch August 29, 2017 13:50
@pp-mo pp-mo mentioned this pull request Sep 4, 2017
@jakirkham

Copy link
Copy Markdown
Member

Incidentally this PR fixed issue ( #2146 ). While there are tests overlapping with the cases noted in that issue, didn't see any specifically covering them. So am adding them in PR ( #2978 ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants