@@ -489,6 +489,27 @@ def __setstate__(self, d):
489
489
HSMParams .default = HSMParams ()
490
490
491
491
492
+ # A helper function that checks if the weight and the badpix bounds are
493
+ # consistent with that of the image, and that the weight is non-negative.
494
+ def _checkWeightAndBadpix (image , weight = None , badpix = None ):
495
+ # Check that the weight and badpix, if given, are sensible and compatible
496
+ # with the image.
497
+ if weight is not None :
498
+ if weight .bounds != image .bounds :
499
+ raise GalSimIncompatibleValuesError (
500
+ "Weight image does not have same bounds as the input Image." ,
501
+ weight = weight , image = image )
502
+ # also make sure there are no negative values
503
+
504
+ if np .any (weight .array < 0 ):
505
+ raise GalSimValueError ("Weight image cannot contain negative values." , weight )
506
+
507
+ if badpix and badpix .bounds != image .bounds :
508
+ raise GalSimIncompatibleValuesError (
509
+ "Badpix image does not have the same bounds as the input Image." ,
510
+ badpix = badpix , image = image )
511
+
512
+
492
513
# A helper function for taking input weight and badpix Images, and returning a weight Image in the
493
514
# format that the C++ functions want
494
515
def _convertMask (image , weight = None , badpix = None ):
@@ -498,18 +519,7 @@ def _convertMask(image, weight=None, badpix=None):
498
519
# if no weight image was supplied, make an int array (same size as gal image) filled with 1's
499
520
if weight is None :
500
521
mask = ImageI (bounds = image .bounds , init_value = 1 )
501
-
502
522
else :
503
- # if weight image was supplied, check if it has the right bounds and is non-negative
504
- if weight .bounds != image .bounds :
505
- raise GalSimIncompatibleValuesError (
506
- "Weight image does not have same bounds as the input Image." ,
507
- weight = weight , image = image )
508
-
509
- # also make sure there are no negative values
510
- if np .any (weight .array < 0 ):
511
- raise GalSimValueError ("Weight image cannot contain negative values." , weight )
512
-
513
523
# if weight is an ImageI, then we can use it as the mask image:
514
524
if weight .dtype == np .int32 :
515
525
if not badpix :
@@ -526,10 +536,6 @@ def _convertMask(image, weight=None, badpix=None):
526
536
# if badpix image was supplied, identify the nonzero (bad) pixels and set them to zero in weight
527
537
# image; also check bounds
528
538
if badpix is not None :
529
- if badpix .bounds != image .bounds :
530
- raise GalSimIncompatibleValuesError (
531
- "Badpix image does not have the same bounds as the input Image." ,
532
- badpix = badpix , image = image )
533
539
mask .array [badpix .array != 0 ] = 0
534
540
535
541
# if no pixels are used, raise an exception
@@ -671,8 +677,10 @@ def EstimateShear(gal_image, PSF_image, weight=None, badpix=None, sky_var=0.0,
671
677
# prepare inputs to C++ routines: ImageF or ImageD for galaxy, PSF, and ImageI for weight map
672
678
gal_image = _convertImage (gal_image )
673
679
PSF_image = _convertImage (PSF_image )
674
- weight = _convertMask (gal_image , weight = weight , badpix = badpix )
680
+ _checkWeightAndBadpix (gal_image , weight = weight , badpix = badpix )
675
681
hsmparams = HSMParams .check (hsmparams )
682
+ weight = _convertMask (gal_image , weight = weight , badpix = badpix )
683
+
676
684
677
685
if guess_centroid is None :
678
686
guess_centroid = gal_image .true_center
@@ -804,8 +812,10 @@ def FindAdaptiveMom(object_image, weight=None, badpix=None, guess_sig=5.0, preci
804
812
"""
805
813
# prepare inputs to C++ routines: ImageF or ImageD for galaxy, PSF, and ImageI for weight map
806
814
object_image = _convertImage (object_image )
807
- weight = _convertMask (object_image , weight = weight , badpix = badpix )
815
+
816
+ _checkWeightAndBadpix (object_image , weight = weight , badpix = badpix )
808
817
hsmparams = HSMParams .check (hsmparams )
818
+ weight = _convertMask (object_image , weight = weight , badpix = badpix )
809
819
810
820
if guess_centroid is None :
811
821
guess_centroid = object_image .true_center
0 commit comments