22 from __future__
import absolute_import, division, print_function
24 __all__ = [
"makeImageFromArray",
"makeMaskFromArray",
"makeMaskedImageFromArrays",
25 "wcsAlmostEqualOverBBox",
"assertWcsAlmostEqualOverBBox",
26 "wcsNearlyEqualOverBBox",
"assertWcsNearlyEqualOverBBox"]
34 import lsst.utils.tests
36 from .image
import Image
37 from .mask
import Mask
38 from .maskedImage
import MaskedImage
42 """Construct an Image from a NumPy array, inferring the Image type from
43 the NumPy type. Return None if input is None.
47 return Image(array, dtype=array.dtype.type)
51 """Construct an Mask from a NumPy array, inferring the Mask type from the
52 NumPy type. Return None if input is None.
56 return Mask(array, dtype=array.dtype.type)
60 """Construct a MaskedImage from three NumPy arrays, inferring the
61 MaskedImage types from the NumPy types.
67 def _compareWcsOverBBox(wcs0, wcs1, bbox, maxDiffSky=0.01*afwGeom.arcseconds,
68 maxDiffPix=0.01, nx=5, ny=5, doShortCircuit=
True):
69 """!Compare two WCS over a rectangular grid of pixel positions
71 @param[in] wcs0 WCS 0 (an lsst.afw.image.Wcs)
72 @param[in] wcs1 WCS 1 (an lsst.afw.image.Wcs)
73 @param[in] bbox boundaries of pixel grid over which to compare the WCSs (an lsst.afw.geom.Box2I or Box2D)
74 @param[in] maxDiffSky maximum separation between sky positions computed using Wcs.pixelToSky
75 (an lsst.afw.geom.Angle)
76 @param[in] maxDiffPix maximum separation between pixel positions computed using Wcs.skyToPixel
77 @param[in] nx number of points in x for the grid of pixel positions
78 @param[in] ny number of points in y for the grid of pixel positions
79 @param[in] doShortCircuit if True then stop at the first error, else test all values in the grid
80 and return information about the worst violations found
82 @return return an empty string if the WCS are sufficiently close; else return a string describing
83 the largest error measured in pixel coordinates (if sky to pixel error was excessive) and sky coordinates
84 (if pixel to sky error was excessive). If doShortCircuit is true then the reported error is likely to be
85 much less than the maximum error across the whole pixel grid.
89 "nx = %s and ny = %s must both be positive" % (nx, ny))
90 if maxDiffSky <= 0*afwGeom.arcseconds:
91 raise RuntimeError(
"maxDiffSky = %s must be positive" % (maxDiffSky,))
93 raise RuntimeError(
"maxDiffPix = %s must be positive" % (maxDiffPix,))
96 xList = numpy.linspace(bboxd.getMinX(), bboxd.getMaxX(), nx)
97 yList = numpy.linspace(bboxd.getMinY(), bboxd.getMaxY(), ny)
100 measDiffSky = (maxDiffSky,
"?")
101 measDiffPix = (maxDiffPix,
"?")
102 for x, y
in itertools.product(xList, yList):
104 sky0 = wcs0.pixelToSky(fromPixPos)
105 sky1 = wcs1.pixelToSky(fromPixPos)
106 diffSky = sky0.angularSeparation(sky1)
107 if diffSky > measDiffSky[0]:
108 measDiffSky = (diffSky, fromPixPos)
112 toPixPos0 = wcs0.skyToPixel(sky0)
113 toPixPos1 = wcs1.skyToPixel(sky0)
114 diffPix = math.hypot(*(toPixPos0 - toPixPos1))
115 if diffPix > measDiffPix[0]:
116 measDiffPix = (diffPix, sky0)
121 if measDiffSky[0] > maxDiffSky:
122 msgList.append(
"%s arcsec max measured sky error > %s arcsec max allowed sky error at pix pos=%s" %
123 (measDiffSky[0].asArcseconds(), maxDiffSky.asArcseconds(), measDiffSky[1]))
124 if measDiffPix[0] > maxDiffPix:
125 msgList.append(
"%s max measured pix error > %s max allowed pix error at sky pos=%s" %
126 (measDiffPix[0], maxDiffPix, measDiffPix[1]))
128 return "; ".join(msgList)
132 maxDiffPix=0.01, nx=5, ny=5):
133 """!Return True if two WCS are almost equal over a grid of pixel positions, else False
135 @param[in] wcs0 WCS 0 (an lsst.afw.image.Wcs)
136 @param[in] wcs1 WCS 1 (an lsst.afw.image.Wcs)
137 @param[in] bbox boundaries of pixel grid over which to compare the WCSs (an lsst.afw.geom.Box2I or Box2D)
138 @param[in] maxDiffSky maximum separation between sky positions computed using Wcs.pixelToSky
139 (an lsst.afw.geom.Angle)
140 @param[in] maxDiffPix maximum separation between pixel positions computed using Wcs.skyToPixel
141 @param[in] nx number of points in x for the grid of pixel positions
142 @param[in] ny number of points in y for the grid of pixel positions
144 return not bool(_compareWcsOverBBox(
148 maxDiffSky = maxDiffSky,
149 maxDiffPix = maxDiffPix,
152 doShortCircuit =
True,
156 @lsst.utils.tests.inTestCase
158 maxDiffPix=0.01, nx=5, ny=5, msg=
"WCSs differ"):
159 """!Assert that two WCS are almost equal over a grid of pixel positions
161 Compare pixelToSky and skyToPixel for two WCS over a rectangular grid of pixel positions.
162 If the WCS are too divergent at any point, call testCase.fail; the message describes
163 the largest error measured in pixel coordinates (if sky to pixel error was excessive)
164 and sky coordinates (if pixel to sky error was excessive) across the entire pixel grid.
166 @param[in] testCase unittest.TestCase instance the test is part of;
167 an object supporting one method: fail(self, msgStr)
168 @param[in] wcs0 WCS 0 (an lsst.afw.image.Wcs)
169 @param[in] wcs1 WCS 1 (an lsst.afw.image.Wcs)
170 @param[in] bbox boundaries of pixel grid over which to compare the WCSs (an lsst.afw.geom.Box2I or Box2D)
171 @param[in] maxDiffSky maximum separation between sky positions computed using Wcs.pixelToSky
172 (an lsst.afw.geom.Angle)
173 @param[in] maxDiffPix maximum separation between pixel positions computed using Wcs.skyToPixel
174 @param[in] nx number of points in x for the grid of pixel positions
175 @param[in] ny number of points in y for the grid of pixel positions
176 @param[in] msg exception message prefix; details of the error are appended after ": "
178 errMsg = _compareWcsOverBBox(
182 maxDiffSky = maxDiffSky,
183 maxDiffPix = maxDiffPix,
186 doShortCircuit =
False,
189 testCase.fail(
"%s: %s" % (msg, errMsg))
193 warnings.warn(
"Deprecated. Use wcsAlmostEqualOverBBox", DeprecationWarning)
197 @lsst.utils.tests.inTestCase
199 warnings.warn(
"Deprecated. Use assertWcsAlmostEqualOverBBox",
def wcsAlmostEqualOverBBox
Return True if two WCS are almost equal over a grid of pixel positions, else False.
def wcsNearlyEqualOverBBox
def assertWcsAlmostEqualOverBBox
Assert that two WCS are almost equal over a grid of pixel positions.
A floating-point coordinate rectangle geometry.
def assertWcsNearlyEqualOverBBox
def makeMaskedImageFromArrays