Skip to content

Commit 86984ba

Browse files
committed
TST: Add unit tests for check=True/False
1 parent 8e32318 commit 86984ba

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/test_hsm.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,56 @@ def check_equal(resm, ress, resm_test, ress_test, tag):
504504
"when masking with badpix and weight map")
505505

506506

507+
@timer
508+
def test_masks_with_check():
509+
"""Test which errors in masks for moments and shear estimation routines are caught with check."""
510+
# Set up some toy galaxy and PSF
511+
my_sigma = 1.0
512+
my_pixscale = 0.1
513+
my_g1 = 0.15
514+
my_g2 = -0.4
515+
imsize = 256
516+
g = galsim.Gaussian(sigma = my_sigma)
517+
p = galsim.Gaussian(sigma = my_sigma)
518+
519+
g = g.shear(g1=my_g1, g2=my_g2)
520+
obj = galsim.Convolve(g, p)
521+
im = galsim.ImageF(imsize, imsize)
522+
p_im = galsim.ImageF(imsize, imsize)
523+
obj.drawImage(image=im, scale=my_pixscale, method='no_pixel')
524+
p.drawImage(image=p_im, scale=my_pixscale, method='no_pixel')
525+
526+
## Create a weight image with size different from image
527+
## Let it be smaller in one dimension, and larger in the other.
528+
bad_weight_im = galsim.ImageI(imsize, 2*imsize, init_value=1)
529+
small_weight_im = galsim.ImageI(2*imsize, imsize//4, init_value=1)
530+
# Setting strict=True (default) catches this error even if check=False.
531+
galsim.hsm.FindAdaptiveMom(im, small_weight_im, strict=False, check=False)
532+
assert_raises(galsim.errors.GalSimHSMError, galsim.hsm.FindAdaptiveMom, im, small_weight_im, strict=True, check=False)
533+
534+
# A zero weight image raises errors,
535+
zero_weight_im = galsim.ImageI(imsize, 2*imsize, init_value=0)
536+
assert_raises(galsim.errors.GalSimHSMError, galsim.hsm.FindAdaptiveMom, im, zero_weight_im, check=False)
537+
assert_raises(galsim.errors.GalSimHSMError, galsim.hsm.EstimateShear, im, p_im, zero_weight_im, strict=True, check=False)
538+
# but a negative weight image can slip through with check=False.
539+
negative_weight_im = galsim.ImageI(imsize, 2*imsize, init_value=-1)
540+
galsim.hsm.FindAdaptiveMom(im, negative_weight_im, check=False)
541+
galsim.hsm.EstimateShear(im, p_im, negative_weight_im, check=False)
542+
# But these are unique to ImageI. Passing the same as ImageS will catch the error, but is more expensive due to deep copy.
543+
negative_weight_im_singleprecision = galsim.ImageS(imsize, imsize, init_value=-1)
544+
assert_raises(galsim.errors.GalSimHSMError, galsim.hsm.FindAdaptiveMom, im, negative_weight_im_singleprecision, check=False)
545+
546+
# But all of these errors are caught with check=True.
547+
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.FindAdaptiveMom, im, bad_weight_im, check=True)
548+
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.FindAdaptiveMom, im, zero_weight_im, check=True)
549+
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.FindAdaptiveMom, im, negative_weight_im, check=True)
550+
assert_raises(galsim.errors.GalSimValueError, galsim.hsm.FindAdaptiveMom, im, negative_weight_im_singleprecision, check=True)
551+
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.EstimateShear, im, p_im, bad_weight_im, check=True)
552+
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.EstimateShear, im, p_im, zero_weight_im, check=True)
553+
assert_raises(galsim.errors.GalSimIncompatibleValuesError, galsim.hsm.EstimateShear, im, p_im, negative_weight_im, check=True)
554+
assert_raises(galsim.errors.GalSimValueError, galsim.hsm.EstimateShear, im, p_im, negative_weight_im_singleprecision, check=True)
555+
556+
507557
@timer
508558
def test_shearest_shape():
509559
"""Test that shear estimation is insensitive to shape of input images."""

0 commit comments

Comments
 (0)