Skip to content

Commit 4cf0712

Browse files
authored
Merge pull request #4 from GalSim-developers/moffat-trunc-fix
test: changes to run more of moffat tests
2 parents 1a490c3 + 2336878 commit 4cf0712

1 file changed

Lines changed: 122 additions & 94 deletions

File tree

tests/test_moffat.py

Lines changed: 122 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,56 @@
2626
# Directory containing the reference images.
2727
imgdir = os.path.join(path, "SBProfile_comparison_images")
2828

29+
if is_jax_galsim():
30+
USE_TRUNC = 0.0
31+
else:
32+
USE_TRUNC = 1.0
33+
2934

3035
@timer
3136
def test_moffat():
3237
"""Test the generation of a specific Moffat profile against a known result.
3338
"""
39+
fwhm_backwards_compatible = 1.3178976627539716
3440
savedImg = galsim.fits.read(os.path.join(imgdir, "moffat_2_5.fits"))
3541
dx = 0.2
3642
myImg = galsim.ImageF(savedImg.bounds, scale=dx)
3743
myImg.setCenter(0,0)
3844

39-
# Code was formerly:
40-
# moffat = galsim.Moffat(beta=2, truncationFWHM=5, flux=1, half_light_radius=1)
41-
#
42-
# ...but this is no longer quite so simple since we changed the handling of trunc to be in
43-
# physical units. However, the same profile can be constructed using
44-
# fwhm=1.3178976627539716
45-
# as calculated by interval bisection in devutils/external/calculate_moffat_radii.py
46-
fwhm_backwards_compatible = 1.3178976627539716
47-
moffat = galsim.Moffat(beta=2, half_light_radius=1, trunc=5*fwhm_backwards_compatible, flux=1)
48-
moffat.drawImage(myImg, method="sb", use_true_center=False)
49-
np.testing.assert_array_almost_equal(
50-
myImg.array, savedImg.array, 5,
51-
err_msg="Using GSObject Moffat disagrees with expected result")
52-
53-
# Check with default_params
54-
moffat = galsim.Moffat(beta=2, half_light_radius=1, trunc=5*fwhm_backwards_compatible, flux=1,
55-
gsparams=default_params)
56-
moffat.drawImage(myImg, method="sb", use_true_center=False)
57-
np.testing.assert_array_almost_equal(
58-
myImg.array, savedImg.array, 5,
59-
err_msg="Using GSObject Moffat with default_params disagrees with expected result")
60-
moffat = galsim.Moffat(beta=2, half_light_radius=1, trunc=5*fwhm_backwards_compatible, flux=1,
61-
gsparams=galsim.GSParams())
62-
moffat.drawImage(myImg, method="sb", use_true_center=False)
63-
np.testing.assert_array_almost_equal(
64-
myImg.array, savedImg.array, 5,
65-
err_msg="Using GSObject Moffat with GSParams() disagrees with expected result")
45+
if is_jax_galsim():
46+
pass
47+
else:
48+
# Code was formerly:
49+
# moffat = galsim.Moffat(beta=2, truncationFWHM=5, flux=1, half_light_radius=1)
50+
#
51+
# ...but this is no longer quite so simple since we changed the handling of trunc to be in
52+
# physical units. However, the same profile can be constructed using
53+
# fwhm=1.3178976627539716
54+
# as calculated by interval bisection in devutils/external/calculate_moffat_radii.py
55+
moffat = galsim.Moffat(beta=2, half_light_radius=1, trunc=5*fwhm_backwards_compatible, flux=1)
56+
moffat.drawImage(myImg, method="sb", use_true_center=False)
57+
np.testing.assert_array_almost_equal(
58+
myImg.array, savedImg.array, 5,
59+
err_msg="Using GSObject Moffat disagrees with expected result")
60+
61+
# Check with default_params
62+
moffat = galsim.Moffat(beta=2, half_light_radius=1, trunc=5*fwhm_backwards_compatible, flux=1,
63+
gsparams=default_params)
64+
moffat.drawImage(myImg, method="sb", use_true_center=False)
65+
np.testing.assert_array_almost_equal(
66+
myImg.array, savedImg.array, 5,
67+
err_msg="Using GSObject Moffat with default_params disagrees with expected result")
68+
moffat = galsim.Moffat(beta=2, half_light_radius=1, trunc=5*fwhm_backwards_compatible, flux=1,
69+
gsparams=galsim.GSParams())
70+
moffat.drawImage(myImg, method="sb", use_true_center=False)
71+
np.testing.assert_array_almost_equal(
72+
myImg.array, savedImg.array, 5,
73+
err_msg="Using GSObject Moffat with GSParams() disagrees with expected result")
6674

