Skip to content

Commit 6c5d815

Browse files
committed
REF: Move checks out of _convertMask
1 parent 0a08d0e commit 6c5d815

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

galsim/hsm.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,27 @@ def __setstate__(self, d):
489489
HSMParams.default = HSMParams()
490490

491491

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+
492513
# A helper function for taking input weight and badpix Images, and returning a weight Image in the
493514
# format that the C++ functions want
494515
def _convertMask(image, weight=None, badpix=None):
@@ -498,18 +519,7 @@ def _convertMask(image, weight=None, badpix=None):
498519
# if no weight image was supplied, make an int array (same size as gal image) filled with 1's
499520
if weight is None:
500521
mask = ImageI(bounds=image.bounds, init_value=1)
501-
502522
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-
513523
# if weight is an ImageI, then we can use it as the mask image:
514524
if weight.dtype == np.int32:
515525
if not badpix:
@@ -526,10 +536,6 @@ def _convertMask(image, weight=None, badpix=None):
526536
# if badpix image was supplied, identify the nonzero (bad) pixels and set them to zero in weight
527537
# image; also check bounds
528538
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)
533539
mask.array[badpix.array != 0] = 0
534540

535541
# 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,
671677
# prepare inputs to C++ routines: ImageF or ImageD for galaxy, PSF, and ImageI for weight map
672678
gal_image = _convertImage(gal_image)
673679
PSF_image = _convertImage(PSF_image)
674-
weight = _convertMask(gal_image, weight=weight, badpix=badpix)
680+
_checkWeightAndBadpix(gal_image, weight=weight, badpix=badpix)
675681
hsmparams = HSMParams.check(hsmparams)
682+
weight = _convertMask(gal_image, weight=weight, badpix=badpix)
683+
676684

677685
if guess_centroid is None:
678686
guess_centroid = gal_image.true_center
@@ -804,8 +812,10 @@ def FindAdaptiveMom(object_image, weight=None, badpix=None, guess_sig=5.0, preci
804812
"""
805813
# prepare inputs to C++ routines: ImageF or ImageD for galaxy, PSF, and ImageI for weight map
806814
object_image = _convertImage(object_image)
807-
weight = _convertMask(object_image, weight=weight, badpix=badpix)
815+
816+
_checkWeightAndBadpix(object_image, weight=weight, badpix=badpix)
808817
hsmparams = HSMParams.check(hsmparams)
818+
weight = _convertMask(object_image, weight=weight, badpix=badpix)
809819

810820
if guess_centroid is None:
811821
guess_centroid = object_image.true_center

0 commit comments

Comments
 (0)