New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create ndarray (recarray?) subclass that can hold multiple Quantities of mixed units #3777
Comments
…sn't create a new class but it does at least make arrays of Quantity objects printable without breaking or changing too much else. It adds a new UnitConversionError specifically for errors related to that, and is also a ValueError.
…sn't create a new class but it does at least make arrays of Quantity objects printable without breaking or changing too much else. It adds a new UnitConversionError specifically for errors related to that, and is also a ValueError.
…sn't create a new class but it does at least make arrays of Quantity objects printable without breaking or changing too much else. It adds a new UnitConversionError specifically for errors related to that, and is also a ValueError.
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 |
(I removed "bug" since that aspect has been solved; it really is a "feature request" now) |
One place where this concept would be useful is for making the |
@adrn - the representation classes do store items as they are (i.e., |
@mhvk oops, my mistake for mis-remembering... |
Well, don't use |
I guess I meant an |
Another 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. |
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=object
to hold multipleQuantity
objects. 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
UnitsError
aValueError
subclass.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.
The text was updated successfully, but these errors were encountered: