22"""Utilities that should be imported into the lsst.geom namespace when
25In the case of the assert functions, importing them makes them available in lsst.utils.tests.TestCase
33import lsst.utils.tests
34from ._geom
import arcseconds
38 """Format extra error message, if any
45@lsst.utils.tests.inTestCase
47 ignoreWrap=True, msg="Angles differ"):
48 r"""Assert that two `~lsst.geom.Angle`\ s are almost equal, ignoring
49 wrap differences by default.
51 If both arguments are NaN the assert will
pass. If one of the arguments
52 is NaN but the other
is not the
assert will fail.
56 testCase : `unittest.TestCase`
57 test case the test
is part of; an object supporting one method:
64 maximum difference between the two angles
66 ignore wrap when comparing the angles?
68 -
if True then wrap
is ignored, e.g. 0
and 360 degrees are considered
70 -
if False then wrap matters, e.g. 0
and 360 degrees are considered
74 exception message prefix; details of the error are appended after
": "
79 Raised
if the difference
is greater than ``maxDiff``
81 isNan0 = math.isnan(ang0.asRadians())
82 isNan1 = math.isnan(ang1.asRadians())
86 testCase.fail(
"ang0 is NaN")
88 testCase.fail(
"ang1 is NaN")
89 measDiff = ang1 - ang0
91 measDiff = measDiff.wrapCtr()
92 if abs(measDiff) > maxDiff:
93 testCase.fail(
"%s: measured difference %s arcsec > max allowed %s arcsec" %
94 (msg, measDiff.asArcseconds(), maxDiff.asArcseconds()))
97@lsst.utils.tests.inTestCase
99 """Assert that two Cartesian points are almost equal.
101 Each point can be any indexable pair of two floats, including
102 Point2D or Extent2D, a list
or a tuple.
106 testCase : `unittest.TestCase`
107 test case the test
is part of; an object supporting one method: fail(self, msgStr)
108 pair0 : pair of `float`
110 pair1 : pair of `floats`
113 maximum radial separation between the two points
115 exception message prefix; details of the error are appended after
": "
120 Raised
if the radial difference
is greater than ``maxDiff``
126 Does
not compare types, just compares values.
129 raise RuntimeError(
"len(pair0)=%s != 2" % (len(pair0),))
131 raise RuntimeError(
"len(pair1)=%s != 2" % (len(pair1),))
133 pairDiff = [float(pair1[i] - pair0[i])
for i
in range(2)]
134 measDiff = math.hypot(*pairDiff)
135 if measDiff > maxDiff:
136 testCase.fail(
"%s: measured radial distance = %s > maxDiff = %s, pair0=(%r, %r), pair1=(%r, %r)" %
137 (msg, measDiff, maxDiff, pair0[0], pair0[1], pair1[0], pair1[1]))
140@lsst.utils.tests.inTestCase
142 """Assert that two lists of Cartesian points are almost equal
144 Each point can be any indexable pair of two floats, including
145 Point2D or Extent2D, a list
or a tuple.
149 testCase : `unittest.TestCase`
150 test case the test
is part of; an object supporting one method: fail(self, msgStr)
151 list0 : `list` of pairs of `float`
153 list1 : `list` of pairs of `float`
156 maximum radial separation between the two points
158 additional information
for the error message; appended after
": "
163 Raised
if the radial difference
is greater than ``maxDiff``
169 Does
not compare types, just values.
171 testCase.assertEqual(len(list0), len(list1))
172 lenList1 = np.array([len(val) for val
in list0])
173 lenList2 = np.array([len(val)
for val
in list1])
174 testCase.assertTrue(np.all(lenList1 == 2))
175 testCase.assertTrue(np.all(lenList2 == 2))
177 diffArr = np.array([(val0[0] - val1[0], val0[1] - val1[1])
178 for val0, val1
in zip(list0, list1)], dtype=float)
179 sepArr = np.hypot(diffArr[:, 0], diffArr[:, 1])
180 badArr = sepArr > maxDiff
182 maxInd = np.argmax(sepArr)
183 testCase.fail(
"PairLists differ in %s places; max separation is at %s: %s > %s%s" %
184 (np.sum(badArr), maxInd, sepArr[maxInd], maxDiff,
extraMsg(msg)))
187@lsst.utils.tests.inTestCase
189 r"""Assert that two `~lsst.geom.SpherePoint`\ s are almost equal
193 testCase : `unittest.TestCase`
194 test case the test is part of; an object supporting one method:
203 extra information to be printed
with any error message
208 The SpherePoints are
not equal.
210 if sp0.separation(sp1) > maxSep:
211 testCase.fail(
"Angular separation between %s and %s = %s\" > maxSep = %s\"%s" %
212 (sp0, sp1, sp0.separation(sp1).asArcseconds(), maxSep.asArcseconds(),
extraMsg(msg)))
215@lsst.utils.tests.inTestCase
217 r"""Assert that two lists of `~lsst.geom.SpherePoint`\ s are almost equal
221 testCase : `unittest.TestCase`
222 test case the test is part of; an object supporting one method:
225 list of SpherePoints 0
227 list of SpherePoints 1
231 exception message prefix; details of the error are appended after
": "
236 The SpherePoint lists are
not equal.
238 testCase.assertEqual(len(splist0), len(splist1), msg=msg)
239 sepArr = np.array([sp0.separation(sp1)
240 for sp0, sp1
in zip(splist0, splist1)])
241 badArr = sepArr > maxSep
243 maxInd = np.argmax(sepArr)
244 testCase.fail(
"SpherePointLists differ in %s places; max separation is at %s: %s\" > %s\"%s" %
245 (np.sum(badArr), maxInd, sepArr[maxInd].asArcseconds(),
246 maxSep.asArcseconds(),
extraMsg(msg)))
249@lsst.utils.tests.inTestCase
251 """Assert that two boxes (`~lsst.geom.Box2D` or `~lsst.geom.Box2I`) are
256 testCase : `unittest.TestCase`
257 test case the test is part of; an object supporting one method:
264 maximum radial separation between the min points
and max points
266 exception message prefix; details of the error are appended after
": "
271 Raised
if the radial difference of the min points
or max points
is
278 Does
not compare types, just compares values.
281 box1.getMin(), maxDiff=maxDiff, msg=msg + ": min")
283 box1.getMax(), maxDiff=maxDiff, msg=msg +
": max")
A class representing an angle.
A floating-point coordinate rectangle geometry.
An integer coordinate rectangle.
Point in an unspecified spherical coordinate system.
def assertSpherePointListsAlmostEqual(testCase, splist0, splist1, maxSep=0.001 *arcseconds, msg=None)
def assertPairsAlmostEqual(testCase, pair0, pair1, maxDiff=1e-7, msg="Pairs differ")
def assertBoxesAlmostEqual(testCase, box0, box1, maxDiff=1e-7, msg="Boxes differ")
def assertPairListsAlmostEqual(testCase, list0, list1, maxDiff=1e-7, msg=None)
def assertAnglesAlmostEqual(testCase, ang0, ang1, maxDiff=0.001 *arcseconds, ignoreWrap=True, msg="Angles differ")
def assertSpherePointsAlmostEqual(testCase, sp0, sp1, maxSep=0.001 *arcseconds, msg="")