LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
utils.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division
2 #
3 # LSST Data Management System
4 # Copyright 2015 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 """Utilities that should be imported into the lsst.afw.geom namespace when lsst.afw.geom is used
24 
25 In the case of the assert functions, importing them makes them available in lsst.utils.tests.TestCase
26 """
27 import math
28 
29 import lsst.utils.tests
30 from . import geomLib
31 
32 __all__ = ["assertAnglesNearlyEqual", "assertPairsNearlyEqual", "assertBoxesNearlyEqual"]
33 
34 @lsst.utils.tests.inTestCase
35 def assertAnglesNearlyEqual(testCase, ang0, ang1, maxDiff=0.001*geomLib.arcseconds,
36  ignoreWrap=True, msg="Angles differ"):
37  """!Assert that two angles are nearly equal, ignoring wrap differences by default
38 
39  @param[in] testCase unittest.TestCase instance the test is part of;
40  an object supporting one method: fail(self, msgStr)
41  @param[in] ang0 angle 0 (an lsst.afw.geom.Angle)
42  @param[in] ang1 angle 1 (an lsst.afw.geom.Angle)
43  @param[in] maxDiff maximum difference between the two angles (an lsst.afw.geom.Angle)
44  @param[in] ignoreWrap ignore wrap when comparing the angles?
45  - if True then wrap is ignored, e.g. 0 and 360 degrees are considered equal
46  - if False then wrap matters, e.g. 0 and 360 degrees are considered different
47  @param[in] msg exception message prefix; details of the error are appended after ": "
48 
49  @throw AssertionError if the difference is greater than maxDiff
50  """
51  measDiff = ang1 - ang0
52  if ignoreWrap:
53  measDiff.wrapCtr()
54  if abs(measDiff) > maxDiff:
55  testCase.fail("%s: measured difference %s arcsec > max allowed %s arcsec" %
56  (msg, measDiff.asArcseconds(), maxDiff.asArcseconds()))
57 
58 @lsst.utils.tests.inTestCase
59 def assertPairsNearlyEqual(testCase, pair0, pair1, maxDiff=1e-7, msg="Pairs differ"):
60  """!Assert that two planar pairs (e.g. Point2D or Extent2D) are nearly equal
61 
62  @warning Does not compare types, just compares values.
63 
64  @param[in] testCase unittest.TestCase instance the test is part of;
65  an object supporting one method: fail(self, msgStr)
66  @param[in] pair0 pair 0 (a pair of floats)
67  @param[in] pair1 pair 1 (a pair of floats)
68  @param[in] maxDiff maximum radial separation between the two points
69  @param[in] msg exception message prefix; details of the error are appended after ": "
70 
71  @throw AssertionError if the radial difference is greater than maxDiff
72  """
73  if len(pair0) != 2:
74  raise RuntimeError("len(pair0)=%s != 2" % (len(pair0),))
75  if len(pair1) != 2:
76  raise RuntimeError("len(pair1)=%s != 2" % (len(pair1),))
77 
78  pairDiff = [float(pair1[i] - pair0[i]) for i in range(2)]
79  measDiff = math.hypot(*pairDiff)
80  if measDiff > maxDiff:
81  testCase.fail("%s: measured radial distance = %s > maxDiff = %s" % (msg, measDiff, maxDiff))
82 
83 @lsst.utils.tests.inTestCase
84 def assertBoxesNearlyEqual(testCase, box0, box1, maxDiff=1e-7, msg="Boxes differ"):
85  """!Assert that two boxes (Box2D or Box2I) are nearly equal
86 
87  @warning Does not compare types, just compares values.
88 
89  @param[in] testCase unittest.TestCase instance the test is part of;
90  an object supporting one method: fail(self, msgStr)
91  @param[in] box0 box 0
92  @param[in] box1 box 1
93  @param[in] maxDiff maximum radial separation between the min points and max points
94  @param[in] msg exception message prefix; details of the error are appended after ": "
95 
96  @throw AssertionError if the radial difference of the min points or max points is greater than maxDiff
97  """
98  assertPairsNearlyEqual(testCase, box0.getMin(), box1.getMin(), maxDiff=maxDiff, msg=msg + ": min")
99  assertPairsNearlyEqual(testCase, box0.getMax(), box1.getMax(), maxDiff=maxDiff, msg=msg + ": max")
def assertAnglesNearlyEqual
Assert that two angles are nearly equal, ignoring wrap differences by default.
Definition: utils.py:36
def assertPairsNearlyEqual
Assert that two planar pairs (e.g.
Definition: utils.py:59
def assertBoxesNearlyEqual
Assert that two boxes (Box2D or Box2I) are nearly equal.
Definition: utils.py:84