6775
# Use non-unity values.
68-
moffat = galsim.Moffat(beta=3.7, flux=1.7, half_light_radius=2.3, trunc=8.2)
76+
moffat = galsim.Moffat(beta=3.7, flux=1.7, half_light_radius=2.3, trunc=8.2 * USE_TRUNC)
6977
gsp = galsim.GSParams(xvalue_accuracy=1.e-8, kvalue_accuracy=1.e-8)
70-
moffat2 = galsim.Moffat(beta=3.7, flux=1.7, half_light_radius=2.3, trunc=8.2, gsparams=gsp)
78+
moffat2 = galsim.Moffat(beta=3.7, flux=1.7, half_light_radius=2.3, trunc=8.2 * USE_TRUNC, gsparams=gsp)
7179
assert moffat2 != moffat
7280
assert moffat2 == moffat.withGSParams(gsp)
7381
assert moffat2 == moffat.withGSParams(xvalue_accuracy=1.e-8, kvalue_accuracy=1.e-8)
@@ -106,9 +114,12 @@ def test_moffat():
106114
assert_raises(TypeError, galsim.Moffat, beta=3, scale_radius=3, half_light_radius=1)
107115
assert_raises(TypeError, galsim.Moffat, beta=3)
108116

109-
# beta <= 1.1 needs to be truncated.
110-
assert_raises(ValueError, galsim.Moffat, beta=1.1, scale_radius=3)
111-
assert_raises(ValueError, galsim.Moffat, beta=0.9, scale_radius=3)
117+
if is_jax_galsim():
118+
pass
119+
else:
120+
# beta <= 1.1 needs to be truncated.
121+
assert_raises(ValueError, galsim.Moffat, beta=1.1, scale_radius=3)
122+
assert_raises(ValueError, galsim.Moffat, beta=0.9, scale_radius=3)
112123

