-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
support dask arrays in rolling computations using bottleneck functions #1568
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 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9b1b16d
support dask arrays in rolling computations using bottleneck functions
2147ffc
add dask_array_ops.py and fixes for dask/rolling
cee71ff
skip dask rolling tests when dask is not installed
efe37c6
minimum bottleneck version ==> 1.1
f1819db
cleanup
7bbbbf4
better error/warning messages when unsupported version of bottleneck …
d532a1f
simplier checking of bottleneck version
5945572
stop checking bottleneck version
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ dependencies: | |
| - seaborn | ||
| - toolz | ||
| - rasterio | ||
| - bottleneck | ||
| - pip: | ||
| - coveralls | ||
| - pytest-cov | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """Define core operations for xarray objects. | ||
| """ | ||
| import numpy as np | ||
|
|
||
| try: | ||
| import dask.array as da | ||
| except ImportError: | ||
| pass | ||
|
|
||
|
|
||
| def dask_rolling_wrapper(moving_func, a, window, min_count=None, axis=-1): | ||
| '''wrapper to apply bottleneck moving window funcs on dask arrays''' | ||
| # inputs for ghost | ||
| if axis < 0: | ||
| axis = a.ndim + axis | ||
| depth = {d: 0 for d in range(a.ndim)} | ||
| depth[axis] = window - 1 | ||
| boundary = {d: np.nan for d in range(a.ndim)} | ||
| # create ghosted arrays | ||
| ag = da.ghost.ghost(a, depth=depth, boundary=boundary) | ||
| # apply rolling func | ||
| out = ag.map_blocks(moving_func, window, min_count=min_count, | ||
| axis=axis, dtype=a.dtype) | ||
| # trim array | ||
| result = da.ghost.trim_internal(out, depth) | ||
| return result |
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 |
|---|---|---|
|
|
@@ -2627,6 +2627,18 @@ def da(request): | |
| [0, np.nan, 1, 2, np.nan, 3, 4, 5, np.nan, 6, 7], | ||
| dims='time') | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def da_dask(seed=123): | ||
| pytest.importorskip('dask.array') | ||
| rs = np.random.RandomState(seed) | ||
| times = pd.date_range('2000-01-01', freq='1D', periods=21) | ||
| values = rs.normal(size=(1, 21, 1)) | ||
| da = DataArray(values, dims=('a', 'time', 'x')).chunk({'time': 7}) | ||
| da['time'] = times | ||
| return da | ||
|
|
||
|
|
||
| def test_rolling_iter(da): | ||
|
|
||
| rolling_obj = da.rolling(time=7) | ||
|
|
@@ -2646,8 +2658,6 @@ def test_rolling_doc(da): | |
|
|
||
|
|
||
| def test_rolling_properties(da): | ||
| pytest.importorskip('bottleneck', minversion='1.0') | ||
|
|
||
| rolling_obj = da.rolling(time=4) | ||
|
|
||
| assert rolling_obj.obj.get_axis_num('time') == 1 | ||
|
|
@@ -2669,19 +2679,7 @@ def test_rolling_properties(da): | |
| @pytest.mark.parametrize('center', (True, False, None)) | ||
| @pytest.mark.parametrize('min_periods', (1, None)) | ||
| def test_rolling_wrapped_bottleneck(da, name, center, min_periods): | ||
| pytest.importorskip('bottleneck') | ||
| import bottleneck as bn | ||
|
|
||
| # skip if median and min_periods bottleneck version < 1.1 | ||
| if ((min_periods == 1) and | ||
| (name == 'median') and | ||
| (LooseVersion(bn.__version__) < LooseVersion('1.1'))): | ||
| pytest.skip('rolling median accepts min_periods for bottleneck 1.1') | ||
|
|
||
| # skip if var and bottleneck version < 1.1 | ||
| if ((name == 'median') and | ||
| (LooseVersion(bn.__version__) < LooseVersion('1.1'))): | ||
| pytest.skip('rolling median accepts min_periods for bottleneck 1.1') | ||
| bn = pytest.importorskip('bottleneck', minversion="1.1") | ||
|
|
||
| # Test all bottleneck functions | ||
| rolling_obj = da.rolling(time=7, min_periods=min_periods) | ||
|
|
@@ -2698,14 +2696,23 @@ def test_rolling_wrapped_bottleneck(da, name, center, min_periods): | |
| assert_equal(actual, da['time']) | ||
|
|
||
|
|
||
| def test_rolling_invalid_args(da): | ||
| pytest.importorskip('bottleneck', minversion="1.0") | ||
| import bottleneck as bn | ||
| if LooseVersion(bn.__version__) >= LooseVersion('1.1'): | ||
| pytest.skip('rolling median accepts min_periods for bottleneck 1.1') | ||
| with pytest.raises(ValueError) as exception: | ||
| da.rolling(time=7, min_periods=1).median() | ||
| assert 'Rolling.median does not' in str(exception) | ||
| @pytest.mark.parametrize('name', ('sum', 'mean', 'std', 'min', 'max', | ||
| 'median')) | ||
| @pytest.mark.parametrize('center', (True, False, None)) | ||
| @pytest.mark.parametrize('min_periods', (1, None)) | ||
| def test_rolling_wrapped_bottleneck_dask(da_dask, name, center, min_periods): | ||
| pytest.importorskip('dask.array') | ||
| pytest.importorskip('bottleneck', minversion="1.1") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't these tests now work regardless of whether bottleneck is installed? (obviously performance will be better with bottleneck) |
||
| # dask version | ||
| rolling_obj = da_dask.rolling(time=7, min_periods=min_periods) | ||
| actual = getattr(rolling_obj, name)().load() | ||
| # numpy version | ||
| rolling_obj = da_dask.load().rolling(time=7, min_periods=min_periods) | ||
| expected = getattr(rolling_obj, name)() | ||
|
|
||
| # using all-close becuase rolling over ghost cells introduces some | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because |
||
| # precision errors | ||
| assert_allclose(actual, expected) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('center', (True, False)) | ||
|
|
@@ -2718,8 +2725,8 @@ def test_rolling_pandas_compat(da, center, window, min_periods): | |
| if min_periods is not None and window < min_periods: | ||
| min_periods = window | ||
|
|
||
| s_rolling = pd.rolling_mean(s, window, center=center, | ||
| min_periods=min_periods) | ||
| s_rolling = s.rolling(window, center=center, | ||
| min_periods=min_periods).mean() | ||
| da_rolling = da.rolling(index=window, center=center, | ||
| min_periods=min_periods).mean() | ||
| # pandas does some fancy stuff in the last position, | ||
|
|
@@ -2729,6 +2736,7 @@ def test_rolling_pandas_compat(da, center, window, min_periods): | |
| np.testing.assert_allclose(s_rolling.index, | ||
| da_rolling['index']) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('da', (1, 2), indirect=True) | ||
| @pytest.mark.parametrize('center', (True, False)) | ||
| @pytest.mark.parametrize('min_periods', (None, 1, 2, 3)) | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a descriptive error message here?