def test_djs_laxisgen(): import numpy as np from astropy.tests.helper import raises from .. import djs_laxisgen # # 1d # assert (np.arange(4,dtype='i4') == djs_laxisgen((4,))).all() # # 2d # l = np.array([[0,0,0,0],[1,1,1,1],[2,2,2,2],[3,3,3,3]],dtype='i4') assert (l == djs_laxisgen((4,4))).all() assert (l.T == djs_laxisgen((4,4),iaxis=1)).all() with raises(ValueError): foo = djs_laxisgen((4,4),iaxis=2) # # 3d # l = np.zeros((4,4,4),dtype='i4') l[1,:,:] = 1 l[2,:,:] = 2 l[3,:,:] = 3 assert (l == djs_laxisgen((4,4,4))).all() assert (l.swapaxes(0,1) == djs_laxisgen((4,4,4),iaxis=1)).all() assert (l.swapaxes(0,2) == djs_laxisgen((4,4,4),iaxis=2)).all() with raises(ValueError): foo = djs_laxisgen((4,4,4),iaxis=3) # # More d # with raises(ValueError): foo = djs_laxisgen((4,4,4,4))
def test_filter_thru(): from .. import filter_thru import numpy as np from os.path import dirname, join from astropy.io import fits from astropy.tests.helper import raises # with fits.open(join('pydl','pydlspec2d','spec2d','tests','t','spPlate-4055-55359-0020.fits')) as hdulist: with fits.open(join(dirname(__file__), "t", "spPlate-4055-55359-0020.fits")) as hdulist: flux = hdulist[0].data npix = hdulist[0].header["NAXIS1"] ntrace = hdulist[0].header["NAXIS2"] crval1 = hdulist[0].header["COEFF0"] cd1_1 = hdulist[0].header["COEFF1"] assert flux.shape == (ntrace, npix) loglam0 = crval1 + cd1_1 * np.arange(npix, dtype=flux.dtype) waveimg = 10 ** (np.tile(loglam0, 20).reshape(flux.shape)) assert waveimg.shape == flux.shape f = filter_thru(flux, waveimg=waveimg) idl_data_file = join(dirname(__file__), "t", "filter_thru_idl_data.txt") idl_data = np.loadtxt(idl_data_file, dtype="f", delimiter=",").T assert f.shape == (20, 5) assert np.allclose(f, idl_data, atol=1.0e-6) # # Test bad input. # with raises(ValueError): f = filter_thru(flux) with raises(ValueError): f = filter_thru(flux, waveimg=waveimg, filter_prefix="sdss") return
def test_filter_thru(self): fname = get_pkg_data_filename('t/spPlate-4055-55359-0020.fits') with fits.open(fname) as hdulist: flux = hdulist[0].data npix = hdulist[0].header['NAXIS1'] ntrace = hdulist[0].header['NAXIS2'] crval1 = hdulist[0].header['COEFF0'] cd1_1 = hdulist[0].header['COEFF1'] assert flux.shape == (ntrace, npix) loglam0 = crval1 + cd1_1*np.arange(npix, dtype=flux.dtype) waveimg = 10**(np.tile(loglam0, 20).reshape(flux.shape)) assert waveimg.shape == flux.shape f = filter_thru(flux, waveimg=waveimg) idl_data_file = get_pkg_data_filename('t/filter_thru_idl_data.txt') idl_data = np.loadtxt(idl_data_file, dtype='f', delimiter=',').T assert f.shape == (20, 5) assert np.allclose(f, idl_data, atol=1.0e-6) # # Test bad input. # with raises(ValueError): f = filter_thru(flux) with raises(ValueError): f = filter_thru(flux, waveimg=waveimg, filter_prefix='sdss') return
def test_djs_laxisnum(self): # # 1d # assert (np.zeros((4,), dtype='i4') == djs_laxisnum((4,))).all() # # 2d # l = np.array([[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], dtype='i4') assert (l == djs_laxisnum((4, 4))).all() assert (l.T == djs_laxisnum((4, 4), iaxis=1)).all() with raises(ValueError): foo = djs_laxisnum((4, 4), iaxis=2) # # 3d # l = np.zeros((4, 4, 4), dtype='i4') l[1, :, :] = 1 l[2, :, :] = 2 l[3, :, :] = 3 assert (l == djs_laxisnum((4, 4, 4))).all() assert (l.swapaxes(0, 1) == djs_laxisnum((4, 4, 4), iaxis=1)).all() assert (l.swapaxes(0, 2) == djs_laxisnum((4, 4, 4), iaxis=2)).all() with raises(ValueError): foo = djs_laxisnum((4, 4, 4), iaxis=3) # # More d # with raises(ValueError): foo = djs_laxisnum((4, 4, 4, 4))
def test_rebin(self): x = np.arange(40) with raises(ValueError): r = rebin(x, d=(10, 10)) with raises(ValueError): r = rebin(x, d=(70, )) with raises(ValueError): r = rebin(x, d=(30, )) x = np.array([[1.0, 2.0], [2.0, 3.0]]) rexpect = np.array([[1.0, 2.0], [1.5, 2.5], [2.0, 3.0], [2.0, 3.0]]) r = rebin(x, d=(4, 2)) assert np.allclose(r, rexpect) rexpect = np.array([[1.0, 1.5, 2.0, 2.0], [2.0, 2.5, 3.0, 3.0]]) r = rebin(x, d=(2, 4)) assert np.allclose(r, rexpect) rexpect = np.array([[1.0, 2.0], [1.0, 2.0], [2.0, 3.0], [2.0, 3.0]]) r = rebin(x, d=(4, 2), sample=True) assert np.allclose(r, rexpect) rexpect = np.array([[1.0, 1.0, 2.0, 2.0], [2.0, 2.0, 3.0, 3.0]]) r = rebin(x, d=(2, 4), sample=True) assert np.allclose(r, rexpect) x = np.arange(10) rexpect = np.array([0.0, 2.0, 4.0, 6.0, 8.0]) r = rebin(x, d=(5, ), sample=True) assert np.allclose(r, rexpect) x = np.array([[1.0, 2.0, 3.0, 4.0], [2.0, 3.0, 4.0, 5.0], [3.0, 4.0, 5.0, 6.0], [4.0, 5.0, 6.0, 7.0]]) rexpect = np.array([[2.0, 4.0], [4.0, 6.0]]) r = rebin(x, d=(2, 2)) assert np.allclose(r, rexpect) rexpect = np.array([[1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 4.5], [3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 6.5]]) r = rebin(x, d=(2, 8)) assert np.allclose(r, rexpect)
def test_filter_thru(self): fname = get_pkg_data_filename('t/spPlate-4055-55359-0020.fits') with fits.open(fname) as hdulist: flux = hdulist[0].data npix = hdulist[0].header['NAXIS1'] ntrace = hdulist[0].header['NAXIS2'] crval1 = hdulist[0].header['COEFF0'] cd1_1 = hdulist[0].header['COEFF1'] assert flux.shape == (ntrace, npix) loglam0 = crval1 + cd1_1 * np.arange(npix, dtype=flux.dtype) waveimg = 10**(np.tile(loglam0, 20).reshape(flux.shape)) assert waveimg.shape == flux.shape f = filter_thru(flux, waveimg=waveimg) idl_data_file = get_pkg_data_filename('t/filter_thru_idl_data.txt') idl_data = np.loadtxt(idl_data_file, dtype='f', delimiter=',').T assert f.shape == (20, 5) assert np.allclose(f, idl_data, atol=1.0e-6) # # Test bad input. # with raises(ValueError): f = filter_thru(flux) with raises(ValueError): f = filter_thru(flux, waveimg=waveimg, filter_prefix='sdss') return
def test_filter_thru(): from .. import filter_thru import numpy as np from os.path import dirname, join from astropy.io import fits from astropy.tests.helper import raises # with fits.open(join('pydl','pydlspec2d','spec2d','tests','t','spPlate-4055-55359-0020.fits')) as hdulist: with fits.open(join(dirname(__file__), 't', 'spPlate-4055-55359-0020.fits')) as hdulist: flux = hdulist[0].data npix = hdulist[0].header['NAXIS1'] ntrace = hdulist[0].header['NAXIS2'] crval1 = hdulist[0].header['COEFF0'] cd1_1 = hdulist[0].header['COEFF1'] assert flux.shape == (ntrace, npix) loglam0 = crval1 + cd1_1 * np.arange(npix, dtype=flux.dtype) waveimg = 10**(np.tile(loglam0, 20).reshape(flux.shape)) assert waveimg.shape == flux.shape f = filter_thru(flux, waveimg=waveimg) idl_data_file = join(dirname(__file__), 't', 'filter_thru_idl_data.txt') idl_data = np.loadtxt(idl_data_file, dtype='f', delimiter=',').T assert f.shape == (20, 5) assert np.allclose(f, idl_data, atol=1.0e-6) # # Test bad input. # with raises(ValueError): f = filter_thru(flux) with raises(ValueError): f = filter_thru(flux, waveimg=waveimg, filter_prefix='sdss') return
def test_sdss_flagval(self): val = sdss_flagval('TARGET', 'ROSAT_A') assert val == 2**9 val = sdss_flagval('ANCILLARY_TARGET1', ['BLAZGX', 'ELG', 'BRIGHTGAL']) assert val == 2310346608843161600 with raises(KeyError): val = sdss_flagval('TARGET', 'ROSAT_Q') with raises(KeyError): val = sdss_flagval('ABADMASK', "ABADFLAG")
def test_nw_cut_to_box(self): colors = np.random.random((10, 10)) with raises(ValueError): boxed_colors = nw_cut_to_box(colors) colors = np.random.random((10, 10, 5)) with raises(ValueError): boxed_colors = nw_cut_to_box(colors) colors = np.random.random((10, 10, 3)) with raises(ValueError): boxed_colors = nw_cut_to_box(colors, origin=(1.0, 1.0)) boxed_colors = nw_cut_to_box(colors) assert np.allclose(boxed_colors, colors)
def test_fchebyshev_split(): from .. import fchebyshev_split import numpy as np from astropy.tests.helper import raises x = np.array([-1, -0.5, 0, 0.5, 1], dtype="d") # # Test order # with raises(ValueError): f = fchebyshev_split(x, 0) with raises(ValueError): f = fchebyshev_split(x, 1) # # m = 2 # f = fchebyshev_split(x, 2) foo = np.ones((2, x.size), dtype="d") foo[0, :] = (x >= 0).astype(x.dtype) assert np.allclose(f, foo) # # m = 3 # f = fchebyshev_split(x, 3) foo = np.ones((3, x.size), dtype="d") foo[0, :] = (x >= 0).astype(x.dtype) foo[2, :] = x assert np.allclose(f, foo) # # m = 4 # f = fchebyshev_split(x, 4) foo = np.ones((4, x.size), dtype="d") foo[0, :] = (x >= 0).astype(x.dtype) foo[2, :] = x foo[3, :] = 2.0 * x ** 2 - 1.0 assert np.allclose(f, foo) # # m = 5 # f = fchebyshev_split(x, 5) foo = np.ones((5, x.size), dtype="d") foo[0, :] = (x >= 0).astype(x.dtype) foo[2, :] = x foo[3, :] = 2.0 * x ** 2 - 1.0 foo[4, :] = 4.0 * x ** 3 - 3.0 * x assert np.allclose(f, foo) # # random float # f = fchebyshev_split(2.88, 3) assert np.allclose(f, np.array([[1.00], [1.00], [2.88]]))
def test_nw_scale_rgb(self): colors = np.random.random((10, 10)) with raises(ValueError): scaled_colors = nw_scale_rgb(colors) colors = np.random.random((10, 10, 5)) with raises(ValueError): scaled_colors = nw_scale_rgb(colors) colors = np.random.random((10, 10, 3)) with raises(ValueError): scaled_colors = nw_scale_rgb(colors, scales=(1.0, 1.0)) colors = np.ones((2, 2, 3)) scaled_colors = nw_scale_rgb(colors, scales=(2.0, 2.0, 2.0)) assert np.allclose(scaled_colors, 2.0)
def test_read_table_yanny(self): """Test reading to an astropy Table. """ filename = self.data('test_table.par') with raises(PydlutilsException): t = Table.read(filename) with raises(KeyError): t = Table.read(filename, tablename='foo') t = Table.read(filename, tablename='test') assert isinstance(t.meta, OrderedDict) assert t.meta['name'] == 'first table' assert (t['a'] == np.array([1, 4, 5])).all() assert (t['c'] == np.array([b'x', b'y', b'z'])).all()
def test_nw_arcsinh(self): colors = np.random.random((10, 10)) with raises(ValueError): fitted_colors = nw_arcsinh(colors) colors = np.random.random((10, 10, 5)) with raises(ValueError): fitted_colors = nw_arcsinh(colors) colors = np.random.random((10, 10, 3)) fitted_colors = nw_arcsinh(colors, nonlinearity=0) assert (fitted_colors == colors).all() colors = np.ones((2, 2, 3)) fac = np.arcsinh(9.0) / 9.0 fitted_colors = nw_arcsinh(colors) assert np.allclose(fitted_colors, fac)
def test_nw_arcsinh(self): colors = np.random.random((10, 10)) with raises(ValueError): fitted_colors = nw_arcsinh(colors) colors = np.random.random((10, 10, 5)) with raises(ValueError): fitted_colors = nw_arcsinh(colors) colors = np.random.random((10, 10, 3)) fitted_colors = nw_arcsinh(colors, nonlinearity=0) assert (fitted_colors == colors).all() colors = np.ones((2, 2, 3)) fac = np.arcsinh(9.0)/9.0 fitted_colors = nw_arcsinh(colors) assert np.allclose(fitted_colors, fac)
def test_fchebyshev_split(self): x = np.array([-1, -0.5, 0, 0.5, 1], dtype='d') # # Test order # with raises(ValueError): f = fchebyshev_split(x, 0) with raises(ValueError): f = fchebyshev_split(x, 1) # # m = 2 # f = fchebyshev_split(x, 2) foo = np.ones((2, x.size), dtype='d') foo[0, :] = (x >= 0).astype(x.dtype) assert np.allclose(f, foo) # # m = 3 # f = fchebyshev_split(x, 3) foo = np.ones((3, x.size), dtype='d') foo[0, :] = (x >= 0).astype(x.dtype) foo[2, :] = x assert np.allclose(f, foo) # # m = 4 # f = fchebyshev_split(x, 4) foo = np.ones((4, x.size), dtype='d') foo[0, :] = (x >= 0).astype(x.dtype) foo[2, :] = x foo[3, :] = (2.0 * x**2 - 1.0) assert np.allclose(f, foo) # # m = 5 # f = fchebyshev_split(x, 5) foo = np.ones((5, x.size), dtype='d') foo[0, :] = (x >= 0).astype(x.dtype) foo[2, :] = x foo[3, :] = (2.0 * x**2 - 1.0) foo[4, :] = (4.0 * x**3 - 3.0 * x) assert np.allclose(f, foo) # # random float # f = fchebyshev_split(2.88, 3) assert np.allclose(f, np.array([[1.00], [1.00], [2.88]]))
def test_fchebyshev_split(self): x = np.array([-1, -0.5, 0, 0.5, 1], dtype='d') # # Test order # with raises(ValueError): f = fchebyshev_split(x, 0) with raises(ValueError): f = fchebyshev_split(x, 1) # # m = 2 # f = fchebyshev_split(x, 2) foo = np.ones((2, x.size), dtype='d') foo[0, :] = (x >= 0).astype(x.dtype) assert np.allclose(f, foo) # # m = 3 # f = fchebyshev_split(x, 3) foo = np.ones((3, x.size), dtype='d') foo[0, :] = (x >= 0).astype(x.dtype) foo[2, :] = x assert np.allclose(f, foo) # # m = 4 # f = fchebyshev_split(x, 4) foo = np.ones((4, x.size), dtype='d') foo[0, :] = (x >= 0).astype(x.dtype) foo[2, :] = x foo[3, :] = (2.0*x**2 - 1.0) assert np.allclose(f, foo) # # m = 5 # f = fchebyshev_split(x, 5) foo = np.ones((5, x.size), dtype='d') foo[0, :] = (x >= 0).astype(x.dtype) foo[2, :] = x foo[3, :] = (2.0*x**2 - 1.0) foo[4, :] = (4.0*x**3 - 3.0*x) assert np.allclose(f, foo) # # random float # f = fchebyshev_split(2.88, 3) assert np.allclose(f, np.array([[1.00], [1.00], [2.88]]))
def test_template_metadata(self): with raises(Pydlspec2dException): slist, metadata = template_metadata('/no/such/file.par') inputfile = get_pkg_data_filename('t/test_template_metadata.par') slist, metadata = template_metadata(inputfile) assert metadata['object'] == 'gal' assert not metadata['nonnegative']
def test_window_score_no_photo_resolve(self, monkeypatch): monkeypatch.setenv('PHOTO_CALIB', '/fake/directory') monkeypatch.delenv('PHOTO_RESOLVE', raising=False) with raises(PhotoopException) as e: window_score() assert (str(e.value) == 'You have not set the environment variable PHOTO_RESOLVE!')
def test_read_fits_polygons(self): poly = mng.read_fits_polygons(self.poly_fits) use_caps = np.array([31, 15, 31, 7, 31, 15, 15, 7, 15, 15, 15, 31, 15, 15, 15, 15, 15, 15, 31, 15], dtype=np.uint32) # # Attribute access doesn't work on unsigned columns. # assert (poly['USE_CAPS'] == use_caps).all() assert (poly['use_caps'] == use_caps).all() with raises(AttributeError): foo = poly.no_such_attribute cm0 = np.array([-1.0, -0.99369437, 1.0, -1.0, 0.00961538]) assert np.allclose(poly.cm[0][0:poly.ncaps[0]], cm0) assert poly[0]['NCAPS'] == 5 poly = mng.read_fits_polygons(self.poly_fits, convert=True) assert poly[0].use_caps == 31 assert np.allclose(poly[0].cm, cm0) assert poly[0].cmminf() == 4 # # A FITS file might not contain IFIELD. # poly = mng.read_fits_polygons(self.no_id_fits) assert len(poly) == 1 # # A FITS file might contain exactly one polygon with exactly one cap. # poly = mng.read_fits_polygons(self.one_cap_fits, convert=True) assert poly[0].ncaps == 1
def test_aesthetics(): from .. import aesthetics from ... import Pydlspec2dException import numpy as np from astropy.tests.helper import raises np.random.seed(137) flux = np.random.rand(100) ivar = np.random.rand(100) # # No bad # f = aesthetics(flux, ivar) assert (f == flux).all() # # Bad points # ivar[ivar < 0.1] = 0.0 # # Bad method # with raises(Pydlspec2dException): f = aesthetics(flux, ivar, 'badmethod') # # Nothing # f = aesthetics(flux, ivar, 'nothing') assert (f == flux).all()
def test_unwrap_objid(self): objid = unwrap_objid(np.array([1237661382772195474])) assert objid.skyversion == 2 assert objid.rerun == 301 assert objid.run == 3704 assert objid.camcol == 3 assert objid.firstfield == 0 assert objid.frame == 91 assert objid.id == 146 objid = unwrap_objid(np.array(['1237661382772195474'])) assert objid.skyversion == 2 assert objid.rerun == 301 assert objid.run == 3704 assert objid.camcol == 3 assert objid.firstfield == 0 assert objid.frame == 91 assert objid.id == 146 objid = unwrap_objid(np.array([587722984180548043])) assert objid.skyversion == 1 assert objid.rerun == 40 assert objid.run == 752 assert objid.camcol == 5 assert objid.firstfield == 1 assert objid.frame == 618 assert objid.id == 459 with raises(ValueError): objid = unwrap_objid(np.array([3.14159]))
def test_pcomp(self): test_data_file = join(self.data_dir, 'pcomp_data.txt') test_data = np.loadtxt(test_data_file, dtype='d', delimiter=',') with raises(ValueError): foo = pcomp(np.arange(10)) pcomp_data = test_data[0:20, :] m = 4 n = 20 means = np.tile(pcomp_data.mean(0), n).reshape(pcomp_data.shape) newarray = pcomp_data - means foo = pcomp(newarray, covariance=True) # # This array is obtained from the IDL version of PCOMP. # It is only accurate up to an overall sign on each column. # derived = test_data[20:40, :] for k in range(m): assert_allclose(abs(foo.derived[:, k]), abs(derived[:, k]), 1e-4) coefficients = test_data[40:44, :] coefficientsT = coefficients.T for k in range(m): assert_allclose(abs(foo.coefficients[:, k]), abs(coefficientsT[:, k]), 1e-4) eigenvalues = test_data[44, :] assert_allclose(foo.eigenvalues, eigenvalues, 1e-4) variance = test_data[45, :] assert_allclose(foo.variance, variance, 1e-4) # # Test the standardization. # foo = pcomp(pcomp_data, standardize=True, covariance=True) # for k in range(m): # assert_allclose(abs(foo.derived[:, k]), abs(derived[:, k]), 1e-4) # for k in range(m): # assert_allclose(abs(foo.coefficients[:, k]), # abs(coefficientsT[:, k]), # 1e-4) eigenvalues = test_data[46, :] assert_allclose(foo.eigenvalues, eigenvalues, 1e-4) variance = test_data[47, :] assert_allclose(foo.variance, variance, 1e-4) # assert_allclose(foo.derived[0, :], np.array([-1.64153312, # -9.12322038, # 1.41790708, # -8.29359322])) # # Make sure correlation is working at least. # foo = pcomp(pcomp_data, standardize=True) assert_allclose( foo.eigenvalues, np.array([ 2.84968632e+00, 1.00127640e+00, 1.48380121e-01, 6.57156222e-04 ])) assert_allclose( foo.variance, np.array([ 7.12421581e-01, 2.50319100e-01, 3.70950302e-02, 1.64289056e-04 ]))
def test_sdss_flagname(self): names = sdss_flagname('ANCILLARY_TARGET1', 2310346608843161600) assert tuple(names) == ('BRIGHTGAL', 'BLAZGX', 'ELG') names = sdss_flagname('ANCILLARY_TARGET1', 2310346608843161600, concat=True) assert names == 'BRIGHTGAL BLAZGX ELG' with raises(KeyError): names = sdss_flagname('ABADMASK', 123456789)
def test_pcomp(self): test_data_file = join(self.data_dir, 'pcomp_data.txt') test_data = np.loadtxt(test_data_file, dtype='d', delimiter=',') with raises(ValueError): foo = pcomp(np.arange(10)) pcomp_data = test_data[0:20, :] m = 4 n = 20 means = np.tile(pcomp_data.mean(0), n).reshape(pcomp_data.shape) newarray = pcomp_data - means foo = pcomp(newarray, covariance=True) # # This array is obtained from the IDL version of PCOMP. # It is only accurate up to an overall sign on each column. # derived = test_data[20:40, :] for k in range(m): assert_allclose(abs(foo.derived[:, k]), abs(derived[:, k]), 1e-4) coefficients = test_data[40:44, :] coefficientsT = coefficients.T for k in range(m): assert_allclose(abs(foo.coefficients[:, k]), abs(coefficientsT[:, k]), 1e-4) eigenvalues = test_data[44, :] assert_allclose(foo.eigenvalues, eigenvalues, 1e-4) variance = test_data[45, :] assert_allclose(foo.variance, variance, 1e-4) # # Test the standardization. # foo = pcomp(pcomp_data, standardize=True, covariance=True) # for k in range(m): # assert_allclose(abs(foo.derived[:, k]), abs(derived[:, k]), 1e-4) # for k in range(m): # assert_allclose(abs(foo.coefficients[:, k]), # abs(coefficientsT[:, k]), # 1e-4) eigenvalues = test_data[46, :] assert_allclose(foo.eigenvalues, eigenvalues, 1e-4) variance = test_data[47, :] assert_allclose(foo.variance, variance, 1e-4) # assert_allclose(foo.derived[0, :], np.array([-1.64153312, # -9.12322038, # 1.41790708, # -8.29359322])) # # Make sure correlation is working at least. # foo = pcomp(pcomp_data, standardize=True) assert_allclose(foo.eigenvalues, np.array([2.84968632e+00, 1.00127640e+00, 1.48380121e-01, 6.57156222e-04])) assert_allclose(foo.variance, np.array([7.12421581e-01, 2.50319100e-01, 3.70950302e-02, 1.64289056e-04]))
def test_ManglePolygon(self): # # Zero caps # poly = mng.ManglePolygon() assert np.allclose(poly.str, 4.0*np.pi) assert poly.cmminf() is None assert not poly.gzeroar() assert np.allclose(poly.garea(), 4.0*np.pi) # # One cap. # x = np.array([[0.0, 0.0, 1.0]]) cm = np.array([0.5]) poly = mng.ManglePolygon(x=x, cm=cm) assert np.allclose(poly.garea(), np.pi) # # Bad inputs # with raises(ValueError): poly = mng.ManglePolygon(weight=1.0) # # Multiple caps # x = np.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]) cm = np.array([1.0, 1.0, 1.0]) poly = mng.ManglePolygon(x=x, cm=cm, str=np.pi/2.0) assert poly.ncaps == 3 assert poly.weight == 1.0 assert poly.use_caps == (1 << 3) - 1 poly = mng.ManglePolygon(x=x, cm=cm, weight=0.5) assert poly.weight == 0.5 poly = mng.ManglePolygon(x=x, cm=cm, pixel=20) assert poly.pixel == 20 poly = mng.ManglePolygon(x=x, cm=cm, use_caps=3, str=np.pi/2.0) assert poly.use_caps == 3 poly2 = poly.copy() assert poly2.use_caps == poly.use_caps assert (poly2.cm == poly.cm).all() assert np.allclose(poly2.str, np.pi/2.0) x = np.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0]]) cm = np.array([1.0, 1.0]) poly = mng.ManglePolygon(x=x, cm=cm) poly2 = poly.add_caps(np.array([[0.0, 1.0, 0.0], ]), np.array([1.0, ])) assert poly2.ncaps == 3 assert poly2.use_caps == poly.use_caps assert poly2.str == 1.0 # dummy value! poly3 = poly.polyn(poly2, 2) assert poly3.ncaps == 3 assert poly3.use_caps == poly.use_caps assert np.allclose(poly3.x[2, :], np.array([0.0, 1.0, 0.0])) poly3 = poly.polyn(poly2, 2, complement=True) assert poly3.ncaps == 3 assert poly3.use_caps == poly.use_caps assert np.allclose(poly3.cm[2], -1.0)
def test_sdss_name(self): # # Bad ftype # with raises(KeyError): p = sdss_name('fooBar', 137, 4, 42) for ftype in self.name_data: assert sdss_name(ftype, 137, 4, 42, '301', 'r', no_path=True) == self.name_data[ftype]
def test_write_ndarray_to_yanny_exceptions(self): """Make sure certain execptions are raised by the write_ndarray_to_yanny function. """ test_data = self.test_data['tempfile1.par'] table = test_data['structures']['MYSTRUCT0'] dt = self.json2dtype(table['dtype']) mystruct = np.zeros((table['size'],), dtype=dt) for col in table['dtype']: mystruct[col[0]] = np.array(table['data'][col[0]], dtype=col[1]) enums = {'new_flag': test_data['enums']['new_flag']} with raises(PydlutilsException): par = write_ndarray_to_yanny(self.data('test.par'), mystruct, structnames='magnitudes', enums=enums) with raises(PydlutilsException): par = write_ndarray_to_yanny(self.temp('tempfile3.par'), mystruct, structnames=('magnitudes', 'my_status'), enums=enums)
def test_write_ndarray_to_yanny_exceptions(self): """Make sure certain execptions are raised by the write_ndarray_to_yanny function.""" mystruct = np.zeros((4,), dtype=[('ra','f8'), ('dec','f8'), ('mag','f4',(5,)), ('flags','i4'), ('new_flag','|S5')]) mystruct['ra'] = np.array([10.0, 20.5, 30.75, 40.55],dtype=np.float64) mystruct['dec'] = np.array([-5.1234, -10.74832, 67.994523, 11.437281],dtype=np.float64) mystruct['mag'] = np.array([[0.0,1.0,2.0,3.0,4.0], [5.1,6.2,7.3,8.4,9.5], [22.123,23.95,22.6657,21.0286,22.9876], [13.54126,15.37456,14.52647,12.648640,12.0218]],dtype=np.float32) mystruct['flags'] = np.array([2**2, 2**4, 2**6, 2**8 + 2**3],dtype=np.int32) mystruct['new_flag'] = np.array(['FALSE','TRUE','TRUE','FALSE'],dtype='|S5') enums = {'new_flag':('BOOLEAN',('FALSE','TRUE'))} with raises(PydlutilsException): par = write_ndarray_to_yanny(os.path.join(self.data_dir,'test.par'),mystruct,structnames='magnitudes',enums=enums) with raises(PydlutilsException): par = write_ndarray_to_yanny(self.temp('tempfile3.par'),mystruct,structnames=('magnitudes','my_status'),enums=enums)
def test_spheregroup(self): test_data_file = get_pkg_data_filename('t/spheregroup_data.txt') test_data = np.loadtxt(test_data_file, dtype='d', delimiter=',') # np.random.seed(137) # Ngroup = 3 # N = 50 # spread = 0.05 linklength = 3.0 # degrees # x0 = np.concatenate( ( np.random.normal(loc=1,scale=spread,size=(N,)), # np.random.normal(loc=-1,scale=spread,size=(N,)), # np.random.normal(loc=1,scale=spread,size=(N,)), # )).reshape((N*Ngroup,)) # y0 = np.concatenate( ( np.random.normal(loc=1,scale=spread,size=(N,)), # np.random.normal(loc=-1,scale=spread,size=(N,)), # np.random.normal(loc=-1,scale=spread,size=(N,)), # )).reshape((N*Ngroup,)) # z0 = np.concatenate( ( np.random.normal(loc=1,scale=spread,size=(N,)), # np.random.normal(loc=-1,scale=spread,size=(N,)), # np.random.normal(loc=0,scale=spread,size=(N,)), # )).reshape((N*Ngroup,)) # foo = np.arange(N*Ngroup) # np.random.shuffle(foo) # x = x0[foo] # y = y0[foo] # z = z0[foo] # r = np.sqrt(x**2 + y**2 + z**2) # theta = np.degrees(np.arccos(z/r)) # phi = np.degrees(np.arctan2(y,x)) # ra = np.where(phi < 0, phi + 360.0,phi) # dec = 90.0 - theta # group = spheregroup(ra,dec,linklength) # # Reproduce IDL results # ra = test_data[0, :] dec = test_data[1, :] expected_ingroup = test_data[2, :].astype(np.int64) expected_multgroup = test_data[3, :].astype(np.int64) expected_firstgroup = test_data[4, :].astype(np.int64) expected_nextgroup = test_data[5, :].astype(np.int64) group = spheregroup(ra, dec, linklength) assert (group[0] == expected_ingroup).all() assert (group[1] == expected_multgroup).all() assert (group[2] == expected_firstgroup).all() assert (group[3] == expected_nextgroup).all() # # Exceptions # with raises(PydlutilsException): group = spheregroup(np.array([137.0]), np.array([55.0]), linklength) # # warnings # with catch_warnings(PydlutilsUserWarning) as w: group = spheregroup(ra, dec, linklength, chunksize=linklength) # w = recwarn.pop(PydlutilsUserWarning) assert "chunksize changed to" in str(w[0].message)
def test_circle_cap(self): with raises(ValueError): x, cm = mng.circle_cap(90.0, np.array([[1.0, 2.0, 3.0, 4.0], ])) xin = np.array([[0.0, 0.0, 1.0], ]) x, cm = mng.circle_cap(90.0, xin) assert np.allclose(x, xin) assert np.allclose(cm, 1.0) radec = np.array([[0.0, 90.0], ]) x, cm = mng.circle_cap(90.0, radec) assert np.allclose(x, xin) assert np.allclose(cm, 1.0) x, cm = mng.circle_cap(np.float32(90.0), radec) assert np.allclose(x, xin) assert np.allclose(cm, 1.0) x, cm = mng.circle_cap(np.array([90.0, ]), radec) assert np.allclose(x, xin) assert np.allclose(cm, np.array([1.0, ])) with raises(ValueError): x, cm = mng.circle_cap(np.array([90.0, 90.0]), radec)
def test_read_mangle_polygons(self): with raises(PydlutilsException): poly = read_mangle_polygons(self.bad_ply) poly = read_mangle_polygons(self.poly_ply) assert len(poly.header) == 3 assert poly.header[0] == 'pixelization 6s' assert len(poly) == 4 assert np.allclose( poly[0].x[0, :], np.array([0.0436193873653360, 0.9990482215818578, 0.0])) assert poly[3].ncaps == 3
def test_read_ds_cooling(self): with raises(ValueError): logT, loglambda = read_ds_cooling('m-99.cie') logT, logL = read_ds_cooling('m-15.cie') assert np.allclose(logT[0:5], np.array([4.0, 4.05, 4.1, 4.15, 4.2])) assert np.allclose(logL[0:5], np.array([-26.0, -24.66, -23.52, -22.62, -22.11])) logT = np.array([4.025, 4.125, 4.225, 4.325]) logT2, logL = read_ds_cooling('m-15.cie', logT) assert np.allclose(logT2, logT) assert np.allclose(logL, np.array([-25.33, -23.07, -22.04, -22.06]))
def test_read_mangle_polygons(self): with raises(PydlutilsException): poly = read_mangle_polygons(self.bad_ply) poly = read_mangle_polygons(self.poly_ply) assert len(poly.header) == 3 assert poly.header[0] == 'pixelization 6s' assert len(poly) == 4 assert np.allclose(poly[0].x[0, :], np.array([0.0436193873653360, 0.9990482215818578, 0.0])) assert poly[3].ncaps == 3
def test_ManglePolygon(self): with raises(ValueError): poly = ManglePolygon(weight=1.0) with raises(ValueError): poly = ManglePolygon() x = np.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 1.0]]) cm = np.array([1.0, 1.0, 1.0]) poly = ManglePolygon(x=x, cm=cm, str=np.pi / 2.0) assert poly.ncaps == 3 assert poly.weight == 1.0 assert poly.use_caps == (1 << 3) - 1 poly = ManglePolygon(x=x, cm=cm, weight=0.5) assert poly.weight == 0.5 poly = ManglePolygon(x=x, cm=cm, pixel=20) assert poly.pixel == 20 poly = ManglePolygon(x=x, cm=cm, use_caps=3, str=np.pi / 2.0) assert poly.use_caps == 3 poly2 = poly.copy() assert poly2.use_caps == poly.use_caps assert (poly2.cm == poly.cm).all() assert np.allclose(poly2.str, np.pi / 2.0)
def test_djs_maskinterp(self): y = np.array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=np.float64) mask = np.array([0, 1, 0]) with raises(ValueError): yi = djs_maskinterp(y, mask) mask = np.array([0, 1, 0, 0, 0]) x = np.array([0.0, 0.5, 1.0], dtype=y.dtype) with raises(ValueError): yi = djs_maskinterp(y, mask, xval=x) # 1-D case yi = djs_maskinterp(y, mask) assert np.allclose(y, yi) # 2-D case x = np.array([0.0, 0.5, 1.0, 1.5, 2.0], dtype=y.dtype) x = np.vstack((x, x, x)) y = np.vstack((y, y, y)) mask = np.vstack((mask, mask, mask)) with raises(ValueError): yi = djs_maskinterp(y, mask) with raises(ValueError): yi = djs_maskinterp(y, mask, axis=-1) with raises(ValueError): yi = djs_maskinterp(y, mask, axis=2) yi = djs_maskinterp(y, mask, axis=0) assert np.allclose(y, yi) yi = djs_maskinterp(y, mask, axis=0, xval=x) assert np.allclose(y, yi) mask[:, 1] = 0 mask[1, :] = 1 yi = djs_maskinterp(y, mask, axis=1) assert np.allclose(y, yi) yi = djs_maskinterp(y, mask, axis=1, xval=x) assert np.allclose(y, yi) # 3-D case x = np.dstack((x, x, x, x, x, x, x)) y = np.dstack((y, y, y, y, y, y, y)) mask = np.dstack((mask, mask, mask, mask, mask, mask, mask)) mask[:, :, :] = 0 mask[:, :, 5] = 1 yi = djs_maskinterp(y, mask, axis=0) assert np.allclose(y, yi) yi = djs_maskinterp(y, mask, axis=0, xval=x) assert np.allclose(y, yi) mask[:, :, :] = 0 mask[:, 3, :] = 1 yi = djs_maskinterp(y, mask, axis=1) assert np.allclose(y, yi) yi = djs_maskinterp(y, mask, axis=1, xval=x) assert np.allclose(y, yi) mask[:, :, :] = 0 mask[1, :, :] = 1 yi = djs_maskinterp(y, mask, axis=2) assert np.allclose(y, yi) yi = djs_maskinterp(y, mask, axis=2, xval=x) assert np.allclose(y, yi) # 4-D case y = np.random.random((2, 2, 2, 2)) with raises(ValueError): yi = djs_maskinterp(y, (y > 0.5), axis=0)
def test_sdss_flagexist(): from os.path import dirname, join from astropy.tests.helper import raises import pydl.pydlutils.sdss from ..set_maskbits import set_maskbits pydl.pydlutils.sdss.maskbits = set_maskbits(maskbits_file=join(dirname(__file__),'t','testMaskbits.par')) from .. import sdss_flagname names = sdss_flagname('ANCILLARY_TARGET1',2310346608843161600) assert tuple(names) == ('BRIGHTGAL', 'BLAZGX', 'ELG') names = sdss_flagname('ANCILLARY_TARGET1',2310346608843161600,concat=True) assert names == 'BRIGHTGAL BLAZGX ELG' with raises(KeyError): names = sdss_flagname('ABADMASK',123456789)
def test_write_ndarray_to_yanny_exceptions(self): """Make sure certain execptions are raised by the write_ndarray_to_yanny function. """ test_data = self.test_data['tempfile1.par'] table = test_data['structures']['MYSTRUCT0'] dt = self.json2dtype(table['dtype']) mystruct = np.zeros((table['size'], ), dtype=dt) for col in table['dtype']: mystruct[col[0]] = np.array(table['data'][col[0]], dtype=col[1]) enums = {'new_flag': test_data['enums']['new_flag']} with raises(PydlutilsException): par = write_ndarray_to_yanny(self.data('test.par'), mystruct, structnames='magnitudes', enums=enums) with raises(PydlutilsException): par = write_ndarray_to_yanny(self.temp('tempfile3.par'), mystruct, structnames=('magnitudes', 'my_status'), enums=enums)
def test_unwrap_specobjid(self): s = 4565636362342690816 d = unwrap_specobjid(np.array([s], dtype=np.uint64)) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 'v5_7_0' assert d.line == 0 d = unwrap_specobjid(np.array(['4565636362342690816'])) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 'v5_7_0' assert d.line == 0 si = 4565636362342690816 + 137 d = unwrap_specobjid(np.array([si], dtype=np.uint64)) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 'v5_7_0' assert d.line == 137 d = unwrap_specobjid(np.array([si], dtype=np.uint64), specLineIndex=True) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 'v5_7_0' assert d.index == 137 d = unwrap_specobjid(np.array([si], dtype=np.uint64), run2d_integer=True) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 700 assert d.line == 137 # # Because 5-digit plate IDs... # s5 = 11258999466550599680 d = unwrap_specobjid(np.array([s5], dtype=np.uint64)) assert d.plate == 10000 assert d.mjd == 57346 assert d.fiber == 1 assert d.run2d == 'v5_10_0' assert d.line == 0 # # Exceptions # with raises(ValueError): d = unwrap_specobjid(np.array([4565636362342690816], dtype=np.int64))
def test_unwrap_specobjid(self): s = 4565636362342690816 d = unwrap_specobjid(np.array([s], dtype=np.uint64)) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 'v5_7_0' assert d.line == 0 d = unwrap_specobjid(np.array(['4565636362342690816'])) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 'v5_7_0' assert d.line == 0 si = 4565636362342690816 + 137 d = unwrap_specobjid(np.array([si], dtype=np.uint64)) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 'v5_7_0' assert d.line == 137 d = unwrap_specobjid(np.array([si], dtype=np.uint64), specLineIndex=True) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 'v5_7_0' assert d.index == 137 d = unwrap_specobjid(np.array([si], dtype=np.uint64), run2d_integer=True) assert d.plate == 4055 assert d.mjd == 55359 assert d.fiber == 408 assert d.run2d == 700 assert d.line == 137 # # Because 5-digit plate IDs... # s5 = 11258999466550599680 d = unwrap_specobjid(np.array([s5], dtype=np.uint64)) assert d.plate == 10000 assert d.mjd == 57346 assert d.fiber == 1 assert d.run2d == 'v5_10_0' assert d.line == 0 # # Exceptions # with raises(ValueError): d = unwrap_specobjid( np.array([4565636362342690816], dtype=np.int64))
def test_ManglePolygon(self): with raises(ValueError): poly = ManglePolygon(weight=1.0) with raises(ValueError): poly = ManglePolygon() x = np.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 1.0]]) cm = np.array([1.0, 1.0, 1.0]) poly = ManglePolygon(x=x, cm=cm, str=np.pi/2.0) assert poly.ncaps == 3 assert poly.weight == 1.0 assert poly.use_caps == (1 << 3) - 1 poly = ManglePolygon(x=x, cm=cm, weight=0.5) assert poly.weight == 0.5 poly = ManglePolygon(x=x, cm=cm, pixel=20) assert poly.pixel == 20 poly = ManglePolygon(x=x, cm=cm, use_caps=3, str=np.pi/2.0) assert poly.use_caps == 3 poly2 = poly.copy() assert poly2.use_caps == poly.use_caps assert (poly2.cm == poly.cm).all() assert np.allclose(poly2.str, np.pi/2.0)
def test_compose_prefix_unit(): x = u.m.compose(units=(u.m, )) assert x[0].bases[0] is u.m assert x[0].scale == 1.0 x = u.m.compose(units=[u.km], include_prefix_units=True) assert x[0].bases[0] is u.km assert x[0].scale == 0.001 x = u.m.compose(units=[u.km]) assert x[0].bases[0] is u.km assert x[0].scale == 0.001 x = (u.km / u.s).compose(units=(u.pc, u.Myr)) assert x[0].bases == [u.pc, u.Myr] assert_allclose(x[0].scale, 1.0227121650537077) with raises(u.UnitsError): (u.km / u.s).compose(units=(u.pc, u.Myr), include_prefix_units=False)
def test_gcirc(self): np.random.seed(137) # # Start in radians # offset = 5.0e-6 # approx 1 arcsec ra1 = 2.0 * np.pi * np.random.rand(100) dec1 = np.pi / 2.0 - np.arccos(2.0 * np.random.rand(100) - 1.0) ra2 = ra1 + offset ra2 = np.where((ra2 > 2.0 * np.pi), ra2 - 2.0 * np.pi, ra2) dec2 = np.where((dec1 > 0), dec1 - offset, dec1 + offset) deldec2 = (dec2 - dec1) / 2.0 delra2 = (ra2 - ra1) / 2.0 sindis = np.sqrt( np.sin(deldec2) * np.sin(deldec2) + np.cos(dec1) * np.cos(dec2) * np.sin(delra2) * np.sin(delra2)) dis = 2.0 * np.arcsin(sindis) # # units = 0 # d0 = gcirc(ra1, dec1, ra2, dec2, units=0) assert np.allclose(d0, dis) # # units = 2 # d0 = gcirc(np.rad2deg(ra1) / 15.0, np.rad2deg(dec1), np.rad2deg(ra2) / 15.0, np.rad2deg(dec2), units=1) assert np.allclose(d0, np.rad2deg(dis) * 3600.0) # # units = 2 # d0 = gcirc(np.rad2deg(ra1), np.rad2deg(dec1), np.rad2deg(ra2), np.rad2deg(dec2), units=2) assert np.allclose(d0, np.rad2deg(dis) * 3600.0) # # Units = whatever # with raises(ValueError): d0 = gcirc(ra1, dec1, ra2, dec2, units=5)
def test_median(self): odd_data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], dtype=np.float32) even_data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=np.float32) assert median(odd_data) == 7 assert median(odd_data, even=True) == 7 assert median(even_data) == 7 assert median(even_data, even=True) == 6.5 assert (median(odd_data, 3) == odd_data).all() with raises(ValueError): foo = median(np.ones((9, 9, 9)), 3) odd_data2 = np.vstack( (odd_data, odd_data, odd_data, odd_data, odd_data)) assert (median(odd_data2, 3) == odd_data2).all() assert (median(odd_data2, axis=0) == odd_data).all() assert (median(odd_data2, axis=1) == 7 * np.ones( (odd_data2.shape[0], ), dtype=odd_data2.dtype)).all()
def test_sdss_astrombad_raises(self): with raises(ValueError): foo = sdss_astrombad(77, 32, 20) with raises(ValueError): foo = sdss_astrombad(-1, 1, 20) with raises(ValueError): foo = sdss_astrombad(2**17, 1, 20) with raises(ValueError): foo = sdss_astrombad(-2, 1, 20) with raises(ValueError): foo = sdss_astrombad(251, 1, 2**16) with raises(ValueError): foo = sdss_astrombad(np.array([77, 85, 251]), np.array([1]), np.array([20, 15, 151])) with raises(ValueError): foo = sdss_astrombad(np.array([77, 85, 251]), np.array([1, 2, 3]), np.array([20]))
def test_fpoly(self): x = np.array([-1, -0.5, 0, 0.5, 1], dtype='d') # # Test order # with raises(ValueError): f = fpoly(x, 0) # # m = 1 # f = fpoly(x, 1) assert (f == np.ones((1, x.size), dtype='d')).all() # # m = 2 # f = fpoly(x, 2) foo = np.ones((2, x.size), dtype='d') foo[1, :] = x assert np.allclose(f, foo) # # m = 3 # f = fpoly(x, 3) foo = np.ones((3, x.size), dtype='d') foo[1, :] = x foo[2, :] = x**2 assert np.allclose(f, foo) # # m = 4 # f = fpoly(x, 4) foo = np.ones((4, x.size), dtype='d') foo[1, :] = x foo[2, :] = x**2 foo[3, :] = x**3 assert np.allclose(f, foo) # # random float # f = fpoly(2.88, 3) assert np.allclose(f, np.array([[1.00], [2.88], [8.29440]]))
def test_flegendre(self): x = np.array([-1, -0.5, 0, 0.5, 1], dtype='d') # # Test order # with raises(ValueError): f = flegendre(x, 0) # # m = 1 # f = flegendre(x, 1) assert (f == np.ones((1, x.size), dtype='d')).all() # # m = 2 # f = flegendre(x, 2) foo = np.ones((2, x.size), dtype='d') foo[1, :] = x assert np.allclose(f, foo) # # m = 3 # f = flegendre(x, 3) foo = np.ones((3, x.size), dtype='d') foo[1, :] = x foo[2, :] = 0.5 * (3.0 * x**2 - 1.0) assert np.allclose(f, foo) # # m = 4 # f = flegendre(x, 4) foo = np.ones((4, x.size), dtype='d') foo[1, :] = x foo[2, :] = 0.5 * (3.0 * x**2 - 1.0) foo[3, :] = 0.5 * (5.0 * x**3 - 3.0 * x) assert np.allclose(f, foo) # # random float # f = flegendre(2.88, 3) assert np.allclose(f, np.array([[1.00], [2.88], [11.9416]]))