Skip to content

Enable velocities in SkyCoord - #6944

Merged
eteq merged 26 commits into
astropy:masterfrom
eteq:skycoord-velocities
Dec 23, 2017
Merged

eteq merged 26 commits into
astropy:masterfrom
eteq:skycoord-velocities

Conversation

@eteq

@eteq eteq commented Dec 8, 2017

Copy link
Copy Markdown
Member

This PR fixes #6559 and thereby achieves the goal planned from v2.0 of adding the proper motion and radial velocity support in coordinates that was added in v2.0 to work with SkyCoord objects. Basically it all works the way you'd expect following the already established paradigm that the "real work" is done in the frame classes and SkyCoord provides 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 in SkyCoord it'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?

@eteq eteq added this to the v3.0.0 milestone Dec 8, 2017
@astropy-bot

astropy-bot Bot commented Dec 8, 2017 •

Copy link
Copy Markdown

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.

@eteq
eteq force-pushed the skycoord-velocities branch from b3f337f to 67a2812 Compare December 8, 2017 06:45
@eteq
eteq force-pushed the skycoord-velocities branch from 67a2812 to f2d7a2b Compare December 13, 2017 20:09
@eteq
eteq requested review from adrn and mhvk December 15, 2017 23:40
@adrn

adrn commented Dec 19, 2017 •

Copy link
Copy Markdown
Member

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

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-369240089fcb> in <module>()
      3                pm_dec=199*u.mas/u.yr,
      4                differential_cls=coord.SphericalDifferential)
----> 5 coord.SkyCoord(c)

~/projects/astropy/astropy/coordinates/sky_coordinate.py in __init__(self, copy, *args, **kwargs)
    231 
    232         # Finally make the internal coordinate object.
--> 233         self._sky_coord_frame = frame.__class__(copy=copy, **coord_kwargs)
    234 
    235         if not self._sky_coord_frame.has_data:

~/projects/astropy/astropy/coordinates/baseframe.py in __init__(self, copy, representation, differential_cls, *args, **kwargs)
    379                     differential_cls = r.RadialDifferential
    380 
--> 381                 differential_data = differential_cls(copy=copy, **diff_kwargs)
    382 
    383         if len(args) > 0:

TypeError: __init__() missing 1 required positional argument: 'd_lon_coslat'

@adrn

adrn commented Dec 19, 2017

Copy link
Copy Markdown
Member

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'

@eteq

eteq commented Dec 19, 2017

Copy link
Copy Markdown
Member Author

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 mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should mention pm_ra_cosdec, pm_dec, and radial_velocity in the docstring!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Comment thread docs/coordinates/index.rst Outdated
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|

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing "and"

Comment thread docs/coordinates/velocities.rst Outdated
==================================

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: tp -> to

@eteq

eteq commented Dec 20, 2017

Copy link
Copy Markdown
Member Author

OK, the latest commit I think addresses @adrn's failing cases, and fixes up all the existing xfails (except those that are actually expected to fail and are subtle tests of that behavior)... Hopefully travis agrees. Will try to work through @mhvk's other comments tomorrow.

@eteq

eteq commented Dec 21, 2017

Copy link
Copy Markdown
Member Author

The latest batch of comments should address both @mhvk's and @adrn's comments (including fixing up all of the xfails except the one that really is "expected"). Perhaps one of you can approve this if you think it's good now?

@adrn adrn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of fairly minor comments. I don't think I'll have a chance to review again, so I'm approving this now pending those changes. Thanks for the hard work on this @eteq!

Comment thread astropy/coordinates/sky_coordinate.py Outdated
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."

Comment thread astropy/coordinates/sky_coordinate.py Outdated
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: reverse order, so pm_ra_cosdec, pm_dec

Comment thread astropy/coordinates/sky_coordinate.py Outdated
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note for myself that this might lead to an ugly rebase in #6873

Comment thread astropy/coordinates/sky_coordinate.py Outdated
from .funcs import get_constellation

return get_constellation(self, short_name, constellation_list)
# drop the velocities - not needed for a pure-position application

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Comment thread astropy/coordinates/sky_coordinate.py Outdated
"new frame='{1}'. Instead transform the coordinate."
.format(coord_frame_cls.__name__, frame_cls.__name__))

frameclskwargs = {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so you know, this prompts an April 1 idea...

Comment thread astropy/coordinates/sky_coordinate.py Outdated
del frame_attr_names[nameidx]
del repr_attr_classes[nameidx]

if coords.data.differentials:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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']

@eteq eteq Dec 23, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a little more careful, but don't know if it really matters:
orig_vel.get_name().startswith('unitspherical')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread astropy/coordinates/sky_coordinate.py Outdated
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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here about explicitly checking for 's'

sc.separation(sc2)


def test_accessors(sc, scmany):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does sc.velocity work? Now that #6869 is merged, I think we want that to work on skycoord as well.

@eteq eteq Dec 23, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adrn

adrn commented Dec 22, 2017

Copy link
Copy Markdown
Member

Oh, and one other thing I would like to see is the same set of tests run on the frame classes with velocities as SkyCoord's with velocities. That is, I'd like to combine tests in test_frames_with_velocity.py and test_sky_coord_velocities.py. I'm ok with this PR going in without this happening, but make it a stretch goal. If you don't do it in this PR, could you make an issue?

@eteq

eteq commented Dec 23, 2017

Copy link
Copy Markdown
Member Author

@adrn approved this one and tests have all passed... so merging! Hopefully this won't disrupt too many of the PRs from @adrn that are almost ready.

@eteq
eteq merged commit 1c4c585 into astropy:master Dec 23, 2017
@eteq
eteq deleted the skycoord-velocities branch December 23, 2017 03:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SkyCoord should support velocities

3 participants