LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Public Member Functions | Static Public Attributes | List of all members
lsst.pipe.tasks.scaleVariance.ScaleVarianceTask Class Reference
Inheritance diagram for lsst.pipe.tasks.scaleVariance.ScaleVarianceTask:

Public Member Functions

def __init__ (self, *args, **kwargs)
 
def subtractedBackground (self, maskedImage)
 
def run (self, maskedImage)
 
def computeScaleFactors (self, maskedImage)
 
def pixelBased (self, maskedImage)
 
def imageBased (self, maskedImage)
 

Static Public Attributes

 ConfigClass = ScaleVarianceConfig
 

Detailed Description

Scale the variance in a MaskedImage

The variance plane in a convolved or warped image (or a coadd derived
from warped images) does not accurately reflect the noise properties of
the image because variance has been lost to covariance. This Task
attempts to correct for this by scaling the variance plane to match
the observed variance in the image. This is not perfect (because we're
not tracking the covariance) but it's simple and is often good enough.

The task implements a pixel-based and an image-based correction estimator.

Definition at line 48 of file scaleVariance.py.

Constructor & Destructor Documentation

◆ __init__()

def lsst.pipe.tasks.scaleVariance.ScaleVarianceTask.__init__ (   self,
args,
**  kwargs 
)

Definition at line 63 of file scaleVariance.py.

63  def __init__(self, *args, **kwargs):
64  Task.__init__(self, *args, **kwargs)
65  self.makeSubtask("background")
66 

Member Function Documentation

◆ computeScaleFactors()

def lsst.pipe.tasks.scaleVariance.ScaleVarianceTask.computeScaleFactors (   self,
  maskedImage 
)
Calculate and return both variance scaling factors without modifying the image.

Parameters
----------
maskedImage :  `lsst.afw.image.MaskedImage`
    Image for which to determine the variance rescaling factor.

Returns
-------
R : `lsst.pipe.base.Struct`
  - ``pixelFactor`` : `float` The pixel based variance rescaling factor
    or 1 if all pixels are masked or invalid.
  - ``imageFactor`` : `float` The image based variance rescaling factor
    or 1 if all pixels are masked or invalid.

Definition at line 135 of file scaleVariance.py.

135  def computeScaleFactors(self, maskedImage):
136  """Calculate and return both variance scaling factors without modifying the image.
137 
138  Parameters
139  ----------
140  maskedImage : `lsst.afw.image.MaskedImage`
141  Image for which to determine the variance rescaling factor.
142 
143  Returns
144  -------
145  R : `lsst.pipe.base.Struct`
146  - ``pixelFactor`` : `float` The pixel based variance rescaling factor
147  or 1 if all pixels are masked or invalid.
148  - ``imageFactor`` : `float` The image based variance rescaling factor
149  or 1 if all pixels are masked or invalid.
150  """
151  with self.subtractedBackground(maskedImage):
152  pixelFactor = self.pixelBased(maskedImage)
153  imageFactor = self.imageBased(maskedImage)
154  return Struct(pixelFactor=pixelFactor, imageFactor=imageFactor)
155 

◆ imageBased()

def lsst.pipe.tasks.scaleVariance.ScaleVarianceTask.imageBased (   self,
  maskedImage 
)
Determine the variance rescaling factor from image statistics

We calculate average(SNR) = stdev(image)/median(variance), and
the value should be unity. We use the interquartile range as a robust
estimator of the stdev. The variance rescaling factor is the
factor that brings this value to unity.

This may not work well if the pixels from which we measure the
standard deviation of the image are not effectively the same pixels
from which we measure the median of the variance. In that case, use
an alternate method.

Parameters
----------
maskedImage :  `lsst.afw.image.MaskedImage`
    Image for which to determine the variance rescaling factor.

Returns
-------
factor : `float`
    Variance rescaling factor or 1 if all pixels are masked or non-finite.

Definition at line 198 of file scaleVariance.py.

