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

Fix aggregated_by mdtol/masked constant problem #4246

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/src/whatsnew/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ This document explains the changes made to Iris for this release
:func:`iris.plot.plot` no longer defaults to placing a "Y" coordinate (e.g.
latitude) on the y-axis of the plot. (:issue:`4493`, :pull:`4601`)

#. `@rcomer`_ fixed :meth:`~iris.cube.Cube.aggregated_by` with `mdtol` for 1D
cubes where an aggregated section is entirely masked, reported at
:issue:`3190`. (:pull:`4246`)


💣 Incompatible Changes
=======================
Expand Down
6 changes: 5 additions & 1 deletion lib/iris/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,11 @@ def aggregate(self, data, axis, **kwargs):
mdtol = kwargs.pop("mdtol", None)

result = self.call_func(data, axis=axis, **kwargs)
if mdtol is not None and ma.isMaskedArray(data):
if (
mdtol is not None
and ma.is_masked(data)
and result is not ma.masked
):
fraction_not_missing = data.count(axis=axis) / data.shape[axis]
mask_update = 1 - mdtol > fraction_not_missing
if ma.isMaskedArray(result):
Expand Down
13 changes: 13 additions & 0 deletions lib/iris/tests/unit/analysis/test_Aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ def test_unmasked(self):
self.assertArrayAlmostEqual(result, mock_return.copy())
mock_method.assert_called_once_with(data, axis=axis)

def test_allmasked_1D_with_mdtol(self):
data = ma.masked_all((3,))
axis = 0
mdtol = 0.5
mock_return = ma.masked
with mock.patch.object(
self.TEST, "call_func", return_value=mock_return
) as mock_method:
result = self.TEST.aggregate(data, axis, mdtol=mdtol)

self.assertIs(result, mock_return)
mock_method.assert_called_once_with(data, axis=axis)

def test_returning_scalar_mdtol(self):
# Test the case when the data aggregation function returns a scalar and
# turns it into a masked array.
Expand Down