how to apply a mask from one array to another array?

57,527

Solution 1

Why not simply

import numpy as np

y = np.array([2,1,5,2])          # y axis
x = np.array([1,2,3,4])          # x axis
m = np.ma.masked_where(y>2, y)   # filter out values larger than 5
print list(m)
print np.ma.compressed(m)

# mask x the same way
m_ = np.ma.masked_where(y>2, x)   # filter out values larger than 5
# print here the list
print list(m_) 
print np.ma.compressed(m_)

code is for Python 2.x

Also, as proposed by joris, this do the work new_x = x[~m.mask].copy() giving an array

>>> new_x
array([1, 2, 4])

Solution 2

I had a similar issue, but involving loads more masking commands and more arrays to apply them. My solution is that I do all the masking on one array and then use the finally masked array as the condition in the mask_where command.

For example:

y = np.array([2,1,5,2])                         # y axis
x = np.array([1,2,3,4])                         # x axis
m = np.ma.masked_where(y>5, y)                  # filter out values larger than 5
new_x = np.ma.masked_where(np.ma.getmask(m), x) # applies the mask of m on x

The nice thing is you can now apply this mask to many more arrays without going through the masking process for each of them.

Solution 3

This may not bee 100% what OP wanted to know, but it's a cute little piece of code I use all the time - if you want to mask several arrays the same way, you can use this generalized function to mask a dynamic number of numpy arrays at once:

def apply_mask_to_all(mask, *arrays):
assert all([arr.shape == mask.shape for arr in arrays]), "All Arrays need to have the same shape as the mask"
return tuple([arr[mask] for arr in arrays])

See this example usage:

    # init 4 equally shaped arrays
x1 = np.random.rand(3,4)
x2 = np.random.rand(3,4)
x3 = np.random.rand(3,4)
x4 = np.random.rand(3,4)

# create a mask
mask = x1 > 0.8

# apply the mask to all arrays at once
x1, x2, x3, x4 = apply_mask_to_all(m, x1, x2, x3, x4)
Share:
57,527
Balthasar
Author by

Balthasar

Updated on November 24, 2020

Comments

  • Balthasar
    Balthasar over 3 years

    I've read the masked array documentation several times now, searched everywhere and feel thoroughly stupid. I can't figure out for the life in me how to apply a mask from one array to another.

    Example:

    import numpy as np
    
    y = np.array([2,1,5,2])          # y axis
    x = np.array([1,2,3,4])          # x axis
    m = np.ma.masked_where(y>2, y)   # filter out values larger than 5
    print m
    [2 1 -- 2]
    print np.ma.compressed(m)
    [2 1 2]
    

    So this works fine.... but to plot this y axis, I need a matching x axis. How do I apply the mask from the y array to the x array? Something like this would make sense, but produces rubbish:

    new_x = x[m.mask].copy()
    new_x
    array([5])
    

    So, how on earth is that done (note the new x array needs to be a new array).

    Edit:

    Well, it seems one way to do this works like this:

    >>> import numpy as np
    >>> x = np.array([1,2,3,4])
    >>> y = np.array([2,1,5,2])
    >>> m = np.ma.masked_where(y>2, y)
    >>> new_x = np.ma.masked_array(x, m.mask)
    >>> print np.ma.compressed(new_x)
    [1 2 4]
    

    But that's incredibly messy! I'm trying to find a solution as elegant as IDL...