Skip to content

Remove duck_array_ops.as_like_arrays() #3204

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 3 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,11 @@ def as_shared_dtype(scalars_or_arrays):
return [x.astype(out_type, copy=False) for x in arrays]


def as_like_arrays(*data):
if all(isinstance(d, dask_array_type) for d in data):
return data
elif any(isinstance(d, sparse_array_type) for d in data):
from sparse import COO

return tuple(COO(d) for d in data)
else:
return tuple(np.asarray(d) for d in data)


def allclose_or_equiv(arr1, arr2, rtol=1e-5, atol=1e-8):
"""Like np.allclose, but also allows values to be NaN in both arrays
"""
arr1, arr2 = as_like_arrays(arr1, arr2)
arr1 = asarray(arr1)
arr2 = asarray(arr2)
if arr1.shape != arr2.shape:
return False
return bool(isclose(arr1, arr2, rtol=rtol, atol=atol, equal_nan=True).all())
Expand All @@ -203,34 +193,27 @@ def allclose_or_equiv(arr1, arr2, rtol=1e-5, atol=1e-8):
def array_equiv(arr1, arr2):
"""Like np.array_equal, but also allows values to be NaN in both arrays
"""
arr1, arr2 = as_like_arrays(arr1, arr2)
arr1 = asarray(arr1)
arr2 = asarray(arr2)
if arr1.shape != arr2.shape:
return False

with warnings.catch_warnings():
warnings.filterwarnings("ignore", "In the future, 'NAT == x'")

flag_array = arr1 == arr2
flag_array |= isnull(arr1) & isnull(arr2)

flag_array = (arr1 == arr2) | (isnull(arr1) & isnull(arr2))
return bool(flag_array.all())


def array_notnull_equiv(arr1, arr2):
"""Like np.array_equal, but also allows values to be NaN in either or both
arrays
"""
arr1, arr2 = as_like_arrays(arr1, arr2)
arr1 = asarray(arr1)
arr2 = asarray(arr2)
if arr1.shape != arr2.shape:
return False

with warnings.catch_warnings():
warnings.filterwarnings("ignore", "In the future, 'NAT == x'")

flag_array = arr1 == arr2
flag_array |= isnull(arr1)
flag_array |= isnull(arr2)

flag_array = (arr1 == arr2) | isnull(arr1) | isnull(arr2)
return bool(flag_array.all())


Expand Down
32 changes: 25 additions & 7 deletions xarray/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,13 @@ def test_variable_property(prop):
(do("all"), False),
(do("any"), False),
(do("astype", dtype=int), True),
(do("broadcast_equals", make_xrvar({"x": 10, "y": 5})), False),
(do("clip", min=0, max=1), True),
(do("coarsen", windows={"x": 2}, func=np.sum), True),
(do("compute"), True),
(do("conj"), True),
(do("copy"), True),
(do("count"), False),
(do("equals", make_xrvar({"x": 10, "y": 5})), False),
(do("get_axis_num", dim="x"), False),
(do("identical", other=make_xrvar({"x": 10, "y": 5})), False),
(do("isel", x=slice(2, 4)), True),
(do("isnull"), True),
(do("load"), True),
Expand All @@ -121,6 +118,21 @@ def test_variable_property(prop):
(do("to_base_variable"), True),
(do("transpose"), True),
(do("unstack", dimensions={"x": {"x1": 5, "x2": 2}}), True),
param(
do("broadcast_equals", make_xrvar({"x": 10, "y": 5})),
False,
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
),
param(
do("equals", make_xrvar({"x": 10, "y": 5})),
False,
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
),
param(
do("identical", make_xrvar({"x": 10, "y": 5})),
False,
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
),
param(
do("argmax"),
True,
Expand Down Expand Up @@ -349,15 +361,13 @@ def test_dataarray_property(prop):
(do("assign_attrs", {"foo": "bar"}), True),
(do("assign_coords", x=make_xrarray({"x": 10}).x + 1), True),
(do("astype", int), True),
(do("broadcast_equals", make_xrarray({"x": 10, "y": 5})), False),
(do("clip", min=0, max=1), True),
(do("compute"), True),
(do("conj"), True),
(do("copy"), True),
(do("count"), False),
(do("diff", "x"), True),
(do("drop", "x"), True),
(do("equals", make_xrarray({"x": 10, "y": 5})), False),
(do("expand_dims", {"z": 2}, axis=2), True),
(do("get_axis_num", "x"), False),
(do("get_index", "x"), False),
Expand All @@ -380,10 +390,18 @@ def test_dataarray_property(prop):
(do("stack", z={"x", "y"}), True),
(do("transpose"), True),
# TODO
# isel_points
# sel_points
# set_index
# swap_dims
param(
do("broadcast_equals", make_xrvar({"x": 10, "y": 5})),
False,
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
Copy link
Member Author

Choose a reason for hiding this comment

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

This issue is fixed, so we should be able to re-enable these tests after the next sparse release.

),
param(
do("equals", make_xrvar({"x": 10, "y": 5})),
False,
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
),
param(
do("argmax"),
True,
Expand Down