1

I create a numpy masked array with the shrink-option set to False (which should yield a full-sized mask), and then I check the size of the mask:

import numpy as np
import numpy.ma as ma
x = ma.array(range(10),shrink=False)
print 'mask size = ', np.array(x.mask).size

which yields: mask size = 1, i.e., the mask is still the default (shrunk) scalar mask.

Is this a (known) bug?

Update: It seems that also the option shrink=True does not work properly:

x = ma.array(range(3), mask=True, shrink=True)
x.__setmask__(ma.nomask)                    # remove the mask (should shrink now)
x.mask.size                                 # returns 3, so mask has not shrunk!
_ = x.shrink_mask()                         # enforce shrinking
x.mask.size                                 # returns 1, so only now it's OK 
Rolf Bartstra
  • 1,643
  • 1
  • 16
  • 19

2 Answers2

4

There's a misunderstanding here: the shrink option flag prevents the compression of the mask in operations, not at creation. To get an explicit mask (as a boolean array full of False), use the mask=False flag at creation instead. Nevertheless, I agree it should be considered a bug. Nice catch.


When no explicit mask is given, the default is nomask, a special value corresponding to np.bool_(0): it's a numpy boolean scalar with a value of False, and like any numpy scalar, a shape of () and a size of 1.

Note the difference between mask=False and mask=nomask: mask=False will create a mask as a ndarray with the same shape as the data but full of False (that's a shortcut), while mask=nomask just tell np.ma that the mask is not set (which speeds up computations).

Pierre GM
  • 19,809
  • 3
  • 56
  • 67
  • Thanks, the different behaviour of the shrink-option at array-creation vs. -operation wasn't clear to me from the documentation. Also, isn't it curious that creating an array A with `mask=nomask` results in `A.mask` yielding `False`, but expicitly setting `A.mask=False` yields something different: [False, False, ...]? :-) – Rolf Bartstra Nov 02 '12 at 18:31
  • It seems though that also during array _operation_, the shrink option does not work properly, see update to my question above – Rolf Bartstra Nov 06 '12 at 10:38
2

Just try setting your mask (either to False or True):

>>> np.ma.array(range(10), mask=False, shrink=False)
masked_array(data = [0 1 2 3 4 5 6 7 8 9],
             mask = [False False False False False False False False False False],
       fill_value = 999999)

>>> np.ma.array(range(10), mask=True, shrink=False)
masked_array(data = [-- -- -- -- -- -- -- -- -- --],
             mask = [ True  True  True  True  True  True  True  True  True  True],
       fill_value = 999999)

The default for the mask parameter is nomask, but apparently doesn't generate any mask (and thus nothing can be "unshrunk"). Note sure why it still shows a size of 1 though.