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

# This file is part of pipe_tasks. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# 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 GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

""" 

Tests for bad pixel interpolation task 

 

Run with: 

python test_interpImageTask.py 

or 

pytest test_interpImageTask.py 

""" 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.pex.config as pexConfig 

import lsst.meas.algorithms as measAlg 

from lsst.pipe.tasks.interpImage import InterpImageTask 

 

try: 

display 

except NameError: 

display = False 

else: 

import lsst.afw.display as afwDisplay 

afwDisplay.setDefaultMaskTransparency(75) 

 

 

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

"""A test case for interpolation. 

""" 

 

def setUp(self): 

self.FWHM = 5 

 

def testEdge(self): 

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

""" 

mi = afwImage.MaskedImageF(80, 30) 

ima = mi.getImage().getArray() 

# 

# We'll set the BAD bit in pixels we wish to interpolate over 

# 

pixelPlane = "BAD" 

badBit = afwImage.Mask.getPlaneBitMask(pixelPlane) 

# 

# Set bad columns near left and right sides of image 

# 

nBadCol = 10 

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

 

np.random.seed(666) 

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

 

mi[0:nBadCol, :, afwImage.LOCAL] = (10, badBit, 0) # Bad left edge 

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

mi[-nBadCol:, :, afwImage.LOCAL] = (10, badBit, 0) 

mi[nBadCol + 1:nBadCol + 4, 0:10, afwImage.LOCAL] = (100, badBit, 0) # Bad right edge 

# more bad of columns next to bad right edge 

mi[-nBadCol - 4:-nBadCol - 1, 0:10, afwImage.LOCAL] = (100, badBit, 0) 

 

defectList = measAlg.Defects.fromMask(mi, pixelPlane) 

 

if display: 

afwDisplay.Display(frame=0).mtv(mi, title=self._testMethodName + ": image") 

 

def validateInterp(miInterp, useFallbackValueAtEdge, fallbackValue): 

imaInterp = miInterp.getImage().getArray() 

if display: 

afwDisplay.Display(frame=1).mtv(miInterp, title=self._testMethodName + ": interp image") 

self.assertGreater(np.min(imaInterp), min(-2, 2*fallbackValue)) 

self.assertGreater(max(2, 2*fallbackValue), np.max(imaInterp)) 

val0 = np.mean(miInterp.image[1:2, :, afwImage.LOCAL].array, dtype=float) 

if useFallbackValueAtEdge: 

self.assertAlmostEqual(val0, fallbackValue, 6) 

else: 

self.assertNotEqual(val0, 0) 

 

for useFallbackValueAtEdge in (False, True): 

miInterp = mi.clone() 

config = InterpImageTask.ConfigClass() 

config.useFallbackValueAtEdge = useFallbackValueAtEdge 

interpTask = InterpImageTask(config) 

 

if useFallbackValueAtEdge: 

config.fallbackUserValue = -1.0 

# choiceField fallbackValueType cannot be None if useFallbackValueAtEdge is True 

config.fallbackValueType = None 

self.assertRaises(NotImplementedError, interpTask._setFallbackValue, miInterp) 

# make sure an invalid fallbackValueType raises a pexConfig.FieldValidationError 

with self.assertRaises(pexConfig.FieldValidationError): 

config.fallbackValueType = "NOTUSED" 

# make sure ValueError is raised if both a planeName and defects list are provided 

self.assertRaises(ValueError, interpTask.run, miInterp, defects=defectList, 

planeName=pixelPlane, fwhmPixels=self.FWHM) 

 

for fallbackValueType in ("USER", "MEAN", "MEDIAN", "MEANCLIP"): 

for negativeFallbackAllowed in (True, False): 

config.negativeFallbackAllowed = negativeFallbackAllowed 

config.fallbackValueType = fallbackValueType 

# Should raise if negative not allowed, but USER supplied negative value 

if not negativeFallbackAllowed and fallbackValueType == "USER": 

self.assertRaises(ValueError, config.validate) 

 

interpTask = InterpImageTask(config) 

fallbackValue = interpTask._setFallbackValue(mi) 

# 

# Time to interpolate 

# 

miInterp = mi.clone() 

interpTask.run(miInterp, planeName=pixelPlane, fwhmPixels=self.FWHM) 

validateInterp(miInterp, useFallbackValueAtEdge, fallbackValue) 

miInterp = mi.clone() 

interpTask.run(miInterp, defects=defectList) 

validateInterp(miInterp, useFallbackValueAtEdge, fallbackValue) 

else: 

# 

# Time to interpolate 

# 

miInterp = mi.clone() 

interpTask.run(miInterp, planeName=pixelPlane, fwhmPixels=self.FWHM) 

validateInterp(miInterp, useFallbackValueAtEdge, 0) 

 

def testTranspose(self): 

"""Test transposition before interpolation 

 

Interpolate over a bad row (not a bad column). 

""" 

box = lsst.geom.Box2I(lsst.geom.Point2I(12345, 6789), lsst.geom.Extent2I(123, 45)) 

value = 123.45 

bad = "BAD" 

image = afwImage.MaskedImageF(box) 

image.image.set(value) 

image.mask.set(0) 

 

badRow = box.getHeight()//2 

image.image.array[badRow] = 10*value 

image.mask.array[badRow] = image.mask.getPlaneBitMask(bad) 

 

config = InterpImageTask.ConfigClass() 

config.transpose = True 

task = InterpImageTask(config) 

task.run(image, planeName=bad, fwhmPixels=self.FWHM) 

self.assertFloatsEqual(image.image.array, value) 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

pass 

 

 

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

lsst.utils.tests.init() 

unittest.main()