/usr/lib/python3/dist-packages/reproject/interpolation/tests/test_core.py is in python3-reproject 0.3.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import os
import itertools
import numpy as np
from distutils.version import LooseVersion
from astropy.io import fits
from astropy.wcs import WCS
from astropy.utils.data import get_pkg_data_filename
from astropy.tests.helper import pytest
from ...array_utils import map_coordinates
from ..core_celestial import _reproject_celestial
from ..core_full import _reproject_full
from ..high_level import reproject_interp
# TODO: add reference comparisons
DATA = os.path.join(os.path.dirname(__file__), '..', '..', 'tests', 'data')
def array_footprint_to_hdulist(array, footprint, header):
hdulist = fits.HDUList()
hdulist.append(fits.PrimaryHDU(array, header))
hdulist.append(fits.ImageHDU(footprint, header, name='footprint'))
return hdulist
@pytest.mark.fits_compare()
def test_reproject_celestial_2d_gal2equ():
"""
Test reprojection of a 2D celestial image, which includes a coordinate
system conversion.
"""
hdu_in = fits.open(os.path.join(DATA, 'galactic_2d.fits'))[0]
header_out = hdu_in.header.copy()
header_out['CTYPE1'] = 'RA---TAN'
header_out['CTYPE2'] = 'DEC--TAN'
header_out['CRVAL1'] = 266.39311
header_out['CRVAL2'] = -28.939779
array_out, footprint_out = reproject_interp(hdu_in, header_out)
return array_footprint_to_hdulist(array_out, footprint_out, header_out)
# Note that we can't use independent_celestial_slices=True and reorder the
# axes, hence why we need to prepare the combinations in this way.
AXIS_ORDER = list(itertools.permutations((0, 1, 2)))
COMBINATIONS = [(True, (0, 1, 2))]
for axis_order in AXIS_ORDER:
COMBINATIONS.append((False, axis_order))
@pytest.mark.fits_compare(single_reference=True)
@pytest.mark.parametrize(('indep_slices', 'axis_order'), tuple(COMBINATIONS))
def test_reproject_celestial_3d_equ2gal(indep_slices, axis_order):
"""
Test reprojection of a 3D cube with celestial components, which includes a
coordinate system conversion (the original header is in equatorial
coordinates). We test using both the 'fast' method which assumes celestial
slices are independent, and the 'full' method. We also scramble the input
dimensions of the data and header to make sure that the reprojection can
deal with this.
"""
# Read in the input cube
hdu_in = fits.open(os.path.join(DATA, 'equatorial_3d.fits'))[0]
# Define the output header - this should be the same for all versions of
# this test to make sure we can use a single reference file.
header_out = hdu_in.header.copy()
header_out['NAXIS1'] = 10
header_out['NAXIS2'] = 9
header_out['CTYPE1'] = 'GLON-SIN'
header_out['CTYPE2'] = 'GLAT-SIN'
header_out['CRVAL1'] = 163.16724
header_out['CRVAL2'] = -15.777405
header_out['CRPIX1'] = 6
header_out['CRPIX2'] = 5
# We now scramble the input axes
if axis_order != (0, 1, 2):
wcs_in = WCS(hdu_in.header)
wcs_in = wcs_in.sub((3 - np.array(axis_order)[::-1]).tolist())
hdu_in.header = wcs_in.to_header()
hdu_in.data = np.transpose(hdu_in.data, axis_order)
array_out, footprint_out = reproject_interp(hdu_in, header_out,
independent_celestial_slices=indep_slices)
return array_footprint_to_hdulist(array_out, footprint_out, header_out)
@pytest.mark.fits_compare()
def test_small_cutout():
"""
Test reprojection of a cutout from a larger image (makes sure that the
pre-reprojection cropping works)
"""
hdu_in = fits.open(os.path.join(DATA, 'galactic_2d.fits'))[0]
header_out = hdu_in.header.copy()
header_out['NAXIS1'] = 10
header_out['NAXIS2'] = 9
header_out['CTYPE1'] = 'RA---TAN'
header_out['CTYPE2'] = 'DEC--TAN'
header_out['CRVAL1'] = 266.39311
header_out['CRVAL2'] = -28.939779
header_out['CRPIX1'] = 5.1
header_out['CRPIX2'] = 4.7
array_out, footprint_out = reproject_interp(hdu_in, header_out)
return array_footprint_to_hdulist(array_out, footprint_out, header_out)
def test_small_cutout_outside():
"""
Test reprojection of a cutout from a larger image - in this case the
cutout is completely outside the region of the input image so we should
take a shortcut that returns arrays of NaNs.
"""
hdu_in = fits.open(os.path.join(DATA, 'galactic_2d.fits'))[0]
header_out = hdu_in.header.copy()
header_out['NAXIS1'] = 10
header_out['NAXIS2'] = 9
header_out['CTYPE1'] = 'RA---TAN'
header_out['CTYPE2'] = 'DEC--TAN'
header_out['CRVAL1'] = 216.39311
header_out['CRVAL2'] = -21.939779
header_out['CRPIX1'] = 5.1
header_out['CRPIX2'] = 4.7
array_out, footprint_out = reproject_interp(hdu_in, header_out)
assert np.all(np.isnan(array_out))
assert np.all(footprint_out == 0)
def test_celestial_mismatch_2d():
"""
Make sure an error is raised if the input image has celestial WCS
information and the output does not (and vice-versa). This example will
use the _reproject_celestial route.
"""
hdu_in = fits.open(os.path.join(DATA, 'galactic_2d.fits'))[0]
header_out = hdu_in.header.copy()
header_out['CTYPE1'] = 'APPLES'
header_out['CTYPE2'] = 'ORANGES'
data = hdu_in.data
wcs1 = WCS(hdu_in.header)
wcs2 = WCS(header_out)
with pytest.raises(ValueError) as exc:
array_out, footprint_out = reproject_interp((data, wcs1), wcs2, shape_out=(2, 2))
assert exc.value.args[0] == "Input WCS has celestial components but output WCS does not"
def test_celestial_mismatch_3d():
"""
Make sure an error is raised if the input image has celestial WCS
information and the output does not (and vice-versa). This example will
use the _reproject_full route.
"""
hdu_in = fits.open(os.path.join(DATA, 'equatorial_3d.fits'))[0]
header_out = hdu_in.header.copy()
header_out['CTYPE1'] = 'APPLES'
header_out['CTYPE2'] = 'ORANGES'
header_out['CTYPE3'] = 'BANANAS'
data = hdu_in.data
wcs1 = WCS(hdu_in.header)
wcs2 = WCS(header_out)
with pytest.raises(ValueError) as exc:
array_out, footprint_out = reproject_interp((data, wcs1), wcs2, shape_out=(1, 2, 3))
assert exc.value.args[0] == "Input WCS has celestial components but output WCS does not"
with pytest.raises(ValueError) as exc:
array_out, footprint_out = reproject_interp((data, wcs2), wcs1, shape_out=(1, 2, 3))
assert exc.value.args[0] == "Output WCS has celestial components but input WCS does not"
def test_spectral_mismatch_3d():
"""
Make sure an error is raised if there are mismatches between the presence
or type of spectral axis.
"""
hdu_in = fits.open(os.path.join(DATA, 'equatorial_3d.fits'))[0]
header_out = hdu_in.header.copy()
header_out['CTYPE3'] = 'FREQ'
header_out['CUNIT3'] = 'Hz'
data = hdu_in.data
wcs1 = WCS(hdu_in.header)
wcs2 = WCS(header_out)
with pytest.raises(ValueError) as exc:
array_out, footprint_out = reproject_interp((data, wcs1), wcs2, shape_out=(1, 2, 3))
assert exc.value.args[0] == "The input (VOPT) and output (FREQ) spectral coordinate types are not equivalent."
header_out['CTYPE3'] = 'BANANAS'
wcs2 = WCS(header_out)
with pytest.raises(ValueError) as exc:
array_out, footprint_out = reproject_interp((data, wcs1), wcs2, shape_out=(1, 2, 3))
assert exc.value.args[0] == "Input WCS has a spectral component but output WCS does not"
with pytest.raises(ValueError) as exc:
array_out, footprint_out = reproject_interp((data, wcs2), wcs1, shape_out=(1, 2, 3))
assert exc.value.args[0] == "Output WCS has a spectral component but input WCS does not"
def test_naxis_mismatch():
"""
Make sure an error is raised if the input and output WCS have a different
number of dimensions.
"""
data = np.ones((3, 2, 2))
wcs_in = WCS(naxis=3)
wcs_out = WCS(naxis=2)
with pytest.raises(ValueError) as exc:
array_out, footprint_out = reproject_interp((data, wcs_in), wcs_out, shape_out=(1, 2))
assert exc.value.args[0] == "Number of dimensions between input and output WCS should match"
def test_slice_reprojection():
"""
Test case where only the slices change and the celestial projection doesn't
"""
inp_cube = np.arange(3, dtype='float').repeat(4 * 5).reshape(3, 4, 5)
header_in = fits.Header.fromtextfile(get_pkg_data_filename('../../tests/data/cube.hdr'))
header_in['NAXIS1'] = 5
header_in['NAXIS2'] = 4
header_in['NAXIS3'] = 3
header_out = header_in.copy()
header_out['NAXIS3'] = 2
header_out['CRPIX3'] -= 0.5
wcs_in = WCS(header_in)
wcs_out = WCS(header_out)
out_cube, out_cube_valid = _reproject_full(inp_cube, wcs_in, wcs_out, (2, 4, 5))
# we expect to be projecting from
# inp_cube = np.arange(3, dtype='float').repeat(4*5).reshape(3,4,5)
# to
# inp_cube_interp = (inp_cube[:-1]+inp_cube[1:])/2.
# which is confirmed by
# map_coordinates(inp_cube.astype('float'), new_coords, order=1, cval=np.nan, mode='constant')
# np.testing.assert_allclose(inp_cube_interp, map_coordinates(inp_cube.astype('float'), new_coords, order=1, cval=np.nan, mode='constant'))
assert out_cube.shape == (2, 4, 5)
assert out_cube_valid.sum() == 40.
# We only check that the *valid* pixels are equal
# but it's still nice to check that the "valid" array works as a mask
np.testing.assert_allclose(out_cube[out_cube_valid.astype('bool')],
((inp_cube[:-1] + inp_cube[1:]) / 2.)[out_cube_valid.astype('bool')])
# Actually, I fixed it, so now we can test all
np.testing.assert_allclose(out_cube, ((inp_cube[:-1] + inp_cube[1:]) / 2.))
def test_4d_fails():
header_in = fits.Header.fromtextfile(get_pkg_data_filename('../../tests/data/cube.hdr'))
header_in['NAXIS'] = 4
header_out = header_in.copy()
w_in = WCS(header_in)
w_out = WCS(header_out)
array_in = np.zeros((2, 3, 4, 5))
with pytest.raises(ValueError) as ex:
x_out, y_out, z_out = reproject_interp((array_in, w_in), w_out, shape_out=[2, 4, 5, 6])
assert str(ex.value) == "Length of shape_out should match number of dimensions in wcs_out"
def test_inequal_wcs_dims():
inp_cube = np.arange(3, dtype='float').repeat(4 * 5).reshape(3, 4, 5)
header_in = fits.Header.fromtextfile(get_pkg_data_filename('../../tests/data/cube.hdr'))
header_out = header_in.copy()
header_out['CTYPE3'] = 'VRAD'
header_out['CUNIT3'] = 'm/s'
header_in['CTYPE3'] = 'STOKES'
header_in['CUNIT3'] = ''
wcs_out = WCS(header_out)
with pytest.raises(ValueError) as ex:
out_cube, out_cube_valid = reproject_interp((inp_cube, header_in), wcs_out, shape_out=(2, 4, 5))
assert str(ex.value) == "Output WCS has a spectral component but input WCS does not"
def test_different_wcs_types():
inp_cube = np.arange(3, dtype='float').repeat(4 * 5).reshape(3, 4, 5)
header_in = fits.Header.fromtextfile(get_pkg_data_filename('../../tests/data/cube.hdr'))
header_out = header_in.copy()
header_out['CTYPE3'] = 'VRAD'
header_out['CUNIT3'] = 'm/s'
header_in['CTYPE3'] = 'VELO'
header_in['CUNIT3'] = 'm/s'
wcs_in = WCS(header_in)
wcs_out = WCS(header_out)
with pytest.raises(ValueError) as ex:
out_cube, out_cube_valid = reproject_interp((inp_cube, header_in), wcs_out, shape_out=(2, 4, 5))
assert str(ex.value) == ("The input (VELO) and output (VRAD) spectral "
"coordinate types are not equivalent.")
# TODO: add a test to check the units are the same.
def test_reproject_3d_celestial_correctness_ra2gal():
inp_cube = np.arange(3, dtype='float').repeat(7 * 8).reshape(3, 7, 8)
header_in = fits.Header.fromtextfile(get_pkg_data_filename('../../tests/data/cube.hdr'))
header_in['NAXIS1'] = 8
header_in['NAXIS2'] = 7
header_in['NAXIS3'] = 3
header_out = header_in.copy()
header_out['CTYPE1'] = 'GLON-TAN'
header_out['CTYPE2'] = 'GLAT-TAN'
header_out['CRVAL1'] = 158.5644791
header_out['CRVAL2'] = -21.59589875
# make the cube a cutout approximately in the center of the other one, but smaller
header_out['NAXIS1'] = 4
header_out['CRPIX1'] = 2
header_out['NAXIS2'] = 3
header_out['CRPIX2'] = 1.5
header_out['NAXIS3'] = 2
header_out['CRPIX3'] -= 0.5
wcs_in = WCS(header_in)
wcs_out = WCS(header_out)
out_cube, out_cube_valid = reproject_interp((inp_cube, wcs_in), wcs_out, shape_out=(2, 3, 4))
assert out_cube.shape == (2, 3, 4)
assert out_cube_valid.sum() == out_cube.size
# only compare the spectral axis
np.testing.assert_allclose(out_cube[:, 0, 0], ((inp_cube[:-1] + inp_cube[1:]) / 2.)[:, 0, 0])
# see https://github.com/astrofrog/reproject/issues/111
@pytest.mark.xfail()
def test_reproject_celestial_3d():
"""
Test both full_reproject and slicewise reprojection. We use a case where the
non-celestial slices are the same and therefore where both algorithms can
work.
"""
header_in = fits.Header.fromtextfile(get_pkg_data_filename('../../tests/data/cube.hdr'))
array_in = np.ones((3, 200, 180))
# TODO: here we can check that if we change the order of the dimensions in
# the WCS, things still work properly
wcs_in = WCS(header_in)
wcs_out = wcs_in.deepcopy()
wcs_out.wcs.ctype = ['GLON-SIN', 'GLAT-SIN', wcs_in.wcs.ctype[2]]
wcs_out.wcs.crval = [158.0501, -21.530282, wcs_in.wcs.crval[2]]
wcs_out.wcs.crpix = [50., 50., wcs_in.wcs.crpix[2] + 0.5]
out_full, foot_full = _reproject_full(array_in, wcs_in, wcs_out, (3, 160, 170))
out_celestial, foot_celestial = _reproject_celestial(array_in, wcs_in, wcs_out, (3, 160, 170))
np.testing.assert_allclose(out_full, out_celestial)
np.testing.assert_allclose(foot_full, foot_celestial)
|