From 5c404774f1e876fe655b9df1995785267dac2fe4 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sun, 4 Mar 2018 20:00:08 -0800 Subject: [PATCH 1/4] Better error message for vectorize=True in apply_ufunc with old numpy --- doc/whats-new.rst | 3 +++ xarray/core/computation.py | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ab667ceba3f..937bfd5843f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -50,6 +50,9 @@ Enhancements Bug fixes ~~~~~~~~~ +- Raise an informative error message when using ``apply_ufunc`` with numpy + v1.11 (:issue:`1956`). + By `Stephan Hoyer `_. - Fix the precision drop after indexing datetime64 arrays (:issue:`1932`). By `Keisuke Fujii `_. diff --git a/xarray/core/computation.py b/xarray/core/computation.py index b7590ab6b4b..28ded28440e 100644 --- a/xarray/core/computation.py +++ b/xarray/core/computation.py @@ -3,6 +3,7 @@ NOT PUBLIC API. """ +from distutils.version import LooseVersion import functools import itertools import operator @@ -882,10 +883,21 @@ def earth_mover_distance(first_samples, func = functools.partial(func, **kwargs_) if vectorize: - func = np.vectorize(func, - otypes=output_dtypes, - signature=signature.to_gufunc_string(), - excluded=set(kwargs)) + if signature.all_output_core_dims: + # we need the signature argument + if LooseVersion(np.__version__) < '1.12': # pragma: no cover + raise NotImplementedError( + 'numpy 1.12 or newer required when using vectorize=True ' + 'in xarray.apply_ufunc with non-scalar output core ' + 'dimensions.') + func = np.vectorize(func, + otypes=output_dtypes, + signature=signature.to_gufunc_string(), + excluded=set(kwargs)) + else: + func = np.vectorize(func, + otype=output_dtypes, + excluded=set(kwargs)) variables_ufunc = functools.partial(apply_variable_ufunc, func, signature=signature, From b10f0e64826889537e752cf884c576bb1b30aa5b Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sun, 4 Mar 2018 22:35:51 -0800 Subject: [PATCH 2/4] Typo otype -> otypes --- xarray/core/computation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/computation.py b/xarray/core/computation.py index 28ded28440e..2c9127314f4 100644 --- a/xarray/core/computation.py +++ b/xarray/core/computation.py @@ -896,7 +896,7 @@ def earth_mover_distance(first_samples, excluded=set(kwargs)) else: func = np.vectorize(func, - otype=output_dtypes, + otypes=output_dtypes, excluded=set(kwargs)) variables_ufunc = functools.partial(apply_variable_ufunc, func, From 367c7990514e7f10ab40d015143e20c934634d33 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sun, 4 Mar 2018 22:39:42 -0800 Subject: [PATCH 3/4] add missing __future__ imports --- xarray/core/computation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xarray/core/computation.py b/xarray/core/computation.py index 2c9127314f4..9e203e74469 100644 --- a/xarray/core/computation.py +++ b/xarray/core/computation.py @@ -1,8 +1,7 @@ """ Functions for applying functions that act on arrays to xarray's labeled data. - -NOT PUBLIC API. """ +from __future__ import absolute_import, division, print_function from distutils.version import LooseVersion import functools import itertools From 22ac2bc47ded1b5cec290aabc9bcd1a9d741525c Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Mon, 5 Mar 2018 18:54:28 -0800 Subject: [PATCH 4/4] all_output_core_dims -> all_core_dims --- xarray/core/computation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/computation.py b/xarray/core/computation.py index 9e203e74469..858936aad6c 100644 --- a/xarray/core/computation.py +++ b/xarray/core/computation.py @@ -882,7 +882,7 @@ def earth_mover_distance(first_samples, func = functools.partial(func, **kwargs_) if vectorize: - if signature.all_output_core_dims: + if signature.all_core_dims: # we need the signature argument if LooseVersion(np.__version__) < '1.12': # pragma: no cover raise NotImplementedError(