[Numpy-discussion] Problem with ufunc of a numpy.ndarray derived class

Martin DRUON martin.druon at wanadoo.fr
Mon Jul 11 08:58:35 EDT 2011


Hi,

I have a problem with the ufunc return type of a numpy.ndarray derived class. In fact, I subclass a numpy.ndarray using the tutorial : 
http://docs.scipy.org/doc/numpy/user/basics.subclassing.html

But, for example, if I execute the "max" ufunc from my subclass, the return type differs from the return type of the numpy ufunc.


This is my code (testSubclassNumpy.py), "copy/paste" from the tutorial :

# -*- coding: utf-8 -*-

import numpy

class MySubClass(numpy.ndarray):

def __new__(cls, input_array, info=None):
obj = numpy.asarray(input_array).view(cls)
obj.info = info
return obj

def __array_finalize__(self, obj):
#print 'In __array_finalize__:'
#print ' self is %s' % repr(self)
#print ' obj is %s' % repr(obj)
if obj is None: return
self.info = getattr(obj, 'info', None)

def __array_wrap__(self, out_arr, context=None):
#print 'In __array_wrap__:'
#print ' self is %s' % repr(self)
#print ' arr is %s' % repr(out_arr)
# then just call the parent
return numpy.ndarray.__array_wrap__(self, out_arr, context)



>>> import numpy
>>> numpy.__version__
'1.6.0'
>>> import testSubclassNumpy
>>> a = numpy.random.random(size=(10,10))
>>> t = testSubclassNumpy.MySubClass(a)
>>> type(a)

>>> type(t)


>>> a.max()
0.99207693069079683
>>> t.max()
MySubClass(0.9920769306907968)

>>> type(numpy.max(a))

>>> type(numpy.max(t))



This problem seems to be appeared with the latest version of numpy. Today, I use Python 2.7.2 + numpy 1.6.0 but I didn't have this problem with python 2.6.6 and numpy 
1.5.1.

Is it a bug ? or perhaps I have made mistake somewhere...

thanks,
Martin



More information about the NumPy-Discussion mailing list