Debian Codegraph @

astropy_healpix/tests/test_high_level.py

raw
1 # Licensed under a 3-clause BSD style license - see LICENSE.rst
2 import pytest
3 import numpy as np
4 from numpy.testing import assert_allclose, assert_equal
5
6 from astropy import units as u
7 from astropy.coordinates import Longitude, Latitude, Galactic, SkyCoord
8
9 from ..high_level import HEALPix
10
11
12 class TestHEALPix:
13
14 def setup_class(self):
15 self.pix = HEALPix(nside=256, order='nested')
16
17 def test_pixel_area(self):
18 pixel_area = self.pix.pixel_area
19 assert_allclose(pixel_area.value, 1.5978966540475428e-05)
20 assert pixel_area.unit == u.sr
21
22 def test_pixel_resolution(self):
23 pixel_resolution = self.pix.pixel_resolution
24 assert_allclose(pixel_resolution.value, 13.741945647269624)
25 assert pixel_resolution.unit == u.arcmin
26
27 def test_level(self):
28 assert self.pix.level == 8
29
30 def test_npix(self):
31 assert self.pix.npix == 12 * 256 ** 2
32
33 # For the following tests, the numerical accuracy of this function is
34 # already tested in test_cython_api.py, so we focus here on functionality
35 # specific to the high-level functions.
36
37 def test_healpix_to_lonlat(self):
38 lon, lat = self.pix.healpix_to_lonlat([1, 2, 3])
39
40 assert isinstance(lon, Longitude)
41 assert isinstance(lat, Latitude)
42
43 index = self.pix.lonlat_to_healpix(lon, lat)
44
45 assert_equal(index, [1, 2, 3])
46
47 lon, lat = self.pix.healpix_to_lonlat([1, 2, 3],
48 dx=[0.1, 0.2, 0.3],
49 dy=[0.5, 0.4, 0.7])
50
51 assert isinstance(lon, Longitude)
52 assert isinstance(lat, Latitude)
53
54 index, dx, dy = self.pix.lonlat_to_healpix(lon, lat, return_offsets=True)
55
56 assert_equal(index, [1, 2, 3])
57 assert_allclose(dx, [0.1, 0.2, 0.3])
58 assert_allclose(dy, [0.5, 0.4, 0.7])
59
60 def test_healpix_to_xyz(self):
61 x, y, z = self.pix.healpix_to_xyz([1, 2, 3])
62
63 assert isinstance(x, np.ndarray)
64 assert isinstance(y, np.ndarray)
65 assert isinstance(z, np.ndarray)
66
67 index = self.pix.xyz_to_healpix(x, y, z)
68
69 assert_equal(index, [1, 2, 3])
70
71 x, y, z = self.pix.healpix_to_xyz([1, 2, 3],
72 dx=[0.1, 0.2, 0.3],
73 dy=[0.5, 0.4, 0.7])
74
75 assert isinstance(x, np.ndarray)
76 assert isinstance(y, np.ndarray)
77 assert isinstance(z, np.ndarray)
78
79 index, dx, dy = self.pix.xyz_to_healpix(x, y, z, return_offsets=True)
80
81 assert_equal(index, [1, 2, 3])
82 assert_allclose(dx, [0.1, 0.2, 0.3])
83 assert_allclose(dy, [0.5, 0.4, 0.7])
84
85 def test_nested_to_ring(self):
86 nested_index_1 = [1, 3, 22]
87 ring_index = self.pix.nested_to_ring(nested_index_1)
88 nested_index_2 = self.pix.ring_to_nested(ring_index)
89 assert_equal(nested_index_1, nested_index_2)
90
91 def test_bilinear_interpolation_weights(self):
92 indices, weights = self.pix.bilinear_interpolation_weights([1, 3, 4] * u.deg,
93 [3, 2, 6] * u.deg)
94 assert indices.shape == (4, 3)
95 assert weights.shape == (4, 3)
96
97 def test_interpolate_bilinear_lonlat(self):
98 values = np.ones(12 * 256 ** 2) * 3
99 result = self.pix.interpolate_bilinear_lonlat([1, 3, 4] * u.deg,
100 [3, 2, 6] * u.deg, values)
101 assert_allclose(result, [3, 3, 3])
102
103 def test_interpolate_bilinear_lonlat_invalid(self):
104 values = np.ones(222) * 3
105 with pytest.raises(ValueError) as exc:
106 self.pix.interpolate_bilinear_lonlat([1, 3, 4] * u.deg,
107 [3, 2, 6] * u.deg, values)
108 assert exc.value.args[0] == 'values must be an array of length 786432 (got 222)'
109
110 def test_cone_search_lonlat(self):
111 lon, lat = 1 * u.deg, 4 * u.deg
112 result = self.pix.cone_search_lonlat(lon, lat, 1 * u.deg)
113 assert len(result) == 77
114
115 def test_cone_search_lonlat_invalid(self):
116 lon, lat = [1, 2] * u.deg, [3, 4] * u.deg
117 with pytest.raises(ValueError) as exc:
118 self.pix.cone_search_lonlat(lon, lat, 1 * u.deg)
119 assert exc.value.args[0] == ('The longitude, latitude and radius must '
120 'be scalar Quantity objects')
121
122 def test_boundaries_lonlat(self):
123 lon, lat = self.pix.boundaries_lonlat([10, 20, 30], 4)
124 assert lon.shape == (3, 16)
125 assert lat.shape == (3, 16)
126
127 def test_neighbours(self):
128 neigh = self.pix.neighbours([10, 20, 30])
129 assert neigh.shape == (8, 3)
130
131
132 class TestCelestialHEALPix:
133
134 def setup_class(self):
135 self.pix = HEALPix(nside=256, order='nested', frame=Galactic())
136
137 def test_healpix_from_header(self):
138 """Test instantiation from a FITS header.
139
140 Notes
141 -----
142 We don't need to test all possible options, because
143 :meth:`~astropy_healpix.HEALPix.from_header` is just a wrapper around
144 :meth:`~astropy_healpix.utils.parse_input_healpix_data`, which is
145 tested exhaustively in :mod:`~astropy_healpix.tests.test_utils`.
146 """
147
148 pix = HEALPix.from_header(
149 (np.empty(self.pix.npix), 'G'),
150 nested=self.pix.order == 'nested')
151
152 assert pix.nside == self.pix.nside
153 assert type(pix.frame) == type(self.pix.frame) # noqa
154 assert pix.order == self.pix.order
155
156 def test_healpix_to_skycoord(self):
157 coord = self.pix.healpix_to_skycoord([1, 2, 3])
158
159 assert isinstance(coord, SkyCoord)
160 assert isinstance(coord.frame, Galactic)
161
162 # Make sure that the skycoord_to_healpix method converts coordinates
163 # to the frame of the HEALPix
164 coord = coord.transform_to('fk5')
165
166 index = self.pix.skycoord_to_healpix(coord)
167
168 assert_equal(index, [1, 2, 3])
169
170 coord = self.pix.healpix_to_skycoord([1, 2, 3],
171 dx=[0.1, 0.2, 0.3],
172 dy=[0.5, 0.4, 0.7])
173
174 assert isinstance(coord, SkyCoord)
175 assert isinstance(coord.frame, Galactic)
176
177 # Make sure that the skycoord_to_healpix method converts coordinates
178 # to the frame of the HEALPix
179 coord = coord.transform_to('fk5')
180
181 index, dx, dy = self.pix.skycoord_to_healpix(coord, return_offsets=True)
182
183 assert_equal(index, [1, 2, 3])
184 assert_allclose(dx, [0.1, 0.2, 0.3])
185 assert_allclose(dy, [0.5, 0.4, 0.7])
186
187 def test_interpolate_bilinear_skycoord(self):
188 values = np.ones(12 * 256 ** 2) * 3
189 coord = SkyCoord([1, 2, 3] * u.deg, [4, 3, 1] * u.deg, frame='fk4')
190 result = self.pix.interpolate_bilinear_skycoord(coord, values)
191 assert_allclose(result, [3, 3, 3])
192
193 # Make sure that coordinate system is correctly taken into account
194
195 values = np.arange(12 * 256 ** 2) * 3
196 coord = SkyCoord([1, 2, 3] * u.deg, [4, 3, 1] * u.deg, frame='fk4')
197
198 result1 = self.pix.interpolate_bilinear_skycoord(coord, values)
199 result2 = self.pix.interpolate_bilinear_skycoord(coord.icrs, values)
200
201 assert_allclose(result1, result2)
202
203 def test_cone_search_skycoord(self):
204 coord = SkyCoord(1 * u.deg, 4 * u.deg, frame='galactic')
205 result1 = self.pix.cone_search_skycoord(coord, 1 * u.deg)
206 assert len(result1) == 77
207 result2 = self.pix.cone_search_skycoord(coord.icrs, 1 * u.deg)
208 assert_allclose(result1, result2)
209
210 def test_boundaries_skycoord(self):
211 coord = self.pix.boundaries_skycoord([10, 20, 30], 4)
212 assert coord.shape == (3, 16)
213
214
215 class TestCelestialHEALPixFrameAsClass(TestCelestialHEALPix):
216
217 def setup_class(self):
218 self.pix = HEALPix(nside=256, order='nested', frame=Galactic)
219
220
221 class TestCelestialHEALPixFrameAsString(TestCelestialHEALPix):
222
223 def setup_class(self):
224 self.pix = HEALPix(nside=256, order='nested', frame='galactic')
225
226
227 def test_invalid_frame_name():
228 with pytest.raises(ValueError, match='Coordinate frame name "foobar"'):
229 HEALPix(nside=256, frame='foobar')
230
231
232 def test_invalid_frame_type():
233 with pytest.raises(ValueError, match='Coordinate frame must be a'):
234 HEALPix(nside=256, frame=('obviously', 'not', 'a', 'frame'))