LSST Applications g0265f82a02+0e5473021a,g02d81e74bb+bd2ed33bd6,g1470d8bcf6+c6d6eb38e2,g14a832a312+9d12ad093c,g2079a07aa2+86d27d4dc4,g2305ad1205+91a32aca49,g295015adf3+88246b6574,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g3ddfee87b4+c34e8be1fa,g487adcacf7+85dcfbcc36,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+ea1711114f,g5a732f18d5+53520f316c,g64a986408d+bd2ed33bd6,g858d7b2824+bd2ed33bd6,g8a8a8dda67+585e252eca,g99cad8db69+016a06b37a,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+ef4e3a5875,gb0e22166c9+60f28cb32d,gb6a65358fc+0e5473021a,gba4ed39666+c2a2e4ac27,gbb8dafda3b+b6d7b42999,gc120e1dc64+f745648b3a,gc28159a63d+0e5473021a,gcf0d15dbbd+c34e8be1fa,gdaeeff99f8+f9a426f77a,ge6526c86ff+508d0e0a30,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gf18bd8381d+8d59551888,gf1cff7945b+bd2ed33bd6,w.2024.16
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Attributes | List of all members
lsst.pipe.tasks.background.FocalPlaneBackground Class Reference

Public Member Functions

 fromCamera (cls, config, camera)
 
 fromSimilar (cls, other)
 
 __init__ (self, config, dims, transform, values=None, numbers=None)
 
 __reduce__ (self)
 
 clone (self)
 
 addCcd (self, exposure)
 
 toCcdBackground (self, detector, bbox)
 
 merge (self, other)
 
 __iadd__ (self, other)
 
 getStatsImage (self)
 

Public Attributes

 config
 
 dims
 
 transform
 

Protected Attributes

 _values
 
 _numbers
 

Detailed Description

Background model for a focal plane camera

We model the background empirically with the "superpixel" method: we
measure the background in each superpixel and interpolate between
superpixels to yield the model.

The principal difference between this and `lsst.afw.math.BackgroundMI`
is that here the superpixels are defined in the frame of the focal
plane of the camera which removes discontinuities across detectors.

The constructor you probably want to use is the `fromCamera` classmethod.

There are two use patterns for building a background model:

* Serial: create a `FocalPlaneBackground`, then `addCcd` for each of the
  CCDs in an exposure.

* Parallel: create a `FocalPlaneBackground`, then `clone` it for each
  of the CCDs in an exposure and use those to `addCcd` their respective
  CCD image. Finally, `merge` all the clones into the original.

Once you've built the background model, you can apply it to individual
CCDs with the `toCcdBackground` method.

Definition at line 498 of file background.py.

Constructor & Destructor Documentation

◆ __init__()

lsst.pipe.tasks.background.FocalPlaneBackground.__init__ ( self,
config,
dims,
transform,
values = None,
numbers = None )
Constructor

Developers should note that changes to the signature of this method
require coordinated changes to the `__reduce__` and `clone` methods.

Parameters
----------
config : `FocalPlaneBackgroundConfig`
    Configuration for measuring backgrounds.
dims : `lsst.geom.Extent2I`
    Dimensions for background samples.
transform : `lsst.afw.geom.TransformPoint2ToPoint2`
    Transformation from focal plane coordinates to sample coordinates.
values : `lsst.afw.image.ImageF`
    Measured background values.
numbers : `lsst.afw.image.ImageF`
    Number of pixels in each background measurement.

Definition at line 569 of file background.py.

569 def __init__(self, config, dims, transform, values=None, numbers=None):
570 """Constructor
571
572 Developers should note that changes to the signature of this method
573 require coordinated changes to the `__reduce__` and `clone` methods.
574
575 Parameters
576 ----------
577 config : `FocalPlaneBackgroundConfig`
578 Configuration for measuring backgrounds.
579 dims : `lsst.geom.Extent2I`
580 Dimensions for background samples.
581 transform : `lsst.afw.geom.TransformPoint2ToPoint2`
582 Transformation from focal plane coordinates to sample coordinates.
583 values : `lsst.afw.image.ImageF`
584 Measured background values.
585 numbers : `lsst.afw.image.ImageF`
586 Number of pixels in each background measurement.
587 """
588 self.config = config
589 self.dims = dims
590 self.transform = transform
591
592 if values is None:
593 values = afwImage.ImageF(self.dims)
594 values.set(0.0)
595 else:
596 values = values.clone()
597 assert values.getDimensions() == self.dims
598 self._values = values
599 if numbers is None:
600 numbers = afwImage.ImageF(self.dims) # float for dynamic range and convenience
601 numbers.set(0.0)
602 else:
603 numbers = numbers.clone()
604 assert numbers.getDimensions() == self.dims
605 self._numbers = numbers
606

