5

There is a numpy.testing package for comparing numpy arrays, but there doesn't appear to be an equivalent for masked arrays. Is there a library out there that does this already?

I notice that numpy.ma itself has some comparison functions like numpy.ma.allequal, but this function doesn't seem to check that both array masks are identical. E.g. -

In [151]: a = ma.masked_array([0,1,2,3],[True,False,False,False])

In [152]: b = ma.masked_array([0,1,2,3],[True,True,False,False])

In [153]: ma.allequal(a,b)
Out[153]: True
  • `numpy.testing` isn't for comparing numpy arrays; everything comparison-related in there is an assert method completely inappropriate for general-purpose array comparisons. – user2357112 Mar 16 '16 at 16:56
  • What comparison semantics do you want, anyway? You want equal masks, apparently. Do you want equal fillvalues? Exactly equal elements, or up to a tolerance? Identical dtypes? Identical memory layout? If a cell is masked in both masked arrays, do the masked-out values in the data arrays matter? – user2357112 Mar 16 '16 at 16:58

1 Answers1

1

ma.masked_array.__eq__ is actually implemented in numpy, but maybe it does not have the semantics you are looking for? You can get to the documentation with help(ma.masked_array.__eq__) with a python interpreter, it states:

Check whether other equals self elementwise

Which is what I can see given your example: it does the comparison where the data is marked as valid, and returns the result in the data field of a masked array. Wherever the data was invalid (for a or b) the resulting masked array field is masked.

>>> import numpy as np
>>> import numpy.ma as ma
>>> a = ma.masked_array([0,1,2,3],[True,False,False,False])
>>> b = ma.masked_array([0,1,2,3],[True,True,False,False])
>>> a==b
masked_array(data = [-- -- True True],
             mask = [ True  True False False],
       fill_value = True)
>>> b = ma.masked_array([0,1,2,4],[True,True,False,False])
>>> a==b
masked_array(data = [-- -- True False],
             mask = [ True  True False False],
       fill_value = True)

If you want to check that all the fields are valid and equal you could use:

np.allfalse((a==b).data)

edit: actually, I think you would need:

not np.any((a==b).mask) and np.alltrue((a==b).compressed())

If you want to check that all the valid fields are equal you could use:

np.alltrue((a==b).compressed())

As user2357112 explained in the comment, numpy.testing provides functions for unit testing on numpy arrays, which might not be what you are looking for. It it is, you could still use the regular assert function with the examples that I provided.

Emilien
  • 2,385
  • 16
  • 24