Coverage for tests/test_assessSpatialKernelVisitor.py: 21%
96 statements
« prev ^ index » next coverage.py v7.3.3, created at 2023-12-17 12:36 +0000
« prev ^ index » next coverage.py v7.3.3, created at 2023-12-17 12:36 +0000
1import unittest
2import numpy as num
5import lsst.utils.tests
6import lsst.afw.image as afwImage
7import lsst.afw.math as afwMath
8import lsst.ip.diffim as ipDiffim
9import lsst.geom as geom
10import lsst.utils.logging as logUtils
11import lsst.pex.config as pexConfig
13# Increase the number for more verbose messages; decrease for fewer messages
14logUtils.trace_set_at("lsst.ip.diffim", 4)
17class DiffimTestCases(unittest.TestCase):
19 def setUp(self):
20 config = ipDiffim.PsfMatchConfigAL()
22 self.ps = pexConfig.makePropertySet(config)
23 self.kList = ipDiffim.makeKernelBasisList(config)
25 self.ksize = self.ps['kernelSize']
27 def makeSpatialKernel(self, order):
28 basicGaussian1 = afwMath.GaussianFunction2D(2., 2., 0.)
29 basicKernel1 = afwMath.AnalyticKernel(self.ksize, self.ksize, basicGaussian1)
31 basicGaussian2 = afwMath.GaussianFunction2D(5., 3., 0.5 * num.pi)
32 basicKernel2 = afwMath.AnalyticKernel(self.ksize, self.ksize, basicGaussian2)
34 basisList = []
35 basisList.append(basicKernel1)
36 basisList.append(basicKernel2)
37 basisList = ipDiffim.renormalizeKernelList(basisList)
39 spatialKernelFunction = afwMath.PolynomialFunction2D(order)
40 spatialKernel = afwMath.LinearCombinationKernel(basisList, spatialKernelFunction)
41 kCoeffs = [[0.0 for x in range(1, spatialKernelFunction.getNParameters()+1)],
42 [0.01 * x for x in range(1, spatialKernelFunction.getNParameters()+1)]]
43 kCoeffs[0][0] = 1.0 # it does not vary spatially; constant across image
44 spatialKernel.setSpatialParameters(kCoeffs)
45 return spatialKernel
47 def tearDown(self):
48 del self.ps
49 del self.kList
51 def testGood(self):
52 ti = afwImage.MaskedImageF(geom.Extent2I(100, 100))
53 ti.getVariance().set(0.1)
54 ti[50, 50, afwImage.LOCAL] = (1., 0x0, 1.)
55 sKernel = self.makeSpatialKernel(2)
56 si = afwImage.MaskedImageF(ti.getDimensions())
57 convolutionControl = afwMath.ConvolutionControl()
58 convolutionControl.setDoNormalize(True)
59 afwMath.convolve(si, ti, sKernel, convolutionControl)
61 bbox = geom.Box2I(geom.Point2I(25, 25),
62 geom.Point2I(75, 75))
63 si = afwImage.MaskedImageF(si, bbox, origin=afwImage.LOCAL)
64 ti = afwImage.MaskedImageF(ti, bbox, origin=afwImage.LOCAL)
65 kc = ipDiffim.KernelCandidateF(50., 50., ti, si, self.ps)
67 sBg = afwMath.PolynomialFunction2D(1)
68 bgCoeffs = [0., 0., 0.]
69 sBg.setParameters(bgCoeffs)
71 # must be initialized
72 bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps)
73 bskv.processCandidate(kc)
74 self.assertEqual(kc.isInitialized(), True)
76 askv = ipDiffim.AssessSpatialKernelVisitorF(sKernel, sBg, self.ps)
77 askv.processCandidate(kc)
79 self.assertEqual(askv.getNProcessed(), 1)
80 self.assertEqual(askv.getNRejected(), 0)
81 self.assertEqual(kc.getStatus(), afwMath.SpatialCellCandidate.GOOD)
83 def testBad(self):
84 ti = afwImage.MaskedImageF(geom.Extent2I(100, 100))
85 ti.getVariance().set(0.1)
86 ti[50, 50, afwImage.LOCAL] = (1., 0x0, 1.)
87 sKernel = self.makeSpatialKernel(2)
88 si = afwImage.MaskedImageF(ti.getDimensions())
89 convolutionControl = afwMath.ConvolutionControl()
90 convolutionControl.setDoNormalize(True)
91 afwMath.convolve(si, ti, sKernel, convolutionControl)
93 bbox = geom.Box2I(geom.Point2I(25, 25),
94 geom.Point2I(75, 75))
95 si = afwImage.MaskedImageF(si, bbox, origin=afwImage.LOCAL)
96 ti = afwImage.MaskedImageF(ti, bbox, origin=afwImage.LOCAL)
97 kc = ipDiffim.KernelCandidateF(50., 50., ti, si, self.ps)
99 badGaussian = afwMath.GaussianFunction2D(1., 1., 0.)
100 badKernel = afwMath.AnalyticKernel(self.ksize, self.ksize, badGaussian)
101 basisList = []
102 basisList.append(badKernel)
103 badSpatialKernelFunction = afwMath.PolynomialFunction2D(0)
104 badSpatialKernel = afwMath.LinearCombinationKernel(basisList, badSpatialKernelFunction)
105 badSpatialKernel.setSpatialParameters([[1, ]])
107 sBg = afwMath.PolynomialFunction2D(1)
108 bgCoeffs = [10., 10., 10.]
109 sBg.setParameters(bgCoeffs)
111 # must be initialized
112 bskv = ipDiffim.BuildSingleKernelVisitorF(self.kList, self.ps)
113 bskv.processCandidate(kc)
114 self.assertEqual(kc.isInitialized(), True)
116 askv = ipDiffim.AssessSpatialKernelVisitorF(badSpatialKernel, sBg, self.ps)
117 askv.processCandidate(kc)
119 self.assertEqual(askv.getNProcessed(), 1)
120 self.assertEqual(askv.getNRejected(), 1)
121 self.assertEqual(kc.getStatus(), afwMath.SpatialCellCandidate.BAD)
124#####
126class TestMemory(lsst.utils.tests.MemoryTestCase):
127 pass
130def setup_module(module):
131 lsst.utils.tests.init()
134if __name__ == "__main__": 134 ↛ 135line 134 didn't jump to line 135, because the condition on line 134 was never true
135 lsst.utils.tests.init()
136 unittest.main()