Coverage for tests/test_cr.py : 35%

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# 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.log.utils as logUtils
31import lsst.meas.algorithms as algorithms
32import lsst.pex.config as pexConfig
33import lsst.utils
34import lsst.utils.tests
36# Increase the number for more verbose messages
37logUtils.traceSetAt("algorithms.CR", 3)
39try:
40 display
41except NameError:
42 display = False
43else:
44 import lsst.afw.display as afwDisplay
45 afwDisplay.setDefaultMaskTransparency(75)
47try:
48 afwdataDir = lsst.utils.getPackageDir('afwdata')
49 imageFile0 = os.path.join(afwdataDir, "CFHT", "D4", "cal-53535-i-797722_1.fits")
50except Exception:
51 imageFile0 = None
54class CosmicRayTestCase(lsst.utils.tests.TestCase):
55 """A test case for Cosmic Ray detection."""
57 def setUp(self):
58 self.FWHM = 5 # pixels
59 self.psf = algorithms.DoubleGaussianPsf(29, 29, self.FWHM/(2*math.sqrt(2*math.log(2))))
61 self.mi = afwImage.MaskedImageF(imageFile0)
62 self.XY0 = lsst.geom.PointI(0, 0) # origin of the subimage we use
64 if imageFile0:
65 if True: # use full image, trimmed to data section
66 self.XY0 = lsst.geom.PointI(32, 2)
67 self.mi = self.mi.Factory(self.mi, lsst.geom.BoxI(self.XY0, lsst.geom.PointI(2079, 4609)),
68 afwImage.LOCAL)
69 self.mi.setXY0(lsst.geom.PointI(0, 0))
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 measAlgorithmsDir = lsst.utils.getPackageDir('meas_algorithms')
123 badPixels = algorithms.Defects.readText(os.path.join(measAlgorithmsDir,
124 "policy", "BadPixels.ecsv"))
125 # did someone lie about the origin of the maskedImage? If so, adjust bad pixel list
126 if self.XY0.getX() != self.mi.getX0() or self.XY0.getY() != self.mi.getY0():
127 dx = self.XY0.getX() - self.mi.getX0()
128 dy = self.XY0.getY() - self.mi.getY0()
129 for bp in badPixels:
130 bp.shift(-dx, -dy)
132 algorithms.interpolateOverDefects(self.mi, self.psf, badPixels)
134 stats = afwMath.makeStatistics(self.mi.getImage(), afwMath.MEANCLIP | afwMath.STDEVCLIP)
135 background = stats.getValue(afwMath.MEANCLIP)
137 crConfig = algorithms.FindCosmicRaysConfig()
138 crs = algorithms.findCosmicRays(self.mi, self.psf, background, pexConfig.makePropertySet(crConfig))
140 if display:
141 frame += 1
142 disp = afwDisplay.Display(frame=frame)
143 disp.mtv(self.mi, title=self._testMethodName + ": CRs removed")
144 if self.mi.getWidth() > 256:
145 disp.pan(944 - self.mi.getX0(), 260 - self.mi.getY0())
147 print("Detected %d CRs" % len(crs))
148 if display and False:
149 for cr in crs:
150 bbox = cr.getBBox()
151 bbox.shift(lsst.geom.ExtentI(-self.mi.getX0(), -self.mi.getY0()))
152 disp.line([(bbox.getMinX() - 0.5, bbox.getMinY() - 0.5),
153 (bbox.getMaxX() + 0.5, bbox.getMinY() - 0.5),
154 (bbox.getMaxX() + 0.5, bbox.getMaxY() + 0.5),
155 (bbox.getMinX() - 0.5, bbox.getMaxY() + 0.5),
156 (bbox.getMinX() - 0.5, bbox.getMinY() - 0.5)])
158 if self.nCR is not None:
159 self.assertEqual(len(crs), self.nCR)
162class CosmicRayNullTestCase(unittest.TestCase):
163 """A test case for no Cosmic Ray detection."""
165 def setUp(self):
166 self.FWHM = 5 # pixels
167 self.size = 128
169 self.psf = algorithms.DoubleGaussianPsf(29, 29, self.FWHM/(2*math.sqrt(2*math.log(2))))
170 self.mi = afwImage.MaskedImageF(128, 128)
171 self.mi.set((0, 0, 1))
173 def tearDown(self):
174 del self.psf
175 del self.mi
177 def testDetection(self):
178 crConfig = algorithms.FindCosmicRaysConfig()
179 crs = algorithms.findCosmicRays(self.mi, self.psf, 0.0, pexConfig.makePropertySet(crConfig))
180 self.assertEqual(len(crs), 0, "Found %d CRs in empty image" % len(crs))
183class TestMemory(lsst.utils.tests.MemoryTestCase):
184 pass
187def setup_module(module):
188 lsst.utils.tests.init()
191if __name__ == "__main__": 191 ↛ 192line 191 didn't jump to line 192, because the condition on line 191 was never true
192 lsst.utils.tests.init()
193 unittest.main()