[Numpy-discussion] np.ma.apply_along_axis mask propagation

Angus McMorland amcmorl at gmail.com
Wed Jan 27 11:21:02 EST 2010


Hi all,

I'm having an issue with np.ma.apply_along_axis not propagating a mask
correctly. This code-snippet should hopefully show what's happening:
--------------------
import numpy as np

xy = np.random.random(size=(5,2))
mask = np.tile(np.array([True] * 3 + [False] * 2)[:,None], (1,2))
xyma = np.ma.array(xy, mask=mask)

def myfunc(vec):
    x,y = vec
    return x,y

xyma2 = np.ma.apply_along_axis(myfunc, 1, xyma)
tst = "np.all(asarray(myfunc(xyma[1])).mask == xyma2[1].mask)"
print tst, ":", eval(tst)
-> np.all(asarray(myfunc(xyma[1])).mask == xyma2[1].mask) : False
--------------------

The point here is not that xyma.mask != xyma2.mask, but that
xyma2.mask != the output of myfunc run on the individual rows, which
it seems like it should.

The following simple change seems to fix this.

--- /usr/lib/python2.6/dist-packages/numpy/ma/extras.py 2009-04-05
04:09:20.000000000 -0400
+++ tmp/extras.py   2010-01-27 10:45:10.000000000 -0500
@@ -322,7 +322,7 @@
                 n -= 1
             i.put(indlist, ind)
             j.put(indlist, ind)
-            res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
+            res = asarray(func1d(arr[tuple(i.tolist())], *args, **kwargs))
             outarr[tuple(flatten_inplace(j.tolist()))] = res
             dtypes.append(asarray(res).dtype)
             k += 1

Does this seem like an improvement? I haven't explored performance
issues yet, pending sanity check.

Thanks,

Angus.
-- 
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh



More information about the NumPy-Discussion mailing list