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

re-add correctly region-extracted cell measures and ancillary variables after extract_region (for Changelog v2.10: authors: @valeriupredoi and @schlunma) #2166

Merged
merged 35 commits into from Aug 9, 2023

Conversation

valeriupredoi
Copy link
Contributor

@valeriupredoi valeriupredoi commented Aug 7, 2023

Description

Cube intersection removes all cell measures and ancillary variables in certain conditions (for certain regions ie certain coordinates of the region box). This has been raised upstream, see below. NOTE for Codacy fails: I intentionally placed the imports inside block for ease of removal when we will eventually remove the workaround code block,

Closes #2162

Brought to my attention by @rebeccaherman1 in the above linked issue, which is, in fact a bug in iris - raised upstream in SciTools/iris#5413 - this is a workaround (temporary too) so that we have something outputting correct data before iris fix it at their end.


Before you get started

Checklist

It is the responsibility of the author to make sure the pull request is ready to review. The icons indicate whether the item will be subject to the πŸ›  Technical or πŸ§ͺ Scientific review.


To help with the number pull requests:

@valeriupredoi valeriupredoi added the bug Something isn't working label Aug 7, 2023
@codecov
Copy link

codecov bot commented Aug 7, 2023

Codecov Report

Merging #2166 (345d7bf) into main (ea29584) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #2166      +/-   ##
==========================================
+ Coverage   93.09%   93.10%   +0.01%     
==========================================
  Files         237      237              
  Lines       12795    12816      +21     
==========================================
+ Hits        11911    11932      +21     
  Misses        884      884              
Files Changed Coverage Ξ”
esmvalcore/preprocessor/_area.py 93.65% <100.00%> (+0.42%) ⬆️

@valeriupredoi valeriupredoi added this to the v2.10.0 milestone Aug 7, 2023
@valeriupredoi valeriupredoi changed the title re-add correctly region-extracted cell measures after extract_region re-add correctly region-extracted cell measures and ancillary variables after extract_region Aug 7, 2023
@valeriupredoi
Copy link
Contributor Author

BTW @ledm I have a feeling that this bit you as well, in your continual battle with fx variables

Copy link
Contributor

@schlunma schlunma left a comment

Choose a reason for hiding this comment

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

Thanks V for tackling this!

I have three main comments:

  1. I think you can simplify this by re-using extract_region on a cell measure/ancillary variable cube (see code suggestions).
  2. Why do change the formatting of unrelated code? This has been formatted by black to look nice and shiny ⭐
  3. Could you maybe come up with a test that doesn't require adding a 1.1 MB file to the repo? We have plenty of example code that sets up a cube with a CellMeasure, e.g., here.

esmvalcore/preprocessor/_area.py Outdated Show resolved Hide resolved
esmvalcore/preprocessor/_area.py Outdated Show resolved Hide resolved
esmvalcore/preprocessor/_area.py Outdated Show resolved Hide resolved
@@ -467,15 +531,13 @@ def _process_ids(geometries, ids: list | dict | None) -> tuple:
if len(ids) != 1:
raise ValueError(
f"If `ids` is given as dict, it needs exactly one entry, got "
f"{ids}"
)
f"{ids}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you change the nice formatting by black? 😝

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yes, totally my bad, I yapf-ed it, flake8 was getting on me nerves - but, we don't use black so why the bother?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if we use it for precommit stuff, I'd turn it off TBF, black is black magic and introduces a lot of shtuff on its own

Copy link
Contributor

Choose a reason for hiding this comment

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

my bad for yapf-ing it, should I restore the old formatting from unrelated sections?

