-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix assert_equal with check_dim_order=False for mixed dimension orders #10718
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
a6d264c
d3f685d
4e6e976
96299cc
87c2065
dad4370
5567340
358032b
317a9ea
6246928
e22aaf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,24 +85,31 @@ def assert_isomorphic(a: DataTree, b: DataTree): | |
|
||
|
||
def maybe_transpose_dims(a, b, check_dim_order: bool): | ||
"""Helper for assert_equal/allclose/identical""" | ||
"""Helper for assert_equal/allclose/identical | ||
|
||
Returns (a, b) tuple with dimensions transposed to canonical order if needed. | ||
""" | ||
|
||
__tracebackhide__ = True | ||
|
||
def _maybe_transpose_dims(a, b): | ||
if not isinstance(a, Variable | DataArray | Dataset): | ||
return b | ||
if set(a.dims) == set(b.dims): | ||
# Ensure transpose won't fail if a dimension is missing | ||
# If this is the case, the difference will be caught by the caller | ||
return b.transpose(*a.dims) | ||
return b | ||
return a, b | ||
|
||
# Find common dimensions and transpose both to canonical order | ||
common_dims = set(a.dims) & set(b.dims) | ||
if common_dims: | ||
# Use sorted order as canonical, with ellipsis for any unique dims | ||
canonical_order = sorted(common_dims) + [...] | ||
return a.transpose(*canonical_order), b.transpose(*canonical_order) | ||
return a, b | ||
|
||
if check_dim_order: | ||
return b | ||
return a, b | ||
|
||
if isinstance(a, DataTree): | ||
return map_over_datasets(_maybe_transpose_dims, a, b) | ||
# DataTree case needs special handling - only transpose b | ||
return a, map_over_datasets(lambda a, b: _maybe_transpose_dims(a, b)[1], a, b) | ||
|
||
|
||
return _maybe_transpose_dims(a, b) | ||
|
||
|
@@ -139,7 +146,7 @@ def assert_equal(a, b, check_dim_order: bool = True): | |
assert type(a) is type(b) or ( | ||
isinstance(a, Coordinates) and isinstance(b, Coordinates) | ||
) | ||
b = maybe_transpose_dims(a, b, check_dim_order) | ||
a, b = maybe_transpose_dims(a, b, check_dim_order) | ||
if isinstance(a, Variable | DataArray): | ||
assert a.equals(b), formatting.diff_array_repr(a, b, "equals") | ||
elif isinstance(a, Dataset): | ||
|
@@ -227,7 +234,7 @@ def assert_allclose( | |
""" | ||
__tracebackhide__ = True | ||
assert type(a) is type(b) | ||
b = maybe_transpose_dims(a, b, check_dim_order) | ||
a, b = maybe_transpose_dims(a, b, check_dim_order) | ||
|
||
equiv = functools.partial( | ||
_data_allclose_or_equiv, rtol=rtol, atol=atol, decode_bytes=decode_bytes | ||
|
Uh oh!
There was an error while loading. Please reload this page.