Skip to content

Commit

Permalink
Display data returned in ufunc error message
Browse files Browse the repository at this point in the history
This makes debugging much easier!
  • Loading branch information
max-sixty committed Sep 13, 2023
1 parent de66dae commit bf75488
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
22 changes: 10 additions & 12 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,11 +766,9 @@ def func(*arrays):
not isinstance(result_data, tuple) or len(result_data) != signature.num_outputs
):
raise ValueError(
"applied function does not have the number of "
"outputs specified in the ufunc signature. "
"Result is not a tuple of {} elements: {!r}".format(
signature.num_outputs, result_data
)
f"applied function does not have the number of "
f"outputs specified in the ufunc signature. "
f"Result is not a tuple of {signature.num_outputs} elements:\n\n{result_data}"
)

objs = _all_of_type(args, Variable)
Expand All @@ -784,21 +782,21 @@ def func(*arrays):
data = as_compatible_data(data)
if data.ndim != len(dims):
raise ValueError(
"applied function returned data with unexpected "
"applied function returned data with an unexpected "
f"number of dimensions. Received {data.ndim} dimension(s) but "
f"expected {len(dims)} dimensions with names: {dims!r}"
f"expected {len(dims)} dimensions with names: {dims!r}. The data returned "
f"was:\n\n{data!r}"
)

var = Variable(dims, data, fastpath=True)
for dim, new_size in var.sizes.items():
if dim in dim_sizes and new_size != dim_sizes[dim]:
raise ValueError(
"size of dimension {!r} on inputs was unexpectedly "
"changed by applied function from {} to {}. Only "
f"size of dimension '{dim}' on inputs was unexpectedly "
f"changed by applied function from {dim_sizes[dim]} to {new_size}. Only "
"dimensions specified in ``exclude_dims`` with "
"xarray.apply_ufunc are allowed to change size.".format(
dim, dim_sizes[dim], new_size
)
"xarray.apply_ufunc are allowed to change size. "
"The data returned was:\n\n{data!r}"
)

var.attrs = attrs
Expand Down
10 changes: 8 additions & 2 deletions xarray/tests/test_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,10 @@ def identity(x):
def tuple3x(x):
return (x, x, x)

with pytest.raises(ValueError, match=r"number of outputs"):
with pytest.raises(
ValueError,
match=r"number of outputs.*Result is not a tuple of 2 elements:\n\n\[0 1 2 3 4 5 6 7 8 9\]",
):
apply_ufunc(identity, variable, output_core_dims=[(), ()])

with pytest.raises(ValueError, match=r"number of outputs"):
Expand All @@ -1682,7 +1685,10 @@ def add_dim(x):
def remove_dim(x):
return x[..., 0]

with pytest.raises(ValueError, match=r"unexpected number of dimensions"):
with pytest.raises(
ValueError,
match=r"unexpected number of dimensions.*The data returned was:\n\n.*array\(\[\[0",
):
apply_ufunc(add_dim, variable, output_core_dims=[("y", "z")])

with pytest.raises(ValueError, match=r"unexpected number of dimensions"):
Expand Down

0 comments on commit bf75488

Please sign in to comment.