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

Make difference module handle circular cubes #1990

Conversation

MO-PeterJordan
Copy link
Contributor

@MO-PeterJordan MO-PeterJordan commented Apr 23, 2024

Split out from #1985 to support calculating gradients for cubes which wrap around the meridian.

Testing:

  • Ran tests and they passed OK
  • Added new tests for the new feature(s)

CLA

  • If a new developer, signed up to CLA

@MO-PeterJordan MO-PeterJordan marked this pull request as draft April 23, 2024 13:19
@MO-PeterJordan MO-PeterJordan changed the title Pjordan make difference module handle circular cubes WIP Pjordan make difference module handle circular cubes Apr 23, 2024
improver/utilities/spatial.py Outdated Show resolved Hide resolved
extra_mean_point = (
np.mean([points[-1], (points[0] + max_value)]) % max_value
)
mean_points = np.hstack([mean_points, extra_mean_point])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Question for the reviewer:

This will only work if the 'wrap around' point is positive. This is the case for test cubes that are 'symmetric' in nature (eg. points in cube at [120, 0, 120] or [-175, 165, 155, ..., -25, -15, -5, 5, 15, 25, ..., 165, 175] - wrap around point = 180) but not ones that are 'offset' (eg. [-170, -160, -150, ..., -20, -10, 0, 10, 20, ..., 150, 160, 170, 180] - wrap around point = -175).

In the offset case given above, this would calculate the 'wrap around point' to be 185. Not 'wrong' per-se but certainly potentially confusing.

I could add extra logic to make this work for offset grids. However given that global data from STaGE is 'symmetrical', and the cubes produced by the set_up_variable_cube test helper match this convention, I'm not sure whether or not this is something worth investing time in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

...Thinking about it, for the symmetrical case above, the np.mean(...) line always resolves to 180, so I could get rid of it altogether and just always put the wrap around point at 180.

The above does better handle the case where the cube uses 0 -> 360 instead of -180 -> 180 but maybe this never comes up.

I suppose the line could also be used as a test. I.e. if np.mean(...) != 180 raise ValueError("Difference Plugin only handles circular cubes if their points are spread symmetrically about a plane intersecting the prime meridian").

Copy link
Contributor

Choose a reason for hiding this comment

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

There's a nice function I knew existed somewhere for calculating the circular mean. It's in scipy https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.circmean.html . This might be helpful but it does still need to be told the bounds to know where to wrap around so I don't know if it solves every problem but should simplify the offset case.

Id keep this function as general as possible. so I wouldn't raise an error, at worst a warning.

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 split this calculation out into its own function which now uses circmean from scipy.

@MO-PeterJordan MO-PeterJordan changed the title WIP Pjordan make difference module handle circular cubes Pjordan make difference module handle circular cubes Apr 24, 2024
@MO-PeterJordan MO-PeterJordan marked this pull request as ready for review April 24, 2024 10:05
@MO-PeterJordan MO-PeterJordan changed the title Pjordan make difference module handle circular cubes Make difference module handle circular cubes Apr 24, 2024
Copy link
Contributor

@mspelman07 mspelman07 left a comment

Choose a reason for hiding this comment

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

A few comments for you. There is also an empty file on this PR that I assume isn't supposed to be a part of it?

improver/utilities/spatial.py Outdated Show resolved Hide resolved
improver/utilities/spatial.py Outdated Show resolved Hide resolved
extra_mean_point = (
np.mean([points[-1], (points[0] + max_value)]) % max_value
)
mean_points = np.hstack([mean_points, extra_mean_point])
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a nice function I knew existed somewhere for calculating the circular mean. It's in scipy https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.circmean.html . This might be helpful but it does still need to be told the bounds to know where to wrap around so I don't know if it solves every problem but should simplify the offset case.

Id keep this function as general as possible. so I wouldn't raise an error, at worst a warning.

diff_axis_number = cube.coord_dims(coord_name)[0]
diff_along_axis = np.diff(cube.data, axis=diff_axis_number)
if self._axis_wraps_around_meridian(diff_axis, cube):
first_column = cube.data[:, :1]
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be clearer to have the index as 0 rather than :1.

As a separate issue it is would be better not to assume the order of the coordinates in a cube instead getting the index of the diff_axis from the cube.

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 replaced the :1/:-1 with reshapes.

I'm not sure I understand the second point. Would be good to discuss.

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 now fixed this. Does a single reshape if needed with [-1, 1] and takes the array axis to diff along from the cube dim-coord number rather than assuming the order of the coordinates on cube.data.

Copy link
Contributor Author

@MO-PeterJordan MO-PeterJordan left a comment

Choose a reason for hiding this comment

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

Implemented all changes except for one, which I didn't understand. Hopefully can discuss.

improver/utilities/spatial.py Outdated Show resolved Hide resolved
improver/utilities/spatial.py Outdated Show resolved Hide resolved
extra_mean_point = (
np.mean([points[-1], (points[0] + max_value)]) % max_value
)
mean_points = np.hstack([mean_points, extra_mean_point])
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 split this calculation out into its own function which now uses circmean from scipy.

diff_axis_number = cube.coord_dims(coord_name)[0]
diff_along_axis = np.diff(cube.data, axis=diff_axis_number)
if self._axis_wraps_around_meridian(diff_axis, cube):
first_column = cube.data[:, :1]
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 replaced the :1/:-1 with reshapes.

I'm not sure I understand the second point. Would be good to discuss.

Comment on lines 329 to 331
first_column = cube.data[:, 0].reshape([-1, 1])
last_column = cube.data[:, -1].reshape([-1, 1])
wrap_around_diff = np.diff(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This assumes that the cube is set up with x as rows and y as columns but this may not be true.
Can use an Improver module to force the cube to be x as rows and y as columns but have to make sure I've put it back again. Alternatively Might be able to work out correct index here on the fly.

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 added a test for a cube using the opposite lat/long axis choices and fixed this.

Below is just my chain of thought/sanity check. Feel free to ignore.
...Wait... I produced the flipped code by transposing the cube. This flips which axis of cube.data each dim coord is assigned to, and also transposes the numpy array, so that the data represented by the cube is the same, but the longitudes now increase along the columns and the latitudes along the rows... which is exactly what I want. Okay, good.

Copy link
Contributor Author

@MO-PeterJordan MO-PeterJordan left a comment

Choose a reason for hiding this comment

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

note to self

…umns not rows and vice versa for latitude). Fixed code to handle this case.
Copy link

codecov bot commented Jun 17, 2024

Codecov Report

Attention: Patch coverage is 94.87179% with 2 lines in your changes missing coverage. Please review.

Project coverage is 98.41%. Comparing base (84a8944) to head (31fceb3).
Report is 12 commits behind head on master.

Files Patch % Lines
improver/utilities/spatial.py 94.87% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1990      +/-   ##
==========================================
+ Coverage   98.39%   98.41%   +0.01%     
==========================================
  Files         124      128       +4     
  Lines       12212    12482     +270     
==========================================
+ Hits        12016    12284     +268     
- Misses        196      198       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@mspelman07 mspelman07 left a comment

Choose a reason for hiding this comment

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

A few small comments, hopefully nothing too large.

improver/utilities/spatial.py Show resolved Hide resolved
improver/utilities/spatial.py Show resolved Hide resolved
improver_tests/utilities/test_DistanceBetweenGridPoints.py Outdated Show resolved Hide resolved
@MoseleyS
Copy link
Contributor

Superseded.

@MoseleyS MoseleyS closed this Jul 16, 2024
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