Coverage for tests/test_cr.py: 33%
99 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-01-17 19:18 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-01-17 19:18 +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/>.
22import math
23import os
24import sys
25import unittest
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
37# Increase the number for more verbose messages
38lsst.utils.logging.trace_set_at("lsst.algorithms.CR", 3)
40try:
41 display
42except NameError:
43 display = False
44else:
45 import lsst.afw.display as afwDisplay
46 afwDisplay.setDefaultMaskTransparency(75)
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
55class CosmicRayTestCase(lsst.utils.tests.TestCase):
56 """A test case for Cosmic Ray detection."""
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))))
62 self.mi = afwImage.MaskedImageF(imageFile0)
63 self.XY0 = lsst.geom.PointI(0, 0) # origin of the subimage we use
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
84 self.mi.getMask().addMaskPlane("DETECTED")
86 def tearDown(self):
87 del self.mi
88 del self.psf
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)
102 im = self.mi.getImage()
103 try:
104 backobj = afwMath.makeBackground(im, bctrl)
105 except Exception as e:
106 print(e, file=sys.stderr)
108 bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT)
109 backobj = afwMath.makeBackground(im, bctrl)
111 im -= backobj.getImageF()
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)
125 stats = afwMath.makeStatistics(self.mi.getImage(), afwMath.MEANCLIP | afwMath.STDEVCLIP)
126 background = stats.getValue(afwMath.MEANCLIP)
128 crConfig = algorithms.FindCosmicRaysConfig()
129 crs = algorithms.findCosmicRays(self.mi, self.psf, background, pexConfig.makePropertySet(crConfig))
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())
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)])
149 if self.nCR is not None:
150 self.assertEqual(len(crs), self.nCR)
153class CosmicRayNullTestCase(unittest.TestCase):
154 """A test case for no Cosmic Ray detection."""
156 def setUp(self):
157 self.FWHM = 5 # pixels
158 self.size = 128
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))
164 def tearDown(self):
165 del self.psf
166 del self.mi
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))
174class TestMemory(lsst.utils.tests.MemoryTestCase):
175 pass
178def setup_module(module):
179 lsst.utils.tests.init()
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()