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

Partial collapse of multi-dimensional string coords #4294

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

rcomer
Copy link
Member

@rcomer rcomer commented Aug 24, 2021

🚀 Pull Request

Description

Fixes #3653.

If you have a 2D string coordinate on a cube, and collapse one of the dimensions spanned by that coordinate, the coordinate's points (and bounds, if any) are now serialised along the collapsed dimension. This seems to have been missed when partial collapse was implemented for the numeric coordinates.


Consult Iris pull request check list

bounds = []
for index in np.ndindex(shape):
index_slice = (slice(None),) + tuple(index)
bounds.append(serialize(self.bounds[index_slice]))
Copy link
Member Author

@rcomer rcomer Aug 24, 2021

Choose a reason for hiding this comment

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

If I've understood the old slicing method, it and the new method are consistent for the basic case of 2D bounds (n_points, n_bounds). For higher dimensions, the old method doesn't look right to me: if we had a 2D (n, m) coordinate with bounds (n, m, n_bounds), it looks like we would get m * n_bounds elements in the list. But I think we would have wanted just n_bounds elements.

I didn't even know bounded string coords were a thing!

# Create the new collapsed coordinate.
coord = self.copy(
points=np.array(points, dtype=dtype), bounds=bounds
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know why it was necessary to set the dtype, but the existing tests pass fine without this now. The old code is 8 years old, so I guess numpy has moved on and is likely doing more for us automatically.

lib/iris/coords.py Outdated Show resolved Hide resolved
@rcomer
Copy link
Member Author

rcomer commented Sep 6, 2021

Rebased. CI is green. Ready for review 😎

@rcomer
Copy link
Member Author

rcomer commented Nov 19, 2021

Added some type hints for the method and function I modified, as well as ones required to tidy up PyRight complaints within the collapsed method. The form import numpy.typing as npt is taken from examples in the numpy docs.

@rcomer
Copy link
Member Author

rcomer commented Nov 19, 2021

Some screenshots of the built docs:

now redundant as we turned hints off in the docs at #4510

ndim_property
bounds_property
core_bounds_method
collapsed_method

lib/iris/coords.py Outdated Show resolved Hide resolved
@rcomer
Copy link
Member Author

rcomer commented Jan 5, 2022

Made some changes to the type hints following a chat with @bjlittle this morning, and taking some inspiration from @jamesp's stubs.

The rendered docs look better:

now redundant as we turned hints off in the docs at #4510

bounds
core_points_and_bounds
collapsed

@bjlittle
Copy link
Member

bjlittle commented Oct 5, 2022

@bjlittle get your finger out...

@bjlittle
Copy link
Member

bjlittle commented Oct 5, 2022

@rcomer Fancy doing a rebase?

@rcomer
Copy link
Member Author

rcomer commented Oct 5, 2022

Now rebased.

@bjlittle it's all on you now 😛

@rcomer rcomer force-pushed the collapse-2d-string-coord branch from 4a3c441 to 68082f0 Compare May 8, 2024 11:27
@rcomer rcomer force-pushed the collapse-2d-string-coord branch from cb44650 to dd13194 Compare May 8, 2024 11:33
Copy link

codecov bot commented May 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.78%. Comparing base (7c313ff) to head (dd13194).
Report is 27 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4294      +/-   ##
==========================================
+ Coverage   89.76%   89.78%   +0.02%     
==========================================
  Files          93       93              
  Lines       22982    23015      +33     
  Branches     5006     5021      +15     
==========================================
+ Hits        20630    20665      +35     
+ Misses       1622     1620       -2     
  Partials      730      730              

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

@rcomer
Copy link
Member Author

rcomer commented May 15, 2024

See #5955 for a version of this fix without the type-hinting rabbit hole.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

cube.collapsed fails with multi-dimensional string coordinates
2 participants