def test_shift(): """Test the translation of a Box profile against a known result. """ import time t1 = time.time() mySBP = galsim.SBBox(xw=0.2, yw=0.2, flux=1) mySBP.applyShift(0.2, -0.2) savedImg = galsim.fits.read(os.path.join(imgdir, "box_shift.fits")) myImg = galsim.ImageF(savedImg.bounds, scale=0.2) mySBP.draw(myImg.view()) printval(myImg, savedImg) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg="Shifted box profile disagrees with expected result") # Repeat with the GSObject version of this: pixel = galsim.Pixel(xw=0.2, yw=0.2) pixel.applyShift(0.2, -0.2) pixel.draw(myImg,dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg="Using GSObject applyShift disagrees with expected result") # Check with default_params pixel = galsim.Pixel(xw=0.2, yw=0.2, gsparams=default_params) pixel.applyShift(0.2, -0.2) pixel.draw(myImg,dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg="Using GSObject applyShift with default_params disagrees with expected result") pixel = galsim.Pixel(xw=0.2, yw=0.2, gsparams=galsim.GSParams()) pixel.applyShift(0.2, -0.2) pixel.draw(myImg,dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg="Using GSObject applyShift with GSParams() disagrees with expected result") # Test photon shooting. do_shoot(pixel,myImg,"shifted Box") # Test kvalues. # Testing a shifted box requires a ridiculously large fft. What we really care about # testing though is the accuracy of the applyShift function. So just shift a Gaussian here. gauss = galsim.Gaussian(sigma=2.3) gauss.applyShift(0.2,-0.2) do_kvalue(gauss, "shifted Gaussian") t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)
def test_realspace_shearconvolve(): """Test the real-space convolution of a sheared Gaussian and a Box SBProfile against a known result. """ import time t1 = time.time() psf = galsim.SBGaussian(flux=1, sigma=1) e1 = 0.04 e2 = 0.0 myShear = galsim.Shear(e1=e1, e2=e2) psf.applyShear(myShear._shear) pix = galsim.SBBox(xw=0.2, yw=0.2, flux=1.) conv = galsim.SBConvolve([psf, pix], real_space=True) saved_img = galsim.fits.read( os.path.join(imgdir, "gauss_smallshear_convolve_box.fits")) img = galsim.ImageF(saved_img.bounds, scale=0.2) conv.draw(img.view()) printval(img, saved_img) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Sheared Gaussian convolved with Box SBProfile disagrees with expected result" ) # Repeat with the GSObject version of this: psf = galsim.Gaussian(flux=1, sigma=1) psf.applyShear(e1=e1, e2=e2) pixel = galsim.Pixel(xw=0.2, yw=0.2, flux=1.) conv = galsim.Convolve([psf, pixel], real_space=True) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) disagrees with expected result") # Check with default_params conv = galsim.Convolve([psf, pixel], real_space=True, gsparams=default_params) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) with default_params disagrees with " "expected result") conv = galsim.Convolve([psf, pixel], real_space=True, gsparams=galsim.GSParams()) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) with GSParams() disagrees with " "expected result") # Other ways to do the convolution: conv = galsim.Convolve(psf, pixel, real_space=True) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve(psf,pixel) disagrees with expected result") # The real-space convolution algorithm is not (trivially) independent of the order of # the two things being convolved. So check the opposite order. conv = galsim.Convolve([pixel, psf], real_space=True) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve([pixel,psf]) disagrees with expected result") t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)
def test_convolve(): """Test the convolution of a Moffat and a Box SBProfile against a known result. """ import time t1 = time.time() # Code was formerly: # mySBP = galsim.SBMoffat(beta=1.5, truncationFWHM=4, flux=1, half_light_radius=1) # # ...but this is no longer quite so simple since we changed the handling of trunc to be in # physical units. However, the same profile can be constructed using # fwhm=1.0927449310213702, # as calculated by interval bisection in devutils/external/calculate_moffat_radii.py fwhm_backwards_compatible = 1.0927449310213702 mySBP = galsim.SBMoffat(beta=1.5, fwhm=fwhm_backwards_compatible, trunc=4 * fwhm_backwards_compatible, flux=1) mySBP2 = galsim.SBBox(xw=0.2, yw=0.2, flux=1.) myConv = galsim.SBConvolve([mySBP, mySBP2]) # Using an exact Maple calculation for the comparison. Only accurate to 4 decimal places. savedImg = galsim.fits.read(os.path.join(imgdir, "moffat_pixel.fits")) myImg = galsim.ImageF(savedImg.bounds, scale=0.2) myConv.draw(myImg.view()) printval(myImg, savedImg) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 4, err_msg= "Moffat convolved with Box SBProfile disagrees with expected result") # Repeat with the GSObject version of this: psf = galsim.Moffat(beta=1.5, fwhm=fwhm_backwards_compatible, trunc=4 * fwhm_backwards_compatible, flux=1) pixel = galsim.Pixel(xw=0.2, yw=0.2, flux=1.) # Note: Since both of these have hard edges, GalSim wants to do this with real_space=True. # Here we are intentionally tesing the Fourier convolution, so we want to suppress the # warning that GalSim emits. import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") # We'll do the real space convolution below conv = galsim.Convolve([psf, pixel], real_space=False) conv.draw(myImg, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 4, err_msg= "Using GSObject Convolve([psf,pixel]) disagrees with expected result" ) # Other ways to do the convolution: conv = galsim.Convolve(psf, pixel, real_space=False) conv.draw(myImg, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 4, err_msg= "Using GSObject Convolve(psf,pixel) disagrees with expected result" ) # Check with default_params conv = galsim.Convolve([psf, pixel], real_space=False, gsparams=default_params) conv.draw(myImg, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 4, err_msg= "Using GSObject Convolve([psf,pixel]) with default_params disagrees with" "expected result") conv = galsim.Convolve([psf, pixel], real_space=False, gsparams=galsim.GSParams()) conv.draw(myImg, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 4, err_msg= "Using GSObject Convolve([psf,pixel]) with GSParams() disagrees with" "expected result") # Test photon shooting. do_shoot(conv, myImg, "Moffat * Pixel") t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)
def test_realspace_distorted_convolve(): """ The same as above, but both the Moffat and the Box are sheared, rotated and shifted to stress test the code that deals with this for real-space convolutions that wouldn't be tested otherwise. """ import time t1 = time.time() fwhm_backwards_compatible = 1.0927449310213702 psf = galsim.SBMoffat(beta=1.5, half_light_radius=1, trunc=4 * fwhm_backwards_compatible, flux=1) #psf = galsim.SBMoffat(beta=1.5, fwhm=fwhm_backwards_compatible, #trunc=4*fwhm_backwards_compatible, flux=1) psf.applyShear(galsim.Shear(g1=0.11, g2=0.17)._shear) psf.applyRotation(13 * galsim.degrees) pixel = galsim.SBBox(xw=0.2, yw=0.2, flux=1.) pixel.applyShear(galsim.Shear(g1=0.2, g2=0.0)._shear) pixel.applyRotation(80 * galsim.degrees) pixel.applyShift(0.13, 0.27) conv = galsim.SBConvolve([psf, pixel], real_space=True) # Note: Using an image created from Maple "exact" calculations. saved_img = galsim.fits.read( os.path.join(imgdir, "moffat_pixel_distorted.fits")) img = galsim.ImageF(saved_img.bounds, scale=0.2) conv.draw(img.view()) printval(img, saved_img) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "distorted Moffat convolved with distorted Box disagrees with expected result" ) # Repeat with the GSObject version of this: psf = galsim.Moffat(beta=1.5, half_light_radius=1, trunc=4 * fwhm_backwards_compatible, flux=1) #psf = galsim.Moffat(beta=1.5, fwhm=fwhm_backwards_compatible, #trunc=4*fwhm_backwards_compatible, flux=1) psf.applyShear(g1=0.11, g2=0.17) psf.applyRotation(13 * galsim.degrees) pixel = galsim.Pixel(xw=0.2, yw=0.2, flux=1.) pixel.applyShear(g1=0.2, g2=0.0) pixel.applyRotation(80 * galsim.degrees) pixel.applyShift(0.13, 0.27) # NB: real-space is chosen automatically conv = galsim.Convolve([psf, pixel]) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using Convolve([psf,pixel]) (distorted) disagrees with expected result" ) # Check with default_params conv = galsim.Convolve([psf, pixel], gsparams=default_params) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using Convolve([psf,pixel]) (distorted) with default_params disagrees with " "expected result") conv = galsim.Convolve([psf, pixel], gsparams=galsim.GSParams()) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using Convolve([psf,pixel]) (distorted) with GSParams() disagrees with " "expected result") # Other ways to do the convolution: conv = galsim.Convolve(psf, pixel) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using Convolve(psf,pixel) (distorted) disagrees with expected result") # The real-space convolution algorithm is not (trivially) independent of the order of # the two things being convolved. So check the opposite order. conv = galsim.Convolve([pixel, psf]) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using Convolve([pixel,psf]) (distorted) disagrees with expected result" ) t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)
def test_realspace_convolve(): """Test the real-space convolution of a Moffat and a Box SBProfile against a known result. """ import time t1 = time.time() # Code was formerly: # mySBP = galsim.SBMoffat(beta=1.5, truncationFWHM=4, flux=1, half_light_radius=1) # # ...but this is no longer quite so simple since we changed the handling of trunc to be in # physical units. However, the same profile can be constructed using # fwhm=1.0927449310213702, # as calculated by interval bisection in devutils/external/calculate_moffat_radii.py fwhm_backwards_compatible = 1.0927449310213702 #psf = galsim.SBMoffat(beta=1.5, fwhm=fwhm_backwards_compatible, #trunc=4*fwhm_backwards_compatible, flux=1) psf = galsim.SBMoffat(beta=1.5, half_light_radius=1, trunc=4 * fwhm_backwards_compatible, flux=1) pixel = galsim.SBBox(xw=0.2, yw=0.2, flux=1.) conv = galsim.SBConvolve([psf, pixel], real_space=True) # Note: Using an image created from Maple "exact" calculations. saved_img = galsim.fits.read(os.path.join(imgdir, "moffat_pixel.fits")) img = galsim.ImageF(saved_img.bounds, scale=0.2) conv.draw(img.view()) printval(img, saved_img) arg = abs(saved_img.array - img.array).argmax() np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Moffat convolved with Box SBProfile disagrees with expected result") # Repeat with the GSObject version of this: psf = galsim.Moffat(beta=1.5, half_light_radius=1, trunc=4 * fwhm_backwards_compatible, flux=1) #psf = galsim.Moffat(beta=1.5, fwhm=fwhm_backwards_compatible, #trunc=4*fwhm_backwards_compatible, flux=1) pixel = galsim.Pixel(xw=0.2, yw=0.2, flux=1.) conv = galsim.Convolve([psf, pixel], real_space=True) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) disagrees with expected result") # Check with default_params conv = galsim.Convolve([psf, pixel], real_space=True, gsparams=default_params) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) with default_params disagrees with " "expected result") conv = galsim.Convolve([psf, pixel], real_space=True, gsparams=galsim.GSParams()) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) with GSParams() disagrees with " "expected result") # Other ways to do the convolution: conv = galsim.Convolve(psf, pixel, real_space=True) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve(psf,pixel) disagrees with expected result") # The real-space convolution algorithm is not (trivially) independent of the order of # the two things being convolved. So check the opposite order. conv = galsim.Convolve([pixel, psf], real_space=True) conv.draw(img, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( img.array, saved_img.array, 5, err_msg= "Using GSObject Convolve([pixel,psf]) disagrees with expected result") # Test kvalues do_kvalue(conv, "Truncated Moffat convolved with Box") t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)
def test_shearconvolve(): """Test the convolution of a sheared Gaussian and a Box SBProfile against a known result. """ import time t1 = time.time() e1 = 0.04 e2 = 0.0 myShear = galsim.Shear(e1=e1, e2=e2) # test at SBProfile level using applyShear mySBP = galsim.SBGaussian(flux=1, sigma=1) mySBP.applyShear(myShear._shear) mySBP2 = galsim.SBBox(xw=0.2, yw=0.2, flux=1.) myConv = galsim.SBConvolve([mySBP, mySBP2]) savedImg = galsim.fits.read( os.path.join(imgdir, "gauss_smallshear_convolve_box.fits")) myImg = galsim.ImageF(savedImg.bounds, scale=0.2) myConv.draw(myImg.view()) printval(myImg, savedImg) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg= "Sheared Gaussian convolved with Box SBProfile disagrees with expected result" ) # Repeat with the GSObject version of this: psf = galsim.Gaussian(flux=1, sigma=1) psf2 = psf.createSheared(e1=e1, e2=e2) psf.applyShear(e1=e1, e2=e2) pixel = galsim.Pixel(xw=0.2, yw=0.2, flux=1.) conv = galsim.Convolve([psf, pixel]) conv.draw(myImg, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) disagrees with expected result") conv2 = galsim.Convolve([psf2, pixel]) conv2.draw(myImg, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) disagrees with expected result") # Check with default_params conv = galsim.Convolve([psf, pixel], gsparams=default_params) conv.draw(myImg, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) with default_params disagrees with " "expected result") conv = galsim.Convolve([psf, pixel], gsparams=galsim.GSParams()) conv.draw(myImg, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg= "Using GSObject Convolve([psf,pixel]) with GSParams() disagrees with " "expected result") # Other ways to do the convolution: conv = galsim.Convolve(psf, pixel) conv.draw(myImg, dx=0.2, normalization="surface brightness", use_true_center=False) np.testing.assert_array_almost_equal( myImg.array, savedImg.array, 5, err_msg= "Using GSObject Convolve(psf,pixel) disagrees with expected result") # Test photon shooting. do_shoot(conv, myImg, "sheared Gaussian * Pixel") t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)