Skip to content

Dask array slicing with boolean arrays - #2658

Merged
mrocklin merged 6 commits into
dask:masterfrom
mrocklin:dask-array-slicing
Sep 12, 2017
Merged

mrocklin merged 6 commits into
dask:masterfrom
mrocklin:dask-array-slicing

Conversation

@mrocklin

@mrocklin mrocklin commented Sep 5, 2017

Copy link
Copy Markdown
Member

Currently we support this:

x[y > 0]

But not this

x[:, y> 0]

This PR attempts to fix this, but currently fails.

see #2651 and #2430

cc @TomAugspurger

@jakirkham

Copy link
Copy Markdown
Member

As noted in the associated issue, it may be worth running reshape to flatten the dimensions that will be selected from and pulling code from compress to apply this selection mask on the flattened dimensions.

@mrocklin

mrocklin commented Sep 6, 2017

Copy link
Copy Markdown
Member Author

I've added an implementation. However I had forgotten that NumPy does some odd things when slicing with boolean arrays. In particular it seems to prefer to bring that axis to the front.

@mrocklin

mrocklin commented Sep 6, 2017

Copy link
Copy Markdown
Member Author

Not sure what to do here.

@jakirkham

Copy link
Copy Markdown
Member

However I had forgotten that NumPy does some odd things when slicing with boolean arrays. In particular it seems to prefer to bring that axis to the front.

Sorry not following. Do you have an example?

@mrocklin

mrocklin commented Sep 6, 2017

Copy link
Copy Markdown
Member Author

I'm finding this behavior to be confusing:

import numpy as np
x = np.ones((10, 10, 10, 10))
x[1:9, 4, :, np.array([True, False, False, True, False, False, True, True, True, False])].shape

# (5, 8, 10)

This PR has an xfailed test that includes this situation

@shoyer

shoyer commented Sep 6, 2017

Copy link
Copy Markdown
Member

This is numpy's confusing mixed basic/advanced indexing rules again. You get the same sort of behavior with integer instead of boolean arrays.

@shoyer

shoyer commented Sep 6, 2017

Copy link
Copy Markdown
Member

One way to resolve this is to add a dedicated oindex method for outer/orthogonal indexing, as proposed for numpy. Then the confusing case (with "advanced" indexes separated by basic indexes) could be explicitly disallowed. See https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#combining-advanced-and-basic-indexing for the full details

@jakirkham

Copy link
Copy Markdown
Member

Have to say am a bit perplexed by that example as well. Would have to agree that it looks like it is transposing things when it shouldn't. Are we sure it isn't a bug?

@shoyer

shoyer commented Sep 6, 2017

Copy link
Copy Markdown
Member

Definitely not a bug, but you're not the only one who's confused by it. (There have been long discussions about the numpy mailing list about this.)

@mrocklin mrocklin changed the title [WIP] - Dask array slicing with boolean arrays Dask array slicing with boolean arrays Sep 8, 2017
@mrocklin

Copy link
Copy Markdown
Member Author

This is ready for review

@TomAugspurger TomAugspurger 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.

I think this looks good. I ran my tests dask-ml that needed the boolean filtering, and all those passed.

Comment thread dask/array/slicing.py Outdated

if any(isinstance(ind, Array) and ind.dtype == bool and ind.ndim != 1
for ind in index):
raise NotImplementedError("Slicing with dask.array only permitted for "

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 found this error message a bit confusing, I think the "only permitted for dimension 1 and dimension n". Maybe something like "only permitted when the indexer has 1 dimension, or the dimensions of the indexer match the dimensions of the array being sliced."? It's a bit wordier.

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.

Fixed

@TomAugspurger TomAugspurger mentioned this pull request Sep 12, 2017
@mrocklin
mrocklin merged commit a1a75a7 into dask:master Sep 12, 2017
@mrocklin
mrocklin deleted the dask-array-slicing branch September 12, 2017 20:06
fujiisoup pushed a commit to fujiisoup/dask that referenced this pull request Feb 3, 2018
* Support slicing with boolean dask arrays

* add failing test for boolean slicing

* squash

* Partially support indexing with dimension 1 < ... < n

* remove cases other than 1 and all dimensions

* update error message
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.

4 participants