113124
# trunc must be > sqrt(2) * hlr
114125
assert_raises(ValueError, galsim.Moffat, beta=3, half_light_radius=1, trunc=1.4)
@@ -121,61 +132,67 @@ def test_moffat():
121132
def test_moffat_properties():
122133
"""Test some basic properties of the Moffat profile.
123134
"""
124-
# Code was formerly:
125-
# psf = galsim.Moffat(beta=2.0, truncationFWHM=2, flux=test_flux, half_light_radius=1)
126-
#
127-
# ...but this is no longer quite so simple since we changed the handling of trunc to be in
128-
# physical units. However, the same profile can be constructed using
129-
# fwhm=1.4686232496771867,
130-
# as calculated by interval bisection in devutils/external/calculate_moffat_radii.py
131-
test_flux = 1.8
132-
fwhm_backwards_compatible = 1.4686232496771867
133-
psf = galsim.Moffat(beta=2.0, fwhm=fwhm_backwards_compatible,
134-
trunc=2*fwhm_backwards_compatible, flux=test_flux)
135-
# Check that we are centered on (0, 0)
136-
cen = galsim.PositionD(0, 0)
137-
np.testing.assert_equal(psf.centroid, cen)
138-
# Check Fourier properties
139135
if is_jax_galsim():
140-
np.testing.assert_allclose(psf.maxk, 11.634597424960159, atol=0, rtol=0.2)
136+
test_flux = 1.8
137+
fwhm_backwards_compatible = 1.4686232496771867
138+
psf = galsim.Moffat(beta=2.0, half_light_radius=1.,
139+
trunc=2*fwhm_backwards_compatible * USE_TRUNC, flux=test_flux)
141140
else:
142-
np.testing.assert_array_almost_equal(psf.maxk, 11.634597424960159)
143-
np.testing.assert_array_almost_equal(psf.stepk, 0.62831853071795873)
144-
np.testing.assert_array_almost_equal(psf.kValue(cen), test_flux+0j)
145-
np.testing.assert_array_almost_equal(psf.half_light_radius, 1.0)
146-
np.testing.assert_array_almost_equal(psf.fwhm, fwhm_backwards_compatible)
147-
np.testing.assert_array_almost_equal(psf.xValue(cen), 0.50654651638242509)
148-
np.testing.assert_array_almost_equal(psf.kValue(cen), (1+0j) * test_flux)
149-
np.testing.assert_array_almost_equal(psf.flux, test_flux)
150-
np.testing.assert_array_almost_equal(psf.xValue(cen), psf.max_sb)
151-
152-
# Now create the same profile using the half_light_radius:
153-
psf = galsim.Moffat(beta=2.0, half_light_radius=1.,
154-
trunc=2*fwhm_backwards_compatible, flux=test_flux)
155-
np.testing.assert_equal(psf.centroid, cen)
156-
if is_jax_galsim():
157-
np.testing.assert_allclose(psf.maxk, 11.634597424960159, atol=0, rtol=0.2)
158-
else:
159-
np.testing.assert_array_almost_equal(psf.maxk, 11.634597424960159)
160-
np.testing.assert_array_almost_equal(psf.stepk, 0.62831853071795862)
161-
np.testing.assert_array_almost_equal(psf.kValue(cen), test_flux+0j)
162-
np.testing.assert_array_almost_equal(psf.half_light_radius, 1.0)
163-
np.testing.assert_array_almost_equal(psf.fwhm, fwhm_backwards_compatible)
164-
np.testing.assert_array_almost_equal(psf.xValue(cen), 0.50654651638242509)
165-
np.testing.assert_array_almost_equal(psf.kValue(cen), (1+0j) * test_flux)
166-
np.testing.assert_array_almost_equal(psf.flux, test_flux)
167-
np.testing.assert_array_almost_equal(psf.xValue(cen), psf.max_sb)
141+
# Code was formerly:
142+
# psf = galsim.Moffat(beta=2.0, truncationFWHM=2, flux=test_flux, half_light_radius=1)
143+
#
144+
# ...but this is no longer quite so simple since we changed the handling of trunc to be in
145+
# physical units. However, the same profile can be constructed using
146+
# fwhm=1.4686232496771867,
147+
# as calculated by interval bisection in devutils/external/calculate_moffat_radii.py
148+
test_flux = 1.8
149+
fwhm_backwards_compatible = 1.4686232496771867
150+
psf = galsim.Moffat(beta=2.0, fwhm=fwhm_backwards_compatible,
151+
trunc=2*fwhm_backwards_compatible, flux=test_flux)
152+
# Check that we are centered on (0, 0)
153+
cen = galsim.PositionD(0, 0)
154+
np.testing.assert_equal(psf.centroid, cen)
155+
# Check Fourier properties
156+
if is_jax_galsim():
157+
np.testing.assert_allclose(psf.maxk, 11.634597424960159, atol=0, rtol=0.2)
158+
else:
159+
np.testing.assert_array_almost_equal(psf.maxk, 11.634597424960159)
160+
np.testing.assert_array_almost_equal(psf.stepk, 0.62831853071795873)
161+
np.testing.assert_array_almost_equal(psf.kValue(cen), test_flux+0j)
162+
np.testing.assert_array_almost_equal(psf.half_light_radius, 1.0)
163+
np.testing.assert_array_almost_equal(psf.fwhm, fwhm_backwards_compatible)
164+
np.testing.assert_array_almost_equal(psf.xValue(cen), 0.50654651638242509)
165+
np.testing.assert_array_almost_equal(psf.kValue(cen), (1+0j) * test_flux)
166+
np.testing.assert_array_almost_equal(psf.flux, test_flux)
167+
np.testing.assert_array_almost_equal(psf.xValue(cen), psf.max_sb)
168+
169+
# Now create the same profile using the half_light_radius:
170+
psf = galsim.Moffat(beta=2.0, half_light_radius=1.,
171+
trunc=2*fwhm_backwards_compatible, flux=test_flux)
172+
np.testing.assert_equal(psf.centroid, cen)
173+
if is_jax_galsim():
174+
np.testing.assert_allclose(psf.maxk, 11.634597424960159, atol=0, rtol=0.2)
175+
else:
176+
np.testing.assert_array_almost_equal(psf.maxk, 11.634597424960159)
177+
np.testing.assert_array_almost_equal(psf.stepk, 0.62831853071795862)
178+
np.testing.assert_array_almost_equal(psf.kValue(cen), test_flux+0j)
179+
np.testing.assert_array_almost_equal(psf.half_light_radius, 1.0)
180+
np.testing.assert_array_almost_equal(psf.fwhm, fwhm_backwards_compatible)
181+
np.testing.assert_array_almost_equal(psf.xValue(cen), 0.50654651638242509)
182+
np.testing.assert_array_almost_equal(psf.kValue(cen), (1+0j) * test_flux)
183+
np.testing.assert_array_almost_equal(psf.flux, test_flux)
184+
np.testing.assert_array_almost_equal(psf.xValue(cen), psf.max_sb)
168185

