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

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

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)

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.

Tags:

Python

Numpy