[Numpy-discussion] PEP: named axis

Travis Oliphant oliphant at enthought.com
Thu Feb 5 23:17:54 EST 2009


Gael Varoquaux wrote:
> On Thu, Feb 05, 2009 at 05:08:49PM -0600, Travis E. Oliphant wrote:
>   
>> I've been fairly quiet on this list for awhile due to work and family 
>> schedule, but I think about how things can improve regularly.    One 
>> feature that's been requested by a few people is the ability to select 
>> multiple fields from a structured array.
>>     
>
> Hey Travis,
>
> I have no opinion on the above, as I don't have this use case. However, as
> you are talking about implementing something, I jump on the occasion to
> suggest another gadget, slightly related: I would like named axis.
> Suppose you have a 5D array, I would like to be able to give each axis
> names, eg (to chose an example you might be familiar with) ('Frontal',
> 'Lateral', 'Axial', 'Time', 'Subjects'). And if this could be understood
> be numpy operations (say ufuncs and fancy indexing) so that I could do (a
> is my 5D array):
>
>   
This could be implemented but would require adding information to the 
NumPy array.   I've been thinking for a long time that we ought to add a 
"dictionary" attribute to the NumPy array (i.e. a new member to the 
PyArrayObject data-structure).   A lot of subclasses of NumPy arrays 
just add meta-information that could be stored there. 

Then, it would be a trivial thing to check to see if the dictionary had 
say an "axis_mapping" keyword and if so then do the conversions found 
there.

I think this has been brought up before, though.  What do people think 
about adding a default dictionary to every instance of a NumPy array.

The question that always arises in this context which I don't have good 
answers for is what do you do with the dictionary on the output of 
ufuncs?   One approach is to always return NULL for the dictionary and 
don't try and guess.   A slightly different one is to at least handle 
the case where all inputs have the same dictionary and return a new 
"shallow" copy of that.


-Travis




More information about the NumPy-Discussion mailing list