LSST Applications 24.1.5,g02d81e74bb+fa3a7a026e,g180d380827+a53a32eff8,g2079a07aa2+86d27d4dc4,g2305ad1205+c0501b3732,g295015adf3+7d3e92f0ec,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+5dd1654d75,g48712c4677+3bf1020dcb,g487adcacf7+065c13d9cf,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+d7ac436cfb,g5a732f18d5+53520f316c,g64a986408d+fa3a7a026e,g858d7b2824+fa3a7a026e,g8a8a8dda67+585e252eca,g99cad8db69+a5a909b84f,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+4cf350ccb2,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+f991a0b59f,gc120e1dc64+9ccbfdb8be,gc28159a63d+0e5473021a,gcf0d15dbbd+5dd1654d75,gd96a1ce819+42fd0ee607,gdaeeff99f8+f9a426f77a,ge6526c86ff+0d71447b4b,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+fa3a7a026e
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Static Protected Attributes | List of all members
lsst.meas.algorithms.scaleVariance.ScaleVarianceTask Class Reference
Inheritance diagram for lsst.meas.algorithms.scaleVariance.ScaleVarianceTask:
lsst.pipe.tasks.scaleVariance.ScaleVarianceTask

Public Member Functions

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

Static Public Attributes

 ConfigClass = ScaleVarianceConfig
 

Static Protected Attributes

str _DefaultName = "scaleVariance"
 

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 47 of file scaleVariance.py.

Constructor & Destructor Documentation

◆ __init__()

lsst.meas.algorithms.scaleVariance.ScaleVarianceTask.__init__ ( self,
* args,
** kwargs )

Definition at line 62 of file scaleVariance.py.

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

Member Function Documentation

◆ computeScaleFactors()

lsst.meas.algorithms.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 134 of file scaleVariance.py.

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

◆ imageBased()

lsst.meas.algorithms.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 197 of file scaleVariance.py.

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

◆ pixelBased()

lsst.meas.algorithms.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 155 of file scaleVariance.py.

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

◆ run()

lsst.meas.algorithms.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 95 of file scaleVariance.py.

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

◆ subtractedBackground()

lsst.meas.algorithms.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 67 of file scaleVariance.py.

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

Member Data Documentation

◆ _DefaultName

str lsst.meas.algorithms.scaleVariance.ScaleVarianceTask._DefaultName = "scaleVariance"
staticprotected

Definition at line 60 of file scaleVariance.py.

◆ ConfigClass

lsst.meas.algorithms.scaleVariance.ScaleVarianceTask.ConfigClass = ScaleVarianceConfig
static

Definition at line 59 of file scaleVariance.py.


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