Skip to content
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 slicing support for ellipsis and None #37

Merged
merged 5 commits into from Dec 25, 2017
Merged

Conversation

mrocklin
Copy link
Collaborator

I ran into this while testing https://github.com/mrocklin/sparse/pull/35 .

@hameerabbasi if you have a moment to look this over I would welcome your review

@hameerabbasi
Copy link
Collaborator

hameerabbasi commented Dec 24, 2017

Looks great to me. One minor point: A check to see there isn't more than one Ellipsis and a subsequent exception + test for that exception would be great. Either way, it doesn't block previous use cases, so should be good to merge.

Edit: Ideally, the exception should be IndexError: an index can only have a single ellipsis ('...') to match Numpy behavior.

@mrocklin
Copy link
Collaborator Author

Added such a test. It looks like Python handles this check for us.

@mrocklin
Copy link
Collaborator Author

Ah, never mind. My test was faulty here

sparse/core.py Outdated
if len(index) - index.count(None) > self.ndim:
raise IndexError("too many indices for array")
if index.count(Ellipsis) > 1:
raise IndexError("an index can only have a single sllipsis ('...')")
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a spelling error here. sllipsis should be Ellipsis.

@mrocklin
Copy link
Collaborator Author

mrocklin commented Dec 24, 2017 via email

sparse/core.py Outdated
@@ -233,10 +233,19 @@ def __sizeof__(self):
def __getitem__(self, index):
if not isinstance(index, tuple):
index = (index,)
index = tuple(ind + self.shape[i] if isinstance(ind, numbers.Integral) and ind < 0 else ind
if len(index) - index.count(None) > self.ndim:
raise IndexError("too many indices for array")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will fail on things like a[1, ...] where a is (n,)-shaped. There should be an additional factor of - index.count(Ellipsis) here.

@hameerabbasi
Copy link
Collaborator

Sorry for requesting so much changes. Here's the last one. It's nitpicky, sure, but I want to make sure all our bases are covered.

@mrocklin
Copy link
Collaborator Author

Sorry for requesting so much changes. Here's the last one. It's nitpicky, sure, but I want to make sure all our bases are covered.

No, this is great. Thank you for all of your work reviewing here.

sparse/core.py Outdated
if coords:
coords = np.stack(coords, axis=0)
else:
coords = np.array(coords)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I may be missing something here, but won't np.array([]) create an array with a dtype of float64 and a shape of (0,)? It would be much nicer to do this: coords = np.empty((len(shape), 0), dtype=np.uint8)

Copy link
Collaborator

Choose a reason for hiding this comment

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

And also to specify a dtype even in the first case, as for empty arrays it might also be float64. np.result_type and np.min_scalar_type should be useful here.

@hameerabbasi
Copy link
Collaborator

Some minor comments that will hopefully address the CI failures.

@mrocklin mrocklin merged commit 8c20514 into master Dec 25, 2017
@mrocklin mrocklin deleted the slice-ellipsis branch December 25, 2017 19:28
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.

None yet

2 participants