[Numpy-discussion] Proxy array class and units

Dan Goodman dg.gmane.comp.python.numeric.general@thesamovar....
Thu Feb 14 10:18:44 CST 2008


Hi Anne,

Thanks for your reply. As you say there are a few different problems in here.
The first is about implementing a system of physical quantities with units. I
reviewed various options for this including the ones you mentioned (unum and
ScientificPython), but they all had some important features missing, so I
implemented my own class which does this (building on the ideas they used). This
is finished now and I don't need to worry about it.

The problem I'm working on now is the best way to deal with arrays of quantities
with units, and efficient computations. At the moment, what I'm going for is an
internal implementation (the code in the package) which knows about the units of
the arrays, but stores them as numpy arrays for speed. What I want to do though
is to have it so that as far as the user of the package is concerned, everything
appears to have units (single quantities or arrays of quantities).

One way of doing this is as you have suggested, defining an array class that has
a single unit. This is quite appealing and I'm trying to implement this in a way
that does everything I want it to do (Unum does this but it has some odd
behaviours that make it unsuitable for what I want to do). There is still a
problem though that one of the main data structures in my package is a 2d array
with each column having a different unit. This wouldn't be a huge problem
because I can pass the user slices of that array which have a single unit.

Perhaps a more specific question I can ask is: what considerations do you need
to take into account when subclassing numpy arrays in a way that changes their
semantics in a substantial way like adding units to them? The documentation that
I have found only seems to deal with fairly simple cases where you are just
adding something to the array (like in the http://scipy.org/Subclasses
document). For example, when I tried to add a single unit to a numpy array, I
tried overriding __getitem__(self,i) so that it returned
numpy.ndarray.__getitem__(self,i)*self.unit which kind of works except that when
I print the array you don't see the units, just the underlying float values. As
I said before, I can probably override this behaviour, but how many other
similar things like that would I need to do?

Dan



More information about the Numpy-discussion mailing list