iris
iris copied to clipboard
Fix array_equal behaviour for masked arrays
Current behaviour will compare the underlying data of a masked array and will sometimes compare masked arrays as unequal when they are otherwise equal for all unmasked points. This would cause an error in an iris-esmf-regrid PR as described here: https://github.com/SciTools-incubator/iris-esmf-regrid/pull/138#issuecomment-991099896
Seems that ignoring the masks was deliberate, but only because “the chosen approach fixes the problem [of all-masked data raising errors], is simpler, and reflects numpy behaviour.” #905
Perhaps using something like ma.filled would help?
Related to https://github.com/numpy/numpy/pull/16022#issuecomment-1174794735, which was merged into numpy, but then reverted (as it broke scipy and astropy)
If we're worried about upsetting the default Iris behaviour, perhaps the neatest solution would be an iris.util function that provides the alternative equality behaviour?
As far as I'm aware the only calls for this alternative behaviour are cases where the caller is explicitly checking equality (rather than wanting internal Iris behaviour to be changed).
There is also a relevant thread at https://github.com/numpy/numpy/issues/14624
I think what Iris does in testing is sensible:
https://github.com/SciTools/iris/blob/56a97d66b8404f96ef61fe2d52433eb58373f338/lib/iris/tests/init.py#L590-L668
There is also a relevant thread at numpy/numpy#14624
@rcomer It's interesting that there is an inconsistency between different numpy functions at the moment, though given that currently iris.util.array_equal behaves in the same way as np.array_equal, that could be enough of a reason to keep this function behaving the same. There's still potentially a question of how we use iris.util.array_equal within iris. It might make sense in places (such ass coord equality) to use an approach of calling ma.filled as you suggest before calling iris.util.array_equal and also checking that the mask is equal.
In order to maintain a backlog of relevant PRs, we automatically label them as stale after 500 days of inactivity.
If this PR is still important to you, then please comment on this PR and the stale label will be removed.
Otherwise this PR will be automatically closed in 28 days time.
This stale PR has been automatically closed due to a lack of community activity.
If you still care about this PR, then please either:
- Re-open this PR, if you have sufficient permissions, or
- Add a comment pinging
@SciTools/iris-devswho will re-open on your behalf.