Member Function Documentation

◆ __iadd__()

lsst.pipe.tasks.background.FocalPlaneBackground.__iadd__ ( self,
other )
Merge with another FocalPlaneBackground

Parameters
----------
other : `FocalPlaneBackground`
    Another background model to merge.

Returns
-------
self : `FocalPlaneBackground`
    The merged background model.

Definition at line 741 of file background.py.

741 def __iadd__(self, other):
742 """Merge with another FocalPlaneBackground
743
744 Parameters
745 ----------
746 other : `FocalPlaneBackground`
747 Another background model to merge.
748
749 Returns
750 -------
751 self : `FocalPlaneBackground`
752 The merged background model.
753 """
754 return self.merge(other)
755

◆ __reduce__()

lsst.pipe.tasks.background.FocalPlaneBackground.__reduce__ ( self)

Definition at line 607 of file background.py.

607 def __reduce__(self):
608 return self.__class__, (self.config, self.dims, self.transform, self._values, self._numbers)
609

◆ addCcd()

lsst.pipe.tasks.background.FocalPlaneBackground.addCcd ( self,
exposure )
Add CCD to model

We measure the background on the CCD (clipped mean), and record
the results in the model.  For simplicity, measurements are made
in a box on the CCD corresponding to the warped coordinates of the
superpixel rather than accounting for little rotations, etc.
We also record the number of pixels used in the measurement so we
can have a measure of confidence in each bin's value.

Parameters
----------
exposure : `lsst.afw.image.Exposure`
    CCD exposure to measure

Definition at line 613 of file background.py.

613 def addCcd(self, exposure):
614 """Add CCD to model
615
616 We measure the background on the CCD (clipped mean), and record
617 the results in the model. For simplicity, measurements are made
618 in a box on the CCD corresponding to the warped coordinates of the
619 superpixel rather than accounting for little rotations, etc.
620 We also record the number of pixels used in the measurement so we
621 can have a measure of confidence in each bin's value.
622
623 Parameters
624 ----------
625 exposure : `lsst.afw.image.Exposure`
626 CCD exposure to measure
627 """
628 detector = exposure.getDetector()
629 transform = detector.getTransformMap().getTransform(detector.makeCameraSys(afwCameraGeom.PIXELS),
630 detector.makeCameraSys(afwCameraGeom.FOCAL_PLANE))
631 image = exposure.getMaskedImage()
632 maskVal = image.getMask().getPlaneBitMask(self.config.mask)
633
634 # Warp the binned image to the focal plane
635 toSample = transform.then(self.transform) # CCD pixels --> focal plane --> sample
636
637 warped = afwImage.ImageF(self._values.getBBox())
638 warpedCounts = afwImage.ImageF(self._numbers.getBBox())
639 width, height = warped.getDimensions()
640
642 stats.setAndMask(maskVal)
643 stats.setNanSafe(True)
644 # Iterating over individual pixels in python is usually bad because it's slow, but there aren't many.
645 pixels = itertools.product(range(width), range(height))
646 for xx, yy in pixels:
647 llc = toSample.applyInverse(geom.Point2D(xx - 0.5, yy - 0.5))
648 urc = toSample.applyInverse(geom.Point2D(xx + 0.5, yy + 0.5))
649 bbox = geom.Box2I(geom.Point2I(llc), geom.Point2I(urc))
650 bbox.clip(image.getBBox())
651 if bbox.isEmpty():
652 continue
653 subImage = image.Factory(image, bbox)
654 result = afwMath.makeStatistics(subImage, afwMath.MEANCLIP | afwMath.NPOINT, stats)
655 mean = result.getValue(afwMath.MEANCLIP)
656 num = result.getValue(afwMath.NPOINT)
657 if not numpy.isfinite(mean) or not numpy.isfinite(num):
658 continue
659 warped[xx, yy, afwImage.LOCAL] = mean*num
660 warpedCounts[xx, yy, afwImage.LOCAL] = num
661
662 self._values += warped
663 self._numbers += warpedCounts
664
Pass parameters to a Statistics object.
Definition Statistics.h:83
An integer coordinate rectangle.
Definition Box.h:55
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

◆ clone()

lsst.pipe.tasks.background.FocalPlaneBackground.clone ( self)

Definition at line 610 of file background.py.

610 def clone(self):
611 return self.__class__(self.config, self.dims, self.transform, self._values, self._numbers)
612

◆ fromCamera()

lsst.pipe.tasks.background.FocalPlaneBackground.fromCamera ( cls,
config,
camera )
Construct from a camera object

