def test_compare_specter(self): #- gpu_specter wavelengths = np.arange(5990.0, 6060.0, 1.0) spots, corners = get_spots(0, 25, wavelengths, self.psfdata) #- Load specter PSF psf = specter.psf.load_psf(self.psffile) #- Compare projection matrix for a few combos of spectra & waves for ispec, nspec, iwave, nwave in ( (0, 5, 10, 25), (10, 5, 30, 25), (7, 3, 20, 12), (0, 6, 0, 70), ): A4, xyrange = projection_matrix( ispec=ispec, nspec=nspec, iwave=iwave, nwave=nwave, spots=spots, corners=corners) A2 = A4.reshape(A4.shape[0]*A4.shape[1], A4.shape[2]*A4.shape[3]) #- Specter A = psf.projection_matrix( (ispec, ispec+nspec), wavelengths[iwave:iwave+nwave], xyrange).toarray() self.assertEqual(A.shape, A2.shape) self.assertTrue(np.allclose(A, A2))
def test_compare_gpu(self): wavelengths = np.arange(5990.0, 6060.0, 1.0) spots, corners = get_spots(0, 25, wavelengths, self.psfdata) spots_gpu = cp.asarray(spots) corners_gpu = [cp.asarray(c) for c in corners] #- Compare projection matrix for a few combos of spectra & waves for ispec, nspec, iwave, nwave in ( (0, 5, 10, 25), (10, 5, 30, 25), (7, 3, 20, 12), (0, 6, 0, 70), ): #- cpu projection matrix A4, xyrange = projection_matrix( ispec=ispec, nspec=nspec, iwave=iwave, nwave=nwave, spots=spots, corners=corners) #- gpu projection matrix A4_gpu, xyrange_gpu = gpu_projection_matrix( ispec=ispec, nspec=nspec, iwave=iwave, nwave=nwave, spots=spots_gpu, corners=corners_gpu) self.assertEqual(xyrange, xyrange_gpu) self.assertEqual(A4.shape, A4_gpu.shape) self.assertTrue(cp.allclose(A4, A4_gpu))
def test_compare_gpu(self): for ispec in np.linspace(0, 499, 20).astype(int): spots, corners = get_spots(ispec, 1, self.wavelengths, self.psfdata) xc, yc = corners spots_gpu, corners_gpu = gpu_get_spots(ispec, 1, self.wavelengths, self.psfdata) xc_gpu, yc_gpu = corners_gpu self.assertTrue(cp.allclose(xc, xc_gpu)) self.assertTrue(cp.allclose(yc, yc_gpu)) self.assertEqual(spots.shape, spots_gpu.shape) self.assertTrue(cp.allclose(spots, spots_gpu))
def test_basics(self): wavelengths = np.arange(6000.0, 6050.0, 1.0) spots, corners = get_spots(0, 25, wavelengths, self.psfdata) #- Projection matrix for a subset of spots and wavelenghts A4, (xmin, xmax, ymin, ymax) = projection_matrix( ispec=0, nspec=5, iwave=0, nwave=25, spots=spots, corners=corners) self.assertEqual(A4.shape[2:4], (5,25)) self.assertEqual(A4.shape[0:2], (ymax-ymin, xmax-xmin)) #- Another subset using same spots, but not starting from (0,0) A4, (xmin, xmax, ymin, ymax) = projection_matrix( ispec=10, nspec=5, iwave=20, nwave=25, spots=spots, corners=corners) self.assertEqual(A4.shape[2:4], (5,25)) self.assertEqual(A4.shape[0:2], (ymax-ymin, xmax-xmin))
def test_compare_specter(self): #- specter version psf = specter.psf.load_psf(self.psffile) for ispec in np.linspace(0, 499, 20).astype(int): spots, corners, psfparams = get_spots(ispec, 1, self.wavelengths, self.psfdata) xc, yc = corners ny, nx = spots.shape[2:4] for iwave, w in enumerate(self.wavelengths): xslice, yslice, pix = psf.xypix(ispec, w) self.assertEqual(pix.shape, (ny, nx)) self.assertEqual(xc[0, iwave], xslice.start) self.assertEqual(yc[0, iwave], yslice.start) self.assertTrue(np.allclose(spots[0, iwave], pix))
def setUpClass(cls): cls.psffile = find_test_file('psf') cls.psfdata = read_psf(cls.psffile) cls.wavelengths = np.arange(6000, 6050, 1) nwave = len(cls.wavelengths) nspec = 5 cls.psferr = cls.psfdata['PSF'].meta['PSFERR'] cls.spots, cls.corners, psfparams = get_spots(0, nspec, cls.wavelengths, cls.psfdata) cls.A4, cls.xyrange = projection_matrix(0, nspec, 0, nwave, cls.spots, cls.corners) phot = np.zeros((nspec, nwave)) phot[0] = 100 phot[1] = 5 * np.arange(nwave) phot[2] = 50 phot[4] = 100 * (1 + np.sin(np.arange(nwave) / 10.)) phot[0, 10] += 500 phot[1, 15] += 200 phot[2, 20] += 300 phot[3, 25] += 1000 phot[4, 30] += 600 cls.phot = phot xmin, xmax, ymin, ymax = cls.xyrange ny = ymax - ymin nx = xmax - xmin A2 = cls.A4.reshape(ny * nx, nspec * nwave) cls.img = A2.dot(cls.phot.ravel()).reshape(ny, nx) cls.readnoise = 3.0 cls.noisyimg = np.random.normal(loc=0.0, scale=cls.readnoise, size=(ny, nx)) cls.noisyimg += np.random.poisson(cls.img) #- for test, cheat by using noiseless img instead of noisyimg to estimate variance cls.imgivar = 1.0 / (cls.img + cls.readnoise**2)
def test_basics(self): nspec = 50 spots, corners, psfparams = get_spots(0, nspec, self.wavelengths, self.psfdata) cx, cy = corners #- Dimensions _nspec, nwave, ny, nx = spots.shape self.assertEqual(_nspec, nspec) self.assertEqual(nwave, len(self.wavelengths)) self.assertEqual(cx.shape, (nspec, nwave)) self.assertEqual(cy.shape, (nspec, nwave)) #- Spots should have an odd number of pixels in each dimension so #- that there is a well defined central pixel self.assertEqual(ny % 2, 1) self.assertEqual(nx % 2, 1) #- positivity and normalization self.assertTrue(np.all(spots >= 0.0)) norm = spots.sum(axis=(2, 3)) self.assertTrue(np.allclose(norm, 1.0)) #- The PSF centroid should be within that central pixel #- Note: X,Y relative to pixel center, not edge dx = self.psfparams['X'][0:nspec, :] - cx - nx // 2 dy = self.psfparams['Y'][0:nspec, :] - cy - ny // 2 self.assertTrue(np.all((-0.5 <= dx) & (dx < 0.5))) self.assertTrue(np.all((-0.5 <= dy) & (dy < 0.5))) #- The actual centroid of the spot should be within that pixel #- Allow some buffer for asymmetric tails for ispec in range(nspec): for iwave in range(len(self.wavelengths)): yy, xx = center_of_mass(spots[ispec, iwave]) dx = xx - nx // 2 dy = yy - ny // 2 msg = f'ispec={ispec}, iwave={iwave}' self.assertTrue((-0.7 <= dx) and (dx < 0.7), msg + f' dx={dx}') self.assertTrue((-0.7 <= dy) and (dy < 0.7), msg + f' dy={dy}')
def test_compare_gpu(self): for ispec in np.linspace(0, 499, 20).astype(int): spots, corners, psfparams = get_spots(ispec, 1, self.wavelengths, self.psfdata) xc, yc = corners spots_gpu, corners_gpu, psfparams_gpu = gpu_get_spots( ispec, 1, self.wavelengths, self.psfdata) xc_gpu, yc_gpu = corners_gpu # compare corners self.assertTrue(np.array_equal(xc, cp.asnumpy(xc_gpu))) self.assertTrue(np.array_equal(yc, cp.asnumpy(yc_gpu))) # compare spots self.assertEqual(spots.shape, spots_gpu.shape) eps_double = np.finfo(np.float64).eps self.assertTrue( np.allclose(spots, cp.asnumpy(spots_gpu), rtol=eps_double, atol=eps_double))