Repository navigation
Create ndarray (recarray?) subclass that can hold multiple Quantities of mixed units #3777
Description
Activity
- added a commit that references this issue
on Aug 11, 2015 Looking over old units issues, I saw this one. Am still somewhat intrigued, though as written I don't see how it could work, as all the mathematical operations don't make sense if different parts of a quantity have a different unit. It is similar to having an
ndarraywith mixed integer and float elements -- it just would not make much sense. Given that analogy, however, what might work is to haverecarraysubclass that has different units for its different columns (of course, this rapidly becomes aTable...). Indeed, if numpy proceeds with making it possible to define the unit on thedtype, then this would be nearly trivial to implement.(I removed "bug" since that aspect has been solved; it really is a "feature request" now)
- changed the title
[-]Create ndarray subclass that can hold multiple Quantities of mixed units[/-][+]Create ndarray (recarray?) subclass that can hold multiple Quantities of mixed units[/+]on Aug 11, 2016 One place where this concept would be useful is for making the
Representationclasses a bit nicer (they are a bit clunky right now IMO). If I recall, they currently store everything internally as Cartesian coordinates? But in principle, if I create aSphericalRepresentationit could avoid that conversion step unless necessary and store the position (length, angle, angle) as a single array (or just be an array/recarray subclass). Do you have any thoughts on this @mhvk or @eteq ?@adrn - the representation classes do store items as they are (i.e.,
SphericalRepresentationdoes store, radius, longitude, latitude). I do think there might be some benefits to usingrecarrayinstead, as it would remove some boiler-plate, but probably not a huge deal.@mhvk oops, my mistake for mis-remembering...
Well, don't use
recarrayspecifically--that class is all but deprecated.I guess I meant an
ndarraywhich uses adtypewith records. It might make sense to be able to associate different units with different records.Reacted by ebuchlinAnother place where this might be useful is when storing phase-space information, e.g., position and velocity components, so this would be a useful feature!
Also for model parameters! (models have a way of accessing arrays of parameters but for models with units we just return a list for now)
Just to note this is in the works, as it really helps deal with ERFA ufuncs that use/return structure dtype arguments (e.g., position and velocity).
@mhvk, your PR closes this, right?
Indeed. Now ensured this will be closed once #11775 is merged.
Reacted by Nathaniel Starkman and P. L. Lim
Over in this comment, @mhvk suggested that if we want an array-like container for Quantities of different units, if nothing else we could just use an ndarray of
dtype=objectto hold multipleQuantityobjects. And in principle I'm fine with that as a solution.However, I suggested in a followup that a subclass specially for this purpose might still be useful, if nothing else, for improved representation. For example, instead of an array of Quantities looking like:
(which looks even worse when you go > 1 dimension), it might be nice to have something that reprs/prints like
etc. This would look especially nice if the repr is justified so that the columns line up :)
However, it turns out that a subclass for this purpose might be necessary even aside from aesthetic improvements, since it seems currently (at least with Numpy 1.9.1 which I've tested this on) it's not even possible to repr an array of Quantities:
It seems that the Numpy array print code at some point tries ordering the values in the array, leading to this crash. This indicates maybe two things:
We could work around this specific error, I think, by making
UnitsErroraValueErrorsubclass.Even if we fixed that specific issue it demonstrates that an array of Quantities can't be displays as well as one might like, and it might be worth fixing that.
A further question is why would we even need an array of Quantities of different units. I think if nothing else Quantity support in modeling may end up wanting something like this.