LSST Applications g02d81e74bb+86cf3d8bc9,g180d380827+7a4e862ed4,g2079a07aa2+86d27d4dc4,g2305ad1205+e1ca1c66fa,g29320951ab+012e1474a1,g295015adf3+341ea1ce94,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+c429d67c83,g48712c4677+f88676dd22,g487adcacf7+27e1e21933,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+b41db86c35,g5a732f18d5+53520f316c,g64a986408d+86cf3d8bc9,g858d7b2824+86cf3d8bc9,g8a8a8dda67+585e252eca,g99cad8db69+84912a7fdc,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+a2b54eae19,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+6681f309db,gc120e1dc64+f0fcc2f6d8,gc28159a63d+0e5473021a,gcf0d15dbbd+c429d67c83,gdaeeff99f8+f9a426f77a,ge6526c86ff+0433e6603d,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+86cf3d8bc9,w.2024.17
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
lsst.pipe.tasks.background.SkyMeasurementTask Class Reference
Inheritance diagram for lsst.pipe.tasks.background.SkyMeasurementTask:

Public Member Functions

 backgroundToExposure (self, statsImage, bbox)
 
 measureBackground (self, image)
 
 averageBackgrounds (self, bgList)
 
 measureScale (self, image, skyBackground)
 
 solveScales (self, scales)
 
 subtractSkyFrame (self, image, skyBackground, scale, bgList=None)
 

Static Public Member Functions

 exposureToBackground (bgExp)
 

Static Public Attributes

 ConfigClass = SkyMeasurementConfig
 

Detailed Description

Task for creating, persisting and using sky frames

A sky frame is like a fringe frame (the sum of many exposures of the night sky,
combined with rejection to remove astrophysical objects) except the structure
is on larger scales, and hence we bin the images and represent them as a
background model (a `lsst.afw.math.BackgroundMI`).  The sky frame represents
the dominant response of the camera to the sky background.

Definition at line 116 of file background.py.

Member Function Documentation

◆ averageBackgrounds()

lsst.pipe.tasks.background.SkyMeasurementTask.averageBackgrounds ( self,
bgList )
Average multiple background models

The input background models should be a `BackgroundList` consisting
of a single `BackgroundMI`.

Parameters
----------
bgList : `list` of `lsst.afw.math.BackgroundList`
    Background models to average.

Returns
-------
bgExp : `lsst.afw.image.Exposure`
    Background model in Exposure format.

Definition at line 227 of file background.py.

227 def averageBackgrounds(self, bgList):
228 """Average multiple background models
229
230 The input background models should be a `BackgroundList` consisting
231 of a single `BackgroundMI`.
232
233 Parameters
234 ----------
235 bgList : `list` of `lsst.afw.math.BackgroundList`
236 Background models to average.
237
238 Returns
239 -------
240 bgExp : `lsst.afw.image.Exposure`
241 Background model in Exposure format.
242 """
243 assert all(len(bg) == 1 for bg in bgList), "Mixed bgList: %s" % ([len(bg) for bg in bgList],)
244 images = [bg[0][0].getStatsImage() for bg in bgList]
245 boxes = [bg[0][0].getImageBBox() for bg in bgList]
246 assert len(set((box.getMinX(), box.getMinY(), box.getMaxX(), box.getMaxY()) for box in boxes)) == 1, \
247 "Bounding boxes not all equal"
248 bbox = boxes.pop(0)
249
250 # Ensure bad pixels are masked
251 maskVal = afwImage.Mask.getPlaneBitMask("BAD")
252 for img in images:
253 bad = numpy.isnan(img.getImage().getArray())
254 img.getMask().getArray()[bad] = maskVal
255
257 stats.setAndMask(maskVal)
258 stats.setNanSafe(True)
259 combined = afwMath.statisticsStack(images, afwMath.MEANCLIP, stats)
260
261 # Set bad pixels to the median
262 # Specifically NOT going to attempt to interpolate the bad values because we're only working on a
263 # single CCD here and can't use the entire field-of-view to do the interpolation (which is what we
264 # would need to avoid introducing problems at the edge of CCDs).
265 array = combined.getImage().getArray()
266 bad = numpy.isnan(array)
267 median = numpy.median(array[~bad])
268 array[bad] = median
269
270 # Put it into an exposure, which is required for calibs
271 return self.backgroundToExposure(combined, bbox)
272
Pass parameters to a Statistics object.
Definition Statistics.h:83
daf::base::PropertySet * set
Definition fits.cc:931
std::shared_ptr< lsst::afw::image::Image< PixelT > > statisticsStack(std::vector< std::shared_ptr< lsst::afw::image::Image< PixelT > > > &images, Property flags, StatisticsControl const &sctrl=StatisticsControl(), std::vector< lsst::afw::image::VariancePixel > const &wvector=std::vector< lsst::afw::image::VariancePixel >(0))
A function to compute some statistics of a stack of Images.

