Coverage for tests / test_cr.py: 31%

102 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-14 23:55 +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 crConfig.cond3_fac = 2.5 

132 crConfig.niteration = 3 

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

134 

135 if display: 

136 frame += 1 

137 disp = afwDisplay.Display(frame=frame) 

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

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

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

141 

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

143 if display and False: 

144 for cr in crs: 

145 bbox = cr.getBBox() 

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

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

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

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

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

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

152 

153 if self.nCR is not None: 

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

155 

156 

157class CosmicRayNullTestCase(unittest.TestCase): 

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

159 

160 def setUp(self): 

161 self.FWHM = 5 # pixels 

162 self.size = 128 

163 

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

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

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

167 

168 def tearDown(self): 

169 del self.psf 

170 del self.mi 

171 

172 def testDetection(self): 

173 crConfig = algorithms.FindCosmicRaysConfig() 

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

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

176 

177 

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

179 pass 

180 

181 

182def setup_module(module): 

183 lsst.utils.tests.init() 

184 

185 

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

187 lsst.utils.tests.init() 

188 unittest.main()