Skip to content

Add isin for dask arrays - #3363

Merged
jakirkham merged 2 commits into
masterfrom
isin
Mar 31, 2018
Merged

jakirkham merged 2 commits into
masterfrom
isin

Conversation

@shoyer

@shoyer shoyer commented Mar 30, 2018

Copy link
Copy Markdown
Member
  • Tests added / passed
  • Passes flake8 dask
  • Fully documented, including docs/source/changelog.rst for all changes
    and one of the docs/source/*-api.rst files for new API

@shoyer
shoyer requested a review from jakirkham March 30, 2018 19:19
[(20, 20), (4, 5)],
])
@pytest.mark.parametrize("invert", [True, False])
def test_isin_rand(seed, low, high, elements_shape, elements_chunks,

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 need to skip these tests for old versions of NumPy.

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.

Seems reasonable (especially given isin was introduced in NumPy 1.13).

There should be some pytest.skip or @pytest.mark.skipif lines floating around that can be used.

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

Thanks @shoyer. Have been meaning to do this for a while. Guess you beat me to it. ;)

Looks pretty good. Had some questions about how test_elements factors into the result shape and a couple minor comments. Otherwise seems fine.

Comment thread dask/array/routines.py
@wraps(getattr(np, 'isin', None))
def isin(element, test_elements, assume_unique=False, invert=False):
element = asarray(element)
test_elements = asarray(test_elements)

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.

Are we ok with this as far as other libraries (e.g. xarray) are concerned?

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 think so? test_elements is just a set of elements to test against.

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.

FWIW I'm ok with it if you are. Was just reminded of issue ( #2912 ).

Comment thread dask/array/tests/test_routines.py Outdated
d1 = da.from_array(a1, chunks=elements_chunks)

a2 = rng.randint(low, high, size=elements_shape) - 5
d2 = da.from_array(a2, chunks=elements_chunks)

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 these be test_shape and test_chunks?

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.

Oops, yes indeed.

Comment thread dask/array/routines.py
@wraps(getattr(np, 'isin', None))
def isin(element, test_elements, assume_unique=False, invert=False):
element = asarray(element)
test_elements = asarray(test_elements)

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.

On a different point, it looks like NumPy's isin flattens the test_elements array first. This might simplify some of the chunk handling below and in the kernel function. Thoughts on doing this here?

@shoyer shoyer Mar 30, 2018 •

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 don't see a major advantage to doing this. The logic would be slightly simpler (only one dimension to keep track of), but reshaping might be slightly expensive if lots of chunks need to move around.

Comment thread docs/source/changelog.rst Outdated

- Add ``piecewise`` for Dask Arrays (:pr:`3350`) `John A Kirkham`_
- Fix handling of ``nan`` in ``broadcast_shapes`` (:pr:`3356`) `John A Kirkham`_
- Add `isin` for dask arrays. `Stephan Hoyer`_

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.

Guessing you were waiting to see what number it would give you. ;)

Comment thread dask/array/routines.py Outdated
element, element_axes,
test_elements, test_axes,
adjust_chunks={axis: lambda _: 1 for axis in test_axes},
dtype=np.bool_,

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.

Slight preference for np.bool8 or bool if that's ok.

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.

These are all basically equivalent, so sure.

@shoyer shoyer mentioned this pull request Mar 31, 2018
3 tasks done
@shoyer

shoyer commented Mar 31, 2018

Copy link
Copy Markdown
Member Author

The build failure looks totally unrelated: test_parquet_pyarrow[hdfs3]

@jakirkham

Copy link
Copy Markdown
Member

Yeah, that's a known issue ( #3345 ) on master. We typically don't run those tests on PRs.

@jakirkham
jakirkham merged commit 3d861c7 into master Mar 31, 2018
@jakirkham
jakirkham deleted the isin branch March 31, 2018 18:07
@jakirkham

Copy link
Copy Markdown
Member

Thanks @shoyer.

Comment thread docs/source/array-api.rst
isclose
iscomplex
isfinite
isin

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.

Missed that we didn't have an autofunction line with isin. Fixing with PR ( #3364 ).

shoyer added a commit to shoyer/xarray that referenced this pull request Apr 5, 2018
I added a section to the indexing docs showing how to use `isin` with `where`
to do idnexing.

Adding support for older numpy turned out to be pretty easy, so I added a copy
of np.isin in npcompat.

This PR also makes use of dask.array.isin, so that we can support a dask array
properly as the second argument:
dask/dask#3363
shoyer added a commit to pydata/xarray that referenced this pull request Apr 6, 2018
* isin: better docs, support older numpy and use dask.array.isin.

I added a section to the indexing docs showing how to use `isin` with `where`
to do idnexing.

Adding support for older numpy turned out to be pretty easy, so I added a copy
of np.isin in npcompat.

This PR also makes use of dask.array.isin, so that we can support a dask array
properly as the second argument:
dask/dask#3363

* lint

* more lint

* yet more lint
@shoyer shoyer mentioned this pull request Apr 16, 2018
3 tasks done
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.

2 participants