Coverage for tests/test_coaddApCorrMap.py: 23%
93 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-01 09:48 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-01 09:48 +0000
1#
2# LSST Data Management System
3#
4# Copyright 2008-2016 AURA/LSST.
5#
6# This product includes software developed by the
7# LSST Project (http://www.lsst.org/).
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 LSST License Statement and
20# the GNU General Public License along with this program. If not,
21# see <https://www.lsstcorp.org/LegalNotices/>.
22#
23import os
24import numpy as np
25import unittest
27import lsst.geom
28import lsst.afw.geom as afwGeom
29import lsst.afw.math as afwMath
30import lsst.afw.table as afwTable
31import lsst.afw.image as afwImage
32import lsst.meas.algorithms as measAlg
33import lsst.utils.tests
35try:
36 type(verbose)
37except NameError:
38 verbose = 0
39 display = False
42class CoaddApCorrMapTest(lsst.utils.tests.TestCase):
44 def testCoaddApCorrMap(self):
45 """Check that we can create and use a coadd ApCorrMap."""
46 coaddBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(100, 100))
47 scale = 5.0e-5*lsst.geom.degrees
48 cdMatrix = afwGeom.makeCdMatrix(scale=scale)
49 crval = lsst.geom.SpherePoint(0.0, 0.0, lsst.geom.degrees)
50 center = lsst.geom.Point2D(lsst.geom.Extent2D(coaddBox.getDimensions())*0.5)
51 coaddWcs = afwGeom.makeSkyWcs(crpix=lsst.geom.Point2D(0, 0), crval=crval, cdMatrix=cdMatrix)
52 schema = afwTable.ExposureTable.makeMinimalSchema()
53 weightKey = schema.addField("customweightname", type="D", doc="Coadd weight")
54 catalog = afwTable.ExposureCatalog(schema)
56 # Non-overlapping
57 num = 5
58 inputBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(10, 10))
59 validBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(7, 7))
60 pointList = []
61 pointListValid = []
63 for i in range(num):
64 value = np.array([[1]], dtype=float) # Constant with value = i+1
65 apCorrMap = afwImage.ApCorrMap()
66 bf = afwMath.ChebyshevBoundedField(inputBox, value*(i + 1))
67 apCorrMap.set("only", bf)
69 point = lsst.geom.Point2D(0, 0) - lsst.geom.Extent2D(coaddBox.getDimensions())*(i+0.5)/num
70 wcs = afwGeom.makeSkyWcs(crpix=point, crval=crval, cdMatrix=cdMatrix)
71 center = inputBox.getCenter()
72 pointList.append(coaddWcs.skyToPixel(wcs.pixelToSky(center)))
74 # This point will only be valid for the second overlapping record
75 pointValid = center + lsst.geom.Extent2D(4, 4)
76 pointListValid.append(coaddWcs.skyToPixel(wcs.pixelToSky(pointValid)))
78 # A record with the valid polygon defining a limited region
79 record = catalog.getTable().makeRecord()
80 record.setWcs(wcs)
81 record.setBBox(inputBox)
82 record.setApCorrMap(apCorrMap)
83 record.set(weightKey, i + 1)
84 record['id'] = i
85 record.setValidPolygon(afwGeom.Polygon(lsst.geom.Box2D(validBox)))
86 catalog.append(record)
88 # An overlapping record with the whole region as valid
89 record = catalog.getTable().makeRecord()
90 record.setWcs(wcs)
91 record.setBBox(inputBox)
92 apCorrMap = afwImage.ApCorrMap()
93 bf = afwMath.ChebyshevBoundedField(inputBox, value*(i + 2))
94 apCorrMap.set("only", bf)
95 record.setApCorrMap(apCorrMap)
96 record.set(weightKey, i + 2)
97 record['id'] = i + num
98 record.setValidPolygon(afwGeom.Polygon(lsst.geom.Box2D(inputBox)))
99 catalog.append(record)
101 apCorrMap = measAlg.makeCoaddApCorrMap(catalog, coaddBox, coaddWcs, "customweightname")
102 # This will test a point where both records contribute
103 self.assertApCorrMap(apCorrMap, pointList)
104 # Only the second record will be valid for this point
105 self.assertApCorrMapValid(apCorrMap, pointListValid)
107 filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "coaddApCorrMap.fits")
108 exposure = afwImage.ExposureF(1, 1)
109 exposure.getInfo().setApCorrMap(apCorrMap)
110 exposure.writeFits(filename)
111 exposure = afwImage.ExposureF(filename)
112 self.assertApCorrMap(exposure.getInfo().getApCorrMap(), pointList)
113 self.assertApCorrMapValid(exposure.getInfo().getApCorrMap(), pointListValid)
114 os.unlink(filename)
116 def assertApCorrMap(self, apCorrMap, pointList):
117 for i, point in enumerate(pointList):
118 weights = [i + 1, i + 2]
119 values = [i + 1, i + 2]
120 expected = sum((w*v for w, v in zip(weights, values)), 0.0)/sum(weights)
121 actual = apCorrMap["only"].evaluate(point)
122 self.assertEqual(actual, expected)
124 def assertApCorrMapValid(self, apCorrMap, pointList):
125 for i, point in enumerate(pointList):
126 weights = [i + 2]
127 values = [i + 2]
128 expected = sum((w*v for w, v in zip(weights, values)), 0.0)/sum(weights)
129 actual = apCorrMap["only"].evaluate(point)
130 self.assertEqual(actual, expected)
133class TestMemory(lsst.utils.tests.MemoryTestCase):
134 pass
137def setup_module(module):
138 lsst.utils.tests.init()
141if __name__ == "__main__": 141 ↛ 142line 141 didn't jump to line 142, because the condition on line 141 was never true
142 lsst.utils.tests.init()
143 unittest.main()