Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

# 

# LSST Data Management System 

# Copyright 2008-2015 AURA/LSST. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

from contextlib import contextmanager 

import lsst.pex.config as pexConfig 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.meas.algorithms as measAlg 

import lsst.pipe.base as pipeBase 

 

__all__ = ["InterpImageConfig", "InterpImageTask"] 

 

 

class InterpImageConfig(pexConfig.Config): 

"""Config for InterpImageTask 

""" 

modelPsf = measAlg.GaussianPsfFactory.makeField(doc="Model Psf factory") 

 

useFallbackValueAtEdge = pexConfig.Field( 

dtype=bool, 

doc="Smoothly taper to the fallback value at the edge of the image?", 

default=True, 

) 

fallbackValueType = pexConfig.ChoiceField( 

dtype=str, 

doc="Type of statistic to calculate edge fallbackValue for interpolation", 

allowed={ 

"MEAN": "mean", 

"MEDIAN": "median", 

"MEANCLIP": "clipped mean", 

"USER": "user value set in fallbackUserValue config", 

}, 

default="MEDIAN", 

) 

fallbackUserValue = pexConfig.Field( 

dtype=float, 

doc="If fallbackValueType is 'USER' then use this as the fallbackValue; ignored otherwise", 

default=0.0, 

) 

negativeFallbackAllowed = pexConfig.Field( 

dtype=bool, 

doc=("Allow negative values for egde interpolation fallbackValue? If False, set " 

"fallbackValue to max(fallbackValue, 0.0)"), 

default=False, 

) 

transpose = pexConfig.Field(dtype=int, default=False, 

doc="Transpose image before interpolating? " 

"This allows the interpolation to act over columns instead of rows.") 

 

def validate(self): 

pexConfig.Config.validate(self) 

if self.useFallbackValueAtEdge: 

if (not self.negativeFallbackAllowed and self.fallbackValueType == "USER" and 

self.fallbackUserValue < 0.0): 

raise ValueError("User supplied fallbackValue is negative (%.2f) but " 

"negativeFallbackAllowed is False" % self.fallbackUserValue) 

 

 

class InterpImageTask(pipeBase.Task): 

"""Interpolate over bad image pixels 

""" 

ConfigClass = InterpImageConfig 

_DefaultName = "interpImage" 

 

def _setFallbackValue(self, mi=None): 

"""Set the edge fallbackValue for interpolation 

 

@param[in] mi input maksedImage on which to calculate the statistics 

Must be provided if fallbackValueType != "USER". 

 

@return fallbackValue The value set/computed based on the fallbackValueType 

and negativeFallbackAllowed config parameters 

""" 

if self.config.fallbackValueType != 'USER': 

assert mi, "No maskedImage provided" 

if self.config.fallbackValueType == 'MEAN': 

fallbackValue = afwMath.makeStatistics(mi, afwMath.MEAN).getValue() 

elif self.config.fallbackValueType == 'MEDIAN': 

fallbackValue = afwMath.makeStatistics(mi, afwMath.MEDIAN).getValue() 

elif self.config.fallbackValueType == 'MEANCLIP': 

fallbackValue = afwMath.makeStatistics(mi, afwMath.MEANCLIP).getValue() 

elif self.config.fallbackValueType == 'USER': 

fallbackValue = self.config.fallbackUserValue 

else: 

raise NotImplementedError("%s : %s not implemented" % 

("fallbackValueType", self.config.fallbackValueType)) 

 

if not self.config.negativeFallbackAllowed and fallbackValue < 0.0: 

self.log.warn("Negative interpolation edge fallback value computed but " 

"negativeFallbackAllowed is False: setting fallbackValue to 0.0") 

fallbackValue = max(fallbackValue, 0.0) 

 

self.log.info("fallbackValueType %s has been set to %.4f" % 

(self.config.fallbackValueType, fallbackValue)) 

 

return fallbackValue 

 

@pipeBase.timeMethod 

def run(self, image, planeName=None, fwhmPixels=None, defects=None): 

"""!Interpolate in place over pixels in a maskedImage marked as bad 

 

Pixels to be interpolated are set by either a mask planeName provided 

by the caller OR a defects list of type `~lsst.meas.algorithms.Defects` 

If both are provided an exception is raised. 

 

Note that the interpolation code in meas_algorithms currently doesn't 

use the input PSF (though it's a required argument), so it's not 

important to set the input PSF parameters exactly. This PSF is set 

here as the psf attached to the "image" (i.e if the image passed in 

is an Exposure). Otherwise, a psf model is created using 

measAlg.GaussianPsfFactory with the value of fwhmPixels (the value 

passed in by the caller, or the default defaultFwhm set in 

measAlg.GaussianPsfFactory if None). 

 

@param[in,out] image MaskedImage OR Exposure to be interpolated 

@param[in] planeName name of mask plane over which to interpolate 

If None, must provide a defects list. 

@param[in] fwhmPixels FWHM of core star (pixels) 

If None the default is used, where the default 

is set to the exposure psf if available 

@param[in] defects List of defects of type measAlg.Defects 

over which to interpolate. 

""" 

