LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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 self.metadata["interpolated_pixel_count"] = len(defectList)
202
203 @contextmanager
204 def transposeContext(self, maskedImage, defects):
205 """Context manager to potentially transpose an image
206
207 This applies the ``transpose`` configuration setting.
208
209 Transposing the image allows us to interpolate along columns instead
210 of rows, which is useful when the saturation trails are typically
211 oriented along rows on the warped/coadded images, instead of along
212 columns as they typically are in raw CCD images.
213
214 Parameters
215 ----------
216 maskedImage : `lsst.afw.image.MaskedImage`
217 Image on which to perform interpolation.
218 defects : `lsst.meas.algorithms.Defects`
219 List of defects to interpolate over.
220
221 Yields
222 ------
223 useImage : `lsst.afw.image.MaskedImage`
224 Image to use for interpolation; it may have been transposed.
225 useDefects : `lsst.meas.algorithms.Defects`
226 List of defects to use for interpolation; they may have been
227 transposed.
228 """
229 def transposeImage(image):
230 """Transpose an image
231
232 Parameters
233 ----------
234 image : `Unknown`
235 """
236 transposed = image.array.T.copy() # Copy to force row-major; required for ndarray+pybind
237 return image.Factory(transposed, False, lsst.geom.Point2I(*reversed(image.getXY0())))
238
239 useImage = maskedImage
240 useDefects = defects
241 if self.config.transpose:
242 useImage = afwImage.makeMaskedImage(transposeImage(maskedImage.image),
243 transposeImage(maskedImage.mask),
244 transposeImage(maskedImage.variance))
245 useDefects = defects.transpose()
246 yield useImage, useDefects
247 if self.config.transpose:
248 maskedImage.image.array = useImage.image.array.T
249 maskedImage.mask.array = useImage.mask.array.T
250 maskedImage.variance.array = useImage.variance.array.T
251
252 def interpolateImage(self, maskedImage, psf, defectList, fallbackValue):
253 """Interpolate over defects in an image
254
255 Parameters
256 ----------
257 maskedImage : `lsst.afw.image.MaskedImage`
258 Image on which to perform interpolation.
259 psf : `lsst.afw.detection.Psf`
260 Point-spread function; currently unused.
261 defectList : `lsst.meas.algorithms.Defects`
262 List of defects to interpolate over.
263 fallbackValue : `float`
264 Value to set when interpolation fails.
265 """
266 if not defectList:
267 return
268 with self.transposeContext(maskedImage, defectList) as (image, defects):
269 measAlg.interpolateOverDefects(image, psf, defects, fallbackValue,
270 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