[Numpy-discussion] read-only or immutable masked array
Gregorio Bastardo
gregorio.bastardo at gmail.com
Mon Jul 15 08:40:18 EDT 2013
Hi Pierre,
> Note as well that hardening the mask only prevents unmasking: you can still grow the mask, which may not be what you want. Use `x.mask.flags.writeable=False` to make the mask really read-only.
I ran into an unmasking problem with the suggested approach:
>>> np.version.version
'1.7.0'
>>> x = np.ma.masked_array(xrange(4), [0,1,0,1])
>>> x
masked_array(data = [0 -- 2 --],
mask = [False True False True],
fill_value = 999999)
>>> x.flags.writeable = False
>>> x.mask.flags.writeable = False
>>> x.mask[1] = 0 # ok
Traceback (most recent call last):
...
ValueError: assignment destination is read-only
>>> x[1] = 0 # ok
Traceback (most recent call last):
...
ValueError: assignment destination is read-only
>>> x.mask[1] = 0 # ??
>>> x
masked_array(data = [0 1 2 --],
mask = [False False False True],
fill_value = 999999)
I noticed that "sharedmask" attribute changes (from True to False)
after "x[1] = 0". Also, some of the ma operations result mask identity
of the new ma, which causes ValueError when the new ma mask is
modified:
>>> x = np.ma.masked_array(xrange(4), [0,1,0,1])
>>> x.flags.writeable = False
>>> x.mask.flags.writeable = False
>>> x1 = x > 0
>>> x1.mask is x.mask # ok
False
>>> x2 = x != 0
>>> x2.mask is x.mask # ??
True
>>> x2.mask[1] = 0
Traceback (most recent call last):
...
ValueError: assignment destination is read-only
which is a bit confusing. And I experienced that *_like operations
give mask identity too:
>>> y = np.ones_like(x)
>>> y.mask is x.mask
True
but for that I found a recent discussion ("empty_like for masked
arrays") on the mailing list:
http://mail.scipy.org/pipermail/numpy-discussion/2013-June/066836.html
I might be missing something but could you clarify these issues?
Thanks,
Gregorio
More information about the NumPy-Discussion
mailing list