198  def imageBased(self, maskedImage):
199  """Determine the variance rescaling factor from image statistics
200 
201  We calculate average(SNR) = stdev(image)/median(variance), and
202  the value should be unity. We use the interquartile range as a robust
203  estimator of the stdev. The variance rescaling factor is the
204  factor that brings this value to unity.
205 
206  This may not work well if the pixels from which we measure the
207  standard deviation of the image are not effectively the same pixels
208  from which we measure the median of the variance. In that case, use
209  an alternate method.
210 
211  Parameters
212  ----------
213  maskedImage : `lsst.afw.image.MaskedImage`
214  Image for which to determine the variance rescaling factor.
215 
216  Returns
217  -------
218  factor : `float`
219  Variance rescaling factor or 1 if all pixels are masked or non-finite.
220  """
221  maskVal = maskedImage.mask.getPlaneBitMask(self.config.maskPlanes)
222  isGood = (((maskedImage.mask.array & maskVal) == 0)
223  & np.isfinite(maskedImage.image.array)
224  & np.isfinite(maskedImage.variance.array)
225  & (maskedImage.variance.array > 0))
226  nGood = np.sum(isGood)
227  self.log.debug("Number of selected background pixels: %d of %d.", nGood, isGood.size)
228  if nGood < 2:
229  # Not enough good data, np.percentile needs at least 2 points
230  # to estimate a range
231  return 1.0
232  # Robust measurement of stdev
233  q1, q3 = np.percentile(maskedImage.image.array[isGood], (25, 75))
234  ratio = 0.74*(q3 - q1)/np.sqrt(np.median(maskedImage.variance.array[isGood]))
235  return ratio**2

◆ pixelBased()

def lsst.pipe.tasks.scaleVariance.ScaleVarianceTask.pixelBased (   self,
  maskedImage 
)
Determine the variance rescaling factor from pixel statistics

We calculate SNR = image/sqrt(variance), and the distribution
for most of the background-subtracted image should have a standard
deviation of unity. We use the interquartile range as a robust estimator
of the SNR standard deviation. The variance rescaling factor is the
factor that brings that distribution to have unit standard deviation.

This may not work well if the image has a lot of structure in it, as
the assumptions are violated. In that case, use an alternate
method.

Parameters
----------
maskedImage : `lsst.afw.image.MaskedImage`
    Image for which to determine the variance rescaling factor.

Returns
-------
factor : `float`
    Variance rescaling factor or 1 if all pixels are masked or non-finite.

Definition at line 156 of file scaleVariance.py.

156  def pixelBased(self, maskedImage):
157  """Determine the variance rescaling factor from pixel statistics
158 
159  We calculate SNR = image/sqrt(variance), and the distribution
160  for most of the background-subtracted image should have a standard
161  deviation of unity. We use the interquartile range as a robust estimator
162  of the SNR standard deviation. The variance rescaling factor is the
163  factor that brings that distribution to have unit standard deviation.
164 
165  This may not work well if the image has a lot of structure in it, as
166  the assumptions are violated. In that case, use an alternate
167  method.
168 
169  Parameters
170  ----------
171  maskedImage : `lsst.afw.image.MaskedImage`
172  Image for which to determine the variance rescaling factor.
173 
174  Returns
175  -------
176  factor : `float`
177  Variance rescaling factor or 1 if all pixels are masked or non-finite.
178 
179  """
180  maskVal = maskedImage.mask.getPlaneBitMask(self.config.maskPlanes)
181  isGood = (((maskedImage.mask.array & maskVal) == 0)
182  & np.isfinite(maskedImage.image.array)
183  & np.isfinite(maskedImage.variance.array)
184  & (maskedImage.variance.array > 0))
185 
186  nGood = np.sum(isGood)
187  self.log.debug("Number of selected background pixels: %d of %d.", nGood, isGood.size)
188  if nGood < 2:
189  # Not enough good data, np.percentile needs at least 2 points
190  # to estimate a range
191  return 1.0
192  # Robust measurement of stdev using inter-quartile range
193  snr = maskedImage.image.array[isGood]/np.sqrt(maskedImage.variance.array[isGood])
194  q1, q3 = np.percentile(snr, (25, 75))
195  stdev = 0.74*(q3 - q1)
196  return stdev**2
197 