169186
# Check input flux vs output flux
170187
for inFlux in np.logspace(-2, 2, 10):
171188
psfFlux = galsim.Moffat(2.0, fwhm=fwhm_backwards_compatible,
172-
trunc=2*fwhm_backwards_compatible, flux=inFlux)
189+
trunc=2*fwhm_backwards_compatible * USE_TRUNC, flux=inFlux)
173190
outFlux = psfFlux.flux
174191
np.testing.assert_array_almost_equal(outFlux, inFlux)
175192

176193
# Check that stepk and maxk scale correctly with radius
177194
psf2 = galsim.Moffat(beta=2.0, half_light_radius=5.,
178-
trunc=5*2*fwhm_backwards_compatible, flux=test_flux)
195+
trunc=5*2*fwhm_backwards_compatible * USE_TRUNC, flux=test_flux)
179196
np.testing.assert_almost_equal(psf2.maxk, psf.maxk/5)
180197
np.testing.assert_almost_equal(psf2.stepk, psf.stepk/5)
181198

@@ -205,19 +222,24 @@ def test_moffat_maxk():
205222
galsim.Moffat(beta=1.22, scale_radius=23, flux=23),
206223
galsim.Moffat(beta=3.6, scale_radius=2, flux=23),
207224
galsim.Moffat(beta=12.9, scale_radius=5, flux=23),
208-
galsim.Moffat(beta=1.22, scale_radius=7, flux=23, trunc=30),
209-
galsim.Moffat(beta=3.6, scale_radius=9, flux=23, trunc=50),
210-
galsim.Moffat(beta=12.9, scale_radius=11, flux=23, trunc=1000),
225+
galsim.Moffat(beta=1.22, scale_radius=7, flux=23, trunc=30 * USE_TRUNC),
226+
galsim.Moffat(beta=3.6, scale_radius=9, flux=23, trunc=50 * USE_TRUNC),
227+
galsim.Moffat(beta=12.9, scale_radius=11, flux=23, trunc=1000 * USE_TRUNC),
211228
]
212229
threshs = [1.e-3, 1.e-4, 0.03]
213-
print('beta \t trunc \t thresh \t kValue(maxk) \t maxk')
230+
print('\nbeta \t trunc \t thresh \t kValue(maxk) \t maxk')
214231
for psf in psfs:
215232
for thresh in threshs:
216233
psf = psf.withGSParams(maxk_threshold=thresh)
217234
rtol = 1.e-7 if psf.trunc == 0 else 3.e-3
235+
if is_jax_galsim():
236+
atol = 5e-6
237+
rtol = 0
238+
else:
239+
atol = 0
218240
fk = psf.kValue(psf.maxk,0).real/psf.flux
219241
print(f'{psf.beta} \t {int(psf.trunc)} \t {thresh:.1e} \t {fk:.3e} \t {psf.maxk:.3e}')
220-
np.testing.assert_allclose(abs(psf.kValue(psf.maxk,0).real)/psf.flux, thresh, rtol=rtol)
242+
np.testing.assert_allclose(abs(psf.kValue(psf.maxk,0).real)/psf.flux, thresh, rtol=rtol, atol=atol)
221243

222244

223245
@timer
@@ -326,7 +348,7 @@ def test_moffat_radii():
326348

