lsst.pipe.tasks
g2c8cee2ce7+cc5eabfb3a
Toggle main menu visibility
Loading...
Searching...
No Matches
python
lsst
pipe
tasks
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
28
from
contextlib
import
contextmanager
29
30
import
lsst.pex.config
as
pexConfig
31
import
lsst.geom
32
import
lsst.afw.image
as
afwImage
33
import
lsst.afw.math
as
afwMath
34
import
lsst.ip.isr
as
ipIsr
35
import
lsst.meas.algorithms
as
measAlg
36
import
lsst.pipe.base
as
pipeBase
37
from
lsst.utils.timer
import
timeMethod
38
39
40
class
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)
78
if
self.
useFallbackValueAtEdge
:
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
85
class
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.debug(
"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.trace(
"Setting psf for interpolation from image"
)
188
except
AttributeError:
189
self.log.debug(
"Creating psf model for interpolation from fwhm(pixels) = %s"
,
190
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.debug(
"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)
lsst::geom::Point< int, 2 >
lsst.pipe.tasks.interpImage.InterpImageConfig
Definition
interpImage.py:40
lsst.pipe.tasks.interpImage.InterpImageConfig.fallbackValueType
str fallbackValueType
Definition
interpImage.py:50
lsst.pipe.tasks.interpImage.InterpImageConfig.fallbackUserValue
fallbackUserValue
Definition
interpImage.py:61
lsst.pipe.tasks.interpImage.InterpImageConfig.negativeFallbackAllowed
negativeFallbackAllowed
Definition
interpImage.py:66
lsst.pipe.tasks.interpImage.InterpImageConfig.useFallbackValueAtEdge
useFallbackValueAtEdge
Definition
interpImage.py:45
lsst.pipe.tasks.interpImage.InterpImageConfig.validate
validate(self)
Definition
interpImage.py:76
lsst.pipe.tasks.interpImage.InterpImageTask
Definition
interpImage.py:85
lsst.pipe.tasks.interpImage.InterpImageTask.interpolateImage
interpolateImage(self, maskedImage, psf, defectList, fallbackValue)
Definition
interpImage.py:252
lsst.pipe.tasks.interpImage.InterpImageTask._setFallbackValue
_setFallbackValue(self, mi=None)
Definition
interpImage.py:91
lsst.pipe.tasks.interpImage.InterpImageTask.run
run(self, image, planeName=None, fwhmPixels=None, defects=None)
Definition
interpImage.py:131
lsst.pipe.tasks.interpImage.InterpImageTask.transposeContext
transposeContext(self, maskedImage, defects)
Definition
interpImage.py:204
lsst::afw::image
lsst::afw::math
lsst::geom
lsst::ip::isr
lsst::meas::algorithms
lsst::pex::config
Generated on
for lsst.pipe.tasks by
1.17.0