-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Unit/conversions as properties #837
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
Conversation
astropy/units/quantity.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor grammer: I think this is "A list of equivalence pairs allow conversions between." Or maybe a more just "A list of unit equivalencies. See :ref:unit_equivalencies for details."
|
Once this is merged, we should create an issue to adapt |
|
This is some evil genius stuff right here. I'll have to mull over it a bit more but this looks pretty much like what I had in mind. |
|
Given this, maybe it's time to reopen the discussion about |
|
By the way, should we mention this PR on the mailing list, given that it is quite an important addition? |
docs/whatsnew/0.3.rst
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: "no" should be "now".
|
I'm out sick, so just popping in, but my two bits: yes, let's point this out on mailing list and I think this does make angle as subclass of quantity a lot more appealing. |
|
Shall I continue to refactor |
|
@mdboom - I think separate pull requests might be best. I will test this one out now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth mentioning that the downside of this is that it only works for single units, i.e. if you have a velocity, you can't convert to e.g. km/s with this interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, not out-of-the-box, but you can do:
>>> u.def_unit('kms', u.km / u.s, register=True)
Unit("kms")
>>> x = 1.0 * (u.mile / u.hr)
>>> x.kms
0.00044704
In other words, it works with any unit in the registry, whether built-in or not.
|
I've tested this out, and it works nicely! For beginners, it might not be obvious why My second concern is that tab-complete for quantities is now cluttered for things like distances. In the spirit of my above comment, I think that this way of concerting should be only regarded as a convenience, and I don't think all the units should appear when one tab-completes, otherwise things like What does everyone else think? |
|
I'm not sure how to deal with the fact that the autocompletion is much more cluttered now. On the one hand, at least there is some autocompletion, unlike the lack of autocompletion one gets with |
|
What's the consensus here? I think I would summarize as: this is handy, and will make other parts of the code simpler (once |
|
@mdboom - could we not overload |
|
@astrofrog -- that's certainly a possibility. Another is to put all this in a subobject, e.g.: (Of course, we can't use |
|
I like how the subobject approach works for tab completion purposes. My only concern is that it might make Either that or using a prefix (without the subobject) might also help make the I think Another option might be |
|
@eteq: I like the |
|
That's a good point. It's also easier, so perhaps we should start there, and if anyone is feeling ambitions they can try the subobject approach? |
|
With |
|
FWIW I read through this discussion and spent a little time thinking about the options here. In the end I have a slight preference for the |
|
I agree with what's been suggested, e.g. the |
|
@mhvk raises a good point about q.value_in(units) - in fact, it's IMHO a better solution than q.value_in. because the latter only works for a very small subset of quantity types, whereas the first (for a couple of extra characters) is available for all unit types. With the latter, you'd have to tell users that to convert quantities like distances, they should use So However, we are missing the opportunity to simply define a shorthand convenience of the form Because the last one is a convenience, it would only work for certain kinds of quantities, but that's ok, because it's just a convenience. |
|
@astrofrog: I see your point about To step back a bit -- the initial motivation for this PR for me was to bring some of the features of the specialized quantity classes into the generic So the I'm not crazy about the idea about adding (virtual) members that don't show up with tab completion. Most intro Python tutorials with IPython extoll the virtues of using completion (or even just Is there a way we could instead limit the number of metric (SI) prefixes we include? Some of them are in common usage and are really handy, but others are just there for pedantic completeness. If we stored with the units some sort of metadata about which prefixes are truly handy, we could reduce the number of members dramatically. If we did that, I'd say we make |
|
@mdboom - I'd be happy with keeping tab-completion if we can reduce the number of units proposed. So then you are suggesting we implement: ? I'd be fine with that. |
|
Yeah, the question just is how to determine which units prefixes show up in a way that isn't annoying and adding a lot of metadata wrangling for us. One could probably go a long way with just including every third one: |
|
Ok -- I've whittled this down so it only will use full names, e.g. kilometer rather than km for this purpose. I think this whittles it down considerably, and also makes the non-unit names stand out a little better. Let me know what you think. |
|
There's a single failure in travis w/ scipy that says Based on the diff, I think you just want Also, @mdboom - can you clarify what behavior I should be expecting in this version? If I do |
|
@eteq: That's weird. That Travis failure is referencing code on master that doesn't (yet) exist on this branch. Is it possible Travis is testing the wrong thing? The "magic" members are added to the Quantity class, not the Unit class, so, no, |
|
I see what was going on. This is one of those rare times where the code needed to be updated to work with master but there wasn't actually a merge conflict. I've rebased on master, and we should get the tests passing again. |
|
I've had that happen to me before too. I'm pretty sure Travis tries to merge your PR with upstream/master when doing the tests, which is sensible. |
|
Travis failure appears to be network related (false positive). |
|
@mdboom - Duh... I don't know why I thought this was supposed to work on units. Anyway, it now looks just as I thought. As far as you're concerned this is good, right? Looks all set to me. I re-started the Travis job that was a problem, so hopefully it will pass once Travis works out their current problem (http://status.travis-ci.com/incidents/h2mtcw1llvnc). On a slightly related note: are |
|
Yeah, I think it's good to go. It can still create fairly large namespaces on the objects in some cases, but about 1/3 of what it was originally. 😁 |
|
oops, look like something recently merged conflicts, @mdboom ... needs a rebase (or you can merge it manually) |
Quantity that allow the user to easily convert to other units. This involved add a more complex UnitRegistry class, that also keeps a dictionary of all registered units sorted by their physical type. From this new registry, a `compose` can also limit the set of units that it needs to search through for a match, making it faster, so this PR has the added bonus of being a performance improvement.
Based an idea by @adrn in #794, this adds "automatic" members to
Quantity that allow the user to easily convert to other units.
This involved adding a more complex UnitRegistry class, that also keeps
a dictionary of all registered units sorted by their physical
type.
From this new registry, a
composecan also limit the set of unitsthat it needs to search through for a match, making it faster, so
this PR has the added bonus of being a performance improvement.