Repository navigation
Enable velocities in SkyCoord - #6944
Conversation
|
Hi there @eteq 👋 - thanks for the pull request! I'm just a friendly 🤖 that checks for issues related to the changelog and making sure that this pull request is milestoned and labeled correctly. This is mainly intended for the maintainers, so if you are not a maintainer you can ignore this, and a maintainer will let you know if any action is required on your part 😃. Everything looks good from my point of view! 👍 If there are any issues with this message, please report them here. |
b3f337f to
67a2812
Compare
…tion matching their default
…rimental warning)
67a2812 to
f2d7a2b
Compare
|
Ran out of time to do a thorough review tonight (will do tomorrow), but in cleaning up my PR that is based off of this branch, I stumbled on a failure that should work: c = coord.ICRS(ra=150*u.deg, dec=-11*u.deg,
pm_ra=100*u.mas/u.yr,
pm_dec=199*u.mas/u.yr,
differential_cls=coord.SphericalDifferential)
coord.SkyCoord(c)currently fails on |
|
Some more failures I've stumbled on: (1) >>> rep = coord.CartesianRepresentation([10, 0., 0.]*u.pc)
>>> dif = coord.CartesianDifferential([0, 100, 0.]*u.pc/u.Myr)
>>> rep = rep.with_differentials(dif)
>>> c = coord.SkyCoord(rep)
>>> c.pm_ra_cosdec
TypeError: Frame data has no associated differentials (i.e. the frame has no velocity data) - represent_as() only accepts a new representation.(2) >>> c = coord.SkyCoord(ra=11.23*u.degree, dec=58.13*u.degree,
... distance=213.4*u.pc,
... pm_ra_cosdec=9*u.mas/u.yr, pm_dec=3*u.mas/u.yr,
... radial_velocity=-61*u.km/u.s)
>>> c.galactic
AttributeError: 'CartesianDifferential' object has no attribute 'd_lon_coslat' |
|
Note (mostly to self) re (2) in #6944 (comment) : apparently that failure is only when all 6 dimensions are provided. if you drop distance, RV, or both, all is fine. |
mhvk
left a comment
There was a problem hiding this comment.
Bit of a rough review, but it looks pretty good! Some small comments, including some about the tests. Do need to fix the failing ones!
| setattr(self, attr, kwargs[attr]) | ||
|
|
||
| coord_kwargs = {} | ||
| component_names = frame.representation_component_names |
There was a problem hiding this comment.
We should mention pm_ra_cosdec, pm_dec, and radial_velocity in the docstring!
| assert_quantity_allclose(sc3.dec, 4*u.deg + 5*u.arcmin + 6*u.arcsec) | ||
| # might as well check with sillier units? | ||
| assert_quantity_allclose(sc3.pm_ra_cosdec, 1.2776637006616473e-07 * u.arcmin / u.fortnight) | ||
| assert_quantity_allclose(sc3.pm_dec, 6.388318503308237e-08 * u.arcmin / u.fortnight) |
There was a problem hiding this comment.
Should have a test with radial_velocity as well, both on its own and together with proper motion, and both with and without a distance.
There was a problem hiding this comment.
With the lest set of commits this is now implicitly tested via anything using the sc fixture.
|
|
||
| @pytest.mark.xfail #TODO: FIX | ||
| def test_creation_copy(): | ||
| i = ICRS(1*u.deg, 2*u.deg, pm_ra_cosdec=.2*u.mas/u.yr, pm_dec=.1*u.mas/u.yr) |
There was a problem hiding this comment.
Since you're using the same inputs, I would suggest making a test class, in which these are set up in setup.
| def test_accessors(sc, scmany): | ||
| sc.data.differentials['s'] | ||
| sc.spherical | ||
| sc.galactic |
There was a problem hiding this comment.
Are these useful to check without checking that the result makes sense? In particular, for spherical and cartesian, it would be good to check that the differential does the right thing.
|
|
||
| @pytest.mark.xfail #TODO: FIX | ||
| def test_skyoffset_frame(sc): | ||
| sc.skyoffset_frame() |
There was a problem hiding this comment.
That's an interesting one; the sky offset frame now would logically be a moving one! I think this should not be allowed to work if a proper motion is present...
| experimental, but is accessible for more information see the :doc:`velocities` page. | ||
| In addition to positional coordinates, `~astropy.coordinates` supports storing | ||
| and transforming velocities. These are available both via the lower-level | ||
| :doc:`coordinate frame classes <frames>`, (new in v3.0) via |skycoord| |
| ================================== | ||
|
|
||
| The easiest way of getting a coordinate object with velocities is to use the | ||
| |skycoord| interface. For example, a |skycoord| tp represent a star with a |
| Cartesian coordinates values for the Galactic frame. | ||
| radial_velocity : `~astropy.units.Quantity`, optional | ||
| The component of the velocity along the line-of-sight (i.e., the | ||
| radial direction). Should have velocity units. |
There was a problem hiding this comment.
Nitpick: to be more similar to next line, "The component of the velocity along the line-of-sight (i.e., the radial direction), in velocity units."
| radial_velocity : `~astropy.units.Quantity`, optional | ||
| The component of the velocity along the line-of-sight (i.e., the | ||
| radial direction). Should have velocity units. | ||
| pm_dec, pm_ra_cosdec : `~astropy.units.Quantity`, optional |
There was a problem hiding this comment.
Nitpick: reverse order, so pm_ra_cosdec, pm_dec
| The component of the velocity along the line-of-sight (i.e., the | ||
| radial direction). Should have velocity units. | ||
| pm_dec, pm_ra_cosdec : `~astropy.units.Quantity`, optional | ||
| Proper motion components, in angle per time units. |
There was a problem hiding this comment.
Actually I might reorganize this whole section to be something like:
ra, dec : valid `~astropy.coordinates.Angle` initializer, optional
RA and Dec for frames where ``ra`` and ``dec`` are keys in the
frame's ``representation_component_names``, including `ICRS`,
`FK5`, `FK4`, and `FK4NoETerms`.
pm_ra_cosdec, pm_dec : `~astropy.units.Quantity`, optional
Proper motion components in ICRS (or other equatorial frames), in angle per time units.
l, b : valid `~astropy.coordinates.Angle` initializer, optional
Galactic ``l`` and ``b`` for for frames where ``l`` and ``b`` are
keys in the frame's ``representation_component_names``, including
the `Galactic` frame.
pm_l_cosb, pm_b : `~astropy.units.Quantity`, optional
Proper motion components in the `Galactic` frame, in angle per time units.
x, y, z : float or `~astropy.units.Quantity`, optional
Cartesian coordinates values
u, v, w : float or `~astropy.units.Quantity`, optional
Cartesian coordinates values for the Galactic frame.
radial_velocity : `~astropy.units.Quantity`, optional
The component of the velocity along the line-of-sight (i.e., the
radial direction). Should have velocity units.
| coord_kwargs = {} | ||
| component_names = frame.representation_component_names | ||
| component_names.update(frame.get_representation_component_names('s')) | ||
| if 'representation' in kwargs: |
There was a problem hiding this comment.
Just a note for myself that this might lead to an ugly rebase in #6873
| from .funcs import get_constellation | ||
|
|
||
| return get_constellation(self, short_name, constellation_list) | ||
| # drop the velocities - not needed for a pure-position application |
There was a problem hiding this comment.
Is there a reason to do this? Does get_constellation do an operation on the representation, and thereby throw an error with a velocity? If so, can you add a short description "why" under "drop the velocities"?
There was a problem hiding this comment.
It's actually revealing a bug hidden here: the issue is that get_constellation coverts to PrecessedGeocentric and the velocity machinery has some corner cases there where the velocities muck up the transform due to some unit-related issues. I think this goes back to the frames themselves, though, so didn't want it to hold up this PR, but wanted to leave the test in since that's what revealed the bug in the first place.
At some point hopefully it can be fixed (I think it's a bug and not a subtle thing that's failing correctly), and then this could remove though. Perhaps I should add a TODO on this (mapping to an issue to track for the future)?
There was a problem hiding this comment.
Ah, hmmm. Ok, a TODO and/or an issue would be good. If you have a code snippet showing the error, can you include in the issue? I'd like to take a look at that...
| "new frame='{1}'. Instead transform the coordinate." | ||
| .format(coord_frame_cls.__name__, frame_cls.__name__)) | ||
|
|
||
| frameclskwargs = {} |
There was a problem hiding this comment.
King of all nitpicks: I would use underscores for variable names like this frame_cls_kwargs. But "Framecl Skwargs" would be a good fantasy name...
There was a problem hiding this comment.
Just so you know, this prompts an April 1 idea...
| del frame_attr_names[nameidx] | ||
| del repr_attr_classes[nameidx] | ||
|
|
||
| if coords.data.differentials: |
There was a problem hiding this comment.
I think you want if 's' in coord.data.differentials: - if the data has a differential with different units, I think it will fail on orig_vel = coords.data.differentials['s']
There was a problem hiding this comment.
Your suggestion doesn't work because it fails for things without differentials. But I think coords.data.differentials and 's' in coord.data.differentials is the way to go now that you mention this.
| vel = coords.data.represent_as(frame.representation, frame.get_representation_cls('s')).differentials['s'] | ||
| for frname, reprname in frame.get_representation_component_names('s').items(): | ||
| if (reprname == 'd_distance' and not hasattr(orig_vel, reprname) and | ||
| 'unit' in orig_vel.get_name()): |
There was a problem hiding this comment.
Could be a little more careful, but don't know if it really matters:
orig_vel.get_name().startswith('unitspherical')
There was a problem hiding this comment.
my thinking here is that there could be other unit-something-spherical-like representations in user code, and this formulation seems safer. E.g. unitphysicistspherical or something like that.
| elif isinstance(coords, BaseRepresentation): | ||
| data = coords.represent_as(frame.representation) | ||
| values = [getattr(data, repr_attr_name) for repr_attr_name in repr_attr_names] | ||
| if coords.differentials: |
There was a problem hiding this comment.
Same comment here about explicitly checking for 's'
| sc.separation(sc2) | ||
|
|
||
|
|
||
| def test_accessors(sc, scmany): |
There was a problem hiding this comment.
Does sc.velocity work? Now that #6869 is merged, I think we want that to work on skycoord as well.
There was a problem hiding this comment.
Turns out this reveals a subtle (but probably not that important) bug in the frames (which is related to the one I referenced above). I'm just xfailing the case for now.
|
Oh, and one other thing I would like to see is the same set of tests run on the frame classes with velocities as |
This PR fixes #6559 and thereby achieves the goal planned from v2.0 of adding the proper motion and radial velocity support in
coordinatesthat was added in v2.0 to work withSkyCoordobjects. Basically it all works the way you'd expect following the already established paradigm that the "real work" is done in the frame classes andSkyCoordprovides a veneer to make it easier to create these objects. So with that in mind I've also removed the warning at the top of the velocities docs that say "this is experimental". I think that's fair because once it's inSkyCoordit's basically "ready for prime time".For now (and arguably for ever), there's no clever parsing done on the velocity components. Unlike the spatial components (e.g. ra/dec), there are no clear conventions for how to represent these. So keywords are required. That simplified the documentation as it's basically all just referencing the frame class machinery for creation/etc syntax.
One tricky bit that was required to get this working: in v2.0 doing
SomeFrame(representation=SphericalRepresentation,...)would make it so that the default differential was ignored, leading to some confusing behavior (e.g. you couldn't create frame objects using the default differential component names even if the representation matched the default representation). The fix is straightforward (5cb4156), but @adrn may want to look at that to make sure that it doesn't violate the idea of that frame creation pathway as originally intended.Thanks to @adrn and @mhvk for help and brainstorming on making this work. One question for both of you: are there any important test cases you think I might be missing based on our discussion of all the possible creation scenarios?