Skip to content

Commit

Permalink
Fix aggregated_by mdtol/masked constant problem (#4246)
Browse files Browse the repository at this point in the history
* add failing test

* pass test

* whatsnew

* remove redundant copy call
  • Loading branch information
rcomer authored Mar 24, 2022
1 parent 5420c5f commit c213516
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/src/whatsnew/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ This document explains the changes made to Iris for this release
#. `@rcomer`_ enabled passing of scalar objects to :func:`~iris.plot.plot` and
:func:`~iris.plot.scatter`. (:pull:`4616`)

#. `@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

0 comments on commit c213516

Please sign in to comment.