Skip to content

Commit

Permalink
only use bottleneck version 1.0 and later (#1190)
Browse files Browse the repository at this point in the history
* only use bottleneck version 1.0 and later

* move bottleneck version warning to Rolling.__init__
  • Loading branch information
Joe Hamman authored Jan 7, 2017
1 parent 27d04a1 commit 417797e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/installing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ For accelerating xarray

- `bottleneck <https://github.com/kwgoodman/bottleneck>`__: speeds up
NaN-skipping and rolling window aggregations by a large factor
(1.0 or later)
- `cyordereddict <https://github.com/shoyer/cyordereddict>`__: speeds up most
internal operations with xarray data structures (for python versions < 3.5)

Expand Down
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ Breaking changes
``vmax`` when these kwargs are explicitly provided (:issue:`1191`). The
automated level selection logic also slightly changed.
By `Fabien Maussion <https://github.com/fmaussion>`_.
- xarray no longer supports python 3.3 or versions of dask prior to v0.9.0.
- xarray no longer supports python 3.3, versions of dask prior to v0.9.0,
or versions of bottleneck prior to v1.0.

Deprecations
~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions xarray/backends/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
except ImportError:
from threading import Lock


# Create a logger object, but don't add any handlers. Leave that to user code.
logger = logging.getLogger(__name__)

Expand Down
4 changes: 4 additions & 0 deletions xarray/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import inspect
import operator
import warnings
from distutils.version import StrictVersion

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -504,6 +505,9 @@ def inject_bottleneck_rolling_methods(cls):

# bottleneck rolling methods
if has_bottleneck:
if StrictVersion(bn.__version__) < StrictVersion('1.0'):
return

for bn_name, method_name in BOTTLENECK_ROLLING_METHODS.items():
f = getattr(bn, bn_name)
func = cls._bottleneck_reduce(f)
Expand Down
11 changes: 10 additions & 1 deletion xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from __future__ import division
from __future__ import print_function
import numpy as np
import warnings
from distutils.version import StrictVersion

from .pycompat import OrderedDict, zip
from .common import ImplementsRollingArrayReduce, full_like
from .combine import concat
from .ops import inject_bottleneck_rolling_methods
from .ops import inject_bottleneck_rolling_methods, has_bottleneck, bn


class Rolling(object):
Expand Down Expand Up @@ -48,6 +50,13 @@ def __init__(self, obj, min_periods=None, center=False, **windows):
rolling : type of input argument
"""

if (has_bottleneck and
(StrictVersion(bn.__version__) < StrictVersion('1.0'))):
warnings.warn('xarray requires bottleneck version of 1.0 or '
'greater for rolling operations. Rolling '
'aggregation methods will use numpy instead'
'of bottleneck.')

if len(windows) != 1:
raise ValueError('exactly one dim/window should be provided')

Expand Down
10 changes: 9 additions & 1 deletion xarray/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import print_function
import warnings
from contextlib import contextmanager
from distutils.version import StrictVersion

import numpy as np
from numpy.testing import assert_array_equal
Expand Down Expand Up @@ -68,6 +69,8 @@

try:
import bottleneck
if StrictVersion(bottleneck.__version__) < StrictVersion('1.0'):
raise ImportError('Fall back to numpy')
has_bottleneck = True
except ImportError:
has_bottleneck = False
Expand All @@ -76,6 +79,7 @@
# Generally `pytest.importorskip('package')` inline is even easier
requires_matplotlib = pytest.mark.skipif(not has_matplotlib, reason='requires matplotlib')


def requires_scipy(test):
return test if has_scipy else pytest.mark.skip('requires scipy')(test)

Expand Down Expand Up @@ -192,16 +196,18 @@ def assertDataArrayIdentical(self, ar1, ar2):
def assertDataArrayAllClose(self, ar1, ar2, rtol=1e-05, atol=1e-08):
assert_xarray_allclose(ar1, ar2, rtol=rtol, atol=atol)


def assert_xarray_equal(a, b):
import xarray as xr
___tracebackhide__ = True # noqa: F841
assert type(a) == type(b)
if isinstance(a, (xr.Variable, xr.DataArray, xr.Dataset)):
assert a.equals(b), '{}\n{}'.format(a, b)
else:
else:
raise TypeError('{} not supported by assertion comparison'
.format(type(a)))


def assert_xarray_identical(a, b):
import xarray as xr
___tracebackhide__ = True # noqa: F841
Expand All @@ -215,6 +221,7 @@ def assert_xarray_identical(a, b):
raise TypeError('{} not supported by assertion comparison'
.format(type(a)))


def assert_xarray_allclose(a, b, rtol=1e-05, atol=1e-08):
import xarray as xr
___tracebackhide__ = True # noqa: F841
Expand All @@ -241,6 +248,7 @@ def assert_xarray_allclose(a, b, rtol=1e-05, atol=1e-08):
raise TypeError('{} not supported by assertion comparison'
.format(type(a)))


class UnexpectedDataAccess(Exception):
pass

Expand Down
7 changes: 5 additions & 2 deletions xarray/test/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2381,7 +2381,9 @@ def da(request):
return da

if request.param == 2:
return DataArray([0, np.nan, 1, 2, np.nan, 3, 4, 5, np.nan, 6, 7], dims='time')
return DataArray([0, np.nan, 1, 2, np.nan, 3, 4, 5, np.nan, 6, 7],
dims='time')


def test_rolling_iter(da):

Expand All @@ -2393,8 +2395,9 @@ def test_rolling_iter(da):
for i, (label, window_da) in enumerate(rolling_obj):
assert label == da['time'].isel(time=i)


def test_rolling_properties(da):
pytest.importorskip('bottleneck')
pytest.importorskip('bottleneck', minversion='1.0')

rolling_obj = da.rolling(time=4)

Expand Down

0 comments on commit 417797e

Please sign in to comment.