[Numpy-discussion] can't resize ndarray subclass

Zachary Pincus zpincus at stanford.edu
Tue Feb 28 11:30:04 CST 2006


In answer to my previous question, to get an ndarray subclass that  
owns its own data, copy() must be called on the new "view" of the  
subclass. This makes sense and is reasonable.

However a new problem has me nearly tearing my hair out. Calling the  
resize method on an instance of such a subclass works fine. However,  
calling a method that calls 'self.resize' breaks! And worse, it  
breaks in such a way that then subsequent calls to resize also break.

Check it out:

class f(numpy.ndarray):
   def __new__(cls, obj):
     return numpy.array(obj).view(cls).copy()
   def expand(self):
     self.resize([self.shape[0] + 1, self.shape[1]])

g = f([[1,2],[3,4]])
g.resize([3,2]) # this works, thanks to the '.copy()' above

g = f([[1,2],[3,4]])
g.expand() # just internally calls self.resize([3,2])
   ValueError: cannot resize an array that has been referenced or is  
referencing another array in this way.  Use the resize function
g.resize([3,2]) # this NOW DOES NOT WORK!
   ValueError: cannot resize an array that has been referenced or is  
referencing
another array in this way.  Use the resize function


Can anyone help? Please?

Zach


On Feb 28, 2006, at 4:35 AM, Zachary Pincus wrote:

> Thus far, it seems like the way to get instances of ndarray  
> subclasses is to use the 'view(subclass)' method on a proper  
> ndarray, either in the subclass __new__, or after constructing the  
> array.
>
> However, subclass instances so created *do not own their own data*,  
> as far as I can tell. They are just "views" on an other array's  
> data. This means that these objects can't be resized, or have other  
> operations performed on them which requires 'owning' the data buffer.
>
> E.g.:
> class f(numpy.ndarray):
>   def __new__(cls, *p, **kw):
>     return numpy.array(*p, **kw).view(cls)
>
> f([1,2,3]).resize((10,))
>    ValueError: cannot resize this array:  it does not own its data
> numpy.array([1,2,3]).view(f).resize((10,))
>    ValueError: cannot resize this array:  it does not own its data
> numpy.array([1,2,3]).resize((10,))
>    (no problem)
>
> Is there another way to create ndarray subclasses which do own  
> their own data?
>
> Note that numpy.resize(f([1,2,3]), (10,)) works fine. But this  
> isn't the same as having an object that owns its data.  
> Specifically, there's just no way to have an ndarray subclass  
> resize itself as a result of calling a method if that object  
> doesn't own its data. (Imagine a variable-resolution polygon type  
> that could interpolate or decimate vertices as needed: such a class  
> would need to resize itself.)
>
> Zach
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting  
> language
> that extends applications into web and mobile media. Attend the  
> live webcast
> and join the prime developer group breaking into this new coding  
> territory!
> http://sel.as-us.falkag.net/sel? 
> cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion





More information about the Numpy-discussion mailing list