Support NumPy array API (experimental) by tomwhite · Pull Request #6804 · pydata/xarray (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation21 Commits12 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

tomwhite

Note: I haven't actually tested this with pytorch (which is the motivating example for #3232).

@tomwhite

dcherian

if isinstance(data, NON_NUMPY_SUPPORTED_ARRAY_TYPES):
if (
isinstance(data, NON_NUMPY_SUPPORTED_ARRAY_TYPES)
or hasattr(data, "__array_function__")

Choose a reason for hiding this comment

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

There's an __array_function__ check down below (lines 245, so it seems like this change should go there).

I suspect we can also delete cupy, dask from NON_NUMPY_SUPPORTED_ARRAY_TYPES

Choose a reason for hiding this comment

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

There's an __array_function__ check down below (lines 245, so it seems like this change should go there).

Good point - thanks for the suggestion! I've moved the change to down below.

I suspect we can also delete cupy, dask from NON_NUMPY_SUPPORTED_ARRAY_TYPES

I haven't tried this. Sounds like this would be a separate change?

andersy005

value = value[(slice(None),) * axis + (subkey, Ellipsis)]
return value
else:
assert isinstance(key, VectorizedIndexer)

Choose a reason for hiding this comment

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

nit pick: since assert statements are removed when python is invoked with -O and -OO parameters, could we raise a proper exception?

Choose a reason for hiding this comment

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

Yes we should raise I think

Choose a reason for hiding this comment

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

Replaced with a TypeError

andersy005

Comment on lines 1321 to 1324

if isinstance(key, BasicIndexer):
self.array[key.tuple] = value
elif isinstance(key, OuterIndexer):
self.array[key.tuple] = value

Choose a reason for hiding this comment

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

unless i'm missing something, we should be able to combine these two branches into one, correct?

if isinstance(key, BasicIndexer):
self.array[key.tuple] = value
elif isinstance(key, OuterIndexer):
self.array[key.tuple] = value
if isinstance(key, (BasicIndexer, OuterIndexer)):
self.array[key.tuple] = value

Choose a reason for hiding this comment

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

Correct - fixed.

@tomwhite

Illviljan

Illviljan

@tomwhite @Illviljan

Co-authored-by: Illviljan 14371165+Illviljan@users.noreply.github.com

@tomwhite @Illviljan

Co-authored-by: Illviljan 14371165+Illviljan@users.noreply.github.com

Illviljan

Choose a reason for hiding this comment

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

Get mypy to check the typing on the tests as well.

Would be nice with some typing on the ArrayApiIndexingAdapter too. But the dask version is unfortunately missing typing, although the pandas version has so maybe it's possible to draw inspiration from that one?

Illviljan

@tomwhite @Illviljan

Co-authored-by: Illviljan 14371165+Illviljan@users.noreply.github.com

@tomwhite @Illviljan

Co-authored-by: Illviljan 14371165+Illviljan@users.noreply.github.com

@tomwhite @Illviljan

Co-authored-by: Illviljan 14371165+Illviljan@users.noreply.github.com

@tomwhite @Illviljan

Co-authored-by: Illviljan 14371165+Illviljan@users.noreply.github.com

@tomwhite @Illviljan

Co-authored-by: Illviljan 14371165+Illviljan@users.noreply.github.com

@tomwhite

dcherian

dcherian

@dcherian

dcherian

Choose a reason for hiding this comment

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

LGTM!

Illviljan

@TomNicholas

@jakirkham

dcherian added a commit to keewis/xarray that referenced this pull request

Jul 22, 2022

@dcherian

This was referenced

May 18, 2023