Skip to content

Commit

Permalink
Merge pull request astropy#16215 from eerovaher/coord-separation-tests
Browse files Browse the repository at this point in the history
Reorganize coordinate separation method tests
  • Loading branch information
mhvk authored Mar 19, 2024
2 parents 476358b + 15154d4 commit 96cc7fb
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 245 deletions.
48 changes: 2 additions & 46 deletions astropy/coordinates/tests/test_angular_separation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import pytest

from astropy import units as u
from astropy.coordinates import Angle, Distance
from astropy.coordinates.builtin_frames import FK5, ICRS, Galactic
from astropy.tests.helper import assert_quantity_allclose as assert_allclose
from astropy.coordinates import Angle
from astropy.coordinates.builtin_frames import ICRS

# lon1, lat1, lon2, lat2 in degrees
coords = [
Expand Down Expand Up @@ -46,58 +45,15 @@ def test_angsep():
assert np.fabs(angsep - conv(corrsep)) < conv(correctness_margin)


def test_fk5_seps():
"""
This tests if `separation` works for FK5 objects.
This is a regression test for github issue #891
"""
a = FK5(1.0 * u.deg, 1.0 * u.deg)
b = FK5(2.0 * u.deg, 2.0 * u.deg)
a.separation(b)


def test_proj_separations():
"""
Test angular separation functionality
"""
c1 = ICRS(ra=0 * u.deg, dec=0 * u.deg)
c2 = ICRS(ra=0 * u.deg, dec=1 * u.deg)

sep = c2.separation(c1)
# returns an Angle object
assert isinstance(sep, Angle)

assert_allclose(sep.degree, 1.0)
assert_allclose(sep.arcminute, 60.0)

# these operations have ambiguous interpretations for points on a sphere
with pytest.raises(TypeError):
c1 + c2
with pytest.raises(TypeError):
c1 - c2

ngp = Galactic(l=0 * u.degree, b=90 * u.degree)
ncp = ICRS(ra=0 * u.degree, dec=90 * u.degree)

# if there is a defined conversion between the relevant coordinate systems,
# it will be automatically performed to get the right angular separation
assert_allclose(
ncp.separation(ngp.transform_to(ICRS())).degree, ncp.separation(ngp).degree
)

# distance from the north galactic pole to celestial pole
assert_allclose(ncp.separation(ngp.transform_to(ICRS())).degree, 62.87174758503201)


def test_3d_separations():
"""
Test 3D separation functionality
"""
c1 = ICRS(ra=1 * u.deg, dec=1 * u.deg, distance=9 * u.kpc)
c2 = ICRS(ra=1 * u.deg, dec=1 * u.deg, distance=10 * u.kpc)

sep3d = c2.separation_3d(c1)

assert isinstance(sep3d, Distance)
assert_allclose(sep3d - 1 * u.kpc, 0 * u.kpc, atol=1e-12 * u.kpc)
17 changes: 0 additions & 17 deletions astropy/coordinates/tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,6 @@ def test_array_precession():
npt.assert_array_less(0.05, np.abs(fk5.dec.degree - fk5_2.dec.degree))


def test_array_separation():
c1 = ICRS([0, 0] * u.deg, [0, 0] * u.deg)
c2 = ICRS([1, 2] * u.deg, [0, 0] * u.deg)

npt.assert_array_almost_equal(c1.separation(c2).degree, [1, 2])

c3 = ICRS([0, 3.0] * u.deg, [0.0, 0] * u.deg, distance=[1, 1.0] * u.kpc)
c4 = ICRS([1, 1.0] * u.deg, [0.0, 0] * u.deg, distance=[1, 1.0] * u.kpc)

# the 3-1 separation should be twice the 0-1 separation, but not *exactly* the same
sep = c3.separation_3d(c4)
sepdiff = sep[1] - (2 * sep[0])

assert abs(sepdiff.value) < 1e-5
assert sepdiff != 0


def test_array_indexing():
ra = np.linspace(0, 360, 10)
dec = np.linspace(-90, 90, 10)
Expand Down
39 changes: 0 additions & 39 deletions astropy/coordinates/tests/test_frame_skycoord_common_methods.py

This file was deleted.

60 changes: 0 additions & 60 deletions astropy/coordinates/tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,66 +691,6 @@ def test_setitem_exceptions():
sc1[0] = sc2[0]


def test_sep():
i1 = ICRS(ra=0 * u.deg, dec=1 * u.deg)
i2 = ICRS(ra=0 * u.deg, dec=2 * u.deg)

sep = i1.separation(i2)
assert_allclose(sep.deg, 1.0)

i3 = ICRS(ra=[1, 2] * u.deg, dec=[3, 4] * u.deg, distance=[5, 6] * u.kpc)
i4 = ICRS(ra=[1, 2] * u.deg, dec=[3, 4] * u.deg, distance=[4, 5] * u.kpc)

sep3d = i3.separation_3d(i4)
assert_allclose(sep3d.to(u.kpc), np.array([1, 1]) * u.kpc)

# check that it works even with velocities
i5 = ICRS(
ra=[1, 2] * u.deg,
dec=[3, 4] * u.deg,
distance=[5, 6] * u.kpc,
pm_ra_cosdec=[1, 2] * u.mas / u.yr,
pm_dec=[3, 4] * u.mas / u.yr,
radial_velocity=[5, 6] * u.km / u.s,
)
i6 = ICRS(
ra=[1, 2] * u.deg,
dec=[3, 4] * u.deg,
distance=[7, 8] * u.kpc,
pm_ra_cosdec=[1, 2] * u.mas / u.yr,
pm_dec=[3, 4] * u.mas / u.yr,
radial_velocity=[5, 6] * u.km / u.s,
)

sep3d = i5.separation_3d(i6)
assert_allclose(sep3d.to(u.kpc), np.array([2, 2]) * u.kpc)

# 3d separations of dimensionless distances should still work
i7 = ICRS(ra=1 * u.deg, dec=2 * u.deg, distance=3 * u.one)
i8 = ICRS(ra=1 * u.deg, dec=2 * u.deg, distance=4 * u.one)
sep3d = i7.separation_3d(i8)
assert_allclose(sep3d, 1 * u.one)

# but should fail with non-dimensionless
with pytest.raises(ValueError):
i7.separation_3d(i3)


@pytest.mark.parametrize(
"method,expectation",
[
pytest.param("separation", 0.69815121 * u.deg, id="separation"),
pytest.param("separation_3d", 0.12184962 * u.pc, id="separation_3d"),
],
)
def test_seps_with_skycoord(method, expectation):
coords = (1 * u.deg, 2 * u.deg, 10 * u.pc)
assert_allclose(
getattr(FK5(*coords), method)(SkyCoord(*coords, frame=FK5, equinox="B1950")),
expectation,
)


def test_time_inputs():
"""
Test validation and conversion of inputs for equinox and obstime attributes.
Expand Down
Loading

0 comments on commit 96cc7fb

Please sign in to comment.