1 from __future__
import absolute_import, division, print_function
23 """Utilities that should be imported into the lsst.afw.geom namespace when lsst.afw.geom is used
25 In the case of the assert functions, importing them makes them available in lsst.utils.tests.TestCase
27 __all__ = [
"assertAnglesAlmostEqual",
"assertPairsAlmostEqual",
28 "assertPairListsAlmostEqual",
"assertSpherePointsAlmostEqual",
29 "assertSpherePointListsAlmostEqual",
"assertBoxesAlmostEqual",
30 "makeEndpoints",
"assertAnglesNearlyEqual",
"assertPairsNearlyEqual",
31 "assertBoxesNearlyEqual"]
33 from builtins
import range
39 import lsst.utils.tests
40 from .angle
import arcseconds
41 from .endpoint
import GenericEndpoint, Point2Endpoint, IcrsCoordEndpoint
45 """Format extra error message, if any
52 @lsst.utils.tests.inTestCase
54 ignoreWrap=
True, msg=
"Angles differ"):
55 """!Assert that two angles are almost equal, ignoring wrap differences by default
57 @param[in] testCase unittest.TestCase instance the test is part of;
58 an object supporting one method: fail(self, msgStr)
59 @param[in] ang0 angle 0 (an lsst.afw.geom.Angle)
60 @param[in] ang1 angle 1 (an lsst.afw.geom.Angle)
61 @param[in] maxDiff maximum difference between the two angles (an lsst.afw.geom.Angle)
62 @param[in] ignoreWrap ignore wrap when comparing the angles?
63 - if True then wrap is ignored, e.g. 0 and 360 degrees are considered equal
64 - if False then wrap matters, e.g. 0 and 360 degrees are considered different
65 @param[in] msg exception message prefix; details of the error are appended after ": "
67 @throw AssertionError if the difference is greater than maxDiff
69 measDiff = ang1 - ang0
71 measDiff = measDiff.wrapCtr()
72 if abs(measDiff) > maxDiff:
73 testCase.fail(
"%s: measured difference %s arcsec > max allowed %s arcsec" %
74 (msg, measDiff.asArcseconds(), maxDiff.asArcseconds()))
77 @lsst.utils.tests.inTestCase
79 """!Assert that two Cartesian points are almost equal.
81 Each point can be any indexable pair of two floats, including
82 Point2D or Extent2D, a list or a tuple.
84 @warning Does not compare types, just compares values.
86 @param[in] testCase unittest.TestCase instance the test is part of;
87 an object supporting one method: fail(self, msgStr)
88 @param[in] pair0 pair 0 (a pair of floats)
89 @param[in] pair1 pair 1 (a pair of floats)
90 @param[in] maxDiff maximum radial separation between the two points
91 @param[in] msg exception message prefix; details of the error are appended after ": "
93 @throw AssertionError if the radial difference is greater than maxDiff
96 raise RuntimeError(
"len(pair0)=%s != 2" % (len(pair0),))
98 raise RuntimeError(
"len(pair1)=%s != 2" % (len(pair1),))
100 pairDiff = [float(pair1[i] - pair0[i])
for i
in range(2)]
101 measDiff = math.hypot(*pairDiff)
102 if measDiff > maxDiff:
103 testCase.fail(
"%s: measured radial distance = %s > maxDiff = %s" % (
104 msg, measDiff, maxDiff))
107 @lsst.utils.tests.inTestCase
109 """!Assert that two lists of Cartesian points are almost equal
111 Each point can be any indexable pair of two floats, including
112 Point2D or Extent2D, a list or a tuple.
114 @warning Does not compare types, just values.
116 @param[in] testCase unittest.TestCase instance the test is part of;
117 an object supporting one method: fail(self, msgStr)
118 @param[in] list0 list of pairs 0 (each element a pair of floats)
119 @param[in] list1 list of pairs 1
120 @param[in] maxDiff maximum radial separation between the two points
121 @param[in] msg additional information for the error message; appended after ": "
123 @throw AssertionError if the radial difference is greater than maxDiff
125 testCase.assertEqual(len(list0), len(list1))
126 lenList1 = np.array([len(val)
for val
in list0])
127 lenList2 = np.array([len(val)
for val
in list1])
128 testCase.assertTrue(np.all(lenList1 == 2))
129 testCase.assertTrue(np.all(lenList2 == 2))
131 diffArr = np.array([(val0[0] - val1[0], val0[1] - val1[1])
132 for val0, val1
in zip(list0, list1)], dtype=float)
133 sepArr = np.hypot(diffArr[:, 0], diffArr[:, 1])
134 badArr = sepArr > maxDiff
136 maxInd = np.argmax(sepArr)
137 testCase.fail(
"PairLists differ in %s places; max separation is at %s: %s > %s%s" %
138 (np.sum(badArr), maxInd, sepArr[maxInd], maxDiff,
extraMsg(msg)))
141 @lsst.utils.tests.inTestCase
143 """!Assert that two SpherePoints are almost equal
145 @param[in] testCase unittest.TestCase instance the test is part of;
146 an object supporting one method: fail(self, msgStr)
147 @param[in] sp0 SpherePoint 0
148 @param[in] sp1 SpherePoint 1
149 @param[in] maxSep maximum separation, an lsst.afw.geom.Angle
150 @param[in] msg extra information to be printed with any error message
152 if sp0.separation(sp1) > maxSep:
153 testCase.fail(
"Angular separation between %s and %s = %s\" > maxSep = %s\"%s" %
154 (sp0, sp1, sp0.separation(sp1).asArcseconds(), maxSep.asArcseconds(),
extraMsg(msg)))
157 @lsst.utils.tests.inTestCase
159 """!Assert that two lists of SpherePoints are almost equal
161 @param[in] testCase unittest.TestCase instance the test is part of;
162 an object supporting one method: fail(self, msgStr)
163 @param[in] splist0 list of SpherePoints 0
164 @param[in] splist1 list of SpherePoints 1
165 @param[in] maxSep maximum separation, an lsst.afw.geom.Angle
166 @param[in] msg exception message prefix; details of the error are appended after ": "
168 testCase.assertEqual(len(splist0), len(splist1), msg=msg)
169 sepArr = np.array([sp0.separation(sp1)
170 for sp0, sp1
in zip(splist0, splist1)])
171 badArr = sepArr > maxSep
173 maxInd = np.argmax(sepArr)
174 testCase.fail(
"SpherePointLists differ in %s places; max separation is at %s: %s\" > %s\"%s" %
175 (np.sum(badArr), maxInd, sepArr[maxInd].asArcseconds(),
176 maxSep.asArcseconds(),
extraMsg(msg)))
179 @lsst.utils.tests.inTestCase
181 """!Assert that two boxes (Box2D or Box2I) are almost equal
183 @warning Does not compare types, just compares values.
185 @param[in] testCase unittest.TestCase instance the test is part of;
186 an object supporting one method: fail(self, msgStr)
187 @param[in] box0 box 0
188 @param[in] box1 box 1
189 @param[in] maxDiff maximum radial separation between the min points and max points
190 @param[in] msg exception message prefix; details of the error are appended after ": "
192 @throw AssertionError if the radial difference of the min points or max points is greater than maxDiff
195 box1.getMin(), maxDiff=maxDiff, msg=msg +
": min")
197 box1.getMax(), maxDiff=maxDiff, msg=msg +
": max")
200 @lsst.utils.tests.inTestCase
202 """Generate a representative sample of Endpoints.
207 List of endpoints with enough diversity to exercise Endpoint-related
208 code. Each invocation of this method shall return independent objects.
210 return [GenericEndpoint(n)
for n
in range(1, 6)] + \
214 @lsst.utils.tests.inTestCase
216 warnings.warn(
"Deprecated. Use assertAnglesAlmostEqual",
217 DeprecationWarning, 2)
221 @lsst.utils.tests.inTestCase
223 warnings.warn(
"Deprecated. Use assertPairsAlmostEqual", DeprecationWarning, 2)
227 @lsst.utils.tests.inTestCase
229 warnings.warn(
"Deprecated. Use assertBoxesAlmostEqual", DeprecationWarning, 2)
def assertPairListsAlmostEqual
Assert that two lists of Cartesian points are almost equal.
def assertSpherePointsAlmostEqual
Assert that two SpherePoints are almost equal.
def assertAnglesNearlyEqual
def assertAnglesAlmostEqual
Assert that two angles are almost equal, ignoring wrap differences by default.
def assertPairsAlmostEqual
Assert that two Cartesian points are almost equal.
def assertSpherePointListsAlmostEqual
Assert that two lists of SpherePoints are almost equal.
def assertPairsNearlyEqual
def assertBoxesAlmostEqual
Assert that two boxes (Box2D or Box2I) are almost equal.
An endpoint for IcrsCoord.
def assertBoxesNearlyEqual