Re: [Numpy-discussion] read-only or immutable masked array

2013-07-15 Thread Gregorio Bastardo
> Ouch… > Quick workaround: use `x.harden_mask()` *then* `x.mask.flags.writeable=False` Thanks for the update and the detailed explanation. I'll try this trick. > This may change in the future, depending on a yet-to-be-achieved consensus on > the definition of 'least-surprising behaviour'. Rig

Re: [Numpy-discussion] read-only or immutable masked array

2013-07-15 Thread Pierre Gerard-Marchant
On Jul 15, 2013, at 14:40 , Gregorio Bastardo wrote: > 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 unm

Re: [Numpy-discussion] read-only or immutable masked array

2013-07-15 Thread Gregorio Bastardo
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

Re: [Numpy-discussion] read-only or immutable masked array

2013-07-15 Thread Pierre Gerard-Marchant
On Jul 15, 2013, at 10:04 , Gregorio Bastardo wrote: > Hi Pierre, > >> I'm a bit surprised, though. Here's what I tried >> > np.version.version >> <<< 1.7.0 > x = np.ma.array([1,2,3], mask=[0,1,0]) > x.flags.writeable=False > x[0]=-1 >> <<< ValueError: assignment destination i

Re: [Numpy-discussion] read-only or immutable masked array

2013-07-15 Thread Gregorio Bastardo
Hi Pierre, > I'm a bit surprised, though. Here's what I tried > np.version.version > <<< 1.7.0 x = np.ma.array([1,2,3], mask=[0,1,0]) x.flags.writeable=False x[0]=-1 > <<< ValueError: assignment destination is read-only Thanks, it works perfectly =) Sorry, probably have overlo

Re: [Numpy-discussion] read-only or immutable masked array

2013-07-14 Thread Pierre GM
On Jul 13, 2013, at 13:36 , Gregorio Bastardo wrote: > Hi Stéfan, > > Thanks for the suggestion, but it does not protect the array: Thinking about it, it can't: when `x` is a MaskedArray, `x.data` is just a view of the underlying array as a regular ndarray. As far as I understand, changing

Re: [Numpy-discussion] read-only or immutable masked array

2013-07-13 Thread Gregorio Bastardo
Hi Stéfan, Thanks for the suggestion, but it does not protect the array: >>> x = np.ma.masked_array(xrange(4), [0,1,0,1]) >>> x masked_array(data = [0 -- 2 --], mask = [False True False True], fill_value = 99) >>> x.mask.flags.writeable = False >>> x.data.flags.writeable

Re: [Numpy-discussion] read-only or immutable masked array

2013-07-12 Thread Stéfan van der Walt
On Fri, Jul 12, 2013 at 4:41 PM, Gregorio Bastardo wrote: > array.flags.writeable = False > > is perfectly fine, but it does not work on ma-s. Moreover, mask > hardening only protects masked elements, and does not raise error (as > I'd expect). You probably have to modify the underlying array and

[Numpy-discussion] read-only or immutable masked array

2013-07-12 Thread Gregorio Bastardo
Hi, I use masked arrays to mark missing values in data and found it very convenient, although sometimes counterintuitive. I'd like to make a pool of masked arrays (shared between several processing steps) read-only (both data and mask property) to protect the arrays from accidental modification (