[Numpy-discussion] Behavior of __array_wrap__?

Brian Granger ellisonbg.net@gmail....
Wed Apr 2 10:32:28 CDT 2008


Hi,

I am creating a custom array type (distributed memory arrays -
DistArray) and I am using the __array__ and __array_wrap__ methods and
__array_priority__ attribute to get these arrays to work with numpy's
ufuncs.  Things are working fine when I call a ufunc like this:

# This works fine (c comes back as an DistArray)
a = DistArray(10)
b = DistArray(10)
c = np.add(a, b)

But, when you pass in an ndarray as the return array, the
__array_wrap__ doesn't get called:

# Passing in an ndarray as the return array causes __array_wrap__ to not
# be called.  Thus, you get back an ndarray.
d = np.empty_like(a.array)
d = np.add(a, b, d)

But, the most problematic case is when you try to pass in a DistArray
as the return type:

# This fails saying that the return array must be an ArrayType
d = DistArray(10)
d = np.add(a, b, d)

The documentation for __array__ and __array_wrap__ describe a very
different behavior:

1) The result of the ufunc is assigned to the aray returned by the
__array__ method of the return array.

2) The resulting object is then passed to the __array_wrap__ method of
that array to be properly wrapped.

But, it seems that numpy's ufuncs are not even allowing anything but a
full blown ndarray as the return type.

Is this a bug (I will then file a bug report)?  Has the behavior
changed (documentation probably needs to be updated)?

I am attaching some sample code that demonstrates the issue for a
simple class.

Thanks

Brian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testarray.py
Type: text/x-python-script
Size: 898 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080402/55954d7c/attachment-0001.bin 


More information about the Numpy-discussion mailing list