Skip to content

Commit

Permalink
Avoid numpy warnings of invalid values. Prevent correction factor fro…
Browse files Browse the repository at this point in the history
…m being nan.
  • Loading branch information
Gabor Kovacs committed Feb 9, 2021
1 parent 17ea508 commit ca1f9e8
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions python/lsst/pipe/tasks/scaleVariance.py
Expand Up @@ -121,11 +121,11 @@ def run(self, maskedImage):
"""
with self.subtractedBackground(maskedImage):
factor = self.pixelBased(maskedImage)
if np.isnan(factor) or factor > self.config.limit:
if factor > self.config.limit:
self.log.warn("Pixel-based variance rescaling factor (%f) exceeds configured limit (%f); "
"trying image-based method", factor, self.config.limit)
factor = self.imageBased(maskedImage)
if np.isnan(factor) or factor > self.config.limit:
if factor > self.config.limit:
raise RuntimeError("Variance rescaling factor (%f) exceeds configured limit (%f)" %
(factor, self.config.limit))
self.log.info("Renormalizing variance by %f" % (factor,))
Expand All @@ -143,10 +143,9 @@ def calculateBothFactors(self, maskedImage):
Returns
-------
R : `lsst.pipe.base.Struct`
- ``pixFactor : `float` The pixel based variance rescaling factor
or `nan` if all pixels are masked or invalid.
- ``pixFactor : `float` The pixel based variance rescaling factor.
- ``imageFactor`` : `float` The image based variance rescaling factor
or `nan` if all pixels are masked or invalid.
or 1 if all pixels are masked or invalid.
"""
with self.subtractedBackground(maskedImage):
pixFactor = self.pixelBased(maskedImage)
Expand Down Expand Up @@ -174,24 +173,24 @@ def pixelBased(self, maskedImage):
Returns
-------
factor : `float`
Variance rescaling factor.
Variance rescaling factor or 1 if all pixels are masked or non-finite.
"""
maskVal = maskedImage.mask.getPlaneBitMask(self.config.maskPlanes)
isGood = ((maskedImage.mask.array & maskVal) == 0) & (maskedImage.variance.array > 0)
self.log.debugf("Number of selected background pixels: {} of {}.",
np.sum(isGood), isGood.size)

variance = maskedImage.variance
snr = maskedImage.image.array/np.sqrt(variance.array)
# Robust measurement of stdev using inter-quartile range
try:
q1, q3 = np.percentile(snr[isGood], (25, 75))
except IndexError:
# This error is raised when all data is nan. Catch error so that it does not
# propagate any higher. Set the stdev to one will not fix the bad data, but
# it will allow the task to complete. It should be the job of higher level
# tasks to handle missing or bad data
isGood = (((maskedImage.mask.array & maskVal) == 0)
& np.isfinite(maskedImage.image.array)
& np.isfinite(maskedImage.variance.array)
& (maskedImage.variance.array > 0))

nGood = np.sum(isGood)
self.log.debugf("Number of selected background pixels: {} of {}.", nGood, isGood.size)
if nGood < 2:
# Not enough good data, np.percentile needs at least 2 points
# to estimate a range
return 1.0
# Robust measurement of stdev using inter-quartile range
snr = maskedImage.image.array[isGood]/np.sqrt(maskedImage.variance.array[isGood])
q1, q3 = np.percentile(snr, (25, 75))
stdev = 0.74*(q3 - q1)
return stdev**2

Expand All @@ -216,12 +215,19 @@ def imageBased(self, maskedImage):
Returns
-------
factor : `float`
Variance rescaling factor.
Variance rescaling factor or 1 if all pixels are masked or non-finite.
"""
maskVal = maskedImage.mask.getPlaneBitMask(self.config.maskPlanes)
isGood = ((maskedImage.mask.array & maskVal) == 0) & (maskedImage.variance.array > 0)
self.log.debugf("Number of selected background pixels: {} of {}.",
np.sum(isGood), isGood.size)
isGood = (((maskedImage.mask.array & maskVal) == 0)
& np.isfinite(maskedImage.image.array)
& np.isfinite(maskedImage.variance.array)
& (maskedImage.variance.array > 0))
nGood = np.sum(isGood)
self.log.debugf("Number of selected background pixels: {} of {}.", nGood, isGood.size)
if nGood < 2:
# Not enough good data, np.percentile needs at least 2 points
# to estimate a range
return 1.0
# Robust measurement of stdev
q1, q3 = np.percentile(maskedImage.image.array[isGood], (25, 75))
ratio = 0.74*(q3 - q1)/np.sqrt(np.median(maskedImage.variance.array[isGood]))
Expand Down

0 comments on commit ca1f9e8

Please sign in to comment.