Coverage for tests/test_warpExposure.py : 52%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
# # LSST Data Management System # Copyright 2008, 2009, 2010 LSST Corporation. # # This product includes software developed by the # LSST Project (http://www.lsst.org/). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the LSST License Statement and # the GNU General Public License along with this program. If not, # see <http://www.lsstcorp.org/LegalNotices/>. #
"""
# Change the level to Log.DEBUG to see debug messages
# set True to save afw-warped images as FITS files # set True to save failed afw-warped images as FITS files even if # SAVE_FITS_FILES is False
else: dataDir = os.path.join(afwdataDir, "data")
originalExposureName = "medexp.fits" originalExposurePath = os.path.join(dataDir, originalExposureName) subExposureName = "medsub.fits" subExposurePath = os.path.join(dataDir, originalExposureName) originalFullExposureName = os.path.join( "CFHT", "D4", "cal-53535-i-797722_1.fits") originalFullExposurePath = os.path.join(dataDir, originalFullExposureName)
"""Return a non-NaN visitInfo.""" return afwImage.VisitInfo(exposureId=10313423, exposureTime=10.01, darkTime=11.02, date=dafBase.DateTime(65321.1, dafBase.DateTime.MJD, dafBase.DateTime.TAI), ut1=12345.1, era=45.1*lsst.geom.degrees, boresightRaDec=lsst.geom.SpherePoint(23.1, 73.2, lsst.geom.degrees), boresightAzAlt=lsst.geom.SpherePoint(134.5, 33.3, lsst.geom.degrees), boresightAirmass=1.73, boresightRotAngle=73.2*lsst.geom.degrees, rotType=afwImage.RotType.SKY, observatory=Observatory(11.1*lsst.geom.degrees, 22.2*lsst.geom.degrees, 0.333), weather=Weather(1.1, 2.2, 34.5), )
"""Test case for warpExposure """
"""Test that warpExposure maps an image onto itself.
Note: - NO_DATA and off-CCD pixels must be ignored - bad mask pixels get smeared out so we have to excluded all bad mask pixels from the output image when comparing masks. """ filterPolicyFile = pexPolicy.DefaultPolicyFile( "afw", "SdssFilters.paf", "tests") filterPolicy = pexPolicy.Policy.createPolicy( filterPolicyFile, filterPolicyFile.getRepositoryPath(), True) imageUtils.defineFiltersFromPolicy(filterPolicy, reset=True)
originalExposure = afwImage.ExposureF(originalExposurePath) originalExposure.getInfo().setVisitInfo(makeVisitInfo()) originalFilter = afwImage.Filter("i") originalCalib = afwImage.Calib() originalCalib.setFluxMag0(1.0e5, 1.0e3) originalExposure.setFilter(originalFilter) originalExposure.setCalib(originalCalib) afwWarpedExposure = afwImage.ExposureF( originalExposure.getBBox(), originalExposure.getWcs()) warpingControl = afwMath.WarpingControl( "lanczos4", "", 0, interpLength) afwMath.warpExposure( afwWarpedExposure, originalExposure, warpingControl) if SAVE_FITS_FILES: afwWarpedExposure.writeFits("afwWarpedExposureNull.fits")
self.assertEqual(afwWarpedExposure.getFilter().getName(), originalFilter.getName()) self.assertEqual(afwWarpedExposure.getCalib().getFluxMag0(), originalCalib.getFluxMag0()) self.assertEqual(afwWarpedExposure.getInfo().getVisitInfo(), originalExposure.getInfo().getVisitInfo())
afwWarpedMaskedImage = afwWarpedExposure.getMaskedImage() afwWarpedMask = afwWarpedMaskedImage.getMask() noDataBitMask = afwWarpedMask.getPlaneBitMask("NO_DATA") afwWarpedMaskedImageArrSet = afwWarpedMaskedImage.getArrays() afwWarpedMaskArr = afwWarpedMaskedImageArrSet[1]
# compare all non-DATA pixels of image and variance, but relax specs a bit # because of minor noise introduced by bad pixels noDataMaskArr = afwWarpedMaskArr & noDataBitMask msg = "afw null-warped MaskedImage (all pixels, relaxed tolerance)" self.assertMaskedImagesAlmostEqual(afwWarpedMaskedImage, originalExposure.getMaskedImage(), doMask=False, skipMask=noDataMaskArr, atol=1e-5, msg=msg)
# compare good pixels (mask=0) of image, mask and variance using full # tolerance msg = "afw null-warped MaskedImage (good pixels, max tolerance)" self.assertMaskedImagesAlmostEqual(afwWarpedMaskedImage, originalExposure.getMaskedImage(), skipMask=afwWarpedMask, msg=msg)
"""Test that warpImage maps an image onto itself. """ originalExposure = afwImage.ExposureF(originalExposurePath) afwWarpedExposure = afwImage.ExposureF(originalExposurePath) originalImage = originalExposure.getMaskedImage().getImage() afwWarpedImage = afwWarpedExposure.getMaskedImage().getImage() originalWcs = originalExposure.getWcs() afwWarpedWcs = afwWarpedExposure.getWcs() warpingControl = afwMath.WarpingControl( "lanczos4", "", 0, interpLength) afwMath.warpImage(afwWarpedImage, afwWarpedWcs, originalImage, originalWcs, warpingControl) if SAVE_FITS_FILES: afwWarpedImage.writeFits("afwWarpedImageNull.fits") afwWarpedImageArr = afwWarpedImage.getArray() noDataMaskArr = np.isnan(afwWarpedImageArr) # relax specs a bit because of minor noise introduced by bad pixels msg = "afw null-warped Image" self.assertImagesAlmostEqual(originalImage, afwWarpedImage, skipMask=noDataMaskArr, atol=1e-5, msg=msg)
"""Cannot warp from or into an exposure without a Wcs. """ exposureWithWcs = afwImage.ExposureF(originalExposurePath) mi = exposureWithWcs.getMaskedImage() exposureWithoutWcs = afwImage.ExposureF(mi.getDimensions()) warpingControl = afwMath.WarpingControl( "bilinear", "", 0, interpLength)
with self.assertRaises(pexExcept.InvalidParameterError): afwMath.warpExposure(exposureWithWcs, exposureWithoutWcs, warpingControl)
with self.assertRaises(pexExcept.InvalidParameterError): afwMath.warpExposure(exposureWithoutWcs, exposureWithWcs, warpingControl)
"""Cannot warp in-place """ crpix=lsst.geom.Point2D(0, 0), crval=lsst.geom.SpherePoint(359, 0, lsst.geom.degrees), cdMatrix=afwGeom.makeCdMatrix(1.0e-8*lsst.geom.degrees), ) "bilinear", "", 0, interpLength)
"""Test the basic mechanics of WarpingControl """
wc.getMaskWarpingKernel().getCacheSize(), cacheSize) wc.getWarpingKernel().getCacheSize(), newCacheSize) wc.getMaskWarpingKernel().getCacheSize(), newCacheSize)
"""Test error handling of WarpingControl """ # error: mask kernel smaller than main kernel ("bilinear", "lanczos3"), ("bilinear", "lanczos4"), ("lanczos3", "lanczos4"), ):
# error: new mask kernel larger than main kernel
# error: new kernel smaller than mask kernel
# OK: main kernel at least as big as mask kernel ("bilinear", "bilinear"), ("lanczos3", "lanczos3"), ("lanczos3", "bilinear"), ("lanczos4", "lanczos3"), ): # this should not raise any exception
# invalid kernel names ("badname", ""), ("lanczos", ""), # no digit after lanczos ("lanczos3", "badname"), ("lanczos3", "lanczos"), ):
"""Test that warping the mask plane with a different kernel does the right thing """ ("bilinear", "bilinear"), ("lanczos3", "lanczos3"), ("lanczos3", "bilinear"), ("lanczos4", "lanczos3"), ): kernelName=kernelName, maskKernelName=maskKernelName, growFullMask=growFullMask, )
"""Test that warpExposure matches swarp using a bilinear warping kernel """
"""Test that warpExposure matches swarp using a bilinear warping kernel """ useSubregion=False, useDeepCopy=True)
"""Test that warpExposure matches swarp using a lanczos2 warping kernel """
"""Test that warpExposure matches swarp using a lanczos2 warping kernel. """
"""Test that warpExposure matches swarp using a lanczos2 warping kernel with a subexposure """ useSubregion=True, useDeepCopy=useDeepCopy)
"""Test that warpExposure matches swarp using a lanczos2 warping kernel """
"""Test that warpExposure matches swarp using a lanczos4 warping kernel. """
"""Test that warpExposure matches swarp using a lanczos2 warping kernel """
"""Test that warpExposure matches swarp using a lanczos4 warping kernel. """
"""Test that warpExposure matches swarp using a nearest neighbor warping kernel """
def testTransformBasedWarp(self): """Test warping using TransformPoint2ToPoint2 """ for interpLength in (0, 1, 2, 4): kernelName = "lanczos3" rtol = 4e-5 atol = 1e-2 warpingControl = afwMath.WarpingControl( warpingKernelName=kernelName, interpLength=interpLength, )
originalExposure = afwImage.ExposureF(originalExposurePath) originalMetadata = afwImage.DecoratedImageF(originalExposurePath).getMetadata() originalSkyWcs = afwGeom.makeSkyWcs(originalMetadata)
swarpedImageName = "medswarp1%s.fits" % (kernelName,) swarpedImagePath = os.path.join(dataDir, swarpedImageName) swarpedDecoratedImage = afwImage.DecoratedImageF(swarpedImagePath) swarpedImage = swarpedDecoratedImage.getImage()
swarpedMetadata = swarpedDecoratedImage.getMetadata() warpedSkyWcs = afwGeom.makeSkyWcs(swarpedMetadata)
# original image is source, warped image is destination srcToDest = afwGeom.makeWcsPairTransform(originalSkyWcs, warpedSkyWcs)
afwWarpedMaskedImage = afwImage.MaskedImageF(swarpedImage.getDimensions()) originalMaskedImage = originalExposure.getMaskedImage()
numGoodPix = afwMath.warpImage(afwWarpedMaskedImage, originalMaskedImage, srcToDest, warpingControl) self.assertGreater(numGoodPix, 50)
afwWarpedImage = afwWarpedMaskedImage.getImage() afwWarpedImageArr = afwWarpedImage.getArray() noDataMaskArr = np.isnan(afwWarpedImageArr) self.assertImagesAlmostEqual(afwWarpedImage, swarpedImage, skipMask=noDataMaskArr, rtol=rtol, atol=atol)
"""Test ticket 2441: warpExposure sometimes mishandles zero-extent dest exposures""" crpix=lsst.geom.Point2D(0, 0), crval=lsst.geom.SpherePoint(359, 0, lsst.geom.degrees), cdMatrix=afwGeom.makeCdMatrix(scale=1.0e-8*lsst.geom.degrees), )
crpix=lsst.geom.Point2D(410000, 11441), crval=lsst.geom.SpherePoint(45, 0, lsst.geom.degrees), cdMatrix=afwGeom.makeCdMatrix(scale=0.00011*lsst.geom.degrees, flipX=True), projection="CEA", )
# if a bug described in ticket #2441 is present, this will raise an # exception:
"""Test that a uint16 array can be cast to a bool array, to avoid DM-4063 """ # Note: assertEqual(bool arr, bool arr) fails with: # ValueError: The truth value of an array with more than one element is # ambiguous except Exception as e: print("Failed: %r != %r: %s" % (orArr, desOrArr, e)) raise
"""Verify that a source image that is too small will not raise an exception
This tests another bug that was fixed in ticket #2441 """ crpix=lsst.geom.Point2D(0, 0), crval=lsst.geom.SpherePoint(359, 0, lsst.geom.degrees), cdMatrix=afwGeom.makeCdMatrix(scale=1.0e-8*lsst.geom.degrees), )
crpix=lsst.geom.Point2D(0, 0), crval=lsst.geom.SpherePoint(358, 0, lsst.geom.degrees), cdMatrix=afwGeom.makeCdMatrix(scale=1.1e-8*lsst.geom.degrees), )
# if a bug described in ticket #2441 is present, this will raise an # exception:
rtol=4e-05, atol=1e-2): """Verify that using a separate mask warping kernel produces the correct results
Inputs: - kernelName: name of warping kernel in the form used by afwImage.makeKernel - maskKernelName: name of mask warping kernel in the form used by afwImage.makeKernel - interpLength: interpLength argument for lsst.afw.math.WarpingControl - cacheSize: cacheSize argument for lsst.afw.math.WarpingControl; 0 disables the cache 10000 gives some speed improvement but less accurate results (atol must be increased) 100000 gives better accuracy but no speed improvement in this test - rtol: relative tolerance as used by np.allclose - atol: absolute tolerance as used by np.allclose """ crpix=lsst.geom.Point2D(10, 11), crval=lsst.geom.SpherePoint(41.7, 32.9, lsst.geom.degrees), cdMatrix=afwGeom.makeCdMatrix(scale=0.2*lsst.geom.degrees), ) crpix=lsst.geom.Point2D(9, 10), crval=lsst.geom.SpherePoint(41.65, 32.95, lsst.geom.degrees), cdMatrix=afwGeom.makeCdMatrix(scale=0.17*lsst.geom.degrees), )
np.arange(0, shape[0] * shape[1], 1, dtype=np.uint16), shape)
kernelName, maskKernelName, cacheSize, interpLength, growFullMask )
# now compute with two separate mask planes
# we expect the mask planes to differ self.fail("No difference between broad and narrow mask")
narrowArrays[1] & ~growFullMask).astype(np.uint16)
(kernelName, maskKernelName) doImage=True, doMask=True, doVariance=True, rtol=rtol, atol=atol, msg=msg)
useWarpExposure=True, useSubregion=False, useDeepCopy=False, interpLength=10, cacheSize=100000, rtol=4e-05, atol=1e-2): """Compare warpExposure to swarp for given warping kernel.
Note that swarp only warps the image plane, so only test that plane.
Inputs: - kernelName: name of kernel in the form used by afwImage.makeKernel - useWarpExposure: if True, call warpExposure to warp an ExposureF, else call warpImage to warp an ImageF and also call the Transform version - useSubregion: if True then the original source exposure (from which the usual test exposure was extracted) is read and the correct subregion extracted - useDeepCopy: if True then the copy of the subimage is a deep copy, else it is a shallow copy; ignored if useSubregion is False - interpLength: interpLength argument for lsst.afw.math.WarpingControl - cacheSize: cacheSize argument for lsst.afw.math.WarpingControl; 0 disables the cache 10000 gives some speed improvement but less accurate results (atol must be increased) 100000 gives better accuracy but no speed improvement in this test - rtol: relative tolerance as used by np.allclose - atol: absolute tolerance as used by np.allclose """ warpingControl = afwMath.WarpingControl( kernelName, "", # there is no point to a separate mask kernel since we aren't testing the mask plane cacheSize, interpLength, ) if useSubregion: originalFullExposure = afwImage.ExposureF(originalExposurePath) # "medsub" is a subregion of med starting at 0-indexed pixel (40, 150) of size 145 x 200 bbox = lsst.geom.Box2I(lsst.geom.Point2I(40, 150), lsst.geom.Extent2I(145, 200)) originalExposure = afwImage.ExposureF( originalFullExposure, bbox, afwImage.LOCAL, useDeepCopy) swarpedImageName = "medsubswarp1%s.fits" % (kernelName,) else: originalExposure = afwImage.ExposureF(originalExposurePath) swarpedImageName = "medswarp1%s.fits" % (kernelName,)
swarpedImagePath = os.path.join(dataDir, swarpedImageName) swarpedDecoratedImage = afwImage.DecoratedImageF(swarpedImagePath) swarpedImage = swarpedDecoratedImage.getImage() swarpedMetadata = swarpedDecoratedImage.getMetadata() warpedWcs = afwGeom.makeSkyWcs(swarpedMetadata)
if useWarpExposure: # path for saved afw-warped image afwWarpedImagePath = "afwWarpedExposure1%s.fits" % (kernelName,)
afwWarpedMaskedImage = afwImage.MaskedImageF( swarpedImage.getDimensions()) afwWarpedExposure = afwImage.ExposureF( afwWarpedMaskedImage, warpedWcs) afwMath.warpExposure( afwWarpedExposure, originalExposure, warpingControl) afwWarpedMask = afwWarpedMaskedImage.getMask() if SAVE_FITS_FILES: afwWarpedExposure.writeFits(afwWarpedImagePath) if display: ds9.mtv(afwWarpedExposure, frame=1, title="Warped")
swarpedMaskedImage = afwImage.MaskedImageF(swarpedImage)
if display: ds9.mtv(swarpedMaskedImage, frame=2, title="SWarped")
msg = "afw and swarp %s-warped differ (ignoring bad pixels)" % ( kernelName,) try: self.assertMaskedImagesAlmostEqual(afwWarpedMaskedImage, swarpedMaskedImage, doImage=True, doMask=False, doVariance=False, skipMask=afwWarpedMask, rtol=rtol, atol=atol, msg=msg) except Exception: if SAVE_FAILED_FITS_FILES: afwWarpedExposure.writeFits(afwWarpedImagePath) print("Saved failed afw-warped exposure as: %s" % (afwWarpedImagePath,)) raise else: # path for saved afw-warped image afwWarpedImagePath = "afwWarpedImage1%s.fits" % (kernelName,) afwWarpedImage2Path = "afwWarpedImage1%s_xyTransform.fits" % ( kernelName,)
afwWarpedImage = afwImage.ImageF(swarpedImage.getDimensions()) originalImage = originalExposure.getMaskedImage().getImage() originalWcs = originalExposure.getWcs() afwMath.warpImage(afwWarpedImage, warpedWcs, originalImage, originalWcs, warpingControl) if display: ds9.mtv(afwWarpedImage, frame=1, title="Warped") ds9.mtv(swarpedImage, frame=2, title="SWarped") diff = swarpedImage.Factory(swarpedImage, True) diff -= afwWarpedImage ds9.mtv(diff, frame=3, title="swarp - afw") if SAVE_FITS_FILES: afwWarpedImage.writeFits(afwWarpedImagePath)
afwWarpedImageArr = afwWarpedImage.getArray() noDataMaskArr = np.isnan(afwWarpedImageArr) msg = "afw and swarp %s-warped images do not match (ignoring NaN pixels)" % \ (kernelName,) try: self.assertImagesAlmostEqual(afwWarpedImage, swarpedImage, skipMask=noDataMaskArr, rtol=rtol, atol=atol, msg=msg) except Exception: if SAVE_FAILED_FITS_FILES: # save the image anyway afwWarpedImage.writeFits(afwWarpedImagePath) print("Saved failed afw-warped image as: %s" % (afwWarpedImagePath,)) raise
afwWarpedImage2 = afwImage.ImageF(swarpedImage.getDimensions()) srcToDest = afwGeom.makeWcsPairTransform(originalWcs, warpedWcs) afwMath.warpImage(afwWarpedImage2, originalImage, srcToDest, warpingControl) msg = "afw transform-based and WCS-based %s-warped images do not match" % ( kernelName,) try: self.assertImagesAlmostEqual(afwWarpedImage2, afwWarpedImage, rtol=rtol, atol=atol, msg=msg) except Exception: if SAVE_FAILED_FITS_FILES: # save the image anyway afwWarpedImage.writeFits(afwWarpedImagePath) print("Saved failed afw-warped image as: %s" % (afwWarpedImage2Path,)) raise
lsst.utils.tests.init() unittest.main() |