Skip to content

Support dot with older dask #2205

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 2 commits into from
Jun 1, 2018
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
12 changes: 8 additions & 4 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Documentation
Enhancements
~~~~~~~~~~~~

- `:py:meth:`~DataArray.dot` and :py:func:`~dot` are partly supported with older
dask<0.17.4. (related to :issue:`2203`)
By `Keisuke Fujii <https://github.com/fujiisoup`_.

- :py:meth:`~DataArray.cumsum` and :py:meth:`~DataArray.cumprod` now support
aggregation over multiple dimensions at the same time. This is the default
behavior when dimensions are not specified (previously this raised an error).
Expand All @@ -53,14 +57,14 @@ Enhancements

- :py:meth:`~DataArray.sel`, :py:meth:`~DataArray.isel` & :py:meth:`~DataArray.reindex`,
(and their :py:class:`Dataset` counterparts) now support supplying a ``dict``
as a first argument, as an alternative to the existing approach
as a first argument, as an alternative to the existing approach
of supplying `kwargs`. This allows for more robust behavior
of dimension names which conflict with other keyword names, or are
of dimension names which conflict with other keyword names, or are
not strings.
By `Maximilian Roos <https://github.com/maxim-lian>`_.

- :py:meth:`~DataArray.rename` now supports supplying `kwargs`, as an
alternative to the existing approach of supplying a ``dict`` as the
alternative to the existing approach of supplying a ``dict`` as the
first argument.
By `Maximilian Roos <https://github.com/maxim-lian>`_.

Expand Down Expand Up @@ -96,7 +100,7 @@ Bug fixes
when grouping over dimension coordinates with duplicated entries
(:issue:`2153`).
By `Stephan Hoyer <https://github.com/shoyer>`_

- Fix Dataset.to_netcdf() cannot create group with engine="h5netcdf"
(:issue:`2177`).
By `Stephan Hoyer <https://github.com/shoyer>`_
Expand Down
13 changes: 13 additions & 0 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,19 @@ def dot(*arrays, **kwargs):
output_core_dims = [tuple(d for d in all_dims if d not in
dims + broadcast_dims)]

# older dask than 0.17.4, we use tensordot if possible.
if isinstance(arr.data, dask_array_type):
import dask
if LooseVersion(dask.__version__) < LooseVersion('0.17.4'):
if len(broadcast_dims) == 0 and len(arrays) == 2:
axes = [[arr.get_axis_num(d) for d in arr.dims if d in dims]
for arr in arrays]
return apply_ufunc(duck_array_ops.tensordot, *arrays,
dask='allowed',
input_core_dims=input_core_dims,
output_core_dims=output_core_dims,
kwargs={'axes': axes})

# construct einsum subscripts, such as '...abc,...ab->...c'
# Note: input_core_dims are always moved to the last position
subscripts_list = ['...' + ''.join([dim_map[d] for d in ds]) for ds
Expand Down
8 changes: 5 additions & 3 deletions xarray/tests/test_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,6 @@ def test_dot(use_dask):
if use_dask:
if not has_dask:
pytest.skip('test for dask.')
import dask
if LooseVersion(dask.__version__) < LooseVersion('0.17.3'):
pytest.skip("needs dask.array.einsum")

a = np.arange(30 * 4).reshape(30, 4)
b = np.arange(30 * 4 * 5).reshape(30, 4, 5)
Expand All @@ -784,6 +781,11 @@ def test_dot(use_dask):
assert (actual.data == np.einsum('ij,ijk->k', a, b)).all()
assert isinstance(actual.variable.data, type(da_a.variable.data))

if use_dask:
import dask
if LooseVersion(dask.__version__) < LooseVersion('0.17.3'):
pytest.skip("needs dask.array.einsum")

# for only a single array is passed without dims argument, just return
# as is
actual = xr.dot(da_a)
Expand Down