def test_real_galaxy_ideal(): """Test accuracy of various calculations with fake Gaussian RealGalaxy vs. ideal expectations""" import time t1 = time.time() # read in faked Gaussian RealGalaxy from file rgc = galsim.RealGalaxyCatalog(catalog_file, image_dir) rg = galsim.RealGalaxy(rgc, index = ind_fake) ## for the generation of the ideal right answer, we need to add the intrinsic shape of the ## galaxy and the lensing shear using the rule for addition of distortions which is ugly, but oh ## well: (d1, d2) = galsim.utilities.g1g2_to_e1e2(fake_gal_shear1, fake_gal_shear2) (d1app, d2app) = galsim.utilities.g1g2_to_e1e2(targ_applied_shear1, targ_applied_shear2) denom = 1.0 + d1*d1app + d2*d2app dapp_sq = d1app**2 + d2app**2 d1tot = (d1 + d1app + d2app/dapp_sq*(1.0 - np.sqrt(1.0-dapp_sq))*(d2*d1app - d1*d2app))/denom d2tot = (d2 + d2app + d1app/dapp_sq*(1.0 - np.sqrt(1.0-dapp_sq))*(d1*d2app - d2*d1app))/denom # convolve with a range of Gaussians, with and without shear (note, for this test all the # original and target ePSFs are Gaussian - there's no separate pixel response so that everything # can be calculated analytically) for tps in targ_pixel_scale: for tpf in targ_PSF_fwhm: for tps1 in targ_PSF_shear1: for tps2 in targ_PSF_shear2: print 'tps,tpf,tps1,tps2 = ',tps,tpf,tps1,tps2 # make target PSF targ_PSF = galsim.Gaussian(fwhm = tpf) targ_PSF.applyShear(g1=tps1, g2=tps2) # simulate image sim_image = galsim.simReal( rg, targ_PSF, tps, g1 = targ_applied_shear1, g2 = targ_applied_shear2, rand_rotate = False, target_flux = fake_gal_flux) # galaxy sigma, in units of pixels on the final image sigma_ideal = (fake_gal_fwhm/tps)*fwhm_to_sigma # compute analytically the expected galaxy moments: mxx_gal, myy_gal, mxy_gal = ellip_to_moments(d1tot, d2tot, sigma_ideal) # compute analytically the expected PSF moments: targ_PSF_e1, targ_PSF_e2 = galsim.utilities.g1g2_to_e1e2(tps1, tps2) targ_PSF_sigma = (tpf/tps)*fwhm_to_sigma mxx_PSF, myy_PSF, mxy_PSF = ellip_to_moments( targ_PSF_e1, targ_PSF_e2, targ_PSF_sigma) # get expected e1, e2, sigma for the PSF-convolved image tot_e1, tot_e2, tot_sigma = moments_to_ellip( mxx_gal+mxx_PSF, myy_gal+myy_PSF, mxy_gal+mxy_PSF) # compare with images that are expected expected_gaussian = galsim.Gaussian( flux = fake_gal_flux, sigma = tps*tot_sigma) expected_gaussian.applyShear(e1 = tot_e1, e2 = tot_e2) expected_image = galsim.ImageD( sim_image.array.shape[0], sim_image.array.shape[1]) expected_gaussian.draw(expected_image, dx = tps) printval(expected_image,sim_image) np.testing.assert_array_almost_equal( sim_image.array, expected_image.array, decimal = 3, err_msg = "Error in comparison of ideal Gaussian RealGalaxy calculations") t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)
def test_real_galaxy_saved(): """Test accuracy of various calculations with real RealGalaxy vs. stored SHERA result""" import time t1 = time.time() # read in real RealGalaxy from file rgc = galsim.RealGalaxyCatalog(catalog_file, image_dir) rg = galsim.RealGalaxy(rgc, index=ind_real) # read in expected result for some shear shera_image = galsim.fits.read(shera_file) shera_target_PSF_image = galsim.fits.read(shera_target_PSF_file) # simulate the same galaxy with GalSim sim_image = galsim.simReal(rg, shera_target_PSF_image, shera_target_pixel_scale, g1=targ_applied_shear1, g2=targ_applied_shear2, rand_rotate=False, target_flux=shera_target_flux) # there are centroid issues when comparing Shera vs. SBProfile outputs, so compare 2nd moments # instead of images sbp_res = sim_image.FindAdaptiveMom() shera_res = shera_image.FindAdaptiveMom() np.testing.assert_almost_equal( sbp_res.observed_shape.e1, shera_res.observed_shape.e1, 2, err_msg="Error in comparison with SHERA result: e1") np.testing.assert_almost_equal( sbp_res.observed_shape.e2, shera_res.observed_shape.e2, 2, err_msg="Error in comparison with SHERA result: e2") np.testing.assert_almost_equal( sbp_res.moments_sigma, shera_res.moments_sigma, 2, err_msg="Error in comparison with SHERA result: sigma") # Check picklability do_pickle( rgc, lambda x: [ x.getGal(ind_real), x.getPSF(ind_real), x.getNoiseProperties(ind_real) ]) do_pickle( rgc, lambda x: drawNoise(x.getNoise(ind_real, rng=galsim.BaseDeviate(123)))) do_pickle( rg, lambda x: galsim.Convolve([x, galsim.Gaussian(sigma=1.7)]). drawImage(nx=20, ny=20, scale=0.7)) do_pickle(rgc) do_pickle(rg) t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)
def test_real_galaxy_saved(): """Test accuracy of various calculations with real RealGalaxy vs. stored SHERA result""" import time t1 = time.time() # read in real RealGalaxy from file rgc = galsim.RealGalaxyCatalog(catalog_file, image_dir) rg = galsim.RealGalaxy(rgc, index = ind_real) # read in expected result for some shear shera_image = galsim.fits.read(shera_file) shera_target_PSF_image = galsim.fits.read(shera_target_PSF_file) # simulate the same galaxy with GalSim sim_image = galsim.simReal(rg, shera_target_PSF_image, shera_target_pixel_scale, g1 = targ_applied_shear1, g2 = targ_applied_shear2, rand_rotate = False, target_flux = shera_target_flux) # there are centroid issues when comparing Shera vs. SBProfile outputs, so compare 2nd moments # instead of images sbp_res = sim_image.FindAdaptiveMom() shera_res = shera_image.FindAdaptiveMom() np.testing.assert_almost_equal(sbp_res.observed_shape.e1, shera_res.observed_shape.e1, 2, err_msg = "Error in comparison with SHERA result: e1") np.testing.assert_almost_equal(sbp_res.observed_shape.e2, shera_res.observed_shape.e2, 2, err_msg = "Error in comparison with SHERA result: e2") np.testing.assert_almost_equal(sbp_res.moments_sigma, shera_res.moments_sigma, 2, err_msg = "Error in comparison with SHERA result: sigma") t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)
def test_real_galaxy_saved(): """Test accuracy of various calculations with real RealGalaxy vs. stored SHERA result""" # read in real RealGalaxy from file #rgc = galsim.RealGalaxyCatalog(catalog_file, dir=image_dir) # This is an alternate way to give the directory -- as part of the catalog file name. full_catalog_file = os.path.join(image_dir, catalog_file) rgc = galsim.RealGalaxyCatalog(full_catalog_file) rg = galsim.RealGalaxy(rgc, index=ind_real) # read in expected result for some shear shera_image = galsim.fits.read(shera_file) shera_target_PSF_image = galsim.fits.read(shera_target_PSF_file) # simulate the same galaxy with GalSim sim_image = galsim.simReal(rg, shera_target_PSF_image, shera_target_pixel_scale, g1=targ_applied_shear1, g2=targ_applied_shear2, rand_rotate=False, target_flux=shera_target_flux) # there are centroid issues when comparing Shera vs. SBProfile outputs, so compare 2nd moments # instead of images sbp_res = sim_image.FindAdaptiveMom() shera_res = shera_image.FindAdaptiveMom() np.testing.assert_almost_equal( sbp_res.observed_shape.e1, shera_res.observed_shape.e1, 2, err_msg="Error in comparison with SHERA result: e1") np.testing.assert_almost_equal( sbp_res.observed_shape.e2, shera_res.observed_shape.e2, 2, err_msg="Error in comparison with SHERA result: e2") np.testing.assert_almost_equal( sbp_res.moments_sigma, shera_res.moments_sigma, 2, err_msg="Error in comparison with SHERA result: sigma") check_basic(rg, "RealGalaxy", approx_maxsb=True) # Check picklability do_pickle( rgc, lambda x: [ x.getGal(ind_real), x.getPSF(ind_real), x.getNoiseProperties(ind_real) ]) do_pickle( rgc, lambda x: drawNoise(x.getNoise(ind_real, rng=galsim.BaseDeviate(123)))) do_pickle( rg, lambda x: galsim.Convolve([x, galsim.Gaussian(sigma=1.7)]). drawImage(nx=20, ny=20, scale=0.7)) do_pickle(rgc) do_pickle(rg)
def test_real_galaxy_saved(): """Test accuracy of various calculations with real RealGalaxy vs. stored SHERA result""" import time t1 = time.time() # read in real RealGalaxy from file #rgc = galsim.RealGalaxyCatalog(catalog_file, dir=image_dir) # This is an alternate way to give the directory -- as part of the catalog file name. full_catalog_file = os.path.join(image_dir,catalog_file) rgc = galsim.RealGalaxyCatalog(full_catalog_file) rg = galsim.RealGalaxy(rgc, index=ind_real) # read in expected result for some shear shera_image = galsim.fits.read(shera_file) shera_target_PSF_image = galsim.fits.read(shera_target_PSF_file) # simulate the same galaxy with GalSim sim_image = galsim.simReal(rg, shera_target_PSF_image, shera_target_pixel_scale, g1 = targ_applied_shear1, g2 = targ_applied_shear2, rand_rotate = False, target_flux = shera_target_flux) # there are centroid issues when comparing Shera vs. SBProfile outputs, so compare 2nd moments # instead of images sbp_res = sim_image.FindAdaptiveMom() shera_res = shera_image.FindAdaptiveMom() np.testing.assert_almost_equal(sbp_res.observed_shape.e1, shera_res.observed_shape.e1, 2, err_msg = "Error in comparison with SHERA result: e1") np.testing.assert_almost_equal(sbp_res.observed_shape.e2, shera_res.observed_shape.e2, 2, err_msg = "Error in comparison with SHERA result: e2") np.testing.assert_almost_equal(sbp_res.moments_sigma, shera_res.moments_sigma, 2, err_msg = "Error in comparison with SHERA result: sigma") # Check picklability do_pickle(rgc, lambda x: [ x.getGal(ind_real), x.getPSF(ind_real), x.getNoiseProperties(ind_real) ]) do_pickle(rgc, lambda x: drawNoise(x.getNoise(ind_real,rng=galsim.BaseDeviate(123)))) do_pickle(rg, lambda x: galsim.Convolve([x,galsim.Gaussian(sigma=1.7)]).drawImage( nx=20, ny=20, scale=0.7)) do_pickle(rgc) do_pickle(rg) t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)
def test_real_galaxy_saved(): """Test accuracy of various calculations with real RealGalaxy vs. stored SHERA result""" import time t1 = time.time() # read in real RealGalaxy from file rgc = galsim.RealGalaxyCatalog(catalog_file, image_dir) rg = galsim.RealGalaxy(rgc, index=ind_real) # read in expected result for some shear shera_image = galsim.fits.read(shera_file) shera_target_PSF_image = galsim.fits.read(shera_target_PSF_file) # simulate the same galaxy with GalSim sim_image = galsim.simReal(rg, shera_target_PSF_image, shera_target_pixel_scale, g1=targ_applied_shear1, g2=targ_applied_shear2, rand_rotate=False, target_flux=shera_target_flux) # there are centroid issues when comparing Shera vs. SBProfile outputs, so compare 2nd moments # instead of images sbp_res = sim_image.FindAdaptiveMom() shera_res = shera_image.FindAdaptiveMom() np.testing.assert_almost_equal( sbp_res.observed_shape.e1, shera_res.observed_shape.e1, 2, err_msg="Error in comparison with SHERA result: e1") np.testing.assert_almost_equal( sbp_res.observed_shape.e2, shera_res.observed_shape.e2, 2, err_msg="Error in comparison with SHERA result: e2") np.testing.assert_almost_equal( sbp_res.moments_sigma, shera_res.moments_sigma, 2, err_msg="Error in comparison with SHERA result: sigma") t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)
def test_real_galaxy_ideal(): """Test accuracy of various calculations with fake Gaussian RealGalaxy vs. ideal expectations""" import time t1 = time.time() # read in faked Gaussian RealGalaxy from file rgc = galsim.RealGalaxyCatalog(catalog_file, dir=image_dir) rg = galsim.RealGalaxy(rgc, index=ind_fake) # as a side note, make sure it behaves okay given a legit RNG and a bad RNG # or when trying to specify the galaxy too many ways rg_1 = galsim.RealGalaxy(rgc, index = ind_fake, rng = galsim.BaseDeviate(1234)) rg_2 = galsim.RealGalaxy(rgc, random=True) try: np.testing.assert_raises(TypeError, galsim.RealGalaxy, rgc, index=ind_fake, rng='foo') np.testing.assert_raises(AttributeError, galsim.RealGalaxy, rgc, index=ind_fake, id=0) np.testing.assert_raises(AttributeError, galsim.RealGalaxy, rgc, index=ind_fake, random=True) np.testing.assert_raises(AttributeError, galsim.RealGalaxy, rgc, id=0, random=True) np.testing.assert_raises(AttributeError, galsim.RealGalaxy, rgc) except ImportError: print 'The assert_raises tests require nose' do_pickle(rgc, lambda x: [ x.getGal(ind_fake), x.getPSF(ind_fake), x.getNoiseProperties(ind_fake) ]) do_pickle(rgc, lambda x: drawNoise(x.getNoise(ind_fake,rng=galsim.BaseDeviate(123)))) do_pickle(rgc) do_pickle(rg, lambda x: [ x.gal_image, x.psf_image, repr(x.noise), x.original_psf.flux, x.original_gal.flux, x.flux ]) do_pickle(rg, lambda x: x.drawImage(nx=20, ny=20, scale=0.7)) do_pickle(rg_1, lambda x: x.drawImage(nx=20, ny=20, scale=0.7)) do_pickle(rg) do_pickle(rg_1) ## for the generation of the ideal right answer, we need to add the intrinsic shape of the ## galaxy and the lensing shear using the rule for addition of distortions which is ugly, but oh ## well: (d1, d2) = galsim.utilities.g1g2_to_e1e2(fake_gal_shear1, fake_gal_shear2) (d1app, d2app) = galsim.utilities.g1g2_to_e1e2(targ_applied_shear1, targ_applied_shear2) denom = 1.0 + d1*d1app + d2*d2app dapp_sq = d1app**2 + d2app**2 d1tot = (d1 + d1app + d2app/dapp_sq*(1.0 - np.sqrt(1.0-dapp_sq))*(d2*d1app - d1*d2app))/denom d2tot = (d2 + d2app + d1app/dapp_sq*(1.0 - np.sqrt(1.0-dapp_sq))*(d1*d2app - d2*d1app))/denom # convolve with a range of Gaussians, with and without shear (note, for this test all the # original and target ePSFs are Gaussian - there's no separate pixel response so that everything # can be calculated analytically) for tps in targ_pixel_scale: for tpf in targ_PSF_fwhm: for tps1 in targ_PSF_shear1: for tps2 in targ_PSF_shear2: print 'tps,tpf,tps1,tps2 = ',tps,tpf,tps1,tps2 # make target PSF targ_PSF = galsim.Gaussian(fwhm = tpf).shear(g1=tps1, g2=tps2) # simulate image sim_image = galsim.simReal( rg, targ_PSF, tps, g1 = targ_applied_shear1, g2 = targ_applied_shear2, rand_rotate = False, target_flux = fake_gal_flux) # galaxy sigma, in units of pixels on the final image sigma_ideal = (fake_gal_fwhm/tps)*fwhm_to_sigma # compute analytically the expected galaxy moments: mxx_gal, myy_gal, mxy_gal = ellip_to_moments(d1tot, d2tot, sigma_ideal) # compute analytically the expected PSF moments: targ_PSF_e1, targ_PSF_e2 = galsim.utilities.g1g2_to_e1e2(tps1, tps2) targ_PSF_sigma = (tpf/tps)*fwhm_to_sigma mxx_PSF, myy_PSF, mxy_PSF = ellip_to_moments( targ_PSF_e1, targ_PSF_e2, targ_PSF_sigma) # get expected e1, e2, sigma for the PSF-convolved image tot_e1, tot_e2, tot_sigma = moments_to_ellip( mxx_gal+mxx_PSF, myy_gal+myy_PSF, mxy_gal+mxy_PSF) # compare with images that are expected expected_gaussian = galsim.Gaussian( flux = fake_gal_flux, sigma = tps*tot_sigma) expected_gaussian = expected_gaussian.shear(e1 = tot_e1, e2 = tot_e2) expected_image = galsim.ImageD( sim_image.array.shape[0], sim_image.array.shape[1]) expected_gaussian.drawImage(expected_image, scale=tps, method='no_pixel') printval(expected_image,sim_image) np.testing.assert_array_almost_equal( sim_image.array, expected_image.array, decimal = 3, err_msg = "Error in comparison of ideal Gaussian RealGalaxy calculations") t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)
def test_real_galaxy_ideal(): """Test accuracy of various calculations with fake Gaussian RealGalaxy vs. ideal expectations""" import time t1 = time.time() # read in faked Gaussian RealGalaxy from file rgc = galsim.RealGalaxyCatalog(catalog_file, image_dir) rg = galsim.RealGalaxy(rgc, index=ind_fake) ## for the generation of the ideal right answer, we need to add the intrinsic shape of the ## galaxy and the lensing shear using the rule for addition of distortions which is ugly, but oh ## well: (d1, d2) = galsim.utilities.g1g2_to_e1e2(fake_gal_shear1, fake_gal_shear2) (d1app, d2app) = galsim.utilities.g1g2_to_e1e2(targ_applied_shear1, targ_applied_shear2) denom = 1.0 + d1 * d1app + d2 * d2app dapp_sq = d1app**2 + d2app**2 d1tot = (d1 + d1app + d2app / dapp_sq * (1.0 - np.sqrt(1.0 - dapp_sq)) * (d2 * d1app - d1 * d2app)) / denom d2tot = (d2 + d2app + d1app / dapp_sq * (1.0 - np.sqrt(1.0 - dapp_sq)) * (d1 * d2app - d2 * d1app)) / denom # convolve with a range of Gaussians, with and without shear (note, for this test all the # original and target ePSFs are Gaussian - there's no separate pixel response so that everything # can be calculated analytically) for tps in targ_pixel_scale: for tpf in targ_PSF_fwhm: for tps1 in targ_PSF_shear1: for tps2 in targ_PSF_shear2: print 'tps,tpf,tps1,tps2 = ', tps, tpf, tps1, tps2 # make target PSF targ_PSF = galsim.Gaussian(fwhm=tpf) targ_PSF.applyShear(g1=tps1, g2=tps2) # simulate image sim_image = galsim.simReal(rg, targ_PSF, tps, g1=targ_applied_shear1, g2=targ_applied_shear2, rand_rotate=False, target_flux=fake_gal_flux) # galaxy sigma, in units of pixels on the final image sigma_ideal = (fake_gal_fwhm / tps) * fwhm_to_sigma # compute analytically the expected galaxy moments: mxx_gal, myy_gal, mxy_gal = ellip_to_moments( d1tot, d2tot, sigma_ideal) # compute analytically the expected PSF moments: targ_PSF_e1, targ_PSF_e2 = galsim.utilities.g1g2_to_e1e2( tps1, tps2) targ_PSF_sigma = (tpf / tps) * fwhm_to_sigma mxx_PSF, myy_PSF, mxy_PSF = ellip_to_moments( targ_PSF_e1, targ_PSF_e2, targ_PSF_sigma) # get expected e1, e2, sigma for the PSF-convolved image tot_e1, tot_e2, tot_sigma = moments_to_ellip( mxx_gal + mxx_PSF, myy_gal + myy_PSF, mxy_gal + mxy_PSF) # compare with images that are expected expected_gaussian = galsim.Gaussian(flux=fake_gal_flux, sigma=tps * tot_sigma) expected_gaussian.applyShear(e1=tot_e1, e2=tot_e2) expected_image = galsim.ImageD(sim_image.array.shape[0], sim_image.array.shape[1]) expected_gaussian.draw(expected_image, dx=tps) printval(expected_image, sim_image) np.testing.assert_array_almost_equal( sim_image.array, expected_image.array, decimal=3, err_msg= "Error in comparison of ideal Gaussian RealGalaxy calculations" ) t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)
good_psf_outer = galsim.Gaussian(flux=1.0-central_psf_amp, fwhm=outer_fwhm_mult*good_psf_central_fwhm) good_psf = good_psf_inner + good_psf_outer bad_psf_inner = galsim.Gaussian(flux=central_psf_amp, fwhm=bad_psf_central_fwhm) bad_psf_outer = galsim.Gaussian(flux=1.0-central_psf_amp, fwhm=outer_fwhm_mult*bad_psf_central_fwhm) bad_psf = bad_psf_inner + bad_psf_outer pixel = galsim.Pixel(xw=pixel_scale, yw=pixel_scale) good_epsf = galsim.Convolve(good_psf, pixel) bad_epsf = galsim.Convolve(bad_psf, pixel) # simulate some high-quality ground-based data, e.g., Subaru/CFHT with good seeing; with and without # shear print "Simulating unsheared galaxy in good seeing..." sim_image_good_noshear = galsim.simReal(real_galaxy, good_epsf, pixel_scale, rand_rotate=False) print "Simulating sheared galaxy in good seeing..." sim_image_good_shear = galsim.simReal(real_galaxy, good_epsf, pixel_scale, g1=g1, g2=g2, rand_rotate=False) # simulate some poor-quality ground-based data, e.g., a bad night for SDSS; with and without shear print "Simulating unsheared galaxy in bad seeing..." sim_image_bad_noshear = galsim.simReal(real_galaxy, bad_epsf, pixel_scale, rand_rotate=False) print "Simulating sheared galaxy in bad seeing..." sim_image_bad_shear = galsim.simReal(real_galaxy, bad_epsf, pixel_scale, g1=g1, g2=g2, rand_rotate=False) # write to files: original galaxy, original PSF, 2 target PSFs, 4 simulated images # note: will differ each time it is run, because we chose a random image print "Drawing images and writing to files!"
bad_psf_inner = galsim.Gaussian(flux=central_psf_amp, fwhm=bad_psf_central_fwhm) bad_psf_outer = galsim.Gaussian(flux=1.0 - central_psf_amp, fwhm=outer_fwhm_mult * bad_psf_central_fwhm) bad_psf = bad_psf_inner + bad_psf_outer pixel = galsim.Pixel(xw=pixel_scale, yw=pixel_scale) good_epsf = galsim.Convolve(good_psf, pixel) bad_epsf = galsim.Convolve(bad_psf, pixel) # simulate some high-quality ground-based data, e.g., Subaru/CFHT with good seeing; with and without # shear print "Simulating unsheared galaxy in good seeing..." sim_image_good_noshear = galsim.simReal(real_galaxy, good_epsf, pixel_scale, rand_rotate=False) print "Simulating sheared galaxy in good seeing..." sim_image_good_shear = galsim.simReal(real_galaxy, good_epsf, pixel_scale, g1=g1, g2=g2, rand_rotate=False) # simulate some poor-quality ground-based data, e.g., a bad night for SDSS; with and without shear print "Simulating unsheared galaxy in bad seeing..." sim_image_bad_noshear = galsim.simReal(real_galaxy, bad_epsf, pixel_scale, rand_rotate=False)