-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Can't instantiate Quantity with logarithmic units. #5851
Comments
@weaverba137 - before anything else, very happy to hear the logarithmic units actually get used! The problem you encounter is actually not as much about logarithmic units not being subclasses of regular ones, but more directly about logarithmic and normal units having different rules about how they combine (e.g., normal units can be multiplied and divided together, while logarithmic ones have to be added or subtracted, so the two cannot share methods for operations). The same holds for quantities, which is why one cannot construct a Now, the regular way to construct a logarithmic quantity would be to either use the multiplication as you did in your first example, or, if you want to construct it directly, use the appropriate class, i.e., do
Is there a reason this does not work for you? Note that, in principle, it is possible to make the initialization with
I've played with the idea of implementing this more generally (i.e., one would have |
All right, I understand that, so can you point me to the documentation on this? I've looked through the Quantity class API, and can't find anywhere it says that q = Quantity(value, unit=u.Unit('dex(Angstrom)') is forbidden. Naively, one would assume that would just do the right thing. In fact that is exactly what the intermediate code that triggered this is doing. |
At present, the documentation indeed does little more than point you to what would be the right way to do it, both in the getting started section and in the actual section of logarithmic units. It does indeed say little about mixing; I obviously didn't think about that when I wrote the documentation, perhaps reflecting that, generally, documentation written by developers is often not quite what someone new to the project would like to see. Of course, this also means I'm not the right person to try to change it -- PRs most welcome! (Especially if you can find a way to phrase this not just as a "warning, don't do this", which is pretty useless for documentation.) p.s. Just in case you weren't aware, we do support for serializing to/from yaml (#5486) |
@mhvk - I think I agree with the fundamental point @weaverba137 is making here. That is to say, most users I know of (myself included) have in their head that:
I think it's counter-intuitive to violate that in some cases and not others. I didn't quite understand what you were saying is confusing about that in #5851 (comment) ? (Or am I mis-understanding and you're not saying that?)
100% agree there! |
My worry is simply about breaking the expectation (i.e., another thing that users likely have in their head) that when one uses a class to initialize an instance, the result is an instance of that class, i.e., Now it is true that in our case one would still have that Given this, if we do want a standard way to initialize, my tendency would be to make a new factory function and update our documentation to mostly use |
Thinking a bit further: Right now, Just to see what works now:
Here, the |
I feel like this is the same issue I brought up some time ago, which incidentally interrupted Marten's vacation... #5178 (comment) 😅 |
@weaverba137, what do you think about this? (Asking persuent to @mhvk's point that the non-expert on |
Well, to be honest, I think that is "Python developer"-oriented thinking, rather than "End user"-oriented thinking. I am both, and I can see the viewpoint of both, but I think that the majority of users would tend to want If you want class/type purity and want to just do the right thing, you could replace |
I don't think "the right thing" is all that obvious, but it seems we're converging on that it would be nice to have a factory function that, e.g., avoids the copy that one gets by just multiplying a value with a unit. I note that, including changes to the documentation, this is not that minor an endeavour. It may be best to raise a separate issue for it where we can collect a list of things this function should do and what the ramiifcations are for it, so that someone who is interested could tackle it. |
Another thought: in context of the zen of python -- There should be one-- and preferably only one --obvious way to do it -- yet another suggestion is simply to state that multiplication with a unit is the right way to instantiate a |
@mhvk - hmm, I see the argument there... But that doesn't work for the use case of "I have a thing that I want to convert into a Quantity but I'm not sure what it is" use case. But maybe that already is a "developer" case? So along those lines... @weaverba137, what was your need of the |
Good question. I first encountered this when trying to plot SDSS data in specviz. SDSS almost always stores wavelength values in |
Ah, ok, and I'm pretty sure the specviz use case is indeed the "I have a thing that I want to convert into a Quantity but I'm not sure what it is" use case... so that's a pretty legit concern then. So it might be that the factory function should be something explicitly tailored to that sort of as a developer tool - e.g. |
@eteq , it wouldn't be so hard for SpecViz to refactor a little to just use the |
@pllim - I think it's a subtly different use case, because it's a case of "if it's already a quantity in that unit, it's OK as-is, but if it's an array, assign it the units of But maybe @nmearl can comment more on that use case? |
@weaverba137 - somehow I only remembered now that |
This seems reasonable. I'll take a look at the PR. |
a solution was merged with #5928 |
I'm trying to create a Quantity that represents units of wavelength in
log_10(Angstrom)
, so I figureddex(Angstrom)
would work fine. It only sort of does.So why can't
DexUnit
instances inherit fromUnitBase
? This is a pretty serious problem for me because I usedex(Angstrom)
units all the time. For example, basically every SDSS spectrum file uses these units.The text was updated successfully, but these errors were encountered: