Skip to content

Display data returned in apply_ufunc error message #8179

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 5 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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}"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would to log this sort of info. It's not a "full report" of the error, since there might be other datas.

(I would vote to merge and then change in the future if it turns out to be confusing, but if someone has a different view I'm v happy to make changes)

)

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