How to test if every element in a numpy array is masked

numpy, python

Solution

I think the most Pythonic way would be to just do what you want, and then catch any errors that result if the entire array is masked.

For example:

x = numpy.ma.array(numpy.array([1,2,3]),mask=[True,True,True])
try: 
    my_min = numpy.min(x[~x.mask])
    # Continue with my_min
except ValueError: 
    # Bail or what have you
    print 'Masks all around!'

Problem

How do I test if every element in a numpy array is masked? Here's what I'd like to do: ``` x = #is a maksed numpy array masked_min = numpy.ma.min(x) if masked_min IS NOT A MASKED ELEMENT: #do some stuff only if masked_min is a value ``` In practice I see this: ``` >>> x = numpy.ma.array(numpy.array([1,2,3]),mask=[True,True,True]) >>> masked_min = numpy.ma.min(x) masked ``` Testing for `masked` is not helpful: ``` >>> numpy.ma.sum(x) == numpy.ma.masked masked ```

Original source