try: 

maskedImage = image.getMaskedImage() 

except AttributeError: 

maskedImage = image 

 

# set defectList from defects OR mask planeName provided 

if planeName is None: 

if defects is None: 

raise ValueError("No defects or plane name provided") 

else: 

if not isinstance(defects, measAlg.Defects): 

defectList = measAlg.Defects(defects) 

else: 

defectList = defects 

planeName = "defects" 

else: 

if defects is not None: 

raise ValueError("Provide EITHER a planeName OR a list of defects, not both") 

if planeName not in maskedImage.getMask().getMaskPlaneDict(): 

raise ValueError("maskedImage does not contain mask plane %s" % planeName) 

defectList = measAlg.Defects.fromMask(maskedImage, planeName) 

 

# set psf from exposure if provided OR using modelPsf with fwhmPixels provided 

try: 

psf = image.getPsf() 

self.log.info("Setting psf for interpolation from image") 

except AttributeError: 

self.log.info("Creating psf model for interpolation from fwhm(pixels) = %s" % 

(str(fwhmPixels) if fwhmPixels is not None else 

(str(self.config.modelPsf.defaultFwhm)) + " [default]")) 

psf = self.config.modelPsf.apply(fwhm=fwhmPixels) 

 

fallbackValue = 0.0 # interpolateOverDefects needs this to be a float, regardless if it is used 

if self.config.useFallbackValueAtEdge: 

fallbackValue = self._setFallbackValue(maskedImage) 

 

self.interpolateImage(maskedImage, psf, defectList, fallbackValue) 

 

self.log.info("Interpolated over %d %s pixels." % (len(defectList), planeName)) 

 

@contextmanager 

def transposeContext(self, maskedImage, defects): 

"""Context manager to potentially transpose an image 

 

This applies the ``transpose`` configuration setting. 

 

Transposing the image allows us to interpolate along columns instead 

of rows, which is useful when the saturation trails are typically 

oriented along rows on the warped/coadded images, instead of along 

columns as they typically are in raw CCD images. 

 

Parameters 

---------- 

maskedImage : `lsst.afw.image.MaskedImage` 

Image on which to perform interpolation. 

defects : `lsst.meas.algorithms.Defects` 

List of defects to interpolate over. 

 

Yields 

------ 

useImage : `lsst.afw.image.MaskedImage` 

Image to use for interpolation; it may have been transposed. 

useDefects : `lsst.meas.algorithms.Defects` 

List of defects to use for interpolation; they may have been 

transposed. 

""" 

def transposeImage(image): 

"""Transpose an image""" 

transposed = image.array.T.copy() # Copy to force row-major; required for ndarray+pybind 

return image.Factory(transposed, False, lsst.geom.Point2I(*reversed(image.getXY0()))) 

 

useImage = maskedImage 

useDefects = defects 

if self.config.transpose: 

useImage = afwImage.makeMaskedImage(transposeImage(maskedImage.image), 

transposeImage(maskedImage.mask), 

transposeImage(maskedImage.variance)) 

useDefects = defects.transpose() 

yield useImage, useDefects 

if self.config.transpose: 

maskedImage.image.array = useImage.image.array.T 

maskedImage.mask.array = useImage.mask.array.T 

maskedImage.variance.array = useImage.variance.array.T 

 

def interpolateImage(self, maskedImage, psf, defectList, fallbackValue): 

"""Interpolate over defects in an image 

 

Parameters 

---------- 

maskedImage : `lsst.afw.image.MaskedImage` 

Image on which to perform interpolation. 

psf : `lsst.afw.detection.Psf` 

Point-spread function; currently unused. 

defectList : `lsst.meas.algorithms.Defects` 

List of defects to interpolate over. 

fallbackValue : `float` 

Value to set when interpolation fails. 

""" 

if not defectList: 

return 

with self.transposeContext(maskedImage, defectList) as (image, defects): 

measAlg.interpolateOverDefects(image, psf, defects, fallbackValue, 

self.config.useFallbackValueAtEdge)