327349
# Test constructor using half-light-radius:
328350
test_gal = galsim.Moffat(flux=1., beta=test_beta, half_light_radius=test_hlr,
329-
trunc=2*test_hlr)
351+
trunc=2*test_hlr * USE_TRUNC)
330352
hlr_sum = radial_integrate(test_gal, 0., test_hlr)
331353
print('hlr_sum = ',hlr_sum)
332354
np.testing.assert_almost_equal(
@@ -356,12 +378,15 @@ def test_moffat_radii():
356378
np.testing.assert_equal(
357379
test_gal.scale_radius, got_scale,
358380
err_msg="Moffat scale_radius returned wrong value")
359-
np.testing.assert_equal(
360-
test_gal.trunc, 2*test_hlr,
361-
err_msg="Moffat trunc returned wrong value")
381+
if is_jax_galsim():
382+
pass
383+
else:
384+
np.testing.assert_equal(
385+
test_gal.trunc, 2*test_hlr,
386+
err_msg="Moffat trunc returned wrong value")
362387

363388
# Test constructor using scale radius:
364-
test_gal = galsim.Moffat(flux=1., beta=test_beta, trunc=2*test_scale,
389+
test_gal = galsim.Moffat(flux=1., beta=test_beta, trunc=2*test_scale * USE_TRUNC,
365390
scale_radius=test_scale)
366391
center = test_gal.xValue(galsim.PositionD(0,0))
367392
ratio = test_gal.xValue(galsim.PositionD(test_scale,0)) / center
@@ -389,7 +414,7 @@ def test_moffat_radii():
389414
err_msg="Error in FWHM for truncated Moffat initialized with scale radius")
390415

391416
# Test constructor using FWHM:
392-
test_gal = galsim.Moffat(flux=1, beta=test_beta, trunc=2.*test_fwhm,
417+
test_gal = galsim.Moffat(flux=1, beta=test_beta, trunc=2.*test_fwhm * USE_TRUNC,
393418
fwhm = test_fwhm)
394419
center = test_gal.xValue(galsim.PositionD(0,0))
395420
ratio = test_gal.xValue(galsim.PositionD(test_fwhm/2.,0)) / center
@@ -436,19 +461,19 @@ def test_moffat_flux_scaling():
436461
for test_trunc in [ 0., 8.5 ]:
437462

438463
# init with scale_radius only (should be ok given last tests)
439-
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc,
464+
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc * USE_TRUNC,
440465
flux=test_flux)
441466
obj *= 2.
442467
np.testing.assert_almost_equal(
443468
obj.flux, test_flux * 2., decimal=param_decimal,
444469
err_msg="Flux param inconsistent after __imul__.")
445-
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc,
470+
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc * USE_TRUNC,
446471
flux=test_flux)
447472
obj /= 2.
448473
np.testing.assert_almost_equal(
449474
obj.flux, test_flux / 2., decimal=param_decimal,
450475
err_msg="Flux param inconsistent after __idiv__.")
451-
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc,
476+
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc * USE_TRUNC,
452477
flux=test_flux)
453478
obj2 = obj * 2.
454479
# First test that original obj is unharmed...
@@ -459,7 +484,7 @@ def test_moffat_flux_scaling():
459484
np.testing.assert_almost_equal(
460485
obj2.flux, test_flux * 2., decimal=param_decimal,
461486
err_msg="Flux param inconsistent after __rmul__ (result).")
462-
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc,
487+
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc * USE_TRUNC,
463488
flux=test_flux)
464489
obj2 = 2. * obj
465490
# First test that original obj is unharmed...
@@ -470,7 +495,7 @@ def test_moffat_flux_scaling():
470495
np.testing.assert_almost_equal(
471496
obj2.flux, test_flux * 2., decimal=param_decimal,
472497
err_msg="Flux param inconsistent after __mul__ (result).")
473-
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc,
498+
obj = galsim.Moffat(scale_radius=test_scale, beta=test_beta, trunc=test_trunc * USE_TRUNC,
474499
flux=test_flux)
475500
obj2 = obj / 2.
476501
# First test that original obj is unharmed...
@@ -522,7 +547,7 @@ def test_moffat_shoot():
522547

523548

524549
@timer
525-
def test_ne():
550+
def test_moffat_ne():
526551
"""Test base.py GSObjects for not-equals."""
527552
# Define some universal gsps
528553
gsp = galsim.GSParams(maxk_threshold=1.1e-3, folding_threshold=5.1e-3)
@@ -534,9 +559,12 @@ def test_ne():
534559
galsim.Moffat(beta=3.0, scale_radius=1.1),
535560
galsim.Moffat(beta=3.0, half_light_radius=1.2),
536561
galsim.Moffat(beta=3.0, fwhm=1.3),
537-
galsim.Moffat(beta=3.0, scale_radius=1.1, trunc=2.0),
538562
galsim.Moffat(beta=3.0, scale_radius=1.0, flux=1.1),
539563
galsim.Moffat(beta=3.0, scale_radius=1.0, gsparams=gsp)]
564+
if is_jax_galsim():
565+
pass
566+
else:
567+
gals += [galsim.Moffat(beta=3.0, scale_radius=1.1, trunc=2.0)]
540568
check_all_diff(gals)
541569

542570

0 commit comments

Comments
 (0)