◆ run()

def lsst.pipe.tasks.scaleVariance.ScaleVarianceTask.run (   self,
  maskedImage 
)
Rescale the variance in a maskedImage in place.

Parameters
----------
maskedImage :  `lsst.afw.image.MaskedImage`
    Image for which to determine the variance rescaling factor. The image
    is modified in place.

Returns
-------
factor : `float`
    Variance rescaling factor.

Raises
------
RuntimeError
    If the estimated variance rescaling factor by both methods exceed the
    configured limit.

Notes
-----
The task calculates and applies the pixel-based correction unless
it is over the ``config.limit`` threshold. In this case, the image-based
method is applied.

Definition at line 96 of file scaleVariance.py.

96  def run(self, maskedImage):
97  """Rescale the variance in a maskedImage in place.
98 
99  Parameters
100  ----------
101  maskedImage : `lsst.afw.image.MaskedImage`
102  Image for which to determine the variance rescaling factor. The image
103  is modified in place.
104 
105  Returns
106  -------
107  factor : `float`
108  Variance rescaling factor.
109 
110  Raises
111  ------
112  RuntimeError
113  If the estimated variance rescaling factor by both methods exceed the
114  configured limit.
115 
116  Notes
117  -----
118  The task calculates and applies the pixel-based correction unless
119  it is over the ``config.limit`` threshold. In this case, the image-based
120  method is applied.
121  """
122  with self.subtractedBackground(maskedImage):
123  factor = self.pixelBased(maskedImage)
124  if factor > self.config.limit:
125  self.log.warning("Pixel-based variance rescaling factor (%f) exceeds configured limit (%f); "
126  "trying image-based method", factor, self.config.limit)
127  factor = self.imageBased(maskedImage)
128  if factor > self.config.limit:
129  raise RuntimeError("Variance rescaling factor (%f) exceeds configured limit (%f)" %
130  (factor, self.config.limit))
131  self.log.info("Renormalizing variance by %f", factor)
132  maskedImage.variance *= factor
133  return factor
134 
def run(self, coaddExposures, bbox, wcs)
Definition: getTemplate.py:603

◆ subtractedBackground()

def lsst.pipe.tasks.scaleVariance.ScaleVarianceTask.subtractedBackground (   self,
  maskedImage 
)
Context manager for subtracting the background

We need to subtract the background so that the entire image
(apart from objects, which should be clipped) will have the
image/sqrt(variance) distributed about zero.

This context manager subtracts the background, and ensures it
is restored on exit.

Parameters
----------
maskedImage : `lsst.afw.image.MaskedImage`
    Image+mask+variance to have background subtracted and restored.

Returns
-------
context : context manager
    Context manager that ensure the background is restored.

Definition at line 68 of file scaleVariance.py.

68  def subtractedBackground(self, maskedImage):
69  """Context manager for subtracting the background
70 
71  We need to subtract the background so that the entire image
72  (apart from objects, which should be clipped) will have the
73  image/sqrt(variance) distributed about zero.
74 
75  This context manager subtracts the background, and ensures it
76  is restored on exit.
77 
78  Parameters
79  ----------
80  maskedImage : `lsst.afw.image.MaskedImage`
81  Image+mask+variance to have background subtracted and restored.
82 
83  Returns
84  -------
85  context : context manager
86  Context manager that ensure the background is restored.
87  """
88  bg = self.background.fitBackground(maskedImage)
89  bgImage = bg.getImageF(self.background.config.algorithm, self.background.config.undersampleStyle)
90  maskedImage -= bgImage
91  try:
92  yield
93  finally:
94  maskedImage += bgImage
95 

Member Data Documentation

◆ ConfigClass

lsst.pipe.tasks.scaleVariance.ScaleVarianceTask.ConfigClass = ScaleVarianceConfig
static

Definition at line 60 of file scaleVariance.py.


The documentation for this class was generated from the following file: