LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
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__ = (
23 "InterpImageConfig",
24 "InterpImageTask",
25)
26
27
28from contextlib import contextmanager
29
30import lsst.pex.config as pexConfig
31import lsst.geom
32import lsst.afw.image as afwImage
33import lsst.afw.math as afwMath
34import lsst.ip.isr as ipIsr
35import lsst.meas.algorithms as measAlg
36import lsst.pipe.base as pipeBase
37from lsst.utils.timer import timeMethod
38
39
40class InterpImageConfig(pexConfig.Config):
41 """Config for InterpImageTask
42 """
43 modelPsf = measAlg.GaussianPsfFactory.makeField(doc="Model Psf factory")
44
45 useFallbackValueAtEdge = pexConfig.Field(
46 dtype=bool,
47 doc="Smoothly taper to the fallback value at the edge of the image?",
48 default=True,
49 )
50 fallbackValueType = pexConfig.ChoiceField(
51 dtype=str,
52 doc="Type of statistic to calculate edge fallbackValue for interpolation",
53 allowed={
54 "MEAN": "mean",
55 "MEDIAN": "median",
56 "MEANCLIP": "clipped mean",
57 "USER": "user value set in fallbackUserValue config",
58 },
59 default="MEDIAN",
60 )
61 fallbackUserValue = pexConfig.Field(
62 dtype=float,
63 doc="If fallbackValueType is 'USER' then use this as the fallbackValue; ignored otherwise",
64 default=0.0,
65 )
66 negativeFallbackAllowed = pexConfig.Field(
67 dtype=bool,
68 doc=("Allow negative values for egde interpolation fallbackValue? If False, set "
69 "fallbackValue to max(fallbackValue, 0.0)"),
70 default=False,
71 )
72 transpose = pexConfig.Field(dtype=int, default=False,
73 doc="Transpose image before interpolating? "
74 "This allows the interpolation to act over columns instead of rows.")
75
76 def validate(self):
77 pexConfig.Config.validate(self)
79 if (not self.negativeFallbackAllowed and self.fallbackValueType == "USER"
80 and self.fallbackUserValue < 0.0):
81 raise ValueError("User supplied fallbackValue is negative (%.2f) but "
82 "negativeFallbackAllowed is False" % self.fallbackUserValue)
83
84
85class InterpImageTask(pipeBase.Task):
86 """Interpolate over bad image pixels
87 """
88 ConfigClass = InterpImageConfig
89 _DefaultName = "interpImage"
90
91 def _setFallbackValue(self, mi=None):
92 """Set the edge fallbackValue for interpolation
93
94 Parameters
95 ----------
96 mi : `lsst.afw.image.MaskedImage`, optional
97 Input maskedImage on which to calculate the statistics
98 Must be provided if fallbackValueType != "USER".
99
100 Returns
101 -------
102 fallbackValue : `float`
103 The value set/computed based on the fallbackValueType
104 and negativeFallbackAllowed config parameters.
105 """
106 if self.config.fallbackValueType != 'USER':
107 assert mi, "No maskedImage provided"
108 if self.config.fallbackValueType == 'MEAN':
109 fallbackValue = afwMath.makeStatistics(mi, afwMath.MEAN).getValue()
110 elif self.config.fallbackValueType == 'MEDIAN':
111 fallbackValue = afwMath.makeStatistics(mi, afwMath.MEDIAN).getValue()
112 elif self.config.fallbackValueType == 'MEANCLIP':
113 fallbackValue = afwMath.makeStatistics(mi, afwMath.MEANCLIP).getValue()
114 elif self.config.fallbackValueType == 'USER':
115 fallbackValue = self.config.fallbackUserValue
116 else:
117 raise NotImplementedError("%s : %s not implemented" %
118 ("fallbackValueType", self.config.fallbackValueType))
119
120 if not self.config.negativeFallbackAllowed and fallbackValue < 0.0:
121 self.log.warning("Negative interpolation edge fallback value computed but "
122 "negativeFallbackAllowed is False: setting fallbackValue to 0.0")
123 fallbackValue = max(fallbackValue, 0.0)
124
125 self.log.info("fallbackValueType %s has been set to %.4f",
126 self.config.fallbackValueType, fallbackValue)
127
128 return fallbackValue
129
130 @timeMethod
131 def run(self, image, planeName=None, fwhmPixels=None, defects=None):
132 """Interpolate in place over pixels in a maskedImage marked as bad
133
134 Pixels to be interpolated are set by either a mask planeName provided
135 by the caller OR a defects list of type `~lsst.meas.algorithms.Defects`
136 If both are provided an exception is raised.
137
138 Note that the interpolation code in meas_algorithms currently doesn't
139 use the input PSF (though it's a required argument), so it's not
140 important to set the input PSF parameters exactly. This PSF is set
141 here as the psf attached to the "image" (i.e if the image passed in
142 is an Exposure). Otherwise, a psf model is created using
143 measAlg.GaussianPsfFactory with the value of fwhmPixels (the value
144 passed in by the caller, or the default defaultFwhm set in
145 measAlg.GaussianPsfFactory if None).
146
147 Parameters
148 ----------
149 image : `lsst.afw.image.MaskedImage` or `lsst.afw.image.exposure.Exposure`
150 MaskedImage OR Exposure to be interpolated.
151 planeName : `str`, optional
152 Name of mask plane over which to interpolate.
153 If None, must provide a defects list.
154 fwhmPixels : `int`, optional
155 FWHM of core star (pixels).
156 If None the default is used, where the default
157 is set to the exposure psf if available.
158 defects : `lsst.meas.algorithms.Defects`, optional
159 List of defects of type ipIsr.Defects
160 over which to interpolate.
161 """
162 try:
163 maskedImage = image.getMaskedImage()
164 except AttributeError:
165 maskedImage = image
166
167 # set defectList from defects OR mask planeName provided
168 if planeName is None:
169 if defects is None:
170 raise ValueError("No defects or plane name provided")
171 else:
172 if not isinstance(defects, ipIsr.Defects):
173 defectList = ipIsr.Defects(defects)
174 else:
175 defectList = defects
176 planeName = "defects"
177 else:
178 if defects is not None:
179 raise ValueError("Provide EITHER a planeName OR a list of defects, not both")
180 if planeName not in maskedImage.getMask().getMaskPlaneDict():
181 raise ValueError("maskedImage does not contain mask plane %s" % planeName)
182 defectList = ipIsr.Defects.fromMask(maskedImage, planeName)
183
184 # set psf from exposure if provided OR using modelPsf with fwhmPixels provided
185 try:
186 psf = image.getPsf()
187 self.log.info("Setting psf for interpolation from image")
188 except AttributeError:
189 self.log.info("Creating psf model for interpolation from fwhm(pixels) = %s",
190 str(fwhmPixels) if fwhmPixels is not None else
191 (str(self.config.modelPsf.defaultFwhm)) + " [default]")
192 psf = self.config.modelPsf.apply(fwhm=fwhmPixels)
193
194 fallbackValue = 0.0 # interpolateOverDefects needs this to be a float, regardless if it is used
195 if self.config.useFallbackValueAtEdge:
196 fallbackValue = self._setFallbackValue(maskedImage)
197
198 self.interpolateImage(maskedImage, psf, defectList, fallbackValue)
199
200 self.log.info("Interpolated over %d %s pixels.", len(defectList), planeName)
201
202 @contextmanager
203 def transposeContext(self, maskedImage, defects):
204 """Context manager to potentially transpose an image
205
206 This applies the ``transpose`` configuration setting.
207
208 Transposing the image allows us to interpolate along columns instead
209 of rows, which is useful when the saturation trails are typically
210 oriented along rows on the warped/coadded images, instead of along
211 columns as they typically are in raw CCD images.
212
213 Parameters
214 ----------
215 maskedImage : `lsst.afw.image.MaskedImage`
216 Image on which to perform interpolation.
217 defects : `lsst.meas.algorithms.Defects`
218 List of defects to interpolate over.
219
220 Yields
221 ------
222 useImage : `lsst.afw.image.MaskedImage`
223 Image to use for interpolation; it may have been transposed.
224 useDefects : `lsst.meas.algorithms.Defects`
225 List of defects to use for interpolation; they may have been
226 transposed.
227 """
228 def transposeImage(image):
229 """Transpose an image
230
231 Parameters
232 ----------
233 image : `Unknown`
234 """
235 transposed = image.array.T.copy() # Copy to force row-major; required for ndarray+pybind
236 return image.Factory(transposed, False, lsst.geom.Point2I(*reversed(image.getXY0())))
237
238 useImage = maskedImage
239 useDefects = defects
240 if self.config.transpose:
241 useImage = afwImage.makeMaskedImage(transposeImage(maskedImage.image),
242 transposeImage(maskedImage.mask),
243 transposeImage(maskedImage.variance))
244 useDefects = defects.transpose()
245 yield useImage, useDefects
246 if self.config.transpose:
247 maskedImage.image.array = useImage.image.array.T
248 maskedImage.mask.array = useImage.mask.array.T
249 maskedImage.variance.array = useImage.variance.array.T
250
251 def interpolateImage(self, maskedImage, psf, defectList, fallbackValue):
252 """Interpolate over defects in an image
253
254 Parameters
255 ----------
256 maskedImage : `lsst.afw.image.MaskedImage`
257 Image on which to perform interpolation.
258 psf : `lsst.afw.detection.Psf`
259 Point-spread function; currently unused.
260 defectList : `lsst.meas.algorithms.Defects`
261 List of defects to interpolate over.
262 fallbackValue : `float`
263 Value to set when interpolation fails.
264 """
265 if not defectList:
266 return
267 with self.transposeContext(maskedImage, defectList) as (image, defects):
268 measAlg.interpolateOverDefects(image, psf, defects, fallbackValue,
269 self.config.useFallbackValueAtEdge)
int max
interpolateImage(self, maskedImage, psf, defectList, fallbackValue)
run(self, image, planeName=None, fwhmPixels=None, defects=None)
transposeContext(self, maskedImage, defects)
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.
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