LSST Applications g1653933729+34a971ddd9,g1a997c3884+34a971ddd9,g28da252d5a+32b12a4112,g2bbee38e9b+ffc559ee46,g2bc492864f+ffc559ee46,g2cdde0e794+704103fe75,g3156d2b45e+6e87dc994a,g347aa1857d+ffc559ee46,g35bb328faa+34a971ddd9,g3a166c0a6a+ffc559ee46,g3e281a1b8c+8ec26ec694,g4005a62e65+ba0306790b,g414038480c+9f5be647b3,g41af890bb2+686ab7cd58,g4e1a3235cc+1fd785b14f,g7b55021d1b+da0d0eec6b,g80478fca09+6e21f9dc20,g82479be7b0+6fa35d7547,g858d7b2824+356eaf3287,g9125e01d80+34a971ddd9,ga5288a1d22+ba1f9bb3ef,gae0086650b+34a971ddd9,gb4748cc0e8+fd26a265a0,gb4ec7eb0ab+356eaf3287,gb58c049af0+ace264a4f2,gc081298178+30f12d1e69,gc28159a63d+ffc559ee46,gcf0d15dbbd+4adb3139bd,gd6b7c0dfd1+aae9efebf5,gda3e153d99+356eaf3287,gda6a2b7d83+4adb3139bd,gdaeeff99f8+7774323b41,gdd5a9049c5+ea78b51778,ge2409df99d+fa478de0b0,ge33fd446bb+356eaf3287,ge79ae78c31+ffc559ee46,gf0baf85859+890af219f9,gf5289d68f6+e11c78aeea,w.2024.41
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: