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

Refactor CubeCombiner #1383

Merged
merged 10 commits into from
Dec 16, 2020
Merged

Refactor CubeCombiner #1383

merged 10 commits into from
Dec 16, 2020

Conversation

cgsandford
Copy link
Contributor

Single responsibility principle. Splits CubeCombiner into two plugins: a "parent" plugin to apply linear operators, and a CubeMultiplier plugin to do the additional work of broadcasting to threshold coordinates in the "multiply" case.

Unit tests have not been changed or extended - existing tests have been copied for CubeMultiplier with updated calls. This, and the passing of the "combine" CLI tests, show that functionality is unchanged with this refactor.

Testing:

  • Ran tests and they passed OK

@codecov
Copy link

codecov bot commented Dec 4, 2020

Codecov Report

Merging #1383 (e6c68a7) into master (ff9b7e6) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1383      +/-   ##
==========================================
+ Coverage   96.32%   96.34%   +0.02%     
==========================================
  Files          89       89              
  Lines        7583     7586       +3     
==========================================
+ Hits         7304     7309       +5     
+ Misses        279      277       -2     
Impacted Files Coverage Δ
improver/cube_combiner.py 100.00% <100.00%> (ø)
improver/nbhood/recursive_filter.py 100.00% <0.00%> (+1.96%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ff9b7e6...e6c68a7. Read the comment docs.

Copy link
Contributor

@gavinevans gavinevans left a comment

Choose a reason for hiding this comment

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

Thanks @cgsandford 👍

This seems fine. I've added some very minor comments.

match the dimensions, in order, of the first cube in the list

Args:
cube_list: (iris.cube.CubeList)
Copy link
Contributor

Choose a reason for hiding this comment

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

The colon isn't required here and I think this could be a list as well (similar to the amendment that you've made to the _combine_cube_data method):

Suggested change
cube_list: (iris.cube.CubeList)
cube_list (iris.cube.CubeList or list)

improver/cube_combiner.py Show resolved Hide resolved
"""
if len(cube_list) < 2:
msg = "Expecting 2 or more cubes in cube_list"
raise ValueError(msg)
Copy link
Contributor

Choose a reason for hiding this comment

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

As CodeCov highlights, this exception could do with a unit test to cover it.


Raises:
ValueError: if the operation results in an escalated datatype
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should actually be:

Suggested change
ValueError: if the operation results in an escalated datatype
TypeError: if the operation results in an escalated datatype

gavinevans
gavinevans previously approved these changes Dec 14, 2020
Copy link
Contributor

@MoseleyS MoseleyS left a comment

Choose a reason for hiding this comment

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

Just little things.

self.normalise = operation == "mean"

@staticmethod
def _check_dimensions_match(cube_list, comparators=[eq]):
"""
Check all coordinate dimensions on the input cubes are equal or broadcastable
Copy link
Contributor

Choose a reason for hiding this comment

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

Ability to broadcast has gone, so probably need to amend this doc-string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK.


Args:
cube_list: (iris.cube.CubeList)
cube_list (iris.cube.CubeList or list)
Copy link
Contributor

Choose a reason for hiding this comment

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

Because CubeList is a sub-class of list, and you don't need any of the added functionality, I think you can just specify list here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have changed this in most of the places it occurs - there's nowhere in this module that uses merging or other special CubeList functionality.

Comment on lines -208 to +197
msg = (
r"Operation add on types \{dtype\(\'.*\'\)\} results in "
r"float64 data which cannot be safely coerced to float32"
)
msg = "Operation .* results in float64 data"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change this? The behaviour hasn't changed has it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The behaviour has very slightly changed, in that this error will now have eg np.add instead of add in the string.

In terms of why I cut it down though. I saw this done a while ago in some of the pytests and asked @tjtg about it. The answer was broadly: we don't really need to verify the whole error string, as we might have legitimate reasons for changing the details in future. We just want to check enough to identify that it's the error we wanted to raise, and that any important components are there. In this case I judged that the important components are the fact that we've escalated to 64-bit precision, so I kept that.

@MoseleyS MoseleyS merged commit a7a9a38 into metoppv:master Dec 16, 2020
@cgsandford cgsandford deleted the refactor_combine branch December 17, 2020 08:50
MoseleyS pushed a commit to MoseleyS/improver that referenced this pull request Aug 22, 2024
* Factored multiplication case out of CubeCombiner.  All CLI tests pass.  Need to sort unit tests and then make code nicer.

* Split existing unit tests between plugins and they pass.

* Reduced "broadcast_to_coords" to "broadcast_to_threshold" boolean, consistent with use case and CLI interface.

* Simplified setting up of coords for broadcast to use threshold only.

* Factored out broadcasting into CubeMultiplier.  Coord checking needs tidying up but all tests pass.

* Removed unused method.

* Pythonified coordinate checking.

* Removed "self.operation" and summarised use more clearly.

* Addressed comments from first review.

* Response to second review.
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 this pull request may close these issues.

3 participants