If you try the following code segment
import numpy as np
import numpy.ma as ma
a = np.random.random(100) + 1j*np.random.random(100)
mask = np.ones_like(a, dtype='bool')
mask[0:9] = False
a = ma.masked_array(a, mask)
phase = np.angle(a)
The phase array will not be masked. The angle function will return values for the whole array, even for the masked out values. Am I doing something wrong here or is this the way it should be? If so, why?
Had a quick look at the numpy source, and it might be a bug/not implemented yet.
It’s listed as a “missing feature (work in progress)” on the numpy.ma page, issue #1: http://projects.scipy.org/numpy/wiki/MaskedArray.
The problem is that a number of unary functions such as
np.angle,np.quantilecall[np.]asarrayin the source, which strips out the mask.As the devs explain in the page I linked to, if these functions used
ma.asarrayinstead ofnp.asarraythey’d work, but they don’t :(.I guess this is a patch yet to be submitted?
As a temporary workaround,
np.anglebasically callsnp.arctan2(a.imag,a.real)(optionally multiplying by 180/pi to get degrees), so you could use that.