def testArrayConstructor(self): """Test that construction with coefficient arrays yields an object with copies of those arrays, and that all dimensions must be the same. """ order = 3 xc = makeRandomCoefficientMatrix(order + 1) yc = makeRandomCoefficientMatrix(order + 1) p = PolynomialTransform(xc, yc) self.assertEqual(p.getOrder(), order) self.assertFloatsAlmostEqual(p.getXCoeffs(), xc, atol=0, rtol=0) self.assertFloatsAlmostEqual(p.getYCoeffs(), yc, atol=0, rtol=0) # Test that the coefficients are not a view. old = xc[0, 0] xc[0, 0] += 100.0 self.assertEqual(p.getXCoeffs()[0, 0], old) # Test that rectangular coefficient arrays are not allowed. self.assertRaises( lsst.pex.exceptions.LengthError, PolynomialTransform, np.zeros((5, 4), dtype=float), np.zeros((5, 4), dtype=float) ) # Test that x and y coefficient arrays must have the same shape. self.assertRaises( lsst.pex.exceptions.LengthError, PolynomialTransform, np.zeros((5, 5), dtype=float), np.zeros((4, 4), dtype=float) )
def makeRandomPolynomialTransform(order, sip=False): xc = makeRandomCoefficientMatrix(order + 1) yc = makeRandomCoefficientMatrix(order + 1) if sip: xc[0, 0] = 0 yc[0, 0] = 0 xc[0, 1] = 0 yc[0, 1] = 0 xc[1, 0] = 0 yc[1, 0] = 0 return PolynomialTransform(xc, yc)
def testArrayConstructor(self): """Test that construction with coefficient arrays yields an object with copies of those arrays, and that all dimensions must be the same. """ order = 3 xc = makeRandomCoefficientMatrix(order + 1) yc = makeRandomCoefficientMatrix(order + 1) p = PolynomialTransform(xc, yc) self.assertEqual(p.getOrder(), order) self.assertFloatsAlmostEqual(p.getXCoeffs(), xc, atol=0, rtol=0) self.assertFloatsAlmostEqual(p.getYCoeffs(), yc, atol=0, rtol=0) # Test that the coefficients are not a view. old = xc[0, 0] xc[0, 0] += 100.0 self.assertEqual(p.getXCoeffs()[0, 0], old) # Test that rectangular coefficient arrays are not allowed. self.assertRaises( lsst.pex.exceptions.LengthError, PolynomialTransform, np.zeros((5, 4), dtype=float), np.zeros((5, 4), dtype=float) ) # Test that x and y coefficient arrays must have the same shape. self.assertRaises( lsst.pex.exceptions.LengthError, PolynomialTransform, np.zeros((5, 5), dtype=float), np.zeros((4, 4), dtype=float) )
def testConvertSipReverse(self): """Test that we can convert a SipForwardTransform to a PolynomialTransform. """ sipReverse = makeRandomSipReverseTransform(4) converted = PolynomialTransform.convert(sipReverse) self.assertTransformsAlmostEqual(sipReverse, converted)
def testConvertScaledPolynomial(self): """Test that we can convert a ScaledPolynomialTransform to a PolynomialTransform. """ scaled = makeRandomScaledPolynomialTransform(4) converted = PolynomialTransform.convert(scaled) self.assertTransformsAlmostEqual(scaled, converted)
def testMakeWcs(self): """Test SipForwardTransform, SipReverseTransform and makeWcs """ filename = os.path.join(os.path.dirname(__file__), 'imgCharSources-v85501867-R01-S00.sipheader') sipMetadata = readMetadata(filename) # We're building an ICRS-based TAN-SIP using coefficients read from metadata # so ignore the RADESYS in metadata (which is missing anyway, falling back to FK5) sipMetadata.set("RADESYS", "ICRS") crpix = lsst.afw.geom.Point2D( sipMetadata.get("CRPIX1") - 1, sipMetadata.get("CRPIX2") - 1, ) crval = lsst.afw.geom.SpherePoint( sipMetadata.get("CRVAL1"), sipMetadata.get("CRVAL2"), lsst.afw.geom.degrees, ) cdLinearTransform = lsst.afw.geom.LinearTransform( getCdMatrixFromMetadata(sipMetadata)) aArr = getSipMatrixFromMetadata(sipMetadata, "A") bArr = getSipMatrixFromMetadata(sipMetadata, "B") apArr = getSipMatrixFromMetadata(sipMetadata, "AP") bpArr = getSipMatrixFromMetadata(sipMetadata, "BP") abPoly = PolynomialTransform(aArr, bArr) abRevPoly = PolynomialTransform(apArr, bpArr) fwd = SipForwardTransform(crpix, cdLinearTransform, abPoly) rev = SipReverseTransform(crpix, cdLinearTransform, abRevPoly) wcsFromMakeWcs = lsst.meas.astrom.makeWcs(fwd, rev, crval) wcsFromMetadata = lsst.afw.geom.makeSkyWcs(sipMetadata, strip=False) # Check SipForwardTransform against a local implementation localPixelToIwc = makeSipPixelToIwc(sipMetadata) self.assertTransformsAlmostEqual(fwd, localPixelToIwc.applyForward, maxval=2000) # Compare SipReverseTransform against a local implementation # Use the forward direction first to get sensible inputs localIwcToPixel = makeSipIwcToPixel(sipMetadata) def fwdThenRev(p): return rev(fwd(p)) def fwdThenLocalRev(p): return localIwcToPixel.applyForward(fwd(p)) self.assertTransformsAlmostEqual(fwdThenRev, fwdThenLocalRev, maxval=2000) # Check that SipReverseTransform is the inverse of SipForwardTransform; # this is not perfect because the coefficients don't define a perfect inverse def nullTransform(p): return p self.assertTransformsAlmostEqual(fwdThenRev, nullTransform, maxval=2000, atol=1e-3) # Check SipForwardTransform against the one contained in wcsFromMakeWcs # (Don't bother with the other direction because the WCS transform is iterative, # so it doesn't tell us anything useful about SipReverseTransform pixelToIwc = lsst.afw.geom.getPixelToIntermediateWorldCoords( wcsFromMetadata) self.assertTransformsAlmostEqual(fwd, pixelToIwc.applyForward, maxval=2000) # Check a WCS constructed from SipForwardTransform, SipReverseTransform # against one constructed directly from the metadata bbox = lsst.afw.geom.Box2D(lsst.afw.geom.Point2D(0, 0), lsst.afw.geom.Extent2D(2000, 2000)) self.assertWcsAlmostEqualOverBBox(wcsFromMakeWcs, wcsFromMetadata, bbox)
def testConvertSipReverse(self): """Test that we can convert a SipForwardTransform to a PolynomialTransform. """ sipReverse = makeRandomSipReverseTransform(4) converted = PolynomialTransform.convert(sipReverse) self.assertTransformsAlmostEqual(sipReverse, converted)
def testConvertScaledPolynomial(self): """Test that we can convert a ScaledPolynomialTransform to a PolynomialTransform. """ scaled = makeRandomScaledPolynomialTransform(4) converted = PolynomialTransform.convert(scaled) self.assertTransformsAlmostEqual(scaled, converted)