-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add to_numpy() and as_numpy() methods (#5568)
* added to_numpy() and as_numpy() methods * remove special-casing of cupy arrays in .values in favour of using .to_numpy() * lint * Fix mypy (I think?) * added Dataset.as_numpy() * improved docstrings * add what's new * add to API docs * linting * fix failures by only importing pint when needed * refactor pycompat into class * compute instead of load * added tests * fixed sparse test * tests and fixes for ds.as_numpy() * fix sparse tests * fix linting * tests for Variable * test IndexVariable too * use numpy.asarray to avoid a copy * also convert coords * Force tests again after #5600 * Apply suggestions from code review * Update xarray/core/variable.py * fix import * formatting * remove type check Co-authored-by: Stephan Hoyer <[email protected]> * remove attempt to call to_numpy Co-authored-by: Maximilian Roos <[email protected]> Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: Stephan Hoyer <[email protected]>
- Loading branch information
1 parent
86ca67e
commit c5ee050
Showing
10 changed files
with
363 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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") | ||
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 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 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 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.