LSST Applications g070148d5b3+33e5256705,g0d53e28543+25c8b88941,g0da5cf3356+2dd1178308,g1081da9e2a+62d12e78cb,g17e5ecfddb+7e422d6136,g1c76d35bf8+ede3a706f7,g295839609d+225697d880,g2e2c1a68ba+cc1f6f037e,g2ffcdf413f+853cd4dcde,g38293774b4+62d12e78cb,g3b44f30a73+d953f1ac34,g48ccf36440+885b902d19,g4b2f1765b6+7dedbde6d2,g5320a0a9f6+0c5d6105b6,g56b687f8c9+ede3a706f7,g5c4744a4d9+ef6ac23297,g5ffd174ac0+0c5d6105b6,g6075d09f38+66af417445,g667d525e37+2ced63db88,g670421136f+2ced63db88,g71f27ac40c+2ced63db88,g774830318a+463cbe8d1f,g7876bc68e5+1d137996f1,g7985c39107+62d12e78cb,g7fdac2220c+0fd8241c05,g96f01af41f+368e6903a7,g9ca82378b8+2ced63db88,g9d27549199+ef6ac23297,gabe93b2c52+e3573e3735,gb065e2a02a+3dfbe639da,gbc3249ced9+0c5d6105b6,gbec6a3398f+0c5d6105b6,gc9534b9d65+35b9f25267,gd01420fc67+0c5d6105b6,geee7ff78d7+a14128c129,gf63283c776+ede3a706f7,gfed783d017+0c5d6105b6,w.2022.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
interpImage.py
Go to the documentation of this file.
1# This file is part of pipe_tasks.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22__all__ = ["InterpImageConfig", "InterpImageTask"]
23
24from contextlib import contextmanager
25import lsst.pex.config as pexConfig
26import lsst.geom
27import lsst.afw.image as afwImage
28import lsst.afw.math as afwMath
29import lsst.ip.isr as ipIsr
30import lsst.meas.algorithms as measAlg
31import lsst.pipe.base as pipeBase
32from lsst.utils.timer import timeMethod
33
34
35class InterpImageConfig(pexConfig.Config):
36 """Config for InterpImageTask
37 """
38 modelPsf = measAlg.GaussianPsfFactory.makeField(doc="Model Psf factory")
39
40 useFallbackValueAtEdge = pexConfig.Field(
41 dtype=bool,
42 doc="Smoothly taper to the fallback value at the edge of the image?",
43 default=True,
44 )
45 fallbackValueType = pexConfig.ChoiceField(
46 dtype=str,
47 doc="Type of statistic to calculate edge fallbackValue for interpolation",
48 allowed={
49 "MEAN": "mean",
50 "MEDIAN": "median",
51 "MEANCLIP": "clipped mean",
52 "USER": "user value set in fallbackUserValue config",
53 },
54 default="MEDIAN",
55 )
56 fallbackUserValue = pexConfig.Field(
57 dtype=float,
58 doc="If fallbackValueType is 'USER' then use this as the fallbackValue; ignored otherwise",
59 default=0.0,
60 )
61 negativeFallbackAllowed = pexConfig.Field(
62 dtype=bool,
63 doc=("Allow negative values for egde interpolation fallbackValue? If False, set "
64 "fallbackValue to max(fallbackValue, 0.0)"),
65 default=False,
66 )
67 transpose = pexConfig.Field(dtype=int, default=False,
68 doc="Transpose image before interpolating? "
69 "This allows the interpolation to act over columns instead of rows.")
70
71 def validate(self):
72 pexConfig.Config.validate(self)
74 if (not self.negativeFallbackAllowed and self.fallbackValueType == "USER"
75 and self.fallbackUserValue < 0.0):
76 raise ValueError("User supplied fallbackValue is negative (%.2f) but "
77 "negativeFallbackAllowed is False" % self.fallbackUserValue)
78
79
80class InterpImageTask(pipeBase.Task):
81 """Interpolate over bad image pixels
82 """
83 ConfigClass = InterpImageConfig
84 _DefaultName = "interpImage"
85
86 def _setFallbackValue(self, mi=None):
87 """Set the edge fallbackValue for interpolation
88
89 Parameters
90 ----------
91 mi : `lsst.afw.image.MaskedImage`, optional
92 Input maskedImage on which to calculate the statistics
93 Must be provided if fallbackValueType != "USER".
94
95 Returns
96 -------
97 fallbackValue : `float`
98 The value set/computed based on the fallbackValueType
99 and negativeFallbackAllowed config parameters.
100 """
101 if self.config.fallbackValueType != 'USER':
102 assert mi, "No maskedImage provided"
103 if self.config.fallbackValueType == 'MEAN':
104 fallbackValue = afwMath.makeStatistics(mi, afwMath.MEAN).getValue()
105 elif self.config.fallbackValueType == 'MEDIAN':
106 fallbackValue = afwMath.makeStatistics(mi, afwMath.MEDIAN).getValue()
107 elif self.config.fallbackValueType == 'MEANCLIP':
108 fallbackValue = afwMath.makeStatistics(mi, afwMath.MEANCLIP).getValue()
109 elif self.config.fallbackValueType == 'USER':
110 fallbackValue = self.config.fallbackUserValue
111 else:
112 raise NotImplementedError("%s : %s not implemented" %
113 ("fallbackValueType", self.config.fallbackValueType))
114
115 if not self.config.negativeFallbackAllowed and fallbackValue < 0.0:
116 self.log.warning("Negative interpolation edge fallback value computed but "
117 "negativeFallbackAllowed is False: setting fallbackValue to 0.0")
118 fallbackValue = max(fallbackValue, 0.0)
119
120 self.log.info("fallbackValueType %s has been set to %.4f",
121 self.config.fallbackValueType, fallbackValue)
122
123 return fallbackValue
124
125 @timeMethod
126 def run(self, image, planeName=None, fwhmPixels=None, defects=None):
127 """Interpolate in place over pixels in a maskedImage marked as bad
128
129 Pixels to be interpolated are set by either a mask planeName provided
130 by the caller OR a defects list of type `~lsst.meas.algorithms.Defects`
131 If both are provided an exception is raised.
132
133 Note that the interpolation code in meas_algorithms currently doesn't
134 use the input PSF (though it's a required argument), so it's not
135 important to set the input PSF parameters exactly. This PSF is set
136 here as the psf attached to the "image" (i.e if the image passed in
137 is an Exposure). Otherwise, a psf model is created using
138 measAlg.GaussianPsfFactory with the value of fwhmPixels (the value
139 passed in by the caller, or the default defaultFwhm set in
140 measAlg.GaussianPsfFactory if None).
141
142 Parameters
143 ----------
145 MaskedImage OR Exposure to be interpolated.
146 planeName : `str`, optional
147 Name of mask plane over which to interpolate.
148 If None, must provide a defects list.
149 fwhmPixels : `int`, optional
150 FWHM of core star (pixels).
151 If None the default is used, where the default
152 is set to the exposure psf if available.
153 defects : `lsst.meas.algorithms.Defects`, optional
154 List of defects of type ipIsr.Defects
155 over which to interpolate.
156 """
157 try:
158 maskedImage = image.getMaskedImage()
159 except AttributeError:
160 maskedImage = image
161
162 # set defectList from defects OR mask planeName provided
163 if planeName is None:
164 if defects is None:
165 raise ValueError("No defects or plane name provided")
166 else:
167 if not isinstance(defects, ipIsr.Defects):
168 defectList = ipIsr.Defects(defects)
169 else:
170 defectList = defects
171 planeName = "defects"
172 else:
173 if defects is not None:
174 raise ValueError("Provide EITHER a planeName OR a list of defects, not both")
175 if planeName not in maskedImage.getMask().getMaskPlaneDict():
176 raise ValueError("maskedImage does not contain mask plane %s" % planeName)
177 defectList = ipIsr.Defects.fromMask(maskedImage, planeName)
178
179 # set psf from exposure if provided OR using modelPsf with fwhmPixels provided
180 try:
181 psf = image.getPsf()
182 self.log.info("Setting psf for interpolation from image")
183 except AttributeError:
184 self.log.info("Creating psf model for interpolation from fwhm(pixels) = %s",
185 str(fwhmPixels) if fwhmPixels is not None else
186 (str(self.config.modelPsf.defaultFwhm)) + " [default]")
187 psf = self.config.modelPsf.apply(fwhm=fwhmPixels)
188
189 fallbackValue = 0.0 # interpolateOverDefects needs this to be a float, regardless if it is used
190 if self.config.useFallbackValueAtEdge:
191 fallbackValue = self._setFallbackValue(maskedImage)
192
193 self.interpolateImage(maskedImage, psf, defectList, fallbackValue)
194
195 self.log.info("Interpolated over %d %s pixels.", len(defectList), planeName)
196
197 @contextmanager
198 def transposeContext(self, maskedImage, defects):
199 """Context manager to potentially transpose an image
200
201 This applies the ``transpose`` configuration setting.
202
203 Transposing the image allows us to interpolate along columns instead
204 of rows, which is useful when the saturation trails are typically
205 oriented along rows on the warped/coadded images, instead of along
206 columns as they typically are in raw CCD images.
207
208 Parameters
209 ----------
210 maskedImage : `lsst.afw.image.MaskedImage`
211 Image on which to perform interpolation.
212 defects : `lsst.meas.algorithms.Defects`
213 List of defects to interpolate over.
214
215 Yields
216 ------
217 useImage : `lsst.afw.image.MaskedImage`
218 Image to use for interpolation; it may have been transposed.
219 useDefects : `lsst.meas.algorithms.Defects`
220 List of defects to use for interpolation; they may have been
221 transposed.
222 """
223 def transposeImage(image):
224 """Transpose an image
225
226 Parameters
227 ----------
228 image : `Unknown`
229 """
230 transposed = image.array.T.copy() # Copy to force row-major; required for ndarray+pybind
231 return image.Factory(transposed, False, lsst.geom.Point2I(*reversed(image.getXY0())))
232
233 useImage = maskedImage
234 useDefects = defects
235 if self.config.transpose:
236 useImage = afwImage.makeMaskedImage(transposeImage(maskedImage.image),
237 transposeImage(maskedImage.mask),
238 transposeImage(maskedImage.variance))
239 useDefects = defects.transpose()
240 yield useImage, useDefects
241 if self.config.transpose:
242 maskedImage.image.array = useImage.image.array.T
243 maskedImage.mask.array = useImage.mask.array.T
244 maskedImage.variance.array = useImage.variance.array.T
245
246 def interpolateImage(self, maskedImage, psf, defectList, fallbackValue):
247 """Interpolate over defects in an image
248
249 Parameters
250 ----------
251 maskedImage : `lsst.afw.image.MaskedImage`
252 Image on which to perform interpolation.
254 Point-spread function; currently unused.
255 defectList : `lsst.meas.algorithms.Defects`
256 List of defects to interpolate over.
257 fallbackValue : `float`
258 Value to set when interpolation fails.
259 """
260 if not defectList:
261 return
262 with self.transposeContext(maskedImage, defectList) as (image, defects):
263 measAlg.interpolateOverDefects(image, psf, defects, fallbackValue,
264 self.config.useFallbackValueAtEdge)
int max
A polymorphic base class for representing an image's Point Spread Function.
Definition: Psf.h:76
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:74
def interpolateImage(self, maskedImage, psf, defectList, fallbackValue)
Definition: interpImage.py:246
def transposeContext(self, maskedImage, defects)
Definition: interpImage.py:198
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.
Definition: MaskedImage.h:1241
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