◆ backgroundToExposure()

lsst.pipe.tasks.background.SkyMeasurementTask.backgroundToExposure ( self,
statsImage,
bbox )
Convert a background model to an exposure

Calibs need to be persisted as an Exposure, so we need to convert
the background model to an Exposure.

Parameters
----------
statsImage : `lsst.afw.image.MaskedImageF`
    Background model's statistics image.
bbox : `lsst.geom.Box2I`
    Bounding box for image.

Returns
-------
exp : `lsst.afw.image.Exposure`
    Background model in Exposure format.

Definition at line 158 of file background.py.

158 def backgroundToExposure(self, statsImage, bbox):
159 """Convert a background model to an exposure
160
161 Calibs need to be persisted as an Exposure, so we need to convert
162 the background model to an Exposure.
163
164 Parameters
165 ----------
166 statsImage : `lsst.afw.image.MaskedImageF`
167 Background model's statistics image.
168 bbox : `lsst.geom.Box2I`
169 Bounding box for image.
170
171 Returns
172 -------
173 exp : `lsst.afw.image.Exposure`
174 Background model in Exposure format.
175 """
176 exp = afwImage.makeExposure(statsImage)
177 header = exp.getMetadata()
178 header.set("BOX.MINX", bbox.getMinX())
179 header.set("BOX.MINY", bbox.getMinY())
180 header.set("BOX.MAXX", bbox.getMaxX())
181 header.set("BOX.MAXY", bbox.getMaxY())
182 header.set("ALGORITHM", self.config.background.algorithm)
183 return exp
184
std::shared_ptr< Exposure< ImagePixelT, MaskPixelT, VariancePixelT > > makeExposure(MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > &mimage, std::shared_ptr< geom::SkyWcs const > wcs=std::shared_ptr< geom::SkyWcs const >())
A function to return an Exposure of the correct type (cf.
Definition Exposure.h:484

◆ exposureToBackground()

lsst.pipe.tasks.background.SkyMeasurementTask.exposureToBackground ( bgExp)
static
Convert an exposure to background model

Calibs need to be persisted as an Exposure, so we need to convert
the persisted Exposure to a background model.

Parameters
----------
bgExp : `lsst.afw.image.Exposure`
    Background model in Exposure format.

Returns
-------
bg : `lsst.afw.math.BackgroundList`
    Background model

Definition at line 128 of file background.py.

128 def exposureToBackground(bgExp):
129 """Convert an exposure to background model
130
131 Calibs need to be persisted as an Exposure, so we need to convert
132 the persisted Exposure to a background model.
133
134 Parameters
135 ----------
136 bgExp : `lsst.afw.image.Exposure`
137 Background model in Exposure format.
138
139 Returns
140 -------
141 bg : `lsst.afw.math.BackgroundList`
142 Background model
143 """
144 header = bgExp.getMetadata()
145 xMin = header.getScalar("BOX.MINX")
146 yMin = header.getScalar("BOX.MINY")
147 xMax = header.getScalar("BOX.MAXX")
148 yMax = header.getScalar("BOX.MAXY")
149 algorithm = header.getScalar("ALGORITHM")
150 bbox = geom.Box2I(geom.Point2I(xMin, yMin), geom.Point2I(xMax, yMax))
152 (afwMath.BackgroundMI(bbox, bgExp.getMaskedImage()),
154 afwMath.stringToUndersampleStyle("REDUCE_INTERP_ORDER"),
155 afwMath.ApproximateControl.UNKNOWN,
156 0, 0, False))
157
A class to evaluate image background levels.
Definition Background.h:434
An integer coordinate rectangle.
Definition Box.h:55
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.

◆ measureBackground()

lsst.pipe.tasks.background.SkyMeasurementTask.measureBackground ( self,
image )
Measure a background model for image

This doesn't use a full-featured background model (e.g., no Chebyshev
approximation) because we just want the binning behaviour.  This will
allow us to average the bins later (`averageBackgrounds`).

The `BackgroundMI` is wrapped in a `BackgroundList` so it can be
pickled and persisted.

Parameters
----------
image : `lsst.afw.image.MaskedImage`
    Image for which to measure background.

Returns
-------
bgModel : `lsst.afw.math.BackgroundList`
    Background model.

