Skip to content

Commit

Permalink
Deprecate allow_lazy (#3435)
Browse files Browse the repository at this point in the history
* Deprecate allow_lazy

* add whats-new

* test that reductions are lazy

* minor whats-new fix.

* fix merge wahts=new

* fix bad merge.

* remove tests that only work with nep-18

* Update doc/whats-new.rst

Co-Authored-By: Mathias Hauser <mathause@users.noreply.github.com>

* Update xarray/core/variable.py

Co-Authored-By: Mathias Hauser <mathause@users.noreply.github.com>

* fix whats-new

* Fix test that assumed NEP-18

* fix tests.
  • Loading branch information
dcherian authored Nov 13, 2019
1 parent e70138b commit 94525bb
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Bug fixes
By `Deepak Cherian <https://github.com/dcherian>`_.
- Sync with cftime by removing `dayofwk=-1` for cftime>=1.0.4.
By `Anderson Banihirwe <https://github.com/andersy005>`_.
- Rolling reduction operations no longer compute dask arrays by default. (:issue:`3161`).
In addition, the ``allow_lazy`` kwarg to ``reduce`` is deprecated.
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fix :py:meth:`xarray.core.groupby.DataArrayGroupBy.reduce` and
:py:meth:`xarray.core.groupby.DatasetGroupBy.reduce` when reducing over multiple dimensions.
(:issue:`3402`). By `Deepak Cherian <https://github.com/dcherian/>`_
Expand Down
17 changes: 4 additions & 13 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ def _reduce_method(cls, func: Callable, include_skipna: bool, numeric_only: bool
if include_skipna:

def wrapped_func(self, dim=None, axis=None, skipna=None, **kwargs):
return self.reduce(
func, dim, axis, skipna=skipna, allow_lazy=True, **kwargs
)
return self.reduce(func, dim, axis, skipna=skipna, **kwargs)

else:

def wrapped_func(self, dim=None, axis=None, **kwargs): # type: ignore
return self.reduce(func, dim, axis, allow_lazy=True, **kwargs)
return self.reduce(func, dim, axis, **kwargs)

return wrapped_func

Expand Down Expand Up @@ -83,20 +81,13 @@ def _reduce_method(cls, func: Callable, include_skipna: bool, numeric_only: bool

def wrapped_func(self, dim=None, skipna=None, **kwargs):
return self.reduce(
func,
dim,
skipna=skipna,
numeric_only=numeric_only,
allow_lazy=True,
**kwargs,
func, dim, skipna=skipna, numeric_only=numeric_only, **kwargs
)

else:

def wrapped_func(self, dim=None, **kwargs): # type: ignore
return self.reduce(
func, dim, numeric_only=numeric_only, allow_lazy=True, **kwargs
)
return self.reduce(func, dim, numeric_only=numeric_only, **kwargs)

return wrapped_func

Expand Down
2 changes: 1 addition & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4031,7 +4031,7 @@ def reduce(
keep_attrs: bool = None,
keepdims: bool = False,
numeric_only: bool = False,
allow_lazy: bool = False,
allow_lazy: bool = None,
**kwargs: Any,
) -> "Dataset":
"""Reduce this dataset by applying `func` along some dimension(s).
Expand Down
4 changes: 1 addition & 3 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,7 @@ def _first_or_last(self, op, skipna, keep_attrs):
return self._obj
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=True)
return self.reduce(
op, self._group_dim, skipna=skipna, keep_attrs=keep_attrs, allow_lazy=True
)
return self.reduce(op, self._group_dim, skipna=skipna, keep_attrs=keep_attrs)

def first(self, skipna=None, keep_attrs=None):
"""Return the first element of each group along the group dimension
Expand Down
13 changes: 12 additions & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import itertools
import warnings
from collections import defaultdict
from datetime import timedelta
from distutils.version import LooseVersion
Expand Down Expand Up @@ -1427,7 +1428,7 @@ def reduce(
axis=None,
keep_attrs=None,
keepdims=False,
allow_lazy=False,
allow_lazy=None,
**kwargs,
):
"""Reduce this array by applying `func` along some dimension(s).
Expand Down Expand Up @@ -1468,7 +1469,17 @@ def reduce(

if dim is not None:
axis = self.get_axis_num(dim)

if allow_lazy is not None:
warnings.warn(
"allow_lazy is deprecated and will be removed in version 0.16.0. It is now True by default.",
DeprecationWarning,
)
else:
allow_lazy = True

input_data = self.data if allow_lazy else self.values

if axis is not None:
data = func(input_data, axis=axis, **kwargs)
else:
Expand Down
18 changes: 16 additions & 2 deletions xarray/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import xarray as xr
import xarray.ufuncs as xu
from xarray import DataArray, Dataset, Variable
from xarray.core import duck_array_ops
from xarray.testing import assert_chunks_equal
from xarray.tests import mock

Expand Down Expand Up @@ -217,6 +218,8 @@ def test_reduce(self):
self.assertLazyAndAllClose((u < 1).all("x"), (v < 1).all("x"))
with raises_regex(NotImplementedError, "dask"):
v.median()
with raise_if_dask_computes():
v.reduce(duck_array_ops.mean)

def test_missing_values(self):
values = np.array([0, 1, np.nan, 3])
Expand Down Expand Up @@ -488,7 +491,17 @@ def test_groupby(self):
v = self.lazy_array

expected = u.groupby("x").mean(...)
actual = v.groupby("x").mean(...)
with raise_if_dask_computes():
actual = v.groupby("x").mean(...)
self.assertLazyAndAllClose(expected, actual)

def test_rolling(self):
u = self.eager_array
v = self.lazy_array

expected = u.rolling(x=2).mean()
with raise_if_dask_computes():
actual = v.rolling(x=2).mean()
self.assertLazyAndAllClose(expected, actual)

def test_groupby_first(self):
Expand All @@ -500,7 +513,8 @@ def test_groupby_first(self):
with raises_regex(NotImplementedError, "dask"):
v.groupby("ab").first()
expected = u.groupby("ab").first()
actual = v.groupby("ab").first(skipna=False)
with raise_if_dask_computes():
actual = v.groupby("ab").first(skipna=False)
self.assertLazyAndAllClose(expected, actual)

def test_reindex(self):
Expand Down
4 changes: 4 additions & 0 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,10 @@ def test_reduce(self):

with raises_regex(ValueError, "cannot supply both"):
v.mean(dim="x", axis=0)
with pytest.warns(DeprecationWarning, match="allow_lazy is deprecated"):
v.mean(dim="x", allow_lazy=True)
with pytest.warns(DeprecationWarning, match="allow_lazy is deprecated"):
v.mean(dim="x", allow_lazy=False)

def test_quantile(self):
v = Variable(["x", "y"], self.d)
Expand Down

0 comments on commit 94525bb

Please sign in to comment.