I have some code that does this: # an extra array cast is used because "compressed" returns what *looks* like an array # but is actually something else (I'm not sure exactly what) unmaskedArr = numpy.array( numpy.core.ma.array( dataArr, mask = mask & self.stretchExcludeBits, dtype = float, ).compressed()) That was working fine in numpy 1.0.4 but I've just gotten a report that it fails in 1.1. So...is there a notation that is safer: compatible with the widest possible range of versions? If I replace "numpy.core.ma" with "numpy.ma" this weems to work in 1.0.4 (I'm not sure about 1.1). But I fear it might not work with older versions of numpy. This software is used by a wide range of users with a wide range of versions of numpy. -- Russell
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 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 ?
In article <200807151755.32256.pgmdevlist@gmail.com>, Pierre GM <pgmdevlist@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
On Wednesday 16 July 2008 12:28:40 Russell E. Owen wrote:
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"?)
Indeed, sorry about that.
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.
Works on 1.1.x
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.
.compressed returns an array of the same type as the underlying .data
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).
Try again w/ 1.1.x, but please, please do report bugs when you see them..
participants (2)
-
Pierre GM
-
Russell E. Owen