iris icon indicating copy to clipboard operation
iris copied to clipboard

Fix array_equal behaviour for masked arrays

Open stephenworsley opened this issue 3 years ago • 5 comments

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

stephenworsley avatar Dec 10 '21 16:12 stephenworsley

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?

rcomer avatar Dec 19 '21 13:12 rcomer

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)

bjlittle avatar Jul 22 '22 10:07 bjlittle

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).

trexfeathers avatar Jul 22 '22 10:07 trexfeathers

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

rcomer avatar Jul 22 '22 10:07 rcomer

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.

stephenworsley avatar Jul 26 '22 13:07 stephenworsley

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.

github-actions[bot] avatar Dec 09 '23 00:12 github-actions[bot]

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-devs who will re-open on your behalf.

github-actions[bot] avatar Jan 07 '24 00:01 github-actions[bot]