Coverage for tests/test_transformMap.py: 24%
94 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-01 15:48 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-01 15:48 -0700
1#
2# LSST Data Management System
3# Copyright 2014 LSST Corporation.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
21#
22"""
23Tests for lsst.afw.cameraGeom.TransformMap
24"""
25import unittest
27import lsst.utils.tests
28import lsst.pex.exceptions
29import lsst.geom
30import lsst.afw.geom as afwGeom
31import lsst.afw.cameraGeom as cameraGeom
32import lsst.afw.cameraGeom.testUtils
35class TransformWrapper:
36 """Wrap a TransformMap transformation as a function(Point2D)->Point2D
37 """
39 def __init__(self, transformMap, fromSys, toSys):
40 self.transformMap = transformMap
41 self.fromSys = fromSys
42 self.toSys = toSys
44 def __call__(self, point):
45 return self.transformMap.transform(point, self.fromSys, self.toSys)
48class Composition:
49 """Wrap a pair of function(Point2D)->Point2D functions as a single
50 function that calls the first function, then the second function on the
51 result
52 """
54 def __init__(self, func1, func2):
55 self.func1 = func1
56 self.func2 = func2
58 def __call__(self, point):
59 return self.func2(self.func1(point))
62def unityTransform(point):
63 """Unity function(Point2D)->Point2D
64 """
65 return point
68class CameraTransformMapTestCase(lsst.utils.tests.TestCase):
70 def setUp(self):
71 self.nativeSys = cameraGeom.FOCAL_PLANE
72 self.fieldTransform = afwGeom.makeRadialTransform([0, 0.5, 0.005])
73 transforms = {cameraGeom.FIELD_ANGLE: self.fieldTransform}
74 self.transformMap = cameraGeom.TransformMap(
75 self.nativeSys, transforms)
77 def tearDown(self):
78 self.nativeSys = None
79 self.fieldTransform = None
80 self.transformMap = None
82 def testBasics(self):
83 """Test basic attributes
84 """
85 for methodName in ("begin", "end", "contains", "size"):
86 self.assertFalse(hasattr(self.transformMap, methodName))
88 self.assertIn(self.nativeSys, self.transformMap)
89 self.assertIn(cameraGeom.FIELD_ANGLE, self.transformMap)
90 self.assertNotIn(cameraGeom.CameraSys("garbage"), self.transformMap)
92 self.assertIn(self.nativeSys, self.transformMap)
93 self.assertIn(cameraGeom.FIELD_ANGLE, self.transformMap)
95 def testPersistence(self):
96 """Test round-trip through FITS I/O.
97 """
98 with lsst.utils.tests.getTempFilePath(".fits") as filename:
99 self.transformMap.writeFits(filename)
100 transformMapOut = cameraGeom.TransformMap.readFits(filename)
101 self.assertTransformMapsEqual(self.transformMap, transformMapOut)
103 def testIteration(self):
104 """Test iteration, len and indexing
105 """
106 self.assertEqual(len(self.transformMap), 2)
108 systems = list(self.transformMap)
109 self.assertEqual(len(systems), 2)
111 for cs in systems:
112 self.assertIsInstance(cs, cameraGeom.CameraSys)
114 def testGetItem(self):
115 """Test that the contained transforms are the ones expected
116 """
117 nativeTr = self.transformMap.getTransform(self.nativeSys,
118 self.nativeSys)
119 self.compare2DFunctions(nativeTr.applyForward, unityTransform)
120 self.compare2DFunctions(nativeTr.applyInverse, unityTransform)
122 fieldTr = self.transformMap.getTransform(self.nativeSys,
123 cameraGeom.FIELD_ANGLE)
124 self.compare2DFunctions(fieldTr.applyForward,
125 self.fieldTransform.applyForward)
126 self.compare2DFunctions(fieldTr.applyInverse,
127 self.fieldTransform.applyInverse)
129 fieldTrInv = self.transformMap.getTransform(cameraGeom.FIELD_ANGLE,
130 self.nativeSys)
131 self.compare2DFunctions(fieldTrInv.applyForward,
132 self.fieldTransform.applyInverse)
133 self.compare2DFunctions(fieldTrInv.applyInverse,
134 self.fieldTransform.applyForward)
136 missingCamSys = cameraGeom.CameraSys("missing")
137 with self.assertRaises(lsst.pex.exceptions.Exception):
138 self.transformMap.getTransform(missingCamSys, self.nativeSys)
139 with self.assertRaises(lsst.pex.exceptions.Exception):
140 self.transformMap.getTransform(self.nativeSys, missingCamSys)
142 def testTransform(self):
143 """Test transform method, point version
144 """
145 for fromSys in self.transformMap:
146 for toSys in self.transformMap:
147 trConvFunc = TransformWrapper(
148 self.transformMap, fromSys, toSys)
149 if fromSys == toSys:
150 self.compare2DFunctions(trConvFunc, unityTransform)
151 funcPair = Composition(
152 self.transformMap
153 .getTransform(self.nativeSys, fromSys).applyInverse,
154 self.transformMap
155 .getTransform(self.nativeSys, toSys).applyForward
156 )
157 self.compare2DFunctions(trConvFunc, funcPair)
159 def testTransformList(self):
160 """Test transform method, list version
161 """
162 fromList = []
163 for x in (-1.2, 0.0, 25.3):
164 for y in (-23.4, 0.0, 2.3):
165 fromList.append(lsst.geom.Point2D(x, y))
167 for fromSys in self.transformMap:
168 for toSys in self.transformMap:
169 toList = self.transformMap.transform(fromList, fromSys, toSys)
171 self.assertEqual(len(fromList), len(toList))
172 for fromPoint, toPoint in zip(fromList, toList):
173 predToPoint = self.transformMap.transform(
174 fromPoint, fromSys, toSys)
175 self.assertPairsAlmostEqual(predToPoint, toPoint)
178class MemoryTester(lsst.utils.tests.MemoryTestCase):
179 pass
182def setup_module(module):
183 lsst.utils.tests.init()
186if __name__ == "__main__": 186 ↛ 187line 186 didn't jump to line 187, because the condition on line 186 was never true
187 lsst.utils.tests.init()
188 unittest.main()