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

# 

# LSST Data Management System 

# 

# Copyright 2008-2016 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/>. 

# 

import os 

import unittest 

import math 

import numpy as np 

 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.display.ds9 as ds9 

import lsst.meas.algorithms as algorithms 

import lsst.meas.algorithms.defects as defects 

import lsst.utils.tests 

 

 

try: 

type(display) 

except NameError: 

display = False 

 

# Determine if we have afwdata 

try: 

afwdataDir = lsst.utils.getPackageDir('afwdata') 

except Exception: 

afwdataDir = None 

 

 

class InterpolationTestCase(lsst.utils.tests.TestCase): 

"""A test case for interpolation.""" 

 

def setUp(self): 

self.FWHM = 5 

self.psf = algorithms.DoubleGaussianPsf(15, 15, self.FWHM/(2*math.sqrt(2*math.log(2)))) 

maskedImageFile = os.path.join(afwdataDir, "CFHT", "D4", "cal-53535-i-797722_1.fits") 

 

self.mi = afwImage.MaskedImageF(maskedImageFile) 

if False: # use sub-image? 

self.mi = self.mi.Factory(self.mi, afwImage.BBox(afwImage.PointI(760, 20), 256, 256)) 

self.mi.getMask().addMaskPlane("INTERP") 

 

measAlgorithmsDir = lsst.utils.getPackageDir('meas_algorithms') 

self.badPixels = defects.policyToBadRegionList( 

os.path.join(measAlgorithmsDir, "policy", "BadPixels.paf")) 

 

def tearDown(self): 

del self.mi 

del self.psf 

del self.badPixels 

 

@unittest.skipUnless(afwdataDir, "afwdata not available") 

def testDetection(self): 

"""Test Interp algorithms.""" 

 

if display: 

frame = 0 

ds9.mtv(self.mi, frame=frame, title="Original") 

 

algorithms.interpolateOverDefects(self.mi, self.psf, self.badPixels) 

 

if display: 

ds9.mtv(self.mi, frame=frame + 1, title="Interpolated") 

ds9.mtv(self.mi.getVariance(), frame=frame + 2, title="Variance") 

 

@unittest.skipUnless(afwdataDir, "afwdata not available") 

def test818(self): 

"""A test case for #818; the full test is in /lsst/DC3root/ticketFiles/818""" 

 

badPixels = [] 

defects = [((82, 663), 6, 8), 

((83, 659), 9, 6), 

((85, 660), 10, 11), 

((87, 669), 3, 3), 

] 

 

for xy0, width, height in defects: 

x0, y0 = xy0 

bbox = lsst.geom.BoxI(lsst.geom.PointI(x0, y0), lsst.geom.ExtentI(width, height)) 

badPixels.append(algorithms.Defect(bbox)) 

 

mi = afwImage.MaskedImageF(517, 800) 

 

algorithms.interpolateOverDefects(mi, self.psf, badPixels) 

 

@unittest.skipUnless(afwdataDir, "afwdata not available") 

def test1295(self): 

"""A test case for #1295 (failure to interpolate over groups of defects.""" 

im = afwImage.ImageF(lsst.geom.ExtentI(100, 100)) 

mi = afwImage.makeMaskedImage(im) 

mi.set(100) 

flat = afwImage.ImageF(im.getDimensions()) 

flat.set(1) 

flat[50:51, :, afwImage.LOCAL] = 0.0 

flat[55:56, :, afwImage.LOCAL] = 0.0 

flat[58:59, :, afwImage.LOCAL] = 0.0 

flat[51:60, 51:, afwImage.LOCAL] = 0.0 

 

mi /= flat 

 

if display: 

ds9.mtv(mi, frame=0, title="Raw") 

 

defectList = [] 

bbox = lsst.geom.BoxI(lsst.geom.PointI(50, 0), lsst.geom.ExtentI(1, 100)) 

defectList.append(algorithms.Defect(bbox)) 

bbox = lsst.geom.BoxI(lsst.geom.PointI(55, 0), lsst.geom.ExtentI(1, 100)) 

defectList.append(algorithms.Defect(bbox)) 

bbox = lsst.geom.BoxI(lsst.geom.PointI(58, 0), lsst.geom.ExtentI(1, 100)) 

defectList.append(algorithms.Defect(bbox)) 

