-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add to_numpy() and as_numpy() methods #5568
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
17c5755
added to_numpy() and as_numpy() methods
TomNicholas 48ba107
remove special-casing of cupy arrays in .values in favour of using .t…
TomNicholas ae6e931
lint
max-sixty dc24d3f
Fix mypy (I think?)
max-sixty 6ce6b05
Merge branch 'main' of https://github.com/pydata/xarray into to_numpy
TomNicholas 04d7b02
Merge branch 'to_numpy' of https://github.com/TomNicholas/xarray into…
TomNicholas ee34649
added Dataset.as_numpy()
TomNicholas 552b322
improved docstrings
TomNicholas 1215e69
add what's new
TomNicholas af8a1ee
add to API docs
TomNicholas e095bf0
linting
TomNicholas eb7d84d
fix failures by only importing pint when needed
TomNicholas 74c05e3
refactor pycompat into class
TomNicholas 45245d0
compute instead of load
TomNicholas 27fc4e5
added tests
TomNicholas 3e8cb24
fixed sparse test
TomNicholas f9d6370
tests and fixes for ds.as_numpy()
TomNicholas 50fdf4c
fix sparse tests
TomNicholas 1c94a97
fix linting
TomNicholas 2d07c0f
tests for Variable
TomNicholas 9673cea
test IndexVariable too
TomNicholas 0d624cc
use numpy.asarray to avoid a copy
TomNicholas 2f1ff46
also convert coords
TomNicholas afd35e2
Merge branch 'main' into to_numpy
TomNicholas 6d33b35
Force tests again after #5600
TomNicholas eae95f5
Merge branch 'main' into to_numpy
TomNicholas b90b7e3
Apply suggestions from code review
dcherian f39b301
Update xarray/core/variable.py
dcherian 8b346d3
fix import
TomNicholas 576ab7b
formatting
TomNicholas 4ed1dd8
Fix fsspec error by merging branch 'main' into to_numpy
TomNicholas 976f89a
remove type check
TomNicholas 7bc5d6f
remove attempt to call to_numpy
TomNicholas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,63 @@ | ||
from distutils.version import LooseVersion | ||
from importlib import import_module | ||
|
||
import numpy as np | ||
|
||
from .utils import is_duck_array | ||
|
||
integer_types = (int, np.integer) | ||
|
||
try: | ||
import dask | ||
import dask.array | ||
from dask.base import is_dask_collection | ||
|
||
dask_version = LooseVersion(dask.__version__) | ||
class DuckArrayModule: | ||
""" | ||
Solely for internal isinstance and version checks. | ||
|
||
# solely for isinstance checks | ||
dask_array_type = (dask.array.Array,) | ||
Motivated by having to only import pint when required (as pint currently imports xarray) | ||
https://github.com/pydata/xarray/pull/5561#discussion_r664815718 | ||
""" | ||
|
||
def is_duck_dask_array(x): | ||
return is_duck_array(x) and is_dask_collection(x) | ||
def __init__(self, mod): | ||
try: | ||
duck_array_module = import_module(mod) | ||
duck_array_version = LooseVersion(duck_array_module.__version__) | ||
|
||
if mod == "dask": | ||
duck_array_type = (import_module("dask.array").Array,) | ||
elif mod == "pint": | ||
duck_array_type = (duck_array_module.Quantity,) | ||
elif mod == "cupy": | ||
duck_array_type = (duck_array_module.ndarray,) | ||
elif mod == "sparse": | ||
duck_array_type = (duck_array_module.SparseArray,) | ||
else: | ||
raise NotImplementedError | ||
|
||
except ImportError: # pragma: no cover | ||
duck_array_module = None | ||
duck_array_version = LooseVersion("0.0.0") | ||
duck_array_type = () | ||
|
||
self.module = duck_array_module | ||
self.version = duck_array_version | ||
self.type = duck_array_type | ||
self.available = duck_array_module is not None | ||
|
||
except ImportError: # pragma: no cover | ||
dask_version = LooseVersion("0.0.0") | ||
dask_array_type = () | ||
is_duck_dask_array = lambda _: False | ||
is_dask_collection = lambda _: False | ||
|
||
try: | ||
# solely for isinstance checks | ||
import sparse | ||
def is_duck_dask_array(x): | ||
if DuckArrayModule("dask").available: | ||
from dask.base import is_dask_collection | ||
|
||
return is_duck_array(x) and is_dask_collection(x) | ||
else: | ||
return False | ||
|
||
|
||
sparse_version = LooseVersion(sparse.__version__) | ||
sparse_array_type = (sparse.SparseArray,) | ||
except ImportError: # pragma: no cover | ||
sparse_version = LooseVersion("0.0.0") | ||
sparse_array_type = () | ||
dsk = DuckArrayModule("dask") | ||
TomNicholas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dask_version = dsk.version | ||
dask_array_type = dsk.type | ||
|
||
try: | ||
# solely for isinstance checks | ||
import cupy | ||
sp = DuckArrayModule("sparse") | ||
sparse_array_type = sp.type | ||
sparse_version = sp.version | ||
|
||
cupy_version = LooseVersion(cupy.__version__) | ||
cupy_array_type = (cupy.ndarray,) | ||
except ImportError: # pragma: no cover | ||
cupy_version = LooseVersion("0.0.0") | ||
cupy_array_type = () | ||
cupy_array_type = DuckArrayModule("cupy").type |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.