Skip to content

Preserve masked bounds in Coord.cell (#5158)#7120

Merged
trexfeathers merged 2 commits into
SciTools:mainfrom
gaoflow:fix/cell-preserve-masked-bound
Jun 16, 2026
Merged

Preserve masked bounds in Coord.cell (#5158)#7120
trexfeathers merged 2 commits into
SciTools:mainfrom
gaoflow:fix/cell-preserve-masked-bound

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

🚀 Pull Request

Description

Closes #5158.

When a coordinate has a masked bound, :meth:iris.coords.Coord.cell revealed the value stored underneath the mask instead of reporting it as masked. This also surfaced in the cube / coordinate printout.

The cause is the use of np.array() when extracting the bound, which drops the mask:

>>> import numpy as np
>>> print(np.array(np.ma.masked_array([2], mask=[1])))      # mask lost
[2]
>>> print(np.asanyarray(np.ma.masked_array([2], mask=[1]))) # mask kept
[--]

The point path was already switched to np.asanyarray for exactly this reason (and #5158 was diagnosed by @pp-mo as pointing at Coord.cell); this PR applies the same treatment to bounds.

Reproduction (before)

import numpy.ma as ma
from iris.coords import AuxCoord

coord = AuxCoord([5.0], bounds=ma.masked_array([[3.0, 7.0]], mask=[[True, False]]))
print(coord.cell(0))
# Cell(point=5.0, bound=(3.0, 7.0))   <- 3.0 should be masked

After

print(coord.cell(0))
# Cell(point=5.0, bound=(masked, 7.0))

and the cube printout now shows height 5.0, bound=(--, 7.0) instead of (3.0, 7.0).

Implementation

np.array(x, ndmin=1) is replaced with np.atleast_1d(np.asanyarray(x)), the mask-preserving equivalent (mirroring the existing point handling on the line above). Unmasked bounds are unaffected.

Verification

  • New tests in Test_cell: test_masked_bound (fails on main, passes here) and test_masked_point (guards the already-fixed point path).
  • Full tests/unit/coords/ suite (411), test_Cell (31), and tests/unit/representation/ cube summary/printout suite (59) all pass.
  • ruff check / ruff format clean.

Coord.cell() used np.array() to extract the bound for a given index,
which silently drops the mask and reveals the raw value stored beneath
it. The point path was already changed to np.asanyarray for this reason;
apply the same treatment to bounds so a masked bound stays masked, both
in the returned Cell and in the cube/coordinate printout.

Fixes SciTools#5158.

@trexfeathers trexfeathers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, thanks @gaoflow. I've tested the printout too 👍

@trexfeathers trexfeathers enabled auto-merge (squash) June 16, 2026 17:23
@trexfeathers trexfeathers merged commit 832a0d0 into SciTools:main Jun 16, 2026
21 checks passed
@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.15%. Comparing base (57a5db7) to head (cf40371).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7120   +/-   ##
=======================================
  Coverage   90.15%   90.15%           
=======================================
  Files          91       91           
  Lines       24981    24981           
  Branches     4685     4685           
=======================================
  Hits        22522    22522           
  Misses       1682     1682           
  Partials      777      777           

☔ View full report in Codecov by Harness.
📢 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.

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.

cube printout incorrectly displays a masked scalar coord

2 participants