bbox = lsst.geom.BoxI(lsst.geom.PointI(51, 51), lsst.geom.ExtentI(9, 49)) 

defectList.append(algorithms.Defect(bbox)) 

 

psf = algorithms.DoubleGaussianPsf(15, 15, 1./(2*math.sqrt(2*math.log(2)))) 

algorithms.interpolateOverDefects(mi, psf, defectList, 50.) 

 

if display: 

ds9.mtv(mi, frame=1, title="Interpolated") 

 

self.assertTrue(np.isfinite(mi.image[56, 51, afwImage.LOCAL])) 

 

@unittest.skipUnless(afwdataDir, "afwdata not available") 

def testEdge(self): 

"""Test that we can interpolate to the edge""" 

mi = afwImage.MaskedImageF(80, 30) 

 

ima = mi.getImage().getArray() 

# 

# Loop over number of bad columns at left or right edge of image 

# 

for nBadCol in range(0, 20): 

mi.set((0, 0x0, 0)) 

 

np.random.seed(666) 

ima[:] = np.random.uniform(-1, 1, ima.shape) 

 

defects = [] 

 

if nBadCol > 0: 

# 

# Bad left edge 

# 

ima[:, 0:nBadCol] = 10 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, 0), 

lsst.geom.ExtentI(nBadCol, mi.getHeight()))) 

# 

# With another bad set of columns next to bad left edge 

# 

ima[:, -nBadCol:] = 10 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(mi.getWidth() - nBadCol, 0), 

lsst.geom.ExtentI(nBadCol, mi.getHeight()))) 

# 

# Bad right edge 

# 

ima[0:10, nBadCol+1:nBadCol+4] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(nBadCol+1, 0), 

lsst.geom.ExtentI(3, 10))) 

# 

# With another bad set of columns next to bad right edge 

# 

ima[0:10, -nBadCol-4:-nBadCol-1] = 100 

defects.append((lsst.geom.BoxI(lsst.geom.PointI(mi.getWidth() - nBadCol - 4, 0), 

lsst.geom.ExtentI(3, 10)))) 

# 

# Test cases that left and right bad patches nearly (or do) coalesce 

# 

ima[-3:, 0:mi.getWidth()//2-1] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, mi.getHeight() - 3), 

lsst.geom.ExtentI(mi.getWidth()//2-1, 1))) 

 

ima[-3:, mi.getWidth()//2+1:] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(mi.getWidth()//2 + 1, mi.getHeight() - 3), 

lsst.geom.ExtentI(mi.getWidth()//2 - 1, 1))) 

 

ima[-2:, 0:mi.getWidth()//2] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, mi.getHeight() - 2), 

lsst.geom.ExtentI(mi.getWidth()//2, 1))) 

 

ima[-2:, mi.getWidth()//2+1:] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(mi.getWidth()//2 + 1, mi.getHeight() - 2), 

lsst.geom.ExtentI(mi.getWidth()//2 - 1, 1))) 

 

ima[-1:, :] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, mi.getHeight() - 1), 

lsst.geom.ExtentI(mi.getWidth(), 1))) 

 

# Test fix for HSC-978: long defect stops one pixel shy of the edge (when nBadCol == 0) 

ima[13, :-1] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(0, 13), lsst.geom.ExtentI(mi.getWidth() - 1, 1))) 

ima[14, 1:] = 100 

defects.append(lsst.geom.BoxI(lsst.geom.PointI(1, 14), lsst.geom.ExtentI(mi.getWidth() - 1, 1))) 

 

# 

# Build list of defects to interpolate over 

# 

defectList = [] 

 

for bbox in defects: 

defectList.append(algorithms.Defect(bbox)) 

# 

# Guess a PSF and do the work 

# 

if display: 

ds9.mtv(mi, frame=0) 

 

psf = algorithms.DoubleGaussianPsf(15, 15, 1./(2*math.sqrt(2*math.log(2)))) 

algorithms.interpolateOverDefects(mi, psf, defectList, 0, True) 

 

if display: 

ds9.mtv(mi, frame=1) 

 

self.assertGreater(np.min(ima), -2) 

self.assertGreater(2, np.max(ima)) 

 

 

class TestMemory(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

242 ↛ 243line 242 didn't jump to line 243, because the condition on line 242 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()