22 from __future__
import absolute_import, division, print_function
24 __all__ = [
"BoxGrid",
"makeSipIwcToPixel",
"makeSipPixelToIwc"]
26 from builtins
import range
27 from builtins
import object
36 from numpy.testing
import assert_allclose, assert_array_equal
37 from astshim.test import makeForwardPolyMap, makeTwoWayPolyMap
40 from .box
import Box2I, Box2D
48 """!Divide a box into nx by ny sub-boxes that tile the region 52 """!Construct a BoxGrid 54 The sub-boxes will be of the same type as `box` and will exactly tile `box`; 55 they will also all be the same size, to the extent possible (some variation 56 is inevitable for integer boxes that cannot be evenly divided. 58 @param[in] box box (an lsst.afw.geom.Box2I or Box2D); 59 the boxes in the grid will be of the same type 60 @param[in] numColRow number of columns and rows (a pair of ints) 62 if len(numColRow) != 2:
64 "numColRow=%r; must be a sequence of two integers" % (numColRow,))
65 self.
_numColRow = tuple(int(val)
for val
in numColRow)
67 if isinstance(box, Box2I):
69 elif isinstance(box, Box2D):
72 raise RuntimeError(
"Unknown class %s of box %s" % (type(box), box))
76 minPoint = box.getMin()
78 dtype = np.array(minPoint).dtype
80 self.
_divList = [np.linspace(start=box.getMin()[i],
84 dtype=dtype)
for i
in range(2)]
91 """!Return the box at the specified x,y index (a pair of ints) 99 return self.shape[0]*self.shape[1]
102 """!Return an iterator over all boxes, where column varies most quickly 110 """Information about a FrameSet 117 Index of current frame 118 isBaseSkyFrame : `bool` 119 Is the base frame an `ast.SkyFrame`? 120 isCurrSkyFrame : `bool` 121 Is the current frame an `ast.SkyFrame`? 124 """Construct a FrameSetInfo 128 frameSet : `ast.FrameSet` 129 The FrameSet about which you want information 138 """Return a list of ast.PolyMap coefficients for the specified SIP matrix 140 The returned list of coefficients for an ast.PolyMap 141 that computes the following function: 143 f(dxy) = dxy + sipPolynomial(dxy)) 144 where dxy = pixelPosition - pixelOrigin 145 and sipPolynomial is a polynomial with terms `<name>n_m for x^n y^m` 146 (e.g. A2_0 is the coefficient for x^2 y^0) 150 metadata : lsst.daf.base.PropertySet 151 FITS metadata describing a WCS with the specified SIP coefficients 153 The desired SIP terms: one of A, B, AP, BP 158 A list of coefficients for an ast.PolyMap that computes 159 the specified SIP polynomial, including a term for out = in 163 This is an internal function for use by makeSipIwcToPixel 164 and makeSipPixelToIwc 166 outAxisDict = dict(A=1, B=2, AP=1, BP=2)
167 outAxis = outAxisDict.get(name)
169 raise RuntimeError(
"%s not a supported SIP name" % (name,))
170 width = metadata.getAsInt(
"%s_ORDER" % (name,)) + 1
175 coeffs.append([1.0, outAxis, 1, 0])
177 coeffs.append([1.0, outAxis, 0, 1])
179 for xPower
in range(width):
180 for yPower
in range(width):
181 coeffName =
"%s_%s_%s" % (name, xPower, yPower)
182 if not metadata.exists(coeffName):
185 coeff = metadata.getAsDouble(coeffName)
186 coeffs.append([coeff, outAxis, xPower, yPower])
188 raise RuntimeError(
"No %s coefficients found" % (name,))
193 """Make an IWC to pixel transform with SIP distortion from FITS-WCS metadata 195 This function is primarily intended for unit tests. 196 IWC is intermediate world coordinates, as described in the FITS papers. 200 metadata : lsst.daf.base.PropertySet 201 FITS metadata describing a WCS with inverse SIP coefficients 205 lsst.afw.geom.TransformPoint2ToPoint2 206 Transform from IWC position to pixel position (zero-based) 207 in the forward direction. The inverse direction is not defined. 212 The inverse SIP terms APn_m, BPn_m are polynomial coefficients x^n y^m 213 for computing transformed x, y respectively. If we call the resulting 214 polynomial inverseSipPolynomial, the returned transformation is: 216 pixelPosition = pixel origin + uv + inverseSipPolynomial(uv) 217 where uv = inverseCdMatrix * iwcPosition 219 crpix = (metadata.get(
"CRPIX1") - 1, metadata.get(
"CRPIX2") - 1)
224 coeffArr = np.array(coeffList, dtype=float)
225 sipPolyMap =
ast.PolyMap(coeffArr, 2,
"IterInverse=0")
227 iwcToPixelMap = cdMatrixMap.getInverse().
then(sipPolyMap).
then(pixelRelativeToAbsoluteMap)
232 """Make a pixel to IWC transform with SIP distortion from FITS-WCS metadata 234 This function is primarily intended for unit tests. 235 IWC is intermediate world coordinates, as described in the FITS papers. 239 metadata : lsst.daf.base.PropertySet 240 FITS metadata describing a WCS with forward SIP coefficients 244 lsst.afw.geom.TransformPoint2ToPoint2 245 Transform from pixel position (zero-based) to IWC position 246 in the forward direction. The inverse direction is not defined. 251 The forward SIP terms An_m, Bn_m are polynomial coefficients x^n y^m 252 for computing transformed x, y respectively. If we call the resulting 253 polynomial sipPolynomial, the returned transformation is: 255 iwcPosition = cdMatrix * (dxy + sipPolynomial(dxy)) 256 where dxy = pixelPosition - pixelOrigin 258 crpix = (metadata.get(
"CRPIX1") - 1, metadata.get(
"CRPIX2") - 1)
259 pixelAbsoluteToRelativeMap =
ast.ShiftMap(crpix).getInverse()
263 coeffArr = np.array(coeffList, dtype=float)
264 sipPolyMap =
ast.PolyMap(coeffArr, 2,
"IterInverse=0")
265 pixelToIwcMap = pixelAbsoluteToRelativeMap.then(sipPolyMap).
then(cdMatrixMap)
270 """A FrameSet with base or current frame possibly permuted, with associated 273 Only two-axis frames will be permuted. 277 frameSet : `ast.FrameSet` 278 The FrameSet that may be permuted. A local copy is made. 279 isBaseSkyFrame : `bool` 280 Is the base frame an `ast.SkyFrame`? 281 isCurrSkyFrame : `bool` 282 Is the current frame an `ast.SkyFrame`? 283 isBasePermuted : `bool` 284 Are the base frame axes permuted? 285 isCurrPermuted : `bool` 286 Are the current frame axes permuted? 288 def __init__(self, frameSet, permuteBase, permuteCurr):
289 """Construct a PermutedFrameSet 291 Make a copy of a FrameSet and permute the base and/or current frames if 296 frameSet : `ast.FrameSet` 297 The FrameSet you wish to permute. A deep copy is made. 299 Permute the base frame's axes? 301 Permute the current frame's axes? 306 If you try to permute a frame that does not have 2 axes 313 baseNAxes = self.
frameSet.getFrame(fsInfo.baseInd).nAxes
315 raise RuntimeError(
"Base frame has {} axes; 2 required to permute".format(baseNAxes))
316 self.
frameSet.current = fsInfo.baseInd
318 self.
frameSet.current = fsInfo.currInd
320 currNAxes = self.
frameSet.getFrame(fsInfo.currInd).nAxes
322 raise RuntimeError(
"Current frame has {} axes; 2 required to permute".format(currNAxes))
323 assert self.
frameSet.getFrame(fsInfo.currInd).nAxes == 2
330 """Base class for unit tests of Transform<X>To<Y> 332 Subclasses must call `TransformTestBaseClass.setUp(self)` 333 if they provide their own version. 335 If a package other than afw uses this class then it must 336 override the `getTestDir` method to avoid writing into 337 afw's test directory. 341 """Return a directory where temporary test files can be written 343 The default implementation returns the test directory of the `afw` 346 If this class is used by a test in a package other than `afw` 347 then the subclass must override this method. 354 Subclasses should call this method if they override setUp. 366 "Generic": (1, 2, 3, 4),
376 "SpherePoint": (1, 3, 4),
389 """Make an array of generic point data 391 The data will be suitable for spherical points 396 Number of points in the array 398 Number of axes in the point 402 np.array of floats with shape (nAxes, nPoints) 403 The values are as follows; if nAxes != 2: 404 The first point has values `[0, delta, 2*delta, ..., (nAxes-1)*delta]` 405 The Nth point has those values + N 406 if nAxes == 2 then the data is scaled so that the max value of axis 1 407 is a bit less than pi/2 411 oneAxis = np.arange(nPoints, dtype=float)
413 rawData = np.array([j * delta + oneAxis
for j
in range(nAxes)], dtype=float)
417 maxLatitude = np.max(rawData[1])
418 rawData *= math.pi * 0.4999 / maxLatitude
423 """Make one generic point 428 Number of axes in the point 430 Increment between axis values 434 A list of `nAxes` floats with values `[0, delta, ..., (nAxes-1)*delta] 436 return [i*delta
for i
in range(nAxes)]
445 Endpoint class name prefix; the full class name is name + "Endpoint" 446 nAxes : `int` or `None`, optional 447 number of axes; an int is required if `name` == "Generic"; 452 subclass of `lsst.afw.geom.BaseEndpoint` 453 The constructed endpoint 458 If `name` == "Generic" and `nAxes` is None or <= 0 460 EndpointClassName = name +
"Endpoint" 461 EndpointClass = getattr(afwGeom, EndpointClassName)
462 if name ==
"Generic":
464 raise TypeError(
"nAxes must be an integer for GenericEndpoint")
465 return EndpointClass(nAxes)
466 return EndpointClass()
470 """Return the appropriate frame for the given name and nAxes 475 Endpoint class name prefix; the full class name is name + "Endpoint" 476 nAxes : `int` or `None`, optional 477 number of axes; an int is required if `name` == "Generic"; 483 The constructed frame 488 If `name` == "Generic" and `nAxes` is `None` or <= 0 494 """Return a list of 0 or more frames that are not a valid match for the 500 Endpoint class name prefix; the full class name is name + "Endpoint" 504 Collection of `ast.Frame` 505 A collection of 0 or more frames 524 The FrameSet will contain 4 frames and three transforms connecting them. 525 The idenity of each frame is provided by self.frameIdentDict 527 Frame Index Mapping from this frame to the next 528 `baseFrame` 1 `ast.UnitMap(nIn)` 529 Frame(nIn) 2 `polyMap` 530 Frame(nOut) 3 `ast.UnitMap(nOut)` 534 - `nIn` = `baseFrame.nAxes` 535 - `nOut` = `currFrame.nAxes` 536 - `polyMap` = `makeTwoWayPolyMap(nIn, nOut)` 541 The FrameSet as described above 545 baseFrame : `ast.Frame` 547 currFrame : `ast.Frame` 550 nIn = baseFrame.nAxes
551 nOut = currFrame.nAxes
552 polyMap = makeTwoWayPolyMap(nIn, nOut)
556 baseFrame = baseFrame.copy()
558 currFrame = currFrame.copy()
564 frameSet.addFrame(ast.FrameSet.CURRENT,
ast.UnitMap(nIn), frame2)
567 frameSet.addFrame(ast.FrameSet.CURRENT, polyMap, frame3)
568 frameSet.addFrame(ast.FrameSet.CURRENT,
ast.UnitMap(nOut), currFrame)
573 """Iterator over 0 or more frameSets with SkyFrames axes permuted 575 Only base and current SkyFrames are permuted. If neither the base nor 576 the current frame is a SkyFrame then no frames are returned. 580 iterator over `PermutedFrameSet` 584 if not (fsInfo.isBaseSkyFrame
or fsInfo.isCurrSkyFrame):
587 permuteBaseList = [
False,
True]
if fsInfo.isBaseSkyFrame
else [
False]
588 permuteCurrList = [
False,
True]
if fsInfo.isCurrSkyFrame
else [
False]
589 for permuteBase
in permuteBaseList:
590 for permuteCurr
in permuteCurrList:
595 """Make a Jacobian matrix for the equation described by 601 the dimensions of the input and output data; see makeTwoWayPolyMap 602 inPoint : `numpy.ndarray` 603 an array of size `nIn` representing the point at which the Jacobian 609 an `nOut` x `nIn` array of first derivatives 611 basePolyMapCoeff = 0.001
612 baseCoeff = 2.0 * basePolyMapCoeff
613 coeffs = np.empty((nOut, nIn))
614 for iOut
in range(nOut):
615 coeffOffset = baseCoeff * iOut
616 for iIn
in range(nIn):
617 coeffs[iOut, iIn] = baseCoeff * (iIn + 1) + coeffOffset
618 coeffs[iOut, iIn] *= inPoint[iIn]
619 assert coeffs.ndim == 2
621 assert coeffs.shape == (nOut, nIn)
625 """Check applyForward and applyInverse for a transform 629 transform : `lsst.afw.geom.Transform` 630 The transform to check 631 mapping : `ast.Mapping` 632 The mapping the transform should use. This mapping 633 must contain valid forward or inverse transformations, 634 but they need not match if both present. Hence the 635 mappings returned by make*PolyMap are acceptable. 637 Error message suffix describing test parameters 639 fromEndpoint = transform.fromEndpoint
640 toEndpoint = transform.toEndpoint
641 mappingFromTransform = transform.getMapping()
645 self.assertEqual(nIn, fromEndpoint.nAxes, msg=msg)
646 self.assertEqual(nOut, toEndpoint.nAxes, msg=msg)
650 inPoint = fromEndpoint.pointFromData(rawInPoint)
655 inArray = fromEndpoint.arrayFromData(rawInArray)
657 if mapping.hasForward:
658 self.assertTrue(transform.hasForward)
659 outPoint = transform.applyForward(inPoint)
660 rawOutPoint = toEndpoint.dataFromPoint(outPoint)
661 assert_allclose(rawOutPoint, mapping.applyForward(rawInPoint), err_msg=msg)
662 assert_allclose(rawOutPoint, mappingFromTransform.applyForward(rawInPoint), err_msg=msg)
664 outArray = transform.applyForward(inArray)
665 rawOutArray = toEndpoint.dataFromArray(outArray)
666 self.assertFloatsAlmostEqual(rawOutArray, mapping.applyForward(rawInArray), msg=msg)
667 self.assertFloatsAlmostEqual(rawOutArray, mappingFromTransform.applyForward(rawInArray), msg=msg)
671 outPoint = toEndpoint.pointFromData(rawOutPoint)
673 outArray = toEndpoint.arrayFromData(rawOutArray)
675 self.assertFalse(transform.hasForward)
677 if mapping.hasInverse:
678 self.assertTrue(transform.hasInverse)
682 inversePoint = transform.applyInverse(outPoint)
683 rawInversePoint = fromEndpoint.dataFromPoint(inversePoint)
684 assert_allclose(rawInversePoint, mapping.applyInverse(rawOutPoint), err_msg=msg)
685 assert_allclose(rawInversePoint, mappingFromTransform.applyInverse(rawOutPoint), err_msg=msg)
690 inverseArray = transform.applyInverse(outArray)
691 rawInverseArray = fromEndpoint.dataFromArray(inverseArray)
692 self.assertFloatsAlmostEqual(rawInverseArray, mapping.applyInverse(rawOutArray), msg=msg)
693 self.assertFloatsAlmostEqual(rawInverseArray, mappingFromTransform.applyInverse(rawOutArray),
696 self.assertFalse(transform.hasInverse)
699 """Check that two Transforms are each others' inverses. 703 forward : `lsst.afw.geom.Transform` 704 the reference Transform to test 705 inverse : `lsst.afw.geom.Transform` 706 the transform that should be the inverse of `forward` 708 error message suffix describing test parameters 710 fromEndpoint = forward.fromEndpoint
711 toEndpoint = forward.toEndpoint
712 forwardMapping = forward.getMapping()
713 inverseMapping = inverse.getMapping()
716 self.assertEqual(forward.fromEndpoint,
717 inverse.toEndpoint, msg=msg)
718 self.assertEqual(forward.toEndpoint,
719 inverse.fromEndpoint, msg=msg)
720 self.assertEqual(forward.hasForward, inverse.hasInverse, msg=msg)
721 self.assertEqual(forward.hasInverse, inverse.hasForward, msg=msg)
727 inPoint = fromEndpoint.pointFromData(rawInPoint)
729 outPoint = toEndpoint.pointFromData(rawOutPoint)
734 inArray = fromEndpoint.arrayFromData(rawInArray)
736 outArray = toEndpoint.arrayFromData(rawOutArray)
738 if forward.hasForward:
739 self.assertEqual(forward.applyForward(inPoint),
740 inverse.applyInverse(inPoint), msg=msg)
741 self.assertEqual(forwardMapping.applyForward(rawInPoint),
742 inverseMapping.applyInverse(rawInPoint), msg=msg)
744 assert_array_equal(forward.applyForward(inArray),
745 inverse.applyInverse(inArray),
747 assert_array_equal(forwardMapping.applyForward(rawInArray),
748 inverseMapping.applyInverse(rawInArray),
751 if forward.hasInverse:
752 self.assertEqual(forward.applyInverse(outPoint),
753 inverse.applyForward(outPoint), msg=msg)
754 self.assertEqual(forwardMapping.applyInverse(rawOutPoint),
755 inverseMapping.applyForward(rawOutPoint), msg=msg)
756 assert_array_equal(forward.applyInverse(outArray),
757 inverse.applyForward(outArray),
759 assert_array_equal(forwardMapping.applyInverse(rawOutArray),
760 inverseMapping.applyForward(rawOutArray),
764 """Check Transform_<fromName>_<toName> using the Mapping constructor 768 fromName, toName : `str` 769 Endpoint name prefix for "from" and "to" endpoints, respectively, 770 e.g. "Point2" for `lsst.afw.geom.Point2Endpoint` 771 fromAxes, toAxes : `int` 772 number of axes in fromFrame and toFrame, respectively 774 transformClassName =
"Transform{}To{}".format(fromName, toName)
775 TransformClass = getattr(afwGeom, transformClassName)
776 baseMsg =
"TransformClass={}".format(TransformClass.__name__)
779 for nIn, nOut
in itertools.product(self.
goodNAxes[fromName],
781 msg =
"{}, nIn={}, nOut={}".format(baseMsg, nIn, nOut)
782 polyMap = makeTwoWayPolyMap(nIn, nOut)
783 transform = TransformClass(polyMap)
786 desStr =
"{}[{}->{}]".format(transformClassName, nIn, nOut)
787 self.assertEqual(
"{}".format(transform), desStr)
788 self.assertEqual(repr(transform),
"lsst.afw.geom." + desStr)
793 polyMap = makeForwardPolyMap(nIn, nOut)
794 transform = TransformClass(polyMap)
798 polyMap = makeForwardPolyMap(nOut, nIn).getInverse()
799 transform = TransformClass(polyMap)
803 for nIn, badNOut
in itertools.product(self.
goodNAxes[fromName],
805 badPolyMap = makeTwoWayPolyMap(nIn, badNOut)
806 msg =
"{}, nIn={}, badNOut={}".format(baseMsg, nIn, badNOut)
807 with self.assertRaises(InvalidParameterError, msg=msg):
808 TransformClass(badPolyMap)
811 for badNIn, nOut
in itertools.product(self.
badNAxes[fromName],
813 badPolyMap = makeTwoWayPolyMap(badNIn, nOut)
814 msg =
"{}, badNIn={}, nOut={}".format(baseMsg, nIn, nOut)
815 with self.assertRaises(InvalidParameterError, msg=msg):
816 TransformClass(badPolyMap)
819 """Check Transform_<fromName>_<toName> using the FrameSet constructor 823 fromName, toName : `str` 824 Endpoint name prefix for "from" and "to" endpoints, respectively, 825 e.g. "Point2" for `lsst.afw.geom.Point2Endpoint` 827 transformClassName =
"Transform{}To{}".format(fromName, toName)
828 TransformClass = getattr(afwGeom, transformClassName)
829 baseMsg =
"TransformClass={}".format(TransformClass.__name__)
830 for nIn, nOut
in itertools.product(self.
goodNAxes[fromName],
832 msg =
"{}, nIn={}, nOut={}".format(baseMsg, nIn, nOut)
837 self.assertEqual(frameSet.nFrame, 4)
841 badFrameSet = self.
makeFrameSet(badBaseFrame, currFrame)
842 with self.assertRaises(InvalidParameterError):
843 TransformClass(badFrameSet)
845 reallyBadFrameSet = self.
makeFrameSet(badBaseFrame, badCurrFrame)
846 with self.assertRaises(InvalidParameterError):
847 TransformClass(reallyBadFrameSet)
849 badFrameSet = self.
makeFrameSet(baseFrame, badCurrFrame)
850 with self.assertRaises(InvalidParameterError):
851 TransformClass(badFrameSet)
853 transform = TransformClass(frameSet)
855 desStr =
"{}[{}->{}]".format(transformClassName, nIn, nOut)
856 self.assertEqual(
"{}".format(transform), desStr)
857 self.assertEqual(repr(transform),
"lsst.afw.geom." + desStr)
861 mappingFromTransform = transform.getMapping()
862 transformCopy = TransformClass(mappingFromTransform)
863 self.assertEqual(type(transform), type(transformCopy))
864 self.assertEqual(transform.getMapping(), mappingFromTransform)
866 polyMap = makeTwoWayPolyMap(nIn, nOut)
876 if permutedFS.isBaseSkyFrame:
877 baseFrame = permutedFS.frameSet.getFrame(ast.FrameSet.BASE)
879 desBaseLonAxis = 2
if permutedFS.isBasePermuted
else 1
880 self.assertEqual(baseFrame.lonAxis, desBaseLonAxis)
881 if permutedFS.isCurrSkyFrame:
882 currFrame = permutedFS.frameSet.getFrame(ast.FrameSet.CURRENT)
884 desCurrLonAxis = 2
if permutedFS.isCurrPermuted
else 1
885 self.assertEqual(currFrame.lonAxis, desCurrLonAxis)
887 permTransform = TransformClass(permutedFS.frameSet)
891 """Test Transform<fromName>To<toName>.getInverse 895 fromName, toName : `str` 896 Endpoint name prefix for "from" and "to" endpoints, respectively, 897 e.g. "Point2" for `lsst.afw.geom.Point2Endpoint` 899 transformClassName =
"Transform{}To{}".format(fromName, toName)
900 TransformClass = getattr(afwGeom, transformClassName)
901 baseMsg =
"TransformClass={}".format(TransformClass.__name__)
902 for nIn, nOut
in itertools.product(self.
goodNAxes[fromName],
904 msg =
"{}, nIn={}, nOut={}".format(baseMsg, nIn, nOut)
907 makeTwoWayPolyMap(nIn, nOut),
908 "{}, Map={}".format(msg,
"TwoWay"))
911 makeForwardPolyMap(nIn, nOut),
912 "{}, Map={}".format(msg,
"Forward"))
915 makeForwardPolyMap(nOut, nIn).getInverse(),
916 "{}, Map={}".format(msg,
"Inverse"))
919 """Test Transform<fromName>To<toName>.getInverse for a specific 924 TransformClass : `type` 925 The class of transform to test, such as TransformPoint2ToPoint2 926 mapping : `ast.Mapping` 927 The mapping to use for the transform 931 transform = TransformClass(mapping)
932 inverse = transform.getInverse()
933 inverseInverse = inverse.getInverse()
940 """Test Transform<fromName>To<toName>.getJacobian 944 fromName, toName : `str` 945 Endpoint name prefix for "from" and "to" endpoints, respectively, 946 e.g. "Point2" for `lsst.afw.geom.Point2Endpoint` 948 transformClassName =
"Transform{}To{}".format(fromName, toName)
949 TransformClass = getattr(afwGeom, transformClassName)
950 baseMsg =
"TransformClass={}".format(TransformClass.__name__)
951 for nIn, nOut
in itertools.product(self.
goodNAxes[fromName],
953 msg =
"{}, nIn={}, nOut={}".format(baseMsg, nIn, nOut)
954 polyMap = makeForwardPolyMap(nIn, nOut)
955 transform = TransformClass(polyMap)
956 fromEndpoint = transform.fromEndpoint
960 inPoint = fromEndpoint.pointFromData(rawInPoint)
961 jacobian = transform.getJacobian(inPoint)
962 assert_allclose(jacobian, self.
makeJacobian(nIn, nOut, rawInPoint),
966 inPoint = fromEndpoint.pointFromData(rawInPoint)
967 jacobian = transform.getJacobian(inPoint)
968 assert_allclose(jacobian, self.
makeJacobian(nIn, nOut, rawInPoint),
972 """Test Transform<fromName>To<midName>.then(Transform<midName>To<toName>) 977 the prefix of the starting endpoint (e.g., "Point2" for a 978 Point2Endpoint) for the final, concatenated Transform 980 the prefix for the shared endpoint where two Transforms will be 983 the prefix of the ending endpoint for the final, concatenated 986 TransformClass1 = getattr(afwGeom,
987 "Transform{}To{}".format(fromName, midName))
988 TransformClass2 = getattr(afwGeom,
989 "Transform{}To{}".format(midName, toName))
990 baseMsg =
"{}.then({})".format(TransformClass1.__name__,
991 TransformClass2.__name__)
992 for nIn, nMid, nOut
in itertools.product(self.
goodNAxes[fromName],
995 msg =
"{}, nIn={}, nMid={}, nOut={}".format(
996 baseMsg, nIn, nMid, nOut)
997 polyMap1 = makeTwoWayPolyMap(nIn, nMid)
998 transform1 = TransformClass1(polyMap1)
999 polyMap2 = makeTwoWayPolyMap(nMid, nOut)
1000 transform2 = TransformClass2(polyMap2)
1001 transform = transform1.then(transform2)
1003 fromEndpoint = transform1.fromEndpoint
1004 toEndpoint = transform2.toEndpoint
1007 outPointMerged = transform.applyForward(inPoint)
1008 outPointSeparate = transform2.applyForward(
1009 transform1.applyForward(inPoint))
1010 assert_allclose(toEndpoint.dataFromPoint(outPointMerged),
1011 toEndpoint.dataFromPoint(outPointSeparate),
1015 inPointMerged = transform.applyInverse(outPoint)
1016 inPointSeparate = transform1.applyInverse(
1017 transform2.applyInverse(outPoint))
1019 fromEndpoint.dataFromPoint(inPointMerged),
1020 fromEndpoint.dataFromPoint(inPointSeparate),
1024 if midName ==
"Generic":
1027 polyMap = makeTwoWayPolyMap(nIn, 3)
1028 transform1 = TransformClass1(polyMap)
1029 polyMap = makeTwoWayPolyMap(2, nOut)
1030 transform2 = TransformClass2(polyMap)
1031 with self.assertRaises(InvalidParameterError):
1032 transform = transform1.then(transform2)
1035 if fromName != midName:
1038 joinNAxes =
set(self.
goodNAxes[fromName]).intersection(
1041 for nIn, nMid, nOut
in itertools.product(self.
goodNAxes[fromName],
1044 polyMap = makeTwoWayPolyMap(nIn, nMid)
1045 transform1 = TransformClass1(polyMap)
1046 polyMap = makeTwoWayPolyMap(nMid, nOut)
1047 transform2 = TransformClass1(polyMap)
1048 with self.assertRaises(InvalidParameterError):
1049 transform = transform1.then(transform2)
1052 """Assert that two transforms are equal""" 1053 self.assertEqual(type(transform1), type(transform2))
1054 self.assertEqual(transform1.fromEndpoint, transform2.fromEndpoint)
1055 self.assertEqual(transform1.toEndpoint, transform2.toEndpoint)
1056 self.assertEqual(transform1.getMapping(), transform2.getMapping())
1058 fromEndpoint = transform1.fromEndpoint
1059 toEndpoint = transform1.toEndpoint
1060 mapping = transform1.getMapping()
1064 if mapping.hasForward:
1067 inArray = fromEndpoint.arrayFromData(rawInArray)
1068 outArray = transform1.applyForward(inArray)
1069 outData = toEndpoint.dataFromArray(outArray)
1070 outArrayRoundTrip = transform2.applyForward(inArray)
1071 outDataRoundTrip = toEndpoint.dataFromArray(outArrayRoundTrip)
1072 assert_allclose(outData, outDataRoundTrip)
1074 if mapping.hasInverse:
1077 outArray = toEndpoint.arrayFromData(rawOutArray)
1078 inArray = transform1.applyInverse(outArray)
1079 inData = fromEndpoint.dataFromArray(inArray)
1080 inArrayRoundTrip = transform2.applyInverse(outArray)
1081 inDataRoundTrip = fromEndpoint.dataFromArray(inArrayRoundTrip)
1082 assert_allclose(inData, inDataRoundTrip)
1085 """Check persistence of a transform 1087 className = type(transform).__name__
1090 transformStr = transform.writeString()
1091 serialVersion, serialClassName, serialRest = transformStr.split(
" ", 2)
1092 self.assertEqual(int(serialVersion), 1)
1093 self.assertEqual(serialClassName, className)
1094 badStr1 =
" ".join([
"2", serialClassName, serialRest])
1096 transform.readString(badStr1)
1097 badClassName =
"x" + serialClassName
1098 badStr2 =
" ".join([
"1", badClassName, serialRest])
1100 transform.readString(badStr2)
1101 transformFromStr1 = transform.readString(transformStr)
1105 transformFromStr2 = afwGeom.transformFromString(transformStr)
1113 transform.writeFits(filename)
def __init__(self, frameSet, permuteBase, permuteCurr)
Eigen::Matrix2d getCdMatrixFromMetadata(daf::base::PropertySet &metadata)
Read a CD matrix from FITS WCS metadata.
def __init__(self, frameSet)
std::string getPackageDir(std::string const &packageName)
Divide a box into nx by ny sub-boxes that tile the region.
def makeSipPixelToIwc(metadata)
def __getitem__(self, indXY)
Return the box at the specified x,y index (a pair of ints)
def getTempFilePath(ext, expectOutput=True)
def __init__(self, box, numColRow)
Construct a BoxGrid.
def __iter__(self)
Return an iterator over all boxes, where column varies most quickly.
def makeSipPolyMapCoeffs(metadata, name)
def makeSipIwcToPixel(metadata)
daf::base::PropertySet * set