-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add argument check_dims to assert_allclose to allow transposed inputs (#5733) #8991
Add argument check_dims to assert_allclose to allow transposed inputs (#5733) #8991
Conversation
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this @ignamv !
xarray/testing/assertions.py
Outdated
if not isinstance(a, (Variable, DataArray, Dataset)): | ||
return b | ||
if check_dims == "transpose": | ||
assert set(a.dims) == set(b.dims), f"Dimensions differ: {a.dims} {b.dims}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming that Dataset.dims
returns an ordered type may no longer be true after #8980, but that's my problem, not yours 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This future change is also raising a warning during the tests, not sure if I should be suppressing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay so we should deal with it now then. Basically Dataset
objects don't really have ordered dimensions, and Dataset.dims
will be changed to return a set
type in future. So check_dim_order
can't do anything when comparing dataset objects, the most you can do is check that all the same dims are present.
xarray/testing/assertions.py
Outdated
if check_dims == "transpose": | ||
assert set(a.dims) == set(b.dims), f"Dimensions differ: {a.dims} {b.dims}" | ||
return b.transpose(*a.dims) | ||
assert a.dims == b.dims, f"Dimensions differ: {a.dims} {b.dims}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good - if the dimensions differ there is no point checking anything else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure we don't have better diffing functions further down the stack? We should ensure the output of an error is no worse with this than with this commented out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usual diff would allow you to see if two datasets somehow got created with the same values but swapped dimensions. If we want this, I can skip the transpose when check_dim_order=False and the dims differ (so it doesn't fail on transpose
) and just let it make the whole diff. And I'll always skip the dims check when check_dim_order=True.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I would probably let it fall through, so we have a single place where we check & generate diffs; at least unless there's a good reason not to
(though @TomNicholas let us know if you disagree!)
Co-authored-by: Tom Nicholas <tom@cworthy.org>
xarray/tests/test_assertions.py
Outdated
@pytest.mark.parametrize( | ||
"func", ["assert_equal", "assert_allclose", "assert_identical"] | ||
) | ||
def test_assert_allclose_equal_identical_transpose(func) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice test the tests!
Co-authored-by: Tom Nicholas <tom@cworthy.org>
…ims to maybe_transpose_dims
xarray/testing/assertions.py
Outdated
@@ -162,7 +176,7 @@ def assert_identical(a: DataTree, b: DataTree, from_root: bool = True): ... | |||
|
|||
|
|||
@ensure_warnings | |||
def assert_identical(a, b, from_root=True): | |||
def assert_identical(a, b, from_root=True, check_dim_order: bool = True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it makes sense to have this arg — identical means identical :) — but happy to defer if someone does...
…nspose_dimensions
Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com>
Thanks a lot @ignamv ! |
…#5733) (#8991) * Add argument check_dims to assert_allclose to allow transposed inputs * Update whats-new.rst * Add `check_dims` argument to assert_equal and assert_identical + tests * Assert that dimensions match before transposing or comparing values * Add docstring for check_dims to assert_equal and assert_identical * Update doc/whats-new.rst Co-authored-by: Tom Nicholas <tom@cworthy.org> * Undo fat finger Co-authored-by: Tom Nicholas <tom@cworthy.org> * Add attribution to whats-new.rst * Replace check_dims with bool argument check_dim_order, rename align_dims to maybe_transpose_dims * Remove left-over half-made test * Remove check_dim_order argument from assert_identical * assert_allclose/equal: emit full diff if dimensions don't match * Rename check_dim_order test, test Dataset with different dim orders * Update whats-new.rst * Hide maybe_transpose_dims from Pytest traceback Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> * Ignore mypy error due to missing functools.partial.__name__ --------- Co-authored-by: Tom Nicholas <tom@cworthy.org> Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com>
* main: Avoid auto creation of indexes in concat (#8872) Fix benchmark CI (#9013) Avoid extra read from disk when creating Pandas Index. (#8893) Add a benchmark to monitor performance for large dataset indexing (#9012) Zarr: Optimize `region="auto"` detection (#8997) Trigger CI only if code files are modified. (#9006) Fix for ruff 0.4.3 (#9007) Port negative frequency fix for `pandas.date_range` to `cftime_range` (#8999) Bump codecov/codecov-action from 4.3.0 to 4.3.1 in the actions group (#9004) Speed up localize (#8536) Simplify fast path (#9001) Add argument check_dims to assert_allclose to allow transposed inputs (#5733) (#8991) Fix syntax error in test related to cupy (#9000)
* backend-indexing: Trigger CI only if code files are modified. (pydata#9006) Enable explicit use of key tuples (instead of *Indexer objects) in indexing adapters and explicitly indexed arrays (pydata#8870) add `.oindex` and `.vindex` to `BackendArray` (pydata#8885) temporary enable CI triggers on feature branch Avoid auto creation of indexes in concat (pydata#8872) Fix benchmark CI (pydata#9013) Avoid extra read from disk when creating Pandas Index. (pydata#8893) Add a benchmark to monitor performance for large dataset indexing (pydata#9012) Zarr: Optimize `region="auto"` detection (pydata#8997) Trigger CI only if code files are modified. (pydata#9006) Fix for ruff 0.4.3 (pydata#9007) Port negative frequency fix for `pandas.date_range` to `cftime_range` (pydata#8999) Bump codecov/codecov-action from 4.3.0 to 4.3.1 in the actions group (pydata#9004) Speed up localize (pydata#8536) Simplify fast path (pydata#9001) Add argument check_dims to assert_allclose to allow transposed inputs (pydata#5733) (pydata#8991) Fix syntax error in test related to cupy (pydata#9000)
assert_allclose
transpose datasets? #5733whats-new.rst
api.rst