Yeah, would be nice to change it back 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, bud, shall revert, in a jiffy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will tacke the other suggestions that I got from you (git them in already, but need to make sure all's shipshape, fix the tests too) then will backtrack on this, 🐻 with me for a few, bud

@valeriupredoi
Copy link
Contributor Author

valeriupredoi commented Aug 7, 2023

Cheers muchly, Manu! 🍺

  1. I think you can simplify this by re-using extract_region on a cell measure/ancillary variable cube (see code suggestions).

I'm a bit afraid of unexpected behaviour knowing that cube.intersect is very moody - maybe let's fix it if it's indeed broken

  1. Why do change the formatting of unrelated code? This has been formatted by black to look nice and shiny star

my bad for yapf-ing it, should I restore the old formatting from unrelated sections?

  1. Could you maybe come up with a test that doesn't require adding a 1.1 MB file to the repo? We have plenty of example code that sets up a cube with a CellMeasure, e.g., here.

Indeed, I can do that - I was lazy and knew this will be removed at some point in the future anyway 😁

valeriupredoi and others added 6 commits August 8, 2023 12:39
Co-authored-by: Manuel Schlund <32543114+schlunma@users.noreply.github.com>
Co-authored-by: Manuel Schlund <32543114+schlunma@users.noreply.github.com>
Co-authored-by: Manuel Schlund <32543114+schlunma@users.noreply.github.com>
esmvalcore/preprocessor/_area.py Outdated Show resolved Hide resolved
esmvalcore/preprocessor/_area.py Outdated Show resolved Hide resolved
valeriupredoi and others added 2 commits August 8, 2023 14:02
Co-authored-by: Manuel Schlund <32543114+schlunma@users.noreply.github.com>
Co-authored-by: Manuel Schlund <32543114+schlunma@users.noreply.github.com>
@valeriupredoi
Copy link
Contributor Author

with the latest changes when you try print the cube that's been changed you get this:

Traceback (most recent call last):
  File "/home/valeriu/test_intersect.py", line 27, in <module>
    print("ex1", ex1)
  File "/home/valeriu/miniconda3/envs/esmvaltool/lib/python3.11/site-packages/iris/cube.py", line 2563, in __str__
    return self.summary()
           ^^^^^^^^^^^^^^
  File "/home/valeriu/miniconda3/envs/esmvaltool/lib/python3.11/site-packages/iris/cube.py", line 2558, in summary
    printer = CubePrinter(self)
              ^^^^^^^^^^^^^^^^^
  File "/home/valeriu/miniconda3/envs/esmvaltool/lib/python3.11/site-packages/iris/_representation/cube_printout.py", line 165, in __init__
    cube_summary = CubeSummary(cube_or_summary)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valeriu/miniconda3/envs/esmvaltool/lib/python3.11/site-packages/iris/_representation/cube_summary.py", line 364, in __init__
    add_vector_section(
  File "/home/valeriu/miniconda3/envs/esmvaltool/lib/python3.11/site-packages/iris/_representation/cube_summary.py", line 355, in add_vector_section
    self.vector_sections[title] = VectorSection(
                                  ^^^^^^^^^^^^^^
  File "/home/valeriu/miniconda3/envs/esmvaltool/lib/python3.11/site-packages/iris/_representation/cube_summary.py", line 203, in __init__
    self.contents = [
                    ^
  File "/home/valeriu/miniconda3/envs/esmvaltool/lib/python3.11/site-packages/iris/_representation/cube_summary.py", line 204, in <listcomp>
    VectorSummary(cube, vector, iscoord) for vector in vectors
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/valeriu/miniconda3/envs/esmvaltool/lib/python3.11/site-packages/iris/_representation/cube_summary.py", line 141, in __init__
    dims = vector.cube_dims(cube)
           ^^^^^^^^^^^^^^^^
AttributeError: 'Cube' object has no attribute 'cube_dims'

@valeriupredoi
Copy link
Contributor Author

sorry @schlunma a few too many issues with your review suggestions (both at our end and iris - iris issue which I am not prepared to dive into at the moment, see #2166 (comment)); if you think we'd benefit enough from those changes, I suggest we merge this, then you can open a PR based off main where you apply those changes, and test properly. I'll fix the test not to use the test data (and delete that file), then I'll revert the style changes resulted from yapfing it, then I am ready to merge this 🍺

@valeriupredoi
Copy link
Contributor Author

OK Manu, I have finished poslishing this:

  • used your suggestion to allow for irregular grids to be looked at
  • fixed the test and removed the now unnecessary test data file
  • re-blacked _area.py (well, reverted previously added style changes from yapf)

I think we should do this: merge this to have correct functionality for extract_region, then you and me we can go back to the suggestions you made earlier in a follow up PR, unfortunately there are quite a few hitches (not least the iris one that we should look at) to be looked at here; we may as well not even do it all if we'll remove all this when iris fix their intersection, but at least I'd like to understand that print(cube) issue above. What say you? 🍺

@valeriupredoi
Copy link
Contributor Author

Also, as a note to all of us (I am guilty as charged myself) - everytime we suggest major code changes in a PR, in the form of suggested code via the browser/GitHub API, it would be a lot easier for the PR owner if the reviewer tested those changes by either running the test provided in the PR or by running a small independent test - this, to catch serious issues (not style, minor issues with the suggestions, those can be taken care of the PR owner, after including the suggested code changes). I think we should add this to our best practices PR reviewing docs πŸ‘

@schlunma
Copy link
Contributor

schlunma commented Aug 8, 2023

Well, if you use cube.intersection (and not extract_region) to get the cell measure sub cube, this won't work for irregular grids. Hence you need to reinstate the check for coord('latitude').ndim == 1.

Also, in this form, this won't work (and will actually crash) for cell measures and ancillary variables that do not exactly span latitude/longitude. We need to take care of this properly here, otherwise it will break recipes.

I am honestly confused why my suggestion didn't work. From the failing test on CircleCI it looks just like a flake8 fail? I will give it a try myself in a local branch.

@valeriupredoi
Copy link
Contributor Author

Well, if you use cube.intersection (and not extract_region) to get the cell measure sub cube, this won't work for irregular grids. Hence you need to reinstate the check for coord('latitude').ndim == 1.

Also, in this form, this won't work (and will actually crash) for cell measures and ancillary variables that do not exactly span latitude/longitude. We need to take care of this properly here, otherwise it will break recipes.

I am honestly confused why my suggestion didn't work. From the failing test on CircleCI it looks just like a flake8 fail? I will give it a try myself in a local branch.

sounds like a very good plan! Cheers, Manu! Can grab this branch, edit locally, and when things look OK to you just push changes here - that was my point in #2166 (comment) - reviewer's minimal testing is something we should start doing 🍺

@schlunma
Copy link
Contributor

schlunma commented Aug 8, 2023

Tests pass fine for me locally (your test and my suggestion). Can I push it? 😁

@valeriupredoi
Copy link
Contributor Author

Tests pass fine for me locally (your test and my suggestion). Can I push it? grin

Pliz do 🍺

@valeriupredoi
Copy link
Contributor Author

That is now beautiful, and I don't see any more iris-related issues either - excellent work, Manu! 🍻

@valeriupredoi valeriupredoi changed the title re-add correctly region-extracted cell measures and ancillary variables after extract_region re-add correctly region-extracted cell measures and ancillary variables after extract_region (for Changelog v2.10: authors: @valeriupredoi and @schlunma) Aug 8, 2023
@valeriupredoi
Copy link
Contributor Author

@schlunma let's get this in plz so I can help @rebeccaherman1 with an install at DKRZ 🍺

Copy link
Contributor

@schlunma schlunma left a comment

Choose a reason for hiding this comment

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

Thanks V! Can I be a bother and ask to to test this with the script mentioned here? I think we need to make sure that this properly works for cubes with time dimensions, too (If you like, you could also add a brief test of this 😁).

I will be in a seminar today with limitied availability, so I pre-approve this. Thanks for the fix!!

@valeriupredoi
Copy link
Contributor Author

Thanks V! Can I be a bother and ask to to test this with the script mentioned here? I think we need to make sure that this properly works for cubes with time dimensions, too (If you like, you could also add a brief test of this grin).

I will be in a seminar today with limitied availability, so I pre-approve this. Thanks for the fix!!

Very many thanks, Manu! Yes, that's what I've been using to test the PR (alas, slightly changed, but I'll test with the exact script that Rebecca had initially). Good point, I'll put a time dim in the integration test in this PR 🍺

@valeriupredoi
Copy link
Contributor Author

Boss level, Manu! Added time dim and check on final shapes, think the test is pretty bullet-proof now. Gonna merge and get @rebeccaherman1 an env done on DKRZ so she can get her data done up πŸ‘

@valeriupredoi valeriupredoi merged commit facd794 into main Aug 9, 2023
4 checks passed
@valeriupredoi valeriupredoi deleted the readd_cell_measure_after_extract_region branch August 9, 2023 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

supplementary variables disappear from cubes after extract_region
2 participants