Skip to content

Allow masked data in coords #6468

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

Merged
merged 20 commits into from
May 23, 2025
Merged

Conversation

ukmo-ccbunney
Copy link
Contributor

@ukmo-ccbunney ukmo-ccbunney commented May 15, 2025

🚀 Pull Request

Closes: #3584

Description

Fixes two related issues:

  • Allows masked data in a Cell to be hashed
  • Correctly handles masking and dtype when merging scalar AuxCoords with masked data

Whilst adding test, the following files have been converted from unittest to pytest:

  • unit/coords/test_Cell.py
  • test_merge.py [moved to unit/merge/test_merge.py]

Consult Iris pull request check list

Add any of the below labels to trigger actions on this PR:

  • benchmark_this Request that this pull request be benchmarked to check if it introduces performance shifts

@ukmo-ccbunney ukmo-ccbunney self-assigned this May 15, 2025
Copy link

codecov bot commented May 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.82%. Comparing base (9017fd2) to head (802eeca).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6468   +/-   ##
=======================================
  Coverage   89.81%   89.82%           
=======================================
  Files          90       90           
  Lines       23761    23782   +21     
  Branches     4422     4427    +5     
=======================================
+ Hits        21340    21361   +21     
  Misses       1672     1672           
  Partials      749      749           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ukmo-ccbunney
Copy link
Contributor Author

Mostly working, but outstanding problem with loading a scalar coord with a single masked value.
Whilst this does load ok and get's added to the cube, the dtype of the AuxCoord is always np.float64, regardless of the original dtype of the scalar coord in the netCDF file.

I am not sure yet why this works for an array of all masked values but not a scalar masked value...

@ukmo-ccbunney
Copy link
Contributor Author

Commit dff7cb8 addresses issue where the original dtype of a scalar masked variable was lost.

The dtype of the AuxCoord was being set to np.float64 in all cases as this is the dtype of the np.ma.masked singleton value. Added special handling for this case to return a new 1-element masked array of the correct dtype, rather than the np.ma.masked instance.

@ukmo-ccbunney
Copy link
Contributor Author

There is only one small snag now w.r.t. the dtype returned when working with Cells:

# Scalar coord points now have correct dtype
coord = cube.coord('realization')
coord.points
>> masked_array(data=[--],
>>           mask=[ True],
>>      fill_value=np.int64(999999),
>>            dtype=int16)

# ... but accessing them via the `cell` method still looses it:
coord.cell(0).point
>> masked

coord.cell(0).point.dtype
>> dtype('float64')   # <-- This is the dtype of np.ma.masked, not the coord

Copy link
Contributor

@trexfeathers trexfeathers left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this @ukmo-ccbunney!

@ukmo-ccbunney ukmo-ccbunney marked this pull request as ready for review May 23, 2025 11:47
Copy link
Contributor

@trexfeathers trexfeathers left a comment

Choose a reason for hiding this comment

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

Awesome work @ukmo-ccbunney. Just one change left I think

Copy link
Contributor

@trexfeathers trexfeathers left a comment

Choose a reason for hiding this comment

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

Superb! Thanks again 🚀

@trexfeathers trexfeathers merged commit 8dea269 into SciTools:main May 23, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

TypeError when merging masked scalar coord
2 participants