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
Ensure BaseCoordinateFrame
separation methods work with SkyCoord
input
#15659
Conversation
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
except TypeError: | ||
raise TypeError( |
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.
while we're at it maybe we could also chain these exceptions either as
try:
...
except TypeError as exc:
raise TypeError(...) from exc
or
try:
...
except TypeError:
raise TypeError(...) from None
? (I think the second one is more appropriate here)
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 doesn't look like this code path can ever be triggered. Trying to compute the separation of a frame without data raises a ValueError
, not a TypeError
:
>>> from astropy.coordinates import FK5, SkyCoord
>>> SkyCoord(1, 1, unit="deg").separation(FK5())
Traceback (most recent call last):
...
ValueError: Cannot transform a frame with no data
To me it seems that this error message is clear enough, so this try-except
block can be removed.
477d9ec
to
c1b2192
Compare
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.
Really good to simplify this! I have one name suggestion, but also a general question: do we even need these methods at all? If we don't define them, one falls through to the equivalent methods on the frame - what goes wrong if one uses those? It looks like they do the same thing...
I had a brief look at code performance. Setup: Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.15.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from astropy.coordinates import SkyCoord, ICRS
In [2]: from astropy import units as u
In [3]: sc1 = SkyCoord(1 * u.deg, 2 * u.deg, 1 * u.kpc, frame="galactic")
In [4]: sc2 = SkyCoord(2 * u.deg, 3 * u.deg, 4 * u.kpc)
In [5]: icrs = sc2.frame With 6b36e53 (the commit from which this pull request branched): In [6]: %timeit sc1.separation(sc2)
1.05 ms ± 17.4 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
In [7]: %timeit sc1.separation(icrs)
897 µs ± 27.4 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
In [8]: %timeit sc1.separation_3d(sc2)
602 µs ± 10.4 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
In [9]: %timeit sc1.separation_3d(icrs)
463 µs ± 15.9 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each) With c00bca0: In [6]: %timeit sc1.separation(sc2)
718 µs ± 9.77 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
In [7]: %timeit sc1.separation(icrs)
712 µs ± 3.12 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
In [8]: %timeit sc1.separation_3d(sc2)
472 µs ± 6.66 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
In [9]: %timeit sc1.separation_3d(icrs)
470 µs ± 1.59 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each) |
c1b2192
to
fb417d8
Compare
SkyCoord
separation methodsSkyCoord
methods
p.s. Somewhat surprising, the increase in performance. But nice! |
is there something wrong with codecov reports lately or should a test or two be added to cover these (surprisingly) uncovered paths ? |
There was an error 404 in the upload step in the coverage job here but somehow codecov does not fail the job. When you see weird numbers, go into the log and check the upload step. This happens on and off because pull_request does not get the token to guarantee success. If codecov thinks we're spamming them, this happens. Unless coverage check is critical, I usually ignore. Otherwise, maintainer can opt to rerun just that job. The non-zero number on failure is because CircleCI also uploads coverage but only for visualization tests. Hope this clarifies the matter! |
fb417d8
to
6c6a50b
Compare
SkyCoord
methodsSkyCoord
The changes this pull request now makes to |
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.
There's part of me that worries about omitting all the SkyCoord
non-frame attributes, but since that was the case already, clearly it is not something that is obviously a problem (and it is clear that just merging attributes is a problem).
I do think we should have a changelog entry, since really the old behaviour for Baseframe was a bug for SkyCoord
input.
The new test reveals that the `BaseCoordinateFrame` methods `separation()` and `separation_3d()` can produce incorrect results if their argument is a `SkyCoord` instance. The methods do not claim to support `SkyCoord` input, but the current behaviour is nonetheless dangerous and should be considered to be a bug.
The `BaseCoordinateFrame` `separation()` and `separation_3d()` methods can now safely be used with `SkyCoord` instances as inputs.
6c6a50b
to
c00bca0
Compare
Updates to the |
SkyCoord
BaseCoordinateFrame
separation methods work with SkyCoord
inpput
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.
To me, this looks all good and since there is no actual change in SkyCoord.separation*
- from its perspective, this is just a refactoring - it seems fine to me to merge as is, including the backport. But I won't do that yet just in case others disagree; I'm fine with not backporting or splitting as well.
BaseCoordinateFrame
separation methods work with SkyCoord
inpputBaseCoordinateFrame
separation methods work with SkyCoord
input
@pllim, could you advise if the refactoring commit should be included here, in which case it will be backported with the bugfix, or if a separate pull request should be opened for the refactoring? |
We generally do not backport refactoring. But if it is just a few lines and totally backward compatible, maybe ok. tl;dr -- new PR would be best |
c00bca0
to
4f9bab5
Compare
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.
OK, thanks for splitting the bug off! Looks all OK.
Darn, I merged before checking for the backport label. Was there a simple phrase to get the bot to still do the backport for us? |
@meeseeksdev backport to v6.0.x |
…thods work with `SkyCoord` input
I should have remembered to add the backport label myself, but luckily there is a simple phrase the bot responds to. |
…659-on-v6.0.x Backport PR #15659 on branch v6.0.x (Ensure `BaseCoordinateFrame` separation methods work with `SkyCoord` input)
Description
The first commit here adds a test which demonstrates that it is possible to get wrong results from
BaseCoordinateFrame
separation()
andseparation_3d()
methods if the input to those methods is aSkyCoord
instance. These methods do not claim to supportSkyCoord
input, but if they don't work withSkyCoord
they should fail in an obvious manner. The current behavior is therefore a bug and the second commit here fixes it.The third commit removes theSkyCoord
separation()
andseparation_3d()
methods. This can be removed without loss of functionality after fixing the bug because aSkyCoord
instance then exposes the separation methods of its underlying frame.EDIT: there will be a separate pull request for the third commit.