adding an attribute to an nd-array

Travis Oliphant oliphant at ee.byu.edu
Thu Oct 19 11:45:02 EDT 2006


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 be easier to create a subclass: pleasehave a look here:
>>http://projects.scipy.org/scipy/numpy/attachment/wiki/MaskedArray/test_subclasses.py
>>That's a tiny example of subclassing ndarrays, with some extra attributes.
>>(BTW, I'm not sure that's the most obvious place where to look: if it turns 
>>out to be useful, I'll put it on the scipy wiki)
>>    
>>
>
>Thanks very much, Pierre.
>
>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
>        return N.array(arr).view(info_arr_cls)
>
>When does __array_finalize__ get called, and is it always necessary to
>specify it?
>  
>

Actually something as simple as

class InfoArray(N.ndarray):
         pass

will allow you to add attributes to InfoArray.

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 when the ndarray 
is allocated, and set the tp_dictoffset in the TypeObject structure to 
its location in PyArrayObject.   It takes 4 lines of code with the cost of
creating a new dictionary for every ndarray.



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.

-Travis

P.S.  Here is the patch that adds it:


Index: numpy/core/include/numpy/ndarrayobject.h
===================================================================
--- numpy/core/include/numpy/ndarrayobject.h    (revision 3366)
+++ numpy/core/include/numpy/ndarrayobject.h    (working copy)
@@ -1172,6 +1172,7 @@
         PyArray_Descr *descr;   /* Pointer to type structure */
         int flags;              /* Flags describing array -- see below*/
         PyObject *weakreflist;  /* For weakreferences */
+        PyObject *instancedict; /* For instance attributes */
 } PyArrayObject;

 #define NPY_AO PyArrayObject
Index: numpy/core/src/arrayobject.c
===================================================================
--- numpy/core/src/arrayobject.c        (revision 3366)
+++ numpy/core/src/arrayobject.c        (working copy)
@@ -1906,6 +1906,8 @@
         if (self->weakreflist != NULL)
                 PyObject_ClearWeakRefs((PyObject *)self);

+        Py_DECREF(self->instancedict);
+
         if(self->base) {
                 /* UPDATEIFCOPY means that base points to an
                    array that should be updated with the contents
@@ -5305,6 +5307,7 @@
         self->descr = descr;
         self->base = (PyObject *)NULL;
         self->weakreflist = (PyObject *)NULL;
+        self->instancedict = PyDict_New();

         if (nd > 0) {
                 self->dimensions = PyDimMem_NEW(2*nd);
@@ -6689,7 +6692,7 @@
         0,                                        /* tp_dict */
         0,                                        /* tp_descr_get */
         0,                                        /* tp_descr_set */
-        0,                                        /* tp_dictoffset */
+        offsetof(PyArrayObject, instancedict),    /* tp_dictoffset */
         (initproc)0,                              /* tp_init */
         array_alloc,                              /* tp_alloc */
         (newfunc)array_new,                       /* tp_new */





-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the NumPy-Discussion mailing list