Repository navigation
Alternate constants API 2 - #745
Conversation
|
It looks like some builds errored on Travis - there is one genuine failure which is due to a test using This may happen because constants previously defined in a system are being converted from another system, but in any case, the test should probably use It might be worth fixing that here to force the build to restart anyway (can't restart it manually for some reason). |
|
@iguananaut - this is looking great! Took me a while to understand the code, but I see what it's doing now, and it's very smart! I think it's the right approach, so I'm going to give detailed comments. I've run into a couple of issues while testing:
I fixed this locally by moving the definition of
I'll add a few in-line comments. |
There was a problem hiding this comment.
The docstring above needs to be updated as it refers to the old API.
|
Apart from my comments above, the docs themselves need to be updated. Thanks for all the work you've put into this! The user API ends up being a lot nicer and intuitive :-) |
|
Looks good to me. |
|
I see what's going on with that On the other hand, every attribute lookup is already a dictionary lookup, so adding one more doesn't make much of a difference and is probably a premature optimization sigh |
|
Regarding the |
|
@iguananaut - I don't have a strong opinion, so if you don't think it needs to be exposed to users, then that's fine by me. |
|
Listing the available systems is not a bad idea either. There's still a little incongruity in how this all works though that might be worth revisiting at a later time. If I define In other words, it's possible to have In the future I think we'll want to further rethink the distinction between units and constants given that some units are defined in terms of some constant and vice versa. That's where we've been getting into the most trouble with this. |
|
Ah I see, so cgs wouldn't be listed as a system that can be converted to (in the system framework). Maybe |
|
Well it's designed so that all the systems it can be converted to will show up in the constant's |
|
Out of curiosity I traced the slight difference in that one cosmology test to a slight floating point error that occurred in the old version in converting c from SI to CGS and then to 'km/s' that introduced an error of 0.00000000004. The new value is actually closer to "correct". In any case the test should still use allclose there. |
|
Ah, cool - I am kind of impressed we hadn't run into the floating point test failing before, given the wide range of architectures we've tested in (of course it should be fixed). |
|
I actually had seen that test failing before but kind of ignored it. Since no one else had reported it I assumed it was just something weird about my hand-built SciPy :) |
|
3363515 also fixed the failing cosmology test. |
|
This is looking very nice from an API perspective -- haven't looked at the code yet, but thanks for the work @iguananaut ! |
|
@adrn Great--let me know (and this goes to anyone else) if there are any parts in the code that need to be clarified with further comments. |
There was a problem hiding this comment.
It might be useful to have a comment paragraph just above here that gives a brief overview of how this all works (but just a few sentences).
|
@iguananaut - I'm going to test this again, but it all looks good to me. I just added a comment in a place where mayb 2-3 sentences could be added to give a high-level overview of how the constants code works in general. But that's just for convenience for future developers, and shouldn't hold things back. |
|
@iguananaut - just tested it again, and it's behaving perfectly now! I think this is ready to go. |
|
@iguananaut - if you could include it in this PR, since it's somewhat related, could you fix the mistake in |
|
Granted, addition and subtraction aren't really distinct operators, but okay :) |
|
The latest version seems to have broken something - I can't get the build details to appear on travis, but it fails for me locally - there's a syntax error due to a parenthesis that doesn't seen to be in the right place on line 899 in core.py (I think that was supposed to be for line continuation, but it looks like it won't let you do the parenthesis when you change variable assignments together) |
|
Also odd is that the previous build no longer seems to complete its scipy build in time, even though this worked before. This doesn't change anything about the scipy build, right? |
|
@eteq - the scipy build could happen if the syntax error in |
|
See https://travis-ci.org/eteq/astropy/builds/4715685 for a build that's identical to this branch, but with the syntax error fixed. They all pass! |
There was a problem hiding this comment.
All other changes around here appear to be PEP8, but is this one? It seems to move the assignment inside a block?
There was a problem hiding this comment.
That change was from an earlier commit (f8f3455) which changed the _registry from a list to a dict. It's moved inside the loop because a registry entry is created for each name a unit has. There was something that was made more efficient this way but I can't remember what exactly.
…e, and then uses e.si, e.gauss, e.esu (e.gauss and e.esu in this case are equivalent). e.cgs raises an exception. e still combines with other quantities using its SI value in Coulombs by default, though we could (and perhaps should) modify it to raise an exception by default if a specific system is not used.
…ned, currently just makes that constant an EM constant and requires it to specify a system if it's to be used as a quantity. Not fully working yet though, and the concept is not unique to EM constants either so the concept might be further refined and generalized.
… the astropy.constants module, reworking the imports so that the circularity between astropy.units and astropy.constants is overcome, and reworking the Constant class to have a registry of constants in different systems, among other differences.
…it formatting that was previously used in the docs
…nt defined in that system if it exists, else convert to a Quantity in the appropriate new units.
… avoid a potentially unnecessary slow unit conversion
…nboundLocalError when using e in an expression more than once. Cleaned up no longer relevant comments and docstrings, and updated the documentation.
…t but it's a start) and fixed the mentioned error message for Quantity.__sub__
…ment expressions)
… and the like--if someone really wants those they can either ask for them or derive them themselves--I don't know how commonly these are used?)
|
Had to rebase mostly due to #761. Anyways, going ahead and merging this. |
Alternate constants API 2 Conflicts: astropy/units/format/generic.py
|
Hooray! |
… should be using the value of c (in m/s) as a scaling factor, but its units shouldn't really come into it. This should have been changed to c.value and h.value as part of astropy#745.
Alternate constants API 2 Conflicts: astropy/units/format/generic.py
…sed an UnboundLocalError when using e in an expression more than once. Cleaned up no longer relevant comments and docstrings, and updated the documentation.
…ctor-rebase Alternate constants API 2 Conflicts: astropy/units/format/generic.py
This is the much promised followup to #572. It includes the changes from #572 in some form or another, plus many additional changes as discussed in that issue.
Now it is possible to do
from astropy.constants import <const>. For example:Most constants can now be combined directly with other quantities and values as before:
It can also be converted to other systems of units with different bases:
However, one of the primary motivations of these changes was to be able to deal sensibly with constants that are defined in units that are incompatible with each other between different systems of units--primarily SI and EM extensions to CGS,
e(electron charge) being the primary example. In this case, it is possible to importeas we did withG:And as you can see, the value of e is given in Coulombs (SI). However, this is too ambiguous for general use. What if we try to combine
ewith some other quantity? The result could have completely different physical dimensions depending on which system we're working in. So for constants that are defined with incompatible units depending on the system it's necessary to specify a system in order to combine it with anything:Electromagnetic constants are further a special case, as there are multiple EM extensions to CGS:
Although the original motivation for this was handling EM constants in CGS, this will also pave the way to implementing other incompatible systems of units, such as geometrized units.
Another major change made in this PR is that there is no longer a separate
_constantshidden package. Constants are defined once, in theastropy.constantspackage. This PR shuffles things around in such a way as to carefully avoid any problems with the circular dependency between the constants and units subpackages (though it's a bit fragile, and at a later date we may wish to further revisit this--I think it might make sense to combine units and constants into a single package, while only maintaining the top-levelastropy.unitsandastropy.constantsnamespaces for convenience).