Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-28 Thread Pierre GM
On Friday 27 October 2006 18:37, Albert Strasheim wrote: > Hello all ... > I would like to create an "info" array like this to attach some metadata, > mostly sampling frequency, to some of my arrays. > ... > I browsed through the NumPy book and looked at the __new__ and > __array_finalize__ stuff i

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-27 Thread Albert Strasheim
Hello all > -Original Message- > From: [EMAIL PROTECTED] [mailto:numpy- > [EMAIL PROTECTED] On Behalf Of Travis Oliphant > Sent: Thursday, October 19, 2006 5:45 PM > To: Discussion of Numerical Python > Subject: Re: [Numpy-discussion] adding an attribute to an nd-array &

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-20 Thread Stefan van der Walt
On Thu, Oct 19, 2006 at 09:03:57PM -0400, Pierre GM wrote: > Indeed. That's basically why you have to edit your __array_finalize__ . > > class InfoArray(N.ndarray): > def __new__(info_arr_cls,arr,info={}): > info_arr_cls._info = info > return N.array(arr).view(info_arr_cls) >

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Pierre GM
> > >class InfoArray(N.ndarray): > > >def __new__(info_arr_cls,arr,info={}): > > >info_arr_cls.info = info > > >return N.array(arr).view(info_arr_cls) > > One has to be careful of this approach. It ads *the same* information > to all arrays, i.e. Indeed. That's basically why

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Stefan van der Walt
On Thu, Oct 19, 2006 at 09:45:02AM -0600, Travis Oliphant wrote: > Stefan van der Walt wrote: > > >If I understand correctly, the following should work: > > > >import numpy as N > > > >class InfoArray(N.ndarray): > >def __new__(info_arr_cls,arr,info={}): > >info_arr_cls.info = info > >

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Stefan van der Walt
On Thu, Oct 19, 2006 at 01:59:49PM -0700, Christopher Barker wrote: > Travis Oliphant wrote: > > Actually something as simple as > > > > class InfoArray(N.ndarray): > > pass > > > > will allow you to add attributes to InfoArray. > > Well, sure, but how the heck do you initialize it? Lo

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Travis Oliphant
Christopher Barker wrote: >Travis Oliphant wrote: > > >>Actually something as simple as >> >>class InfoArray(N.ndarray): >> pass >> >>will allow you to add attributes to InfoArray. >> >> > >Well, sure, but how the heck do you initialize it? > > You use the same constructor as ndarr

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Tim Hochberg
Christopher Barker wrote: > Travis Oliphant wrote: > >> Actually something as simple as >> >> class InfoArray(N.ndarray): >> pass >> >> will allow you to add attributes to InfoArray. >> > > Well, sure, but how the heck do you initialize it? > > >>> class InfoArray(N.ndarray): > ..

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Christopher Barker
Travis Oliphant wrote: > Actually something as simple as > > class InfoArray(N.ndarray): > pass > > will allow you to add attributes to InfoArray. Well, sure, but how the heck do you initialize it? >>> class InfoArray(N.ndarray): ... pass ... >>> InfoArray((1,2,3)) InfoArray

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Lisandro Dalcin
On 10/19/06, Robert Kern <[EMAIL PROTECTED]> wrote: > Travis Oliphant wrote: > > I don't think the extra bytes for every ndarray object are worth it, > > given how easy it is to sub-class and create your own ndarray that can > > have attributes attached.What are others opinions. > > I'd say lea

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Robert Kern
Travis Oliphant wrote: > I just learned about how to allow built-ins to have attributes assigned > to their instances. It's actually pretty easy because of Python > support for it --- but it comes at a cost. You have to add a dictionary > to the PyArrayObject structure, create that dictionary

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Charles R Harris
On 10/19/06, Travis Oliphant <[EMAIL PROTECTED]> wrote: Stefan van der Walt wrote:>On Wed, Oct 18, 2006 at 09:17:49PM -0400, Pierre GM wrote:On Wednesday 18 October 2006 20:29, Stefan van der Walt wrote:>> I don't think the extra bytes for every ndarray object are worth it,given how easy it is

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Travis Oliphant
Stefan van der Walt wrote: >On Wed, Oct 18, 2006 at 09:17:49PM -0400, Pierre GM wrote: > > >>On Wednesday 18 October 2006 20:29, Stefan van der Walt wrote: >> >> >>>A quick question on extending numpy arrays: is it possible to easily >>>add an attribute to an ndarray? >>> >>> >>It migh

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Travis Oliphant
Stefan van der Walt wrote: > On Wed, Oct 18, 2006 at 09:17:49PM -0400, Pierre GM wrote: > >> On Wednesday 18 October 2006 20:29, Stefan van der Walt wrote: >> >>> A quick question on extending numpy arrays: is it possible to easily >>> add an attribute to an ndarray? >>> >> It might

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-19 Thread Stefan van der Walt
On Wed, Oct 18, 2006 at 09:17:49PM -0400, Pierre GM wrote: > On Wednesday 18 October 2006 20:29, Stefan van der Walt wrote: > > A quick question on extending numpy arrays: is it possible to easily > > add an attribute to an ndarray? > > It might be easier to create a subclass: pleasehave a look he

Re: [Numpy-discussion] adding an attribute to an nd-array

2006-10-18 Thread Pierre GM
On Wednesday 18 October 2006 20:29, Stefan van der Walt wrote: > A quick question on extending numpy arrays: is it possible to easily > add an attribute to an ndarray? It might be easier to create a subclass: pleasehave a look here: http://projects.scipy.org/scipy/numpy/attachment/wiki/MaskedArray

[Numpy-discussion] adding an attribute to an nd-array

2006-10-18 Thread Stefan van der Walt
A quick question on extending numpy arrays: is it possible to easily add an attribute to an ndarray? With Python-defined classes one can do class X(object): pass x = X() x.foo = 'bar' but with ndarrays you get x = N.array([1,2,3]) x.foo = 'bar' AttributeError: 'numpy.ndarray' object has n