Coverage for tests/test_cr.py: 34%

100 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-01 07:12 +0000

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.meas.algorithms as algorithms 

31import lsst.meas.algorithms.testUtils as testUtils 

32import lsst.pex.config as pexConfig 

33import lsst.utils 

34import lsst.utils.logging 

35import lsst.utils.tests 

36 

37# Increase the number for more verbose messages 

38lsst.utils.logging.trace_set_at("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 # These tests were written with the original 0.6 default. 

130 crConfig.cond3_fac2 = 0.6 

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

132 

133 if display: 

134 frame += 1 

135 disp = afwDisplay.Display(frame=frame) 

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

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

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

139 

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

141 if display and False: 

142 for cr in crs: 

143 bbox = cr.getBBox() 

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

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

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

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

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

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

150 

151 if self.nCR is not None: 

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

153 

154 

155class CosmicRayNullTestCase(unittest.TestCase): 

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

157 

158 def setUp(self): 

159 self.FWHM = 5 # pixels 

160 self.size = 128 

161 

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

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

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

165 

166 def tearDown(self): 

167 del self.psf 

168 del self.mi 

169 

170 def testDetection(self): 

171 crConfig = algorithms.FindCosmicRaysConfig() 

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

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

174 

175 

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

177 pass 

178 

179 

180def setup_module(module): 

181 lsst.utils.tests.init() 

182 

183 

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

185 lsst.utils.tests.init() 

186 unittest.main()