[Numpy-discussion] Attempting to sub-class NumArray

Colin J. Williams cjw at sympatico.ca
Fri Sep 5 12:04:02 EDT 2003

When ravel() is used on an instance of NumArray, the _strides attribute 
is recalculated correctly.
When ravel() is used on an instance of special, a sub-class of NumArray, 
the _strides attribute keeps its old value.  This appears to cause the 
display problem which is reported.

The essentials of the code used are below.

To investigate this problem, two print statements were added to the 
method setshape in generic.py:

            if newnelements == nelements:
                self._shape = tuple(shape)
                print 'In setShape self._shape:', self._shape
                self._strides = self._stridesFromShape()
                print 'In setShape self._strides:', self._strides
                raise ValueError("New shape is not consistent with the
    old shape")

It seems that only the first of these print statements is executed with 
special, but both are executed with NumArray.  In the second case, the 
output is not in the expected sequence.

I would appreciate any suggestion as to a workaround.

Colin W,

Grief when attempting to sub-class NumArray

Code used:

# Try to sub-class
class special(N.NumArray):
  def __init__(self, data= None, shape= None, eType= None):
##                         eType= _nt.Any or AnyType not acceptable %%
    arr= N.array(sequence= data, shape= shape, type= eType)
    for attr in dir(arr):
##      This is a longwinded way of setting up special
##      There must be a better way                     %%
##      Perhaps we should use generic._view
##      It's not documented                 TRY IT LATER
##      There is no need to transfer methods!
      if attr[0] == '_' and attr[1] != '_':
        print attr
        exec 'self.' + attr + '= ' 'arr.' + attr
        exec 'print self.' + attr

a= special(data= [1, 2, 3, 4], shape= (2, 2))
a= N.array([1, 2, 3, 4], shape= (2, 2))
print 'In ts a._strides:', a._strides
b= _gen.ravel(a)
print 'In ts b._strides:', b._strides       #  <<< Unchanged with 
special, OK with NumArray
print 'b:', b

 >C:\Progra~1\Python23\pythonw -u ts.py
In ts a._strides: (8, 4)
In setShape self._shape: (4,)
In ts b._strides: (8, 4)
b: In setShape self._shape: (4,)
Traceback (most recent call last):
  File "ts.py", line 37, in ?
    print 'b:', b
"C:\PROGRA~1\Python23\lib\site-packages\numarray\numarraycore.py", line 
618, in __str__
  File "C:\PROGRA~1\Python23\lib\site-packages\numarray\arrayprint.py", 
line 176, in array2string
    separator, prefix)
  File "C:\PROGRA~1\Python23\lib\site-packages\numarray\arrayprint.py", 
line 138, in _array2string
    max_str_len = max(len(str(max_reduce(data))),
libnumarray.error: maximum_Int32_reduce: access beyond buffer. offset=27 
 >Exit code: 1

# USING NumArray

In setShape self._shape: (2, 2)
In ts a._strides: (8, 4)
In setShape self._shape: (4,)
In ts b._strides: (4,)
b: In setShape self._shape: (4,)
[1 2 3 4]
 >Exit code: 0

More information about the NumPy-Discussion mailing list