def test_scale_unit(): """Test that `scale_unit` keyword correctly sets the units for PhaseScreenPSF.""" aper = galsim.Aperture(diam=1.0) rng = galsim.BaseDeviate(1234) # Test frozen AtmosphericScreen first atm = galsim.Atmosphere(screen_size=30.0, altitude=10.0, speed=0.1, alpha=1.0, rng=rng) psf = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec) im1 = psf.drawImage(nx=32, ny=32, scale=0.1, method='no_pixel') psf2 = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit='arcmin') im2 = psf2.drawImage(nx=32, ny=32, scale=0.1/60.0, method='no_pixel') np.testing.assert_almost_equal( im1.array, im2.array, 8, 'PhaseScreenPSF inconsistent use of scale_unit') opt_psf1 = galsim.OpticalPSF(lam=500.0, diam=1.0, scale_unit=galsim.arcsec) opt_psf2 = galsim.OpticalPSF(lam=500.0, diam=1.0, scale_unit='arcsec') assert opt_psf1 == opt_psf2, "scale unit did not parse as string" assert_raises(ValueError, galsim.OpticalPSF, lam=500.0, diam=1.0, scale_unit='invalid') assert_raises(ValueError, galsim.PhaseScreenPSF, atm, 500.0, aper=aper, scale_unit='invalid') # Check a few other construction errors now too. assert_raises(ValueError, galsim.PhaseScreenPSF, atm, 500.0, scale_unit='arcmin') assert_raises(TypeError, galsim.PhaseScreenPSF, atm, 500.0, aper=aper, theta=34.*galsim.degrees) assert_raises(TypeError, galsim.PhaseScreenPSF, atm, 500.0, aper=aper, theta=(34, 5)) assert_raises(ValueError, galsim.PhaseScreenPSF, atm, 500.0, aper=aper, exptime=-1)
def test_scale_unit(): """Test that `scale_unit` keyword correctly sets the units for PhaseScreenPSF.""" aper = galsim.Aperture(diam=1.0) rng = galsim.BaseDeviate(1234) # Test frozen AtmosphericScreen first atm = galsim.Atmosphere(screen_size=30.0, altitude=10.0, speed=0.1, alpha=1.0, rng=rng) psf = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec) im1 = psf.drawImage(nx=32, ny=32, scale=0.1, method='no_pixel') psf2 = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcmin) im2 = psf2.drawImage(nx=32, ny=32, scale=0.1 / 60.0, method='no_pixel') np.testing.assert_almost_equal( im1.array, im2.array, 8, 'PhaseScreenPSF inconsistent use of scale_unit') opt_psf1 = galsim.OpticalPSF(lam=500.0, diam=1.0, scale_unit=galsim.arcsec) opt_psf2 = galsim.OpticalPSF(lam=500.0, diam=1.0, scale_unit='arcsec') assert opt_psf1 == opt_psf2, "scale unit did not parse as string"
def test_stepk_maxk(): """Test options to specify (or not) stepk and maxk. """ aper = galsim.Aperture(diam=1.0) rng = galsim.BaseDeviate(123456) # Test frozen AtmosphericScreen first atm = galsim.Atmosphere(screen_size=30.0, altitude=10.0, speed=0.1, alpha=1.0, rng=rng) psf = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec) stepk = psf.stepK() maxk = psf.maxK() psf2 = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec, _force_stepk=stepk/1.5, _force_maxk=maxk*2.0) np.testing.assert_almost_equal( psf2.stepK(), stepk/1.5, decimal=7, err_msg="PhaseScreenPSF did not adopt forced value for stepK") np.testing.assert_almost_equal( psf2.maxK(), maxk*2.0, decimal=7, err_msg="PhaseScreenPSF did not adopt forced value for maxK") do_pickle(psf) do_pickle(psf2) # Try out non-geometric-shooting psf3 = atm.makePSF(lam=500.0, aper=aper, geometric_shooting=False) img = galsim.Image(32, 32, scale=0.2) do_shoot(psf3, img, "PhaseScreenPSF") # Also make sure a few other methods at least run psf3.centroid() psf3.maxSB()
def test_stepk_maxk(): """Test options to specify (or not) stepk and maxk. """ aper = galsim.Aperture(diam=1.0) rng = galsim.BaseDeviate(1234) # Test frozen AtmosphericScreen first atm = galsim.Atmosphere(screen_size=30.0, altitude=10.0, speed=0.1, alpha=1.0, rng=rng) psf = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec) stepk = psf.stepK() maxk = psf.maxK() psf2 = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec, _force_stepk=stepk/1.5, _force_maxk=maxk*2.0) np.testing.assert_almost_equal( psf2.stepK(), stepk/1.5, decimal=7, err_msg="PhaseScreenPSF did not adopt forced value for stepK") np.testing.assert_almost_equal( psf2.maxK(), maxk*2.0, decimal=7, err_msg="PhaseScreenPSF did not adopt forced value for maxK") do_pickle(psf) do_pickle(psf2)
def test_sk_phase_psf(): """Test that analytic second kick profile matches what can be obtained from PhaseScreenPSF with an appropriate truncated power spectrum. """ # import matplotlib.pyplot as plt lam = 500.0 r0 = 0.2 diam = 4.0 obscuration = 0.5 rng = galsim.UniformDeviate(1234567890) weights = [0.652, 0.172, 0.055, 0.025, 0.074, 0.022] speed = [rng() * 20 for _ in range(6)] direction = [rng() * 360 * galsim.degrees for _ in range(6)] aper = galsim.Aperture(4.0, obscuration=obscuration, pad_factor=0.5) kcrits = [1, 3, 10] if __name__ == '__main__' else [1] for kcrit in kcrits: # Technically, we should probably use a smaller screen_scale here, but that runs really # slowly. The below seems to work well enough for the tested kcrits. atm = galsim.Atmosphere(r0_500=r0, r0_weights=weights, rng=rng, speed=speed, direction=direction, screen_size=102.4, screen_scale=0.05, suppress_warning=True) atm.instantiate(kmin=kcrit) print('instantiated atm') psf = galsim.PhaseScreenPSF(atm, lam=lam, exptime=10, aper=aper, time_step=0.1) print('made psf') phaseImg = psf.drawImage(nx=64, ny=64, scale=0.02) sk = galsim.SecondKick(lam=lam, r0=r0, diam=diam, obscuration=obscuration, kcrit=kcrit) print('made sk') skImg = sk.drawImage(nx=64, ny=64, scale=0.02) print('made skimg') phaseMom = galsim.hsm.FindAdaptiveMom(phaseImg) skMom = galsim.hsm.FindAdaptiveMom(skImg) print('moments: ', phaseMom.moments_sigma, skMom.moments_sigma) np.testing.assert_allclose(phaseMom.moments_sigma, skMom.moments_sigma, rtol=2e-2)
def test_ne(): """Test Apertures, PhaseScreens, PhaseScreenLists, and PhaseScreenPSFs for not-equals.""" pupil_plane_im = galsim.fits.read(os.path.join(imgdir, pp_file)) # Test galsim.Aperture __ne__ objs = [galsim.Aperture(diam=1.0), galsim.Aperture(diam=1.1), galsim.Aperture(diam=1.0, oversampling=1.5), galsim.Aperture(diam=1.0, pad_factor=1.5), galsim.Aperture(diam=1.0, circular_pupil=False), galsim.Aperture(diam=1.0, obscuration=0.3), galsim.Aperture(diam=1.0, nstruts=3), galsim.Aperture(diam=1.0, nstruts=3, strut_thick=0.2), galsim.Aperture(diam=1.0, nstruts=3, strut_angle=15*galsim.degrees), galsim.Aperture(diam=1.0, pupil_plane_im=pupil_plane_im), galsim.Aperture(diam=1.0, pupil_plane_im=pupil_plane_im, pupil_angle=10.0*galsim.degrees)] all_obj_diff(objs) # Test AtmosphericScreen __ne__ rng = galsim.BaseDeviate(1) objs = [galsim.AtmosphericScreen(10.0, rng=rng), galsim.AtmosphericScreen(1.0, rng=rng), galsim.AtmosphericScreen(10.0, rng=rng, vx=1.0), galsim.AtmosphericScreen(10.0, rng=rng, vy=1.0), galsim.AtmosphericScreen(10.0, rng=rng, alpha=0.999, time_step=0.01), galsim.AtmosphericScreen(10.0, rng=rng, altitude=1.0), galsim.AtmosphericScreen(10.0, rng=rng, alpha=0.999, time_step=0.02), galsim.AtmosphericScreen(10.0, rng=rng, alpha=0.998, time_step=0.02), galsim.AtmosphericScreen(10.0, rng=rng, r0_500=0.1), galsim.AtmosphericScreen(10.0, rng=rng, L0=10.0), galsim.AtmosphericScreen(10.0, rng=rng, vx=10.0), ] all_obj_diff(objs) objs.append(galsim.AtmosphericScreen(10.0, rng=rng)) objs[-1].instantiate() # Should still all be __ne__, but first and last will have the same hash this time. assert hash(objs[0]) == hash(objs[-1]) all_obj_diff(objs, check_hash=False) # Test OpticalScreen __ne__ objs = [galsim.OpticalScreen(diam=1.0), galsim.OpticalScreen(diam=1.0, tip=1.0), galsim.OpticalScreen(diam=1.0, tilt=1.0), galsim.OpticalScreen(diam=1.0, defocus=1.0), galsim.OpticalScreen(diam=1.0, astig1=1.0), galsim.OpticalScreen(diam=1.0, astig2=1.0), galsim.OpticalScreen(diam=1.0, coma1=1.0), galsim.OpticalScreen(diam=1.0, coma2=1.0), galsim.OpticalScreen(diam=1.0, trefoil1=1.0), galsim.OpticalScreen(diam=1.0, trefoil2=1.0), galsim.OpticalScreen(diam=1.0, spher=1.0), galsim.OpticalScreen(diam=1.0, spher=1.0, lam_0=100.0), galsim.OpticalScreen(diam=1.0, aberrations=[0,0,1.1]), # tip=1.1 ] all_obj_diff(objs) # Test PhaseScreenList __ne__ atm = galsim.Atmosphere(10.0, vx=1.0) objs = [galsim.PhaseScreenList(atm), galsim.PhaseScreenList(objs), # Reuse list of OpticalScreens above galsim.PhaseScreenList(objs[0:2])] all_obj_diff(objs) # Test PhaseScreenPSF __ne__ psl = galsim.PhaseScreenList(atm) objs = [galsim.PhaseScreenPSF(psl, 500.0, exptime=0.03, diam=1.0)] objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0)] objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.1)] objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0, flux=1.1)] objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0, interpolant='linear')] stepk = objs[0].stepk maxk = objs[0].maxk objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0, _force_stepk=stepk/1.5)] objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0, _force_maxk=maxk*2.0)] all_obj_diff(objs)
def test_stepk_maxk(): """Test options to specify (or not) stepk and maxk. """ import time aper = galsim.Aperture(diam=1.0) rng = galsim.BaseDeviate(123456) # Test frozen AtmosphericScreen first atm = galsim.Atmosphere(screen_size=30.0, altitude=10.0, speed=0.1, alpha=1.0, rng=rng) psf = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec) t0 = time.time() stepk1 = psf.stepk maxk1 = psf.maxk t1 = time.time() print('stepk1 = ',stepk1) print('maxk1 = ',maxk1) print('t1 = ',t1-t0) psf._prepareDraw() stepk2 = psf.stepk maxk2 = psf.maxk t2 = time.time() print('stepk2 = ',stepk2) print('maxk2 = ',maxk2) print('goodImageSize = ',psf.getGoodImageSize(0.2)) print('t2 = ',t2-t1) np.testing.assert_allclose(stepk1, stepk2, rtol=0.05) np.testing.assert_allclose(maxk1, maxk2, rtol=0.05) # Also make sure that prepareDraw wasn't called to calculate the first one. # Should be very quick to do the first stepk, maxk, but slow to do the second. assert t1-t0 < t2-t1 # Check that stepk changes when gsparams.folding_threshold become more extreme. # (Note: maxk is independent of maxk_threshold because of the hard edge of the aperture.) psf1 = galsim.PhaseScreenPSF(atm, 500.0, diam=1.0, scale_unit=galsim.arcsec, gsparams=galsim.GSParams(folding_threshold=1.e-3, maxk_threshold=1.e-4)) stepk3 = psf1.stepk maxk3 = psf1.maxk print('stepk3 = ',stepk3) print('maxk3 = ',maxk3) print('goodImageSize = ',psf1.getGoodImageSize(0.2)) assert stepk3 < stepk1 assert maxk3 == maxk1 # Check that it respects the force_stepk and force_maxk parameters psf2 = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec, _force_stepk=stepk2/1.5, _force_maxk=maxk2*2.0) np.testing.assert_almost_equal( psf2.stepk, stepk2/1.5, decimal=7, err_msg="PhaseScreenPSF did not adopt forced value for stepk") np.testing.assert_almost_equal( psf2.maxk, maxk2*2.0, decimal=7, err_msg="PhaseScreenPSF did not adopt forced value for maxk") do_pickle(psf) do_pickle(psf2) # Try out non-geometric-shooting psf3 = atm.makePSF(lam=500.0, aper=aper, geometric_shooting=False) img = galsim.Image(32, 32, scale=0.2) do_shoot(psf3, img, "PhaseScreenPSF") # Also make sure a few other methods at least run psf3.centroid psf3.max_sb # If we force stepk very low, it will trigger a warning when we try to draw it. psf4 = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec, _force_stepk=stepk2/3.5) with assert_warns(galsim.GalSimWarning): psf4._prepareDraw() # Can suppress this warning if desired. psf5 = galsim.PhaseScreenPSF(atm, 500.0, aper=aper, scale_unit=galsim.arcsec, _force_stepk=stepk2/3.5, suppress_warning=True) with assert_raises(AssertionError): with assert_warns(galsim.GalSimWarning): psf5._prepareDraw()
def test_ne(): """Test Apertures, PhaseScreens, PhaseScreenLists, and PhaseScreenPSFs for not-equals.""" import copy pupil_plane_im = galsim.fits.read(os.path.join(imgdir, pp_file)) # Test galsim.Aperture __ne__ objs = [galsim.Aperture(diam=1.0), galsim.Aperture(diam=1.1), galsim.Aperture(diam=1.0, oversampling=1.5), galsim.Aperture(diam=1.0, pad_factor=1.5), galsim.Aperture(diam=1.0, circular_pupil=False), galsim.Aperture(diam=1.0, obscuration=0.3), galsim.Aperture(diam=1.0, nstruts=3), galsim.Aperture(diam=1.0, nstruts=3, strut_thick=0.2), galsim.Aperture(diam=1.0, nstruts=3, strut_angle=15*galsim.degrees), galsim.Aperture(diam=1.0, pupil_plane_im=pupil_plane_im), galsim.Aperture(diam=1.0, pupil_plane_im=pupil_plane_im, pupil_angle=10.0*galsim.degrees)] all_obj_diff(objs) # Test AtmosphericScreen __ne__ rng = galsim.BaseDeviate(1) objs = [galsim.AtmosphericScreen(10.0, rng=rng), galsim.AtmosphericScreen(10.0, rng=rng, vx=1.0), galsim.AtmosphericScreen(10.0, rng=rng, vx=1.0), # advance this one below galsim.AtmosphericScreen(10.0, rng=rng, vy=1.0), galsim.AtmosphericScreen(10.0, rng=rng, alpha=0.999), galsim.AtmosphericScreen(10.0, rng=rng, altitude=1.0), galsim.AtmosphericScreen(10.0, rng=rng, time_step=0.1), galsim.AtmosphericScreen(10.0, rng=rng, r0_500=0.1), galsim.AtmosphericScreen(10.0, rng=rng, L0=10.0), galsim.AtmosphericScreen(10.0, rng=rng, vx=10.0), ] objs[2].advance() all_obj_diff(objs) # Test OpticalScreen __ne__ objs = [galsim.OpticalScreen(), galsim.OpticalScreen(tip=1.0), galsim.OpticalScreen(tilt=1.0), galsim.OpticalScreen(defocus=1.0), galsim.OpticalScreen(astig1=1.0), galsim.OpticalScreen(astig2=1.0), galsim.OpticalScreen(coma1=1.0), galsim.OpticalScreen(coma2=1.0), galsim.OpticalScreen(trefoil1=1.0), galsim.OpticalScreen(trefoil2=1.0), galsim.OpticalScreen(spher=1.0), galsim.OpticalScreen(spher=1.0, lam_0=100.0), galsim.OpticalScreen(aberrations=[0,0,1.1]), # tip=1.1 ] all_obj_diff(objs) # Test PhaseScreenList __ne__ atm = galsim.Atmosphere(10.0, vx=1.0) objs = [galsim.PhaseScreenList(atm), galsim.PhaseScreenList(copy.deepcopy(atm)), # advance down below galsim.PhaseScreenList(objs), # Reuse list of OpticalScreens above galsim.PhaseScreenList(objs[0:2])] objs[1].advance() all_obj_diff(objs) # Test PhaseScreenPSF __ne__ objs[0].reset() psl = galsim.PhaseScreenList(atm) objs = [galsim.PhaseScreenPSF(psl, 500.0, exptime=0.03, diam=1.0), galsim.PhaseScreenPSF(psl, 500.0, exptime=0.03, diam=1.0)] # advanced so differs psl.reset() objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0)] psl.reset() objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.1)] psl.reset() objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0, flux=1.1)] psl.reset() objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0, interpolant='linear')] stepk = objs[0].stepK() maxk = objs[0].maxK() psl.reset() objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0, _force_stepk=stepk/1.5)] psl.reset() objs += [galsim.PhaseScreenPSF(psl, 700.0, exptime=0.03, diam=1.0, _force_maxk=maxk*2.0)] all_obj_diff(objs)