Coverage for tests/test_modelPsfMatch.py: 25%
91 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-08 08:40 +0000
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-08 08:40 +0000
1import unittest
3import lsst.utils.tests
4import lsst.daf.base as dafBase
5from lsst.afw.coord import Observatory, Weather
6import lsst.afw.image as afwImage
7import lsst.geom as geom
8import lsst.ip.diffim as ipDiffim
9import lsst.utils.logging as logUtils
10import lsst.meas.algorithms as measAlg
11from lsst.ip.diffim.modelPsfMatch import nextOddInteger
13logUtils.trace_set_at("lsst.ip.diffim", 4)
16class PsfMatchTestCases(lsst.utils.tests.TestCase):
18 def setUp(self):
19 self.config = ipDiffim.ModelPsfMatchTask.ConfigClass()
20 self.subconfig = self.config.kernel.active
21 self.subconfig.scaleByFwhm = False
22 self.subconfig.kernelSize = 9
23 self.subconfig.kernelSizeMin = 9
25 self.imsize = 2 * self.subconfig.sizeCellX
26 self.ksize = 11
27 self.sigma1 = 2.0
28 self.sigma2 = 3.7
29 self.exp = afwImage.ExposureF(geom.Extent2I(self.imsize, self.imsize))
30 self.exp.setPsf(measAlg.DoubleGaussianPsf(self.ksize, self.ksize, self.sigma1))
32 def testTooBig(self):
33 """Test that modelPsfMatchTask raises if kernel size is too big and
34 and automatic padding disabled
35 """
36 self.config.doAutoPadPsf = False
37 self.subconfig.kernelSize = self.ksize
38 psf = measAlg.DoubleGaussianPsf(self.ksize, self.ksize, self.sigma2)
39 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config)
40 try:
41 psfMatch.run(self.exp, psf)
42 except Exception:
43 pass
44 else:
45 self.fail()
47 def testMatch(self):
48 for order in (0, 1):
49 for ksum in (0.5, 1.0, 2.7):
50 self.runMatch(kOrder=order, kSumIn=ksum)
52 def runMatch(self, kOrder=0, kSumIn=3.7):
53 self.subconfig.spatialKernelOrder = kOrder
55 psf = measAlg.DoubleGaussianPsf(self.ksize, self.ksize, self.sigma2)
56 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config)
57 results = psfMatch.run(self.exp, psf, kernelSum=kSumIn)
59 matchingKernel = results.psfMatchingKernel
61 kImage = afwImage.ImageD(matchingKernel.getDimensions())
62 kSumOut = matchingKernel.computeImage(kImage, False)
64 self.assertAlmostEqual(kSumIn, kSumOut)
66 def testAdjustModelSize(self):
67 """Test that modelPsfMatch correctly adjusts the model PSF dimensions to
68 match those of the science PSF.
69 """
70 self.config.doAutoPadPsf = False
71 psfModel = measAlg.DoubleGaussianPsf(self.ksize + 2, self.ksize + 2, self.sigma2)
72 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config)
73 results = psfMatch.run(self.exp, psfModel)
74 self.assertEqual(
75 results.psfMatchedExposure.getPsf().computeImage(
76 results.psfMatchedExposure.getPsf().getAveragePosition()
77 ).getDimensions(),
78 self.exp.getPsf().computeImage(
79 self.exp.getPsf().getAveragePosition()
80 ).getDimensions()
81 )
82 self.assertEqual(results.psfMatchedExposure.getPsf().getSigma1(), self.sigma2)
84 def testPadPsf(self):
85 """Test automatic and manual PSF padding
87 Compare expected Psf size, after padding, to the reference Psf size.
88 The reference Psf Size is proxy for the Sciencee Psf size here.
89 """
90 psfModel = measAlg.DoubleGaussianPsf(self.ksize, self.ksize, self.sigma2)
92 # Test automatic padding (doAutoPadPsf is True by default)
93 autoPaddedKernel = nextOddInteger(self.subconfig.kernelSize*self.config.autoPadPsfTo)
94 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config)
95 results = psfMatch.run(self.exp, psfModel)
96 self.assertEqual(
97 results.psfMatchedExposure.getPsf().computeImage(
98 results.psfMatchedExposure.getPsf().getAveragePosition()
99 ).getWidth(),
100 autoPaddedKernel
101 )
103 # Test manual padding
104 self.config.doAutoPadPsf = False
105 PAD_EVEN_VALUES = [0, 2, 4]
106 for padPix in PAD_EVEN_VALUES:
107 self.config.padPsfBy = padPix
108 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config)
109 results = psfMatch.run(self.exp, psfModel)
110 self.assertEqual(
111 results.psfMatchedExposure.getPsf().computeImage(
112 results.psfMatchedExposure.getPsf().getAveragePosition()
113 ).getWidth(),
114 self.ksize + padPix
115 )
117 PAD_ODD_VALUES = [1, 3, 5]
118 for padPix in PAD_ODD_VALUES:
119 self.config.padPsfBy = padPix
120 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config)
121 with self.assertRaises(ValueError):
122 results = psfMatch.run(self.exp, psfModel)
124 def testPropagateVisitInfo(self):
125 """Test that a PSF-matched exposure preserves the original VisitInfo.
126 """
127 self.exp.getInfo().setVisitInfo(makeVisitInfo())
128 psfModel = measAlg.DoubleGaussianPsf(self.ksize + 2, self.ksize + 2, self.sigma2)
129 psfMatch = ipDiffim.ModelPsfMatchTask(config=self.config)
130 psfMatchedExposure = psfMatch.run(self.exp, psfModel).psfMatchedExposure
131 self.assertEqual(psfMatchedExposure.getInfo().getVisitInfo(),
132 self.exp.getInfo().getVisitInfo())
134 def tearDown(self):
135 del self.exp
136 del self.subconfig
139def makeVisitInfo():
140 """Return a non-NaN visitInfo."""
141 return afwImage.VisitInfo(exposureId=10313423,
142 exposureTime=10.01,
143 darkTime=11.02,
144 date=dafBase.DateTime(65321.1, dafBase.DateTime.MJD, dafBase.DateTime.TAI),
145 ut1=12345.1,
146 era=45.1*geom.degrees,
147 boresightRaDec=geom.SpherePoint(23.1, 73.2, geom.degrees),
148 boresightAzAlt=geom.SpherePoint(134.5, 33.3, geom.degrees),
149 boresightAirmass=1.73,
150 boresightRotAngle=73.2*geom.degrees,
151 rotType=afwImage.RotType.SKY,
152 observatory=Observatory(
153 11.1*geom.degrees, 22.2*geom.degrees, 0.333),
154 weather=Weather(1.1, 2.2, 34.5),
155 )
158class TestMemory(lsst.utils.tests.MemoryTestCase):
159 pass
162def setup_module(module):
163 lsst.utils.tests.init()
166if __name__ == "__main__": 166 ↛ 167line 166 didn't jump to line 167, because the condition on line 166 was never true
167 lsst.utils.tests.init()
168 unittest.main()