Skip to content
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

Use xarray.testing.assert_allclose #104

Closed
TomNicholas opened this issue Aug 29, 2024 · 7 comments · Fixed by #107
Closed

Use xarray.testing.assert_allclose #104

TomNicholas opened this issue Aug 29, 2024 · 7 comments · Fixed by #107

Comments

@TomNicholas
Copy link
Member

Here we are using np.allclose over and over

assert np.allclose(ds["zeta_south"].values, expected_zeta_south)

but we could just compare the whole dataset at once using xarray.testing.assert_allclose

@NoraLoose
Copy link
Collaborator

I'm generally all for using xr.testing.assert_allclose. There are a few reasons why I didn't do it here:

  • I want to make sure that an error is raised if the dimension order is not s_, eta_, xi_ because this is the dimension order that ROMS assumes. xarray is insensitive to reordered dimensions.
  • I'm actually not comparing the whole dataset because the computation of BoundaryForcing is terribly slow, even for a single time slice. (As is, the test is already taking up several minutes, which really slows down the entire test suite.) So I am skipping fields that are similar to others. This argument is more relevant here:
    ds_bgc = bgc_boundary_forcing_from_climatology.ds
    # Check the values in the dataset
    assert np.allclose(ds_bgc["ALK_south"].values, expected_alk_south)
    assert np.allclose(ds_bgc["ALK_east"].values, expected_alk_east)
    assert np.allclose(ds_bgc["ALK_north"].values, expected_alk_north)
    assert np.allclose(ds_bgc["ALK_west"].values, expected_alk_west)

    where I do not call ds.compute() before the comparison.

I'm hoping we can improve performance by working on #43 soon, and then the second point will be irrelevant.

@NoraLoose
Copy link
Collaborator

BTW do you know an easy way to write an entire expected_ds into the test?

When I have boundary_forcing.ds in a notebook and do print(boundary_forcing.ds) it just shows a condensed version like
Screenshot 2024-08-29 at 3 29 13 PM

I can't just copy / paste that.

@TomNicholas
Copy link
Member Author

BTW do you know an easy way to write an entire expected_ds into the test?

No - I mean the Dataset could point to an arbitrarily large amount of data so it's truncated quickly. In general I'm not a fan of hardcoding expected data values into the testing code...

@NoraLoose
Copy link
Collaborator

But having these tests with expected data values has proven really, really useful when refactoring the code. These are the tests that have caught most of my bugs.

@NoraLoose
Copy link
Collaborator

NoraLoose commented Aug 29, 2024

At the same time they are the most annoying tests to maintain if you change small things 1) in the code (like a different interpolation method) or 2) in the test datasets that make the expected values change. But at least we can keep track of changes in the input fields to ROMS. Creating accurate input fields is the major purpose of ROMS-Tools (at least of roms_tools.setup), so I think within ROMS-Tools these tests are important.

@TomNicholas
Copy link
Member Author

Then perhaps we should be saving very small output files as regression tests.

@NoraLoose
Copy link
Collaborator

Yes, that would definitely be better. I will work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants