Example #1
0
    def test_compare_specter(self):
        ny, nx, nspec, nwave = self.A4.shape

        psf = specter.psf.load_psf(self.psffile)
        img = psf.project(self.wavelengths, self.phot, xyrange=self.xyrange)

        # self.assertTrue(np.allclose(self.img, img))

        #- Compare the "signal" decorrelation method
        flux0, ivar0, R0 = specter.extract.ex2d_patch(self.noisyimg,
                                                      self.imgivar,
                                                      psf,
                                                      0,
                                                      nspec,
                                                      self.wavelengths,
                                                      xyrange=self.xyrange,
                                                      ndecorr=False)
        flux1, ivar1, R1 = ex2d_patch(self.noisyimg,
                                      self.imgivar,
                                      self.A4,
                                      decorrelate='signal')

        #- Note that specter is using it's version of the projection matrix
        # A = psf.projection_matrix((0, nspec), self.wavelengths, self.xyrange).toarray()
        # A4 = A.reshape(self.A4.shape)
        # flux1, ivar1, R1 = ex2d_patch(self.noisyimg, self.imgivar, A4, decorrelate='signal')

        self.assertTrue(np.allclose(flux0, flux1))
        self.assertTrue(np.allclose(ivar0, ivar1))
        self.assertTrue(np.allclose(R0, R1))
        #self.assertTrue(np.allclose(np.abs(flux0 - flux1)/np.sqrt(1./ivar0 + 1./ivar1), np.zeros_like(flux0)))

        # Compare the "noise" decorrelation method
        flux0, ivar0, R0 = specter.extract.ex2d_patch(self.noisyimg,
                                                      self.imgivar,
                                                      psf,
                                                      0,
                                                      nspec,
                                                      self.wavelengths,
                                                      xyrange=self.xyrange,
                                                      ndecorr=True)
        flux1, ivar1, R1 = ex2d_patch(self.noisyimg,
                                      self.imgivar,
                                      self.A4,
                                      decorrelate='noise')

        self.assertTrue(np.allclose(flux0, flux1))
        self.assertTrue(np.allclose(ivar0, ivar1))
        self.assertTrue(np.allclose(R0, R1))
Example #2
0
    def test_basics(self):
        ny, nx, nspec, nwave = self.A4.shape

        img = np.random.randn(ny, nx)
        imgivar = np.ones((ny, nx))

        flux, varflux, R = ex2d_patch(img, imgivar, self.A4)

        self.assertEqual(flux.shape, (nspec, nwave))
        self.assertEqual(varflux.shape, (nspec, nwave))
        self.assertEqual(R.shape, (nspec * nwave, nspec * nwave))
Example #3
0
    def test_compare_xp_cpu(self):
        # Compare the "signal" decorrelation method
        flux0, ivar0, R0 = ex2d_patch(self.noisyimg,
                                      self.imgivar,
                                      self.A4,
                                      decorrelate='signal')
        flux1, ivar1, R1 = xp_ex2d_patch(self.noisyimg,
                                         self.imgivar,
                                         self.A4,
                                         decorrelate='signal')

        self.assertTrue(np.allclose(flux0, flux1))
        self.assertTrue(np.allclose(ivar0, ivar1))
        self.assertTrue(np.allclose(R0, R1))
        self.assertTrue(
            np.allclose(
                np.abs(flux0 - flux1) / np.sqrt(1. / ivar0 + 1. / ivar1),
                np.zeros_like(flux0)))

        # Compare the "noise" decorrelation method
        flux0, ivar0, R0 = ex2d_patch(self.noisyimg,
                                      self.imgivar,
                                      self.A4,
                                      decorrelate='noise')
        flux1, ivar1, R1 = xp_ex2d_patch(self.noisyimg,
                                         self.imgivar,
                                         self.A4,
                                         decorrelate='noise')

        self.assertTrue(np.allclose(flux0, flux1))
        self.assertTrue(np.allclose(ivar0, ivar1))
        self.assertTrue(np.allclose(R0, R1))
        self.assertTrue(
            np.allclose(
                np.abs(flux0 - flux1) / np.sqrt(1. / ivar0 + 1. / ivar1),
                np.zeros_like(flux0)))
Example #4
0
    def test_compare_batch_extraction(self):
        from gpu_specter.extract.gpu import _apply_weights, _batch_extraction
        noisyimg_gpu = cp.asarray(self.noisyimg)
        imgivar_gpu = cp.asarray(self.imgivar)
        A4_gpu = cp.asarray(self.A4)

        # Compare the "signal" decorrelation method
        flux0, ivar0, R0 = ex2d_patch(self.noisyimg,
                                      self.imgivar,
                                      self.A4,
                                      decorrelate='signal')

        ny, nx, nspec, nwave = self.A4.shape
        icov, y = _apply_weights(noisyimg_gpu.ravel(),
                                 imgivar_gpu.ravel(),
                                 A4_gpu.reshape(ny * nx, nspec * nwave),
                                 regularize=0)
        flux1_gpu, ivar1_gpu, R1_gpu = _batch_extraction(icov, y, nwave)
        # Rdiags = get_resolution_diags(R, ndiag, nspectot, nwave, wavepad)[specslice[0]]

        flux1 = cp.asnumpy(flux1_gpu.reshape(nspec, nwave))
        ivar1 = cp.asnumpy(ivar1_gpu.reshape(nspec, nwave))
        R1 = cp.asnumpy(R1_gpu.reshape(nspec * nwave, nspec * nwave))

        eps_double = np.finfo(np.float64).eps

        where = np.where(
            ~np.isclose(flux0, flux1, rtol=1e5 * eps_double, atol=0))
        np.testing.assert_allclose(flux0,
                                   flux1,
                                   rtol=1e5 * eps_double,
                                   atol=0,
                                   err_msg=f"where: {where}")
        self.assertTrue(
            np.allclose(ivar0, ivar1, rtol=1e3 * eps_double, atol=0))
        self.assertTrue(
            np.allclose(np.diag(R0),
                        np.diag(R1),
                        rtol=1e2 * eps_double,
                        atol=1e3 * eps_double))
        self.assertTrue(
            np.allclose(
                np.abs(flux0 - flux1) / np.sqrt(1. / ivar0 + 1. / ivar1),
                np.zeros_like(flux0)))
Example #5
0
    def test_compare_xp_gpu(self):
        noisyimg_gpu = cp.asarray(self.noisyimg)
        imgivar_gpu = cp.asarray(self.imgivar)
        A4_gpu = cp.asarray(self.A4)

        # Compare the "signal" decorrelation method
        flux0, ivar0, R0 = ex2d_patch(self.noisyimg,
                                      self.imgivar,
                                      self.A4,
                                      decorrelate='signal')
        flux1_gpu, ivar1_gpu, R1_gpu = xp_ex2d_patch(noisyimg_gpu,
                                                     imgivar_gpu,
                                                     A4_gpu,
                                                     decorrelate='signal')

        flux1 = cp.asnumpy(flux1_gpu)
        ivar1 = cp.asnumpy(ivar1_gpu)
        R1 = cp.asnumpy(R1_gpu)

        eps_double = np.finfo(np.float64).eps

        where = np.where(
            ~np.isclose(flux0, flux1, rtol=1e5 * eps_double, atol=0))
        np.testing.assert_allclose(flux0,
                                   flux1,
                                   rtol=1e5 * eps_double,
                                   atol=0,
                                   err_msg=f"where: {where}")
        self.assertTrue(
            np.allclose(ivar0, ivar1, rtol=1e3 * eps_double, atol=0))
        self.assertTrue(
            np.allclose(np.diag(R0),
                        np.diag(R1),
                        rtol=1e2 * eps_double,
                        atol=1e3 * eps_double))
        self.assertTrue(
            np.allclose(
                np.abs(flux0 - flux1) / np.sqrt(1. / ivar0 + 1. / ivar1),
                np.zeros_like(flux0)))

        # Compare the "noise" decorrelation method
        flux0, ivar0, R0 = ex2d_patch(self.noisyimg,
                                      self.imgivar,
                                      self.A4,
                                      decorrelate='noise')
        flux1_gpu, ivar1_gpu, R1_gpu = xp_ex2d_patch(noisyimg_gpu,
                                                     imgivar_gpu,
                                                     A4_gpu,
                                                     decorrelate='noise')

        flux1 = cp.asnumpy(flux1_gpu)
        ivar1 = cp.asnumpy(ivar1_gpu)
        R1 = cp.asnumpy(R1_gpu)

        self.assertTrue(
            np.allclose(flux0, flux1, rtol=1e5 * eps_double, atol=0))
        self.assertTrue(
            np.allclose(ivar0, ivar1, rtol=1e3 * eps_double, atol=0))
        self.assertTrue(
            np.allclose(np.diag(R0),
                        np.diag(R1),
                        rtol=1e2 * eps_double,
                        atol=0))
        self.assertTrue(
            np.allclose(
                np.abs(flux0 - flux1) / np.sqrt(1. / ivar0 + 1. / ivar1),
                np.zeros_like(flux0)))