[Numpy-discussion] 1.2 tasks

Darren Dale dsdale24@gmail....
Mon Aug 4 16:53:54 CDT 2008


Hi Pierre,

On Monday 04 August 2008 04:29:57 pm Pierre GM wrote:
> On Monday 04 August 2008 14:16:33 Darren Dale wrote:
> > Hi Jarrod,
> >
> > I was wondering if someone knowledgeable would please look into the
> > behavior of concatenate() with subclasses of ndarray.
>
> Darren,
> I ran into similar problems when I started working on numpy.ma and
> scikits.timeseries, and I remember having conversations with Bryce Hendrix
> of Enthought about that on this very list.
>
> I think you have a problem in `NDQuantities.__array_finalize__`: you don't
> initialize `._units` if `obj` doesn't have a `_units`. At least, you should
> use something like
> self._units = getattr(obj, '_units', None),
> which will set `._units` to None if `obj` is not a NDQuantities, and will
> also ensure that your NDQuantities array always have a ._units attribute.
> In other words, if I take a view of a ndarray as a NDQuantities, I should
> have a _units defined by default (None being the most appropriate in that
> case).
>
> About concatenation:
> In a lot of cases, concatenate requires some tweaking. In
> scikits.timeseries, we have to decide what to do when the series being
> concatenated overlap. In your case, you have to decide what to do when
> concatenating 2 arrays A & B, for example:
> * if A & B have the same unit, keep it
> * If A (B) doesn't have a unit, keep the unit of B (A)
> * If the units of A and B are compatible, convert one to the other
> * If the units are incompatible, raise an exception.
> What I suggest is to implement your own concatenate: check the units,
> concatenate the arrays as regular ndarrays, take a view of the result,
> modify the unit of the result accordingly.
>
> About subclassing:
> * You may want to check the cookbook:
> http://www.scipy.org/Subclasses
> * You may also want to check other ndarray subclasses, such as MaskedArray,
> TimeSeries (</pushing product>) for some implementation details.
> * You may also want add your own experience on
> http://www.scipy.org/Subclasses, so that we don't lose it.
>
> Drop me a line offlist if you'd like, I'd be happy to help (depending on
> the time at hand).
> Cheers.

Thank you for the comments. You made a number of good points and I will look 
into each. I agree that it looks like I need to write my own concatenate 
function, but judging from Travis' comment a while back, there may still be 
an issue with numpy's concatenate. In my example, it is not just that concat 
is stripping the units, it is returning a different type. Maybe that is not 
such a bad thing in this case, but I thought it should be brought to the 
numpy maintainers attention.

Darren


More information about the Numpy-discussion mailing list