[Numpy-discussion] read-only or immutable masked array
pgmdevlist at gmail.com
Mon Jul 15 07:11:54 EDT 2013
On Jul 15, 2013, at 10:04 , Gregorio Bastardo <gregorio.bastardo at gmail.com> wrote:
> Hi Pierre,
>> I'm a bit surprised, though. Here's what I tried
>> <<< 1.7.0
>>>>> x = np.ma.array([1,2,3], mask=[0,1,0])
>> <<< ValueError: assignment destination is read-only
> Thanks, it works perfectly =) Sorry, probably have overlooked this
> simple solution, tried to set x.data and x.mask directly. I noticed
> that this only protects the data, so mask also has to be set to
> read-only or be hardened to avoid accidental (un)masking.
Well, yes and no. Settings the flags of `x` doesn't set (yet) the flags of the mask, that's true. Still, `.writeable=False` should prevent you to unmask data, provided you're not trying to modify the mask directly but use basic assignment like `x[…]=…`. However, assigning `np.ma.masked` to array items does modify the mask and only the mask, hence the absence of error if the array is not writeable.
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.
More information about the NumPy-Discussion