[Numpy-discussion] Recommendations for using numpy ma?

Russell E. Owen rowen at cesmail.net
Wed Jul 16 12:28:40 EDT 2008


In article <200807151755.32256.pgmdevlist at gmail.com>,
 Pierre GM <pgmdevlist at gmail.com> wrote:

> Russell,
> 
> What used to be numpy.core.ma is now numpy.oldnumeric.ma, but this latter isd 
> no longer supported and will disappear soon as well. Just use numpy.ma
> 
> If you really need support to ancient versions of numpy, just check the import
> try:
>    import numpy.core.ma as ma
> except ImportError:
>    import numpy as ma

(I assume you mean the last line to be "import numpy .ma as ma"?)

Thanks! I was afraid I would have to do that, but not having ready 
access to ancient versions of numpy I was hoping I was wrong and that 
numpy.ma would work for those as well.

However, I plan to assume a modern numpy first, as in:
try:
   import numpy.ma as ma
except ImportError:
  import numpy.core.ma as ma

> Then, you need to replace every mention of numpy.core.ma in your code by ma.
> Your example would then become:
> 
> unmaskedArr = numpy.array(
>     ma.array(
>     ^^
>         dataArr,
>         mask = mask & self.stretchExcludeBits,
>         dtype = float,
>     ).compressed())
> 
> 
> 
> On another note: wha't the problem with 'compressed' ? It should return a 
> ndarray, why/how doesn't it work ?

The problem is that the returned array does not support the "sort" 
method. Here's an example using numpy 1.0.4:

import numpy
z = numpy.zeros(10, dtype=float)
m = numpy.zeros(10, dtype=bool)
m[1] = 1
mzc = numpy.ma.array(z, mask=m).compressed()
mzc.sort()

the last statement fails witH:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-pac
kages/numpy/core/ma.py", line 2132, in not_implemented
    raise NotImplementedError, "not yet implemented for numpy.ma arrays"
NotImplementedError: not yet implemented for numpy.ma arrays

This seems like a bug to me. The returned object is reported by "repr" 
to be a normal numpy array; there is no obvious way to tell that it is 
anything else. Also I didn't see any reason for "compressed" to return 
anything except an ordinary array. Oh well.

I reported this on the mailing list awhile ago when I first stumbled 
across it, but nobody seemed interested at the time. It wasn't clear to 
me whether it was a bug so I dropped it without reporting it formally 
(and I've still not reported it formally).

-- Russell




More information about the NumPy-Discussion mailing list