Parameters
----------
config : `FocalPlaneBackgroundConfig`
    Configuration for measuring backgrounds.
camera : `lsst.afw.cameraGeom.Camera`
    Camera for which to measure backgrounds.

Definition at line 524 of file background.py.

524 def fromCamera(cls, config, camera):
525 """Construct from a camera object
526
527 Parameters
528 ----------
529 config : `FocalPlaneBackgroundConfig`
530 Configuration for measuring backgrounds.
531 camera : `lsst.afw.cameraGeom.Camera`
532 Camera for which to measure backgrounds.
533 """
534 cameraBox = geom.Box2D()
535 for ccd in camera:
536 for point in ccd.getCorners(afwCameraGeom.FOCAL_PLANE):
537 cameraBox.include(point)
538
539 width, height = cameraBox.getDimensions()
540 # Offset so that we run from zero
541 offset = geom.Extent2D(cameraBox.getMin())*-1
542 # Add an extra pixel buffer on either side
543 dims = geom.Extent2I(int(numpy.ceil(width/config.xSize)) + 2,
544 int(numpy.ceil(height/config.ySize)) + 2)
545 # Transform takes us from focal plane coordinates --> sample coordinates
546 transform = (geom.AffineTransform.makeTranslation(geom.Extent2D(1, 1))
547 * geom.AffineTransform.makeScaling(1.0/config.xSize, 1.0/config.ySize)
548 * geom.AffineTransform.makeTranslation(offset))
549
550 return cls(config, dims, afwGeom.makeTransform(transform))
551
A floating-point coordinate rectangle geometry.
Definition Box.h:413
std::shared_ptr< TransformPoint2ToPoint2 > makeTransform(lsst::geom::AffineTransform const &affine)
Wrap an lsst::geom::AffineTransform as a Transform.

◆ fromSimilar()

lsst.pipe.tasks.background.FocalPlaneBackground.fromSimilar ( cls,
other )
Construct from an object that has the same interface.

Parameters
----------
other : `FocalPlaneBackground`-like
    An object that matches the interface of `FocalPlaneBackground`
    but which may be different.

Returns
-------
background : `FocalPlaneBackground`
    Something guaranteed to be a `FocalPlaneBackground`.

Definition at line 553 of file background.py.

553 def fromSimilar(cls, other):
554 """Construct from an object that has the same interface.
555
556 Parameters
557 ----------
558 other : `FocalPlaneBackground`-like
559 An object that matches the interface of `FocalPlaneBackground`
560 but which may be different.
561
562 Returns
563 -------
564 background : `FocalPlaneBackground`
565 Something guaranteed to be a `FocalPlaneBackground`.
566 """
567 return cls(other.config, other.dims, other.transform, other._values, other._numbers)
568

◆ getStatsImage()

lsst.pipe.tasks.background.FocalPlaneBackground.getStatsImage ( self)
Return the background model data

This is the measurement of the background for each of the superpixels.

Definition at line 756 of file background.py.

756 def getStatsImage(self):
757 """Return the background model data
758
759 This is the measurement of the background for each of the superpixels.
760 """
761 values = self._values.clone()
762 values /= self._numbers
763 thresh = (self.config.minFrac
764 * (self.config.xSize/self.config.pixelSize)*(self.config.ySize/self.config.pixelSize))
765 isBad = self._numbers.getArray() < thresh
766 if self.config.doSmooth:
767 array = values.getArray()
768 array[:] = smoothArray(array, isBad, self.config.smoothScale)
769 isBad = numpy.isnan(values.array)
770 if numpy.any(isBad):
771 interpolateBadPixels(values.getArray(), isBad, self.config.interpolation)
772 return values
773
774

◆ merge()

lsst.pipe.tasks.background.FocalPlaneBackground.merge ( self,
other )
Merge with another FocalPlaneBackground

This allows multiple background models to be constructed from
different CCDs, and then merged to form a single consistent
background model for the entire focal plane.

Parameters
----------
other : `FocalPlaneBackground`
    Another background model to merge.

Returns
-------
self : `FocalPlaneBackground`
    The merged background model.

Definition at line 715 of file background.py.

715 def merge(self, other):
716 """Merge with another FocalPlaneBackground
717
718 This allows multiple background models to be constructed from
719 different CCDs, and then merged to form a single consistent
720 background model for the entire focal plane.
721
722 Parameters
723 ----------
724 other : `FocalPlaneBackground`
725 Another background model to merge.
726
727 Returns
728 -------
729 self : `FocalPlaneBackground`
730 The merged background model.
731 """
732 if (self.config.xSize, self.config.ySize) != (other.config.xSize, other.config.ySize):
733 raise RuntimeError("Size mismatch: %s vs %s" % ((self.config.xSize, self.config.ySize),
734 (other.config.xSize, other.config.ySize)))
735 if self.dims != other.dims:
736 raise RuntimeError("Dimensions mismatch: %s vs %s" % (self.dims, other.dims))
737 self._values += other._values
738 self._numbers += other._numbers
739 return self
740