Definition at line 185 of file background.py.

185 def measureBackground(self, image):
186 """Measure a background model for image
187
188 This doesn't use a full-featured background model (e.g., no Chebyshev
189 approximation) because we just want the binning behaviour. This will
190 allow us to average the bins later (`averageBackgrounds`).
191
192 The `BackgroundMI` is wrapped in a `BackgroundList` so it can be
193 pickled and persisted.
194
195 Parameters
196 ----------
197 image : `lsst.afw.image.MaskedImage`
198 Image for which to measure background.
199
200 Returns
201 -------
202 bgModel : `lsst.afw.math.BackgroundList`
203 Background model.
204 """
206 stats.setAndMask(image.getMask().getPlaneBitMask(self.config.background.mask))
207 stats.setNanSafe(True)
209 self.config.background.algorithm,
210 max(int(image.getWidth()/self.config.background.xBinSize + 0.5), 1),
211 max(int(image.getHeight()/self.config.background.yBinSize + 0.5), 1),
212 "REDUCE_INTERP_ORDER",
213 stats,
214 self.config.background.statistic
215 )
216
217 bg = afwMath.makeBackground(image, ctrl)
218
220 bg,
221 afwMath.stringToInterpStyle(self.config.background.algorithm),
222 afwMath.stringToUndersampleStyle("REDUCE_INTERP_ORDER"),
223 afwMath.ApproximateControl.UNKNOWN,
224 0, 0, False
225 ))
226
int max
Pass parameters to a Background object.
Definition Background.h:56
std::shared_ptr< Background > makeBackground(ImageT const &img, BackgroundControl const &bgCtrl)
A convenience function that uses function overloading to make the correct type of Background.
Definition Background.h:526

◆ measureScale()

lsst.pipe.tasks.background.SkyMeasurementTask.measureScale ( self,
image,
skyBackground )
Measure scale of background model in image

We treat the sky frame much as we would a fringe frame
(except the length scale of the variations is different):
we measure samples on the input image and the sky frame,
which we will use to determine the scaling factor in the
'solveScales` method.

Parameters
----------
image : `lsst.afw.image.Exposure` or `lsst.afw.image.MaskedImage`
    Science image for which to measure scale.
skyBackground : `lsst.afw.math.BackgroundList`
    Sky background model.

Returns
-------
imageSamples : `numpy.ndarray`
    Sample measurements on image.
skySamples : `numpy.ndarray`
    Sample measurements on sky frame.

Definition at line 273 of file background.py.

273 def measureScale(self, image, skyBackground):
274 """Measure scale of background model in image
275
276 We treat the sky frame much as we would a fringe frame
277 (except the length scale of the variations is different):
278 we measure samples on the input image and the sky frame,
279 which we will use to determine the scaling factor in the
280 'solveScales` method.
281
282 Parameters
283 ----------
284 image : `lsst.afw.image.Exposure` or `lsst.afw.image.MaskedImage`
285 Science image for which to measure scale.
286 skyBackground : `lsst.afw.math.BackgroundList`
287 Sky background model.
288
289 Returns
290 -------
291 imageSamples : `numpy.ndarray`
292 Sample measurements on image.
293 skySamples : `numpy.ndarray`
294 Sample measurements on sky frame.
295 """
296 if isinstance(image, afwImage.Exposure):
297 image = image.getMaskedImage()
298 # Ensure more samples than pixels
299 xNumSamples = min(self.config.xNumSamples, image.getWidth())
300 yNumSamples = min(self.config.yNumSamples, image.getHeight())
301 xLimits = numpy.linspace(0, image.getWidth(), xNumSamples + 1, dtype=int)
302 yLimits = numpy.linspace(0, image.getHeight(), yNumSamples + 1, dtype=int)
303 sky = skyBackground.getImage()
304 maskVal = image.getMask().getPlaneBitMask(self.config.stats.mask)
305 ctrl = afwMath.StatisticsControl(self.config.stats.clip, self.config.stats.nIter, maskVal)
306 statistic = afwMath.stringToStatisticsProperty(self.config.stats.statistic)
307 imageSamples = []
308 skySamples = []
309 for xIndex, yIndex in itertools.product(range(xNumSamples), range(yNumSamples)):
310 # -1 on the stop because Box2I is inclusive of the end point and we don't want to overlap boxes
311 xStart, xStop = xLimits[xIndex], xLimits[xIndex + 1] - 1
312 yStart, yStop = yLimits[yIndex], yLimits[yIndex + 1] - 1
313 box = geom.Box2I(geom.Point2I(xStart, yStart), geom.Point2I(xStop, yStop))
314 subImage = image.Factory(image, box)
315 subSky = sky.Factory(sky, box)
316 imageSamples.append(afwMath.makeStatistics(subImage, statistic, ctrl).getValue())
317 skySamples.append(afwMath.makeStatistics(subSky, statistic, ctrl).getValue())
318 return imageSamples, skySamples
319
int min
A class to contain the data, WCS, and other information needed to describe an image of the sky.
Definition Exposure.h:72
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition Statistics.h:361
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)

◆ solveScales()

lsst.pipe.tasks.background.SkyMeasurementTask.solveScales ( self,
scales )
Solve multiple scales for a single scale factor

Having measured samples from the image and sky frame, we
fit for the scaling factor.

Parameters
----------
scales : `list` of a `tuple` of two `numpy.ndarray` arrays
    A `list` of the results from `measureScale` method.

Returns
-------
scale : `float`
    Scale factor.

Definition at line 320 of file background.py.

320 def solveScales(self, scales):
321 """Solve multiple scales for a single scale factor
322
323 Having measured samples from the image and sky frame, we
324 fit for the scaling factor.
325
326 Parameters
327 ----------
328 scales : `list` of a `tuple` of two `numpy.ndarray` arrays
329 A `list` of the results from `measureScale` method.
330
331 Returns
332 -------
333 scale : `float`
334 Scale factor.
335 """
336 imageSamples = []
337 skySamples = []
338 for ii, ss in scales:
339 imageSamples.extend(ii)
340 skySamples.extend(ss)
341 assert len(imageSamples) == len(skySamples)
342 imageSamples = numpy.array(imageSamples)
343 skySamples = numpy.array(skySamples)
344
345 def solve(mask):
346 return afwMath.LeastSquares.fromDesignMatrix(skySamples[mask].reshape(mask.sum(), 1),
347 imageSamples[mask],
348 afwMath.LeastSquares.DIRECT_SVD).getSolution()
349
350 mask = numpy.isfinite(imageSamples) & numpy.isfinite(skySamples)
351 for ii in range(self.config.skyIter):
352 solution = solve(mask)
353 residuals = imageSamples - solution*skySamples
354 lq, uq = numpy.percentile(residuals[mask], [25, 75])
355 stdev = 0.741*(uq - lq) # Robust stdev from IQR
356 with numpy.errstate(invalid="ignore"): # suppress NAN warnings
357 bad = numpy.abs(residuals) > self.config.skyRej*stdev
358 mask[bad] = False
359
360 return solve(mask)
361

◆ subtractSkyFrame()

lsst.pipe.tasks.background.SkyMeasurementTask.subtractSkyFrame ( self,
image,
skyBackground,
scale,
bgList = None )
Subtract sky frame from science image

Parameters
----------
image : `lsst.afw.image.Exposure` or `lsst.afw.image.MaskedImage`
    Science image.
skyBackground : `lsst.afw.math.BackgroundList`
    Sky background model.
scale : `float`
    Scale to apply to background model.
bgList : `lsst.afw.math.BackgroundList`
    List of backgrounds applied to image

Definition at line 362 of file background.py.

362 def subtractSkyFrame(self, image, skyBackground, scale, bgList=None):
363 """Subtract sky frame from science image
364
365 Parameters
366 ----------
367 image : `lsst.afw.image.Exposure` or `lsst.afw.image.MaskedImage`
368 Science image.
369 skyBackground : `lsst.afw.math.BackgroundList`
370 Sky background model.
371 scale : `float`
372 Scale to apply to background model.
373 bgList : `lsst.afw.math.BackgroundList`
374 List of backgrounds applied to image
375 """
376 if isinstance(image, afwImage.Exposure):
377 image = image.getMaskedImage()
378 if isinstance(image, afwImage.MaskedImage):
379 image = image.getImage()
380 image.scaledMinus(scale, skyBackground.getImage())
381 if bgList is not None:
382 # Append the sky frame to the list of applied background models
383 bgData = list(skyBackground[0])
384 bg = bgData[0]
385 statsImage = bg.getStatsImage().clone()
386 statsImage *= scale
387 newBg = afwMath.BackgroundMI(bg.getImageBBox(), statsImage)
388 newBgData = [newBg] + bgData[1:]
389 bgList.append(newBgData)
390
391
A class to manipulate images, masks, and variance as a single object.
Definition MaskedImage.h:74

Member Data Documentation

◆ ConfigClass

lsst.pipe.tasks.background.SkyMeasurementTask.ConfigClass = SkyMeasurementConfig
static

Definition at line 125 of file background.py.


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