Skip to content

Commit

Permalink
TST: Add unit tests for check=True/False
Browse files Browse the repository at this point in the history
  • Loading branch information
arunkannawadi committed Mar 11, 2024
1 parent 8e32318 commit 86984ba
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/test_hsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,56 @@ def check_equal(resm, ress, resm_test, ress_test, tag):
"when masking with badpix and weight map")


@timer
def test_masks_with_check():
"""Test which errors in masks for moments and shear estimation routines are caught with check."""
# Set up some toy galaxy and PSF
my_sigma = 1.0
my_pixscale = 0.1
my_g1 = 0.15
my_g2 = -0.4
imsize = 256
g = galsim.Gaussian(sigma = my_sigma)
p = galsim.Gaussian(sigma = my_sigma)

g = g.shear(g1=my_g1, g2=my_g2)
obj = galsim.Convolve(g, p)
im = galsim.ImageF(imsize, imsize)
p_im = galsim.ImageF(imsize, imsize)
obj.drawImage(image=im, scale=my_pixscale, method='no_pixel')
p.drawImage(image=p_im, scale=my_pixscale, method='no_pixel')

## Create a weight image with size different from image
## Let it be smaller in one dimension, and larger in the other.
bad_weight_im = galsim.ImageI(imsize, 2*imsize, init_value=1)
small_weight_im = galsim.ImageI(2*imsize, imsize//4, init_value=1)
# Setting strict=True (default) catches this error even if check=False.
galsim.hsm.FindAdaptiveMom(im, small_weight_im, strict=False, check=False)
assert_raises(galsim.errors.GalSimHSMError, galsim.hsm.FindAdaptiveMom, im, small_weight_im, strict=True, check=False)

# A zero weight image raises errors,
zero_weight_im = galsim.ImageI(imsize, 2*imsize, init_value=0)
assert_raises(galsim.errors.GalSimHSMError, galsim.hsm.FindAdaptiveMom, im, zero_weight_im, check=False)
assert_raises(galsim.errors.GalSimHSMError, galsim.hsm.EstimateShear, im, p_im, zero_weight_im, strict=True, check=False)
# but a negative weight image can slip through with check=False.
negative_weight_im = galsim.ImageI(imsize, 2*imsize, init_value=-1)
galsim.hsm.FindAdaptiveMom(im, negative_weight_im, check=False)
galsim.hsm.EstimateShear(im, p_im, negative_weight_im, check=False)
# But these are unique to ImageI. Passing the same as ImageS will catch the error, but is more expensive due to deep copy.
negative_weight_im_singleprecision = galsim.ImageS(imsize, imsize, init_value=-1)
assert_raises(galsim.errors.GalSimHSMError, galsim.hsm.FindAdaptiveMom, im, negative_weight_im_singleprecision, check=False)

# But all of these errors are caught with check=True.
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.FindAdaptiveMom, im, bad_weight_im, check=True)
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.FindAdaptiveMom, im, zero_weight_im, check=True)
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.FindAdaptiveMom, im, negative_weight_im, check=True)
assert_raises(galsim.errors.GalSimValueError, galsim.hsm.FindAdaptiveMom, im, negative_weight_im_singleprecision, check=True)
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.EstimateShear, im, p_im, bad_weight_im, check=True)
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.EstimateShear, im, p_im, zero_weight_im, check=True)
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.EstimateShear, im, p_im, negative_weight_im, check=True)
assert_raises(galsim.errors.GalSimValueError, galsim.hsm.EstimateShear, im, p_im, negative_weight_im_singleprecision, check=True)


@timer
def test_shearest_shape():
"""Test that shear estimation is insensitive to shape of input images."""
Expand Down

0 comments on commit 86984ba

Please sign in to comment.