◆ toCcdBackground()

lsst.pipe.tasks.background.FocalPlaneBackground.toCcdBackground ( self,
detector,
bbox )
Produce a background model for a CCD

The superpixel background model is warped back to the
CCD frame, for application to the individual CCD.

Parameters
----------
detector : `lsst.afw.cameraGeom.Detector`
    CCD for which to produce background model.
bbox : `lsst.geom.Box2I`
    Bounding box of CCD exposure.

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

Definition at line 665 of file background.py.

665 def toCcdBackground(self, detector, bbox):
666 """Produce a background model for a CCD
667
668 The superpixel background model is warped back to the
669 CCD frame, for application to the individual CCD.
670
671 Parameters
672 ----------
673 detector : `lsst.afw.cameraGeom.Detector`
674 CCD for which to produce background model.
675 bbox : `lsst.geom.Box2I`
676 Bounding box of CCD exposure.
677
678 Returns
679 -------
680 bg : `lsst.afw.math.BackgroundList`
681 Background model for CCD.
682 """
683 transform = detector.getTransformMap().getTransform(detector.makeCameraSys(afwCameraGeom.PIXELS),
684 detector.makeCameraSys(afwCameraGeom.FOCAL_PLANE))
685 binTransform = (geom.AffineTransform.makeScaling(self.config.binning)
686 * geom.AffineTransform.makeTranslation(geom.Extent2D(0.5, 0.5)))
687
688 # Binned image on CCD --> unbinned image on CCD --> focal plane --> binned focal plane
689 toSample = afwGeom.makeTransform(binTransform).then(transform).then(self.transform)
690
691 focalPlane = self.getStatsImage()
692 fpNorm = afwImage.ImageF(focalPlane.getBBox())
693 fpNorm.set(1.0)
694
695 image = afwImage.ImageF(bbox.getDimensions()//self.config.binning)
696 norm = afwImage.ImageF(image.getBBox())
697 ctrl = afwMath.WarpingControl("bilinear")
698 afwMath.warpImage(image, focalPlane, toSample.inverted(), ctrl)
699 afwMath.warpImage(norm, fpNorm, toSample.inverted(), ctrl)
700 image /= norm
701
702 mask = afwImage.Mask(image.getBBox())
703 isBad = numpy.isnan(image.getArray())
704 mask.getArray()[isBad] = mask.getPlaneBitMask("BAD")
705 image.getArray()[isBad] = image.getArray()[~isBad].mean()
706
709 afwMath.stringToInterpStyle(self.config.interpolation),
710 afwMath.stringToUndersampleStyle("REDUCE_INTERP_ORDER"),
711 afwMath.ApproximateControl.UNKNOWN,
712 0, 0, False)
713 )
714
Represent a 2-dimensional array of bitmask pixels.
Definition Mask.h:77
A class to evaluate image background levels.
Definition Background.h:434
Parameters to control convolution.
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > * makeMaskedImage(typename std::shared_ptr< Image< ImagePixelT > > image, typename std::shared_ptr< Mask< MaskPixelT > > mask=Mask< MaskPixelT >(), typename std::shared_ptr< Image< VariancePixelT > > variance=Image< VariancePixelT >())
A function to return a MaskedImage of the correct type (cf.
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
int warpImage(DestImageT &destImage, geom::SkyWcs const &destWcs, SrcImageT const &srcImage, geom::SkyWcs const &srcWcs, WarpingControl const &control, typename DestImageT::SinglePixel padValue=lsst::afw::math::edgePixel< DestImageT >(typename lsst::afw::image::detail::image_traits< DestImageT >::image_category()))
Warp an Image or MaskedImage to a new Wcs.
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.

Member Data Documentation

◆ _numbers

lsst.pipe.tasks.background.FocalPlaneBackground._numbers
protected

Definition at line 605 of file background.py.

◆ _values

lsst.pipe.tasks.background.FocalPlaneBackground._values
protected

Definition at line 598 of file background.py.

◆ config

lsst.pipe.tasks.background.FocalPlaneBackground.config

Definition at line 588 of file background.py.

◆ dims

lsst.pipe.tasks.background.FocalPlaneBackground.dims

Definition at line 589 of file background.py.

◆ transform

lsst.pipe.tasks.background.FocalPlaneBackground.transform

Definition at line 590 of file background.py.


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