Coverage for tests/test_cr.py: 30%

Shortcuts 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

103 statements  

1# This file is part of meas_algorithms. 

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 

22import math 

23import os 

24import sys 

25import unittest 

26 

27import lsst.geom 

28import lsst.afw.image as afwImage 

29import lsst.afw.math as afwMath 

30import lsst.log.utils as logUtils 

31import lsst.meas.algorithms as algorithms 

32import lsst.meas.algorithms.testUtils as testUtils 

33import lsst.pex.config as pexConfig 

34import lsst.utils 

35import lsst.utils.tests 

36 

37# Increase the number for more verbose messages 

38logUtils.traceSetAt("lsst.algorithms.CR", 3) 

39 

40try: 

41 display 

42except NameError: 

43 display = False 

44else: 

45 import lsst.afw.display as afwDisplay 

46 afwDisplay.setDefaultMaskTransparency(75) 

47 

48try: 

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

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

51except Exception: 

52 imageFile0 = None 

53 

54 

55class CosmicRayTestCase(lsst.utils.tests.TestCase): 

56 """A test case for Cosmic Ray detection.""" 

57 

58 def setUp(self): 

59 self.FWHM = 5 # pixels 

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

61 

62 self.mi = afwImage.MaskedImageF(imageFile0) 

63 self.XY0 = lsst.geom.PointI(0, 0) # origin of the subimage we use 

64 

65 if imageFile0: 

66 if True: # use full image, trimmed to data section 

67 self.XY0 = lsst.geom.PointI(32, 2) 

68 self.mi = self.mi.Factory(self.mi, lsst.geom.BoxI(self.XY0, lsst.geom.PointI(2079, 4609)), 

69 afwImage.LOCAL) 

70 self.nCR = 1076 # number of CRs we should detect 

71 else: # use sub-image 

72 if True: 

73 self.XY0 = lsst.geom.PointI(824, 140) 

74 self.nCR = 10 

75 else: 

76 self.XY0 = lsst.geom.PointI(280, 2750) 

77 self.nCR = 2 

78 self.mi = self.mi.Factory(self.mi, lsst.geom.BoxI(self.XY0, lsst.geom.ExtentI(256, 256), 

79 afwImage.LOCAL)) 

80 self.mi.setXY0(lsst.geom.PointI(0, 0)) 

81 else: 

82 self.nCR = None 

83 

84 self.mi.getMask().addMaskPlane("DETECTED") 

85 

86 def tearDown(self): 

87 del self.mi 

88 del self.psf 

89 

90 @unittest.skipUnless(imageFile0, "afwdata not available") 

91 def testDetection(self): 

92 """Test CR detection.""" 

93 # 

94 # Subtract background 

95 # 

96 bctrl = afwMath.BackgroundControl(afwMath.Interpolate.NATURAL_SPLINE) 

97 bctrl.setNxSample(int(self.mi.getWidth()/256) + 1) 

98 bctrl.setNySample(int(self.mi.getHeight()/256) + 1) 

99 bctrl.getStatisticsControl().setNumSigmaClip(3.0) 

100 bctrl.getStatisticsControl().setNumIter(2) 

101 

102 im = self.mi.getImage() 

103 try: 

104 backobj = afwMath.makeBackground(im, bctrl) 

105 except Exception as e: 

106 print(e, file=sys.stderr) 

107 

108 bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT) 

109 backobj = afwMath.makeBackground(im, bctrl) 

110 

111 im -= backobj.getImageF() 

112 

113 if display: 

114 frame = 0 

115 disp = afwDisplay.Display(frame=frame) 

116 disp.mtv(self.mi, title=self._testMethodName + ": Raw") # raw frame 

117 if self.mi.getWidth() > 256: 

118 disp.pan(944 - self.mi.getX0(), 260 - self.mi.getY0()) 

119 # 

120 # Mask known bad pixels 

121 # 

122 badPixels = testUtils.makeDefectList() 

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

124 

125 stats = afwMath.makeStatistics(self.mi.getImage(), afwMath.MEANCLIP | afwMath.STDEVCLIP) 

126 background = stats.getValue(afwMath.MEANCLIP) 

127 

128 crConfig = algorithms.FindCosmicRaysConfig() 

129 crs = algorithms.findCosmicRays(self.mi, self.psf, background, pexConfig.makePropertySet(crConfig)) 

130 

131 if display: 

132 frame += 1 

133 disp = afwDisplay.Display(frame=frame) 

134 disp.mtv(self.mi, title=self._testMethodName + ": CRs removed") 

135 if self.mi.getWidth() > 256: 

136 disp.pan(944 - self.mi.getX0(), 260 - self.mi.getY0()) 

137 

138 print("Detected %d CRs" % len(crs)) 

139 if display and False: 

140 for cr in crs: 

141 bbox = cr.getBBox() 

142 bbox.shift(lsst.geom.ExtentI(-self.mi.getX0(), -self.mi.getY0())) 

143 disp.line([(bbox.getMinX() - 0.5, bbox.getMinY() - 0.5), 

144 (bbox.getMaxX() + 0.5, bbox.getMinY() - 0.5), 

145 (bbox.getMaxX() + 0.5, bbox.getMaxY() + 0.5), 

146 (bbox.getMinX() - 0.5, bbox.getMaxY() + 0.5), 

147 (bbox.getMinX() - 0.5, bbox.getMinY() - 0.5)]) 

148 

149 if self.nCR is not None: 

150 self.assertEqual(len(crs), self.nCR) 

151 

152 

153class CosmicRayNullTestCase(unittest.TestCase): 

154 """A test case for no Cosmic Ray detection.""" 

155 

156 def setUp(self): 

157 self.FWHM = 5 # pixels 

158 self.size = 128 

159 

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

161 self.mi = afwImage.MaskedImageF(128, 128) 

162 self.mi.set((0, 0, 1)) 

163 

164 def tearDown(self): 

165 del self.psf 

166 del self.mi 

167 

168 def testDetection(self): 

169 crConfig = algorithms.FindCosmicRaysConfig() 

170 crs = algorithms.findCosmicRays(self.mi, self.psf, 0.0, pexConfig.makePropertySet(crConfig)) 

171 self.assertEqual(len(crs), 0, "Found %d CRs in empty image" % len(crs)) 

172 

173 

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

175 pass 

176 

177 

178def setup_module(module): 

179 lsst.utils.tests.init() 

180 

181 

182if __name__ == "__main__": 182 ↛ 183line 182 didn't jump to line 183, because the condition on line 182 was never true

183 lsst.utils.tests.init() 

184 unittest.main()