pylab/masked arrays issue on Intel Mac Pro

Hi,

I downloaded&installed matplotlib 0.87.7 and other binary packages from
pythonmac.org for my new Intel Mac Pro. Then I downloaded and compiled the
latest scipy package. My collection of packages runs ok otherwise but there
seems to be something wrong with pylab/masked array behavior.

This works on my older installations (g5):

import matplotlib
matplotlib.use("TkAgg")
from pylab import *
from MA import *
#import MA
a=arange(100)
a.shape=(5,20)
a=swapaxes(a,0,1)
m=array([1,2,3],mask=[0,1,0])
#m=MA.array([1,2,3],mask=[0,1,0])

On my Intel Mac Pro, I get:
File "test.py", line 12, in ?
    a=swapaxes(a,0,1)
  File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/numpy/core/fromnumeric.py",
line 106, in swapaxes
    return _wrapit(a, 'swapaxes', axis1, axis2)
  File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/numpy/core/fromnumeric.py",
line 39, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
  File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/numpy/core/numeric.py",
line 132, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: object __array__ method not producing an array

There seems to be some kind of name space conflict. The line
'a=swapaxes(a,0,1)' works as expected without 'from MA import *'. If I do
'import MA' and use MA.array etc for all operations with masked arrays
everything seems to work fine.

Any ideas where the conflict might be coming from?

···

--
Teemu Rinne
Dept Psychology
University of Helsinki

Teemu

Any ideas where the conflict might be coming from?

When you type from MA import *, you overwrite the definition of 'array'. And
surprisingly enough, a masked array in numpy.core.ma is an object
independent of ndarray. Some functions such as swapaxes are not imported by
MA (because they were not implemented for one reason or another): you then
use the regular numpy function, which doesn't know how to deal with these
'masked arrays' objects. (I simplify a bit, here, but the gist is there)

As you've noticed, the best solution is to use
import MA
That way, you leave the original numpy namespace alone, and are sure to use
only "MA-approved" functions. But you'll likely gonna have to use MA.sqrt,
MA.log all over your code. And may run into problems with functions that
don't handle masked arrays. Still, it's the cleanest solution (the "from blah
import *" tend to be frowned upon these days anyway).

An alternative solution is to try the new implementation of masked arrays I've
been working on: with this implementation, masked arrays are subclasses of
regular arrays, which simplifies life a lot.The source file is available
here:
http://projects.scipy.org/scipy/numpy/wiki/MaskedArray
Note the disclaimer: it's still a work in progress (even if I use it everyday
and it works fine) and you'll have to modify a line in the matplotlib source
to make it work OK with matplotlib, which is a bit tedious I agree. But it
works. And it's actively maintained.

Hope it helps
P.