1 from __future__
import absolute_import, division
24 """Application Framework image-related classes including Image, Mask and MaskedImage
33 from .
import imageLib
35 __all__ = [
"makeImageFromArray",
"makeMaskFromArray",
"makeMaskedImageFromArrays",
36 "wcsNearlyEqualOverBBox",
"assertWcsNearlyEqualOverBBox"]
38 suffixes = {str(numpy.uint16):
"U", str(numpy.int32): "I", str(numpy.float32): "F", str(numpy.float64): "D"}
41 """Construct an Image from a NumPy array, inferring the Image type from the NumPy type.
42 Return None if input is None.
44 if array
is None:
return None
45 cls = getattr(imageLib,
"Image%s" % (suffixes[str(array.dtype.type)],))
49 """Construct an Mask from a NumPy array, inferring the Mask type from the NumPy type.
50 Return None if input is None.
52 if array
is None:
return None
53 cls = getattr(imageLib,
"Mask%s" % (suffixes[str(array.dtype.type)],))
57 """Construct a MaskedImage from three NumPy arrays, inferring the MaskedImage types from the NumPy types.
59 cls = getattr(imageLib,
"MaskedImage%s" % (suffixes[str(image.dtype.type)],))
63 maxDiffPix=0.01, nx=5, ny=5, doShortCircuit=
True):
64 """!Compare two WCS over a rectangular grid of pixel positions
66 @param[in] wcs0 WCS 0 (an lsst.afw.image.Wcs)
67 @param[in] wcs1 WCS 1 (an lsst.afw.image.Wcs)
68 @param[in] bbox boundaries of pixel grid over which to compare the WCSs (an lsst.afw.geom.Box2I or Box2D)
69 @param[in] maxDiffSky maximum separation between sky positions computed using Wcs.pixelToSky
70 (an lsst.afw.geom.Angle)
71 @param[in] maxDiffPix maximum separation between pixel positions computed using Wcs.skyToPixel
72 @param[in] nx number of points in x for the grid of pixel positions
73 @param[in] ny number of points in y for the grid of pixel positions
74 @param[in] doShortCircuit if True then stop at the first error, else test all values in the grid
75 and return information about the worst violations found
77 @return return an empty string if the WCS are sufficiently close; else return a string describing
78 the largest error measured in pixel coordinates (if sky to pixel error was excessive) and sky coordinates
79 (if pixel to sky error was excessive). If doShortCircuit is true then the reported error is likely to be
80 much less than the maximum error across the whole pixel grid.
83 raise RuntimeError(
"nx = %s and ny = %s must both be positive" % (nx, ny))
84 if maxDiffSky <= 0*afwGeom.arcseconds:
85 raise RuntimeError(
"maxDiffSky = %s must be positive" % (maxDiffSky,))
87 raise RuntimeError(
"maxDiffPix = %s must be positive" % (maxDiffPix,))
90 xList = numpy.linspace(bboxd.getMinX(), bboxd.getMaxX(), nx)
91 yList = numpy.linspace(bboxd.getMinY(), bboxd.getMaxY(), ny)
93 measDiffSky = (maxDiffSky,
"?")
94 measDiffPix = (maxDiffPix,
"?")
95 for x, y
in itertools.product(xList, yList):
97 sky0 = wcs0.pixelToSky(fromPixPos)
98 sky1 = wcs1.pixelToSky(fromPixPos)
99 diffSky = sky0.angularSeparation(sky1)
100 if diffSky > measDiffSky[0]:
101 measDiffSky = (diffSky, fromPixPos)
105 toPixPos0 = wcs0.skyToPixel(sky0)
106 toPixPos1 = wcs1.skyToPixel(sky0)
107 diffPix = math.hypot(*(toPixPos0 - toPixPos1))
108 if diffPix > measDiffPix[0]:
109 measDiffPix = (diffPix, sky0)
114 if measDiffSky[0] > maxDiffSky:
115 msgList.append(
"%s arcsec max measured sky error > %s arcsec max allowed sky error at pix pos=%s" %
116 (measDiffSky[0].asArcseconds(), maxDiffSky.asArcseconds(), measDiffSky[1]))
117 if measDiffPix[0] > maxDiffPix:
118 msgList.append(
"%s max measured pix error > %s max allowed pix error at sky pos=%s" %
119 (measDiffPix[0], maxDiffPix, measDiffPix[1]))
121 return "; ".join(msgList)
124 maxDiffPix=0.01, nx=5, ny=5):
125 """!Return True if two WCS are nearly equal over a grid of pixel positions, else False
127 @param[in] wcs0 WCS 0 (an lsst.afw.image.Wcs)
128 @param[in] wcs1 WCS 1 (an lsst.afw.image.Wcs)
129 @param[in] bbox boundaries of pixel grid over which to compare the WCSs (an lsst.afw.geom.Box2I or Box2D)
130 @param[in] maxDiffSky maximum separation between sky positions computed using Wcs.pixelToSky
131 (an lsst.afw.geom.Angle)
132 @param[in] maxDiffPix maximum separation between pixel positions computed using Wcs.skyToPixel
133 @param[in] nx number of points in x for the grid of pixel positions
134 @param[in] ny number of points in y for the grid of pixel positions
135 @param[in] doShortCircuit if True then stop at the first error, else test all values in the grid
136 and return information about the worst violations found
142 maxDiffSky = maxDiffSky,
143 maxDiffPix = maxDiffPix,
146 doShortCircuit =
True,
149 @lsst.utils.tests.inTestCase
151 nx=5, ny=5, msg=
"WCSs differ"):
152 """!Compare pixelToSky and skyToPixel for two WCS over a rectangular grid of pixel positions
154 If the WCS are too divergent, call testCase.fail; the message describes the largest error measured
155 in pixel coordinates (if sky to pixel error was excessive) and sky coordinates (if pixel to sky error
156 was excessive) across the entire pixel grid.
158 @param[in] testCase unittest.TestCase instance the test is part of;
159 an object supporting one method: fail(self, msgStr)
160 @param[in] wcs0 WCS 0 (an lsst.afw.image.Wcs)
161 @param[in] wcs1 WCS 1 (an lsst.afw.image.Wcs)
162 @param[in] bbox boundaries of pixel grid over which to compare the WCSs (an lsst.afw.geom.Box2I or Box2D)
163 @param[in] maxDiffSky maximum separation between sky positions computed using Wcs.pixelToSky
164 (an lsst.afw.geom.Angle)
165 @param[in] maxDiffPix maximum separation between pixel positions computed using Wcs.skyToPixel
166 @param[in] nx number of points in x for the grid of pixel positions
167 @param[in] ny number of points in y for the grid of pixel positions
168 @param[in] msg exception message prefix; details of the error are appended after ": "
174 maxDiffSky = maxDiffSky,
175 maxDiffPix = maxDiffPix,
178 doShortCircuit =
False,
181 testCase.fail(
"%s: %s" % (msg, errMsg))
def wcsNearlyEqualOverBBox
Return True if two WCS are nearly equal over a grid of pixel positions, else False.
def _compareWcsOverBBox
Compare two WCS over a rectangular grid of pixel positions.
A floating-point coordinate rectangle geometry.
def assertWcsNearlyEqualOverBBox
Compare pixelToSky and skyToPixel for two WCS over a rectangular grid of pixel positions.
def makeMaskedImageFromArrays