def testShrinkGrowBBox(self): """Test Kernel methods shrinkBBox and growBBox """ boxStart = lsst.geom.Point2I(3, -3) for kWidth in (1, 2, 6): for kHeight in (1, 2, 5): for deltaWidth in (-1, 0, 1, 20): fullWidth = kWidth + deltaWidth for deltaHeight in (-1, 0, 1, 20): fullHeight = kHeight + deltaHeight kernel = afwMath.DeltaFunctionKernel( kWidth, kHeight, lsst.geom.Point2I(0, 0)) fullBBox = lsst.geom.Box2I( boxStart, lsst.geom.Extent2I(fullWidth, fullHeight)) if (fullWidth < kWidth) or (fullHeight < kHeight): self.assertRaises(Exception, kernel.shrinkBBox, fullBBox) continue shrunkBBox = kernel.shrinkBBox(fullBBox) self.assertEqual(shrunkBBox.getWidth(), fullWidth + 1 - kWidth) self.assertEqual(shrunkBBox.getHeight(), fullHeight + 1 - kHeight) self.assertEqual(shrunkBBox.getMinX(), boxStart[0] + kernel.getCtrX()) self.assertEqual(shrunkBBox.getMinY(), boxStart[1] + kernel.getCtrY()) newFullBBox = kernel.growBBox(shrunkBBox) self.assertEqual(newFullBBox, fullBBox, "growBBox(shrinkBBox(x)) != x")
def testShrinkGrowBBox(self): """Test Kernel methods shrinkBBox and growBBox """ boxStart = afwGeom.Point2I(3, -3) for kWidth in (1, 2, 6): for kHeight in (1, 2, 5): for deltaWidth in (-1, 0, 1, 20): fullWidth = kWidth + deltaWidth for deltaHeight in (-1, 0, 1, 20): fullHeight = kHeight + deltaHeight kernel = afwMath.DeltaFunctionKernel( kWidth, kHeight, afwGeom.Point2I(0, 0)) fullBBox = afwGeom.Box2I( boxStart, afwGeom.Extent2I(fullWidth, fullHeight)) if (fullWidth < kWidth) or (fullHeight < kHeight): self.assertRaises(Exception, kernel.shrinkBBox, fullBBox) continue shrunkBBox = kernel.shrinkBBox(fullBBox) self.assert_( (shrunkBBox.getWidth() == fullWidth + 1 - kWidth) and (shrunkBBox.getHeight() == fullHeight + 1 - kHeight), "shrinkBBox returned box of wrong size") self.assert_( (shrunkBBox.getMinX() == boxStart[0] + kernel.getCtrX()) and (shrunkBBox.getMinY() == boxStart[1] + kernel.getCtrY()), "shrinkBBox returned box with wrong minimum") newFullBBox = kernel.growBBox(shrunkBBox) self.assert_(newFullBBox == fullBBox, "growBBox(shrinkBBox(x)) != x")
def testMakeBadKernels(self): """Attempt to make various invalid kernels; make sure the constructor shows an exception """ kWidth = 4 kHeight = 3 gaussFunc1 = afwMath.GaussianFunction1D(1.0) gaussFunc2 = afwMath.GaussianFunction2D(1.0, 1.0, 0.0) spFunc = afwMath.PolynomialFunction2D(1) kernelList = [] kernelList.append(afwMath.FixedKernel( afwImage.ImageD(lsst.geom.Extent2I(kWidth, kHeight), 0.1))) kernelList.append(afwMath.FixedKernel( afwImage.ImageD(lsst.geom.Extent2I(kWidth, kHeight), 0.2))) for numKernelParams in (2, 4): spFuncList = [] for ii in range(numKernelParams): spFuncList.append(spFunc.clone()) try: afwMath.AnalyticKernel(kWidth, kHeight, gaussFunc2, spFuncList) self.fail("Should have failed with wrong # of spatial functions") except pexExcept.Exception: pass for numKernelParams in (1, 3): spFuncList = [] for ii in range(numKernelParams): spFuncList.append(spFunc.clone()) try: afwMath.LinearCombinationKernel(kernelList, spFuncList) self.fail("Should have failed with wrong # of spatial functions") except pexExcept.Exception: pass kParamList = [0.2]*numKernelParams try: afwMath.LinearCombinationKernel(kernelList, kParamList) self.fail("Should have failed with wrong # of kernel parameters") except pexExcept.Exception: pass try: afwMath.SeparableKernel( kWidth, kHeight, gaussFunc1, gaussFunc1, spFuncList) self.fail("Should have failed with wrong # of spatial functions") except pexExcept.Exception: pass for pointX in range(-1, kWidth+2): for pointY in range(-1, kHeight+2): if (0 <= pointX < kWidth) and (0 <= pointY < kHeight): continue try: afwMath.DeltaFunctionKernel( kWidth, kHeight, lsst.geom.Point2I(pointX, pointY)) self.fail("Should have failed with point not on kernel") except pexExcept.Exception: pass
def testBasics(self): """ Make sure swig interface works """ testExposure = afwImage.ExposureF(inFilePathSmall) blankImage = afwImage.ImageD(5, 5, 0) nullKernel = afwMath.DeltaFunctionKernel(5, 5, afwImage.PointI(2, 2)) print "testExposure=%r" % (testExposure, ) coaddComp = coaddKaiser.CoaddComponent(testExposure, nullKernel)
def makeDeltaFunctionKernelList(kWidth, kHeight): """Create a list of delta function kernels This is useful for constructing a LinearCombinationKernel. """ kVec = [] for activeCol in range(kWidth): for activeRow in range(kHeight): kVec.append(afwMath.DeltaFunctionKernel( kWidth, kHeight, lsst.geom.Point2I(activeCol, activeRow))) return kVec
def testLinearCombinationKernel(self): """Test LinearCombinationKernel using a set of delta basis functions """ kWidth = 3 kHeight = 2 pol = pexPolicy.Policy() additionalData = dafBase.PropertySet() loc = dafPersist.LogicalLocation( os.path.join(testPath, "data", "kernel5.boost")) persistence = dafPersist.Persistence.getPersistence(pol) # create list of kernels basisImArrList = [] kVec = afwMath.KernelList() for row in range(kHeight): for col in range(kWidth): kernel = afwMath.DeltaFunctionKernel(kWidth, kHeight, afwGeom.Point2I(col, row)) basisImage = afwImage.ImageD(kernel.getDimensions()) kernel.computeImage(basisImage, True) basisImArrList.append(basisImage.getArray().transpose().copy()) kVec.append(kernel) kParams = [0.0] * len(kVec) k = afwMath.LinearCombinationKernel(kVec, kParams) for ii in range(len(kVec)): kParams = [0.0] * len(kVec) kParams[ii] = 1.0 k.setKernelParameters(kParams) storageList = dafPersist.StorageList() storage = persistence.getPersistStorage("XmlStorage", loc) storageList.append(storage) persistence.persist(k, storageList, additionalData) storageList2 = dafPersist.StorageList() storage2 = persistence.getRetrieveStorage("XmlStorage", loc) storageList2.append(storage2) x = persistence.unsafeRetrieve("LinearCombinationKernel", storageList2, additionalData) k2 = afwMath.LinearCombinationKernel.swigConvert(x) self.kernelCheck(k, k2) kIm = afwImage.ImageD(k2.getDimensions()) k2.computeImage(kIm, True) kImArr = kIm.getArray().transpose() if not np.allclose(kImArr, basisImArrList[ii]): self.fail( "%s = %s != %s for the %s'th basis kernel" % (k2.__class__.__name__, kImArr, basisImArrList[ii], ii))
def testDeltaConvolve(self): """Test convolution with various delta function kernels using optimized code """ for kWidth in range(1, 4): for kHeight in range(1, 4): for activeCol in range(kWidth): for activeRow in range(kHeight): kernel = afwMath.DeltaFunctionKernel(kWidth, kHeight, afwGeom.Point2I(activeCol, activeRow)) if display and False: kim = afwImage.ImageD(kWidth, kHeight); kernel.computeImage(kim, False) ds9.mtv(kim, frame=1) self.runStdTest(kernel, kernelDescr="Delta Function Kernel")
def getDeltaLinearCombinationKernel(kSize, imSize, spOrder): """Return a LinearCombinationKernel of delta functions @param kSize: kernel size (scalar; height = width) @param x, y imSize: image size """ kernelList = afwMath.KernelList() for ctrX in range(kSize): for ctrY in range(kSize): kernelList.append(afwMath.DeltaFunctionKernel(kSize, kSize, afwGeom.Point2I(ctrX, ctrY))) polyFunc = afwMath.PolynomialFunction2D(spOrder) kernel = afwMath.LinearCombinationKernel(kernelList, polyFunc) spParams = getSpatialParameters(len(kernelList), polyFunc) kernel.setSpatialParameters(spParams); return kernel
def testDeltaFunctionKernel(self): """Test DeltaFunctionKernel """ pol = pexPolicy.Policy() additionalData = dafBase.PropertySet() loc = dafPersist.LogicalLocation( os.path.join(testPath, "data", "kernel3.boost")) persistence = dafPersist.Persistence.getPersistence(pol) for kWidth in range(1, 4): for kHeight in range(1, 4): for activeCol in range(kWidth): for activeRow in range(kHeight): kernel = afwMath.DeltaFunctionKernel( kWidth, kHeight, afwGeom.Point2I(activeCol, activeRow)) storageList = dafPersist.StorageList() storage = persistence.getPersistStorage( "XmlStorage", loc) storageList.append(storage) persistence.persist(kernel, storageList, additionalData) storageList2 = dafPersist.StorageList() storage2 = persistence.getRetrieveStorage( "XmlStorage", loc) storageList2.append(storage2) x = persistence.unsafeRetrieve("DeltaFunctionKernel", storageList2, additionalData) k2 = afwMath.DeltaFunctionKernel.swigConvert(x) self.kernelCheck(kernel, k2) self.assertEqual(kernel.getPixel(), k2.getPixel()) kImage = afwImage.ImageD(k2.getDimensions()) kSum = k2.computeImage(kImage, False) self.assertEqual(kSum, 1.0) kArr = kImage.getArray().transpose() self.assertEqual(kArr[activeCol, activeRow], 1.0) kArr[activeCol, activeRow] = 0.0 self.assertEqual(kArr.sum(), 0.0)
def testCast(self): instances = [] kVec = makeGaussianKernelList(9, 9, [(2.0, 2.0, 0.0)]) kParams = [0.0] * len(kVec) instances.append(afwMath.LinearCombinationKernel(kVec, kParams)) instances.append( afwMath.AnalyticKernel(7, 7, afwMath.GaussianFunction2D(2.0, 2.0, 0.0))) instances.append( afwMath.DeltaFunctionKernel(5, 5, afwGeom.Point2I(1, 1))) instances.append( afwMath.FixedKernel(afwImage.ImageD(afwGeom.Extent2I(7, 7)))) instances.append( afwMath.SeparableKernel(3, 3, afwMath.PolynomialFunction1D(0), afwMath.PolynomialFunction1D(0))) for instance in instances: Class = type(instance) base = instance.clone() self.assertEqual(type(base), afwMath.Kernel) derived = Class.cast(base) self.assertEqual(type(derived), Class)
def testLinearCombinationKernel(self): """Test LinearCombinationKernel using a set of delta basis functions """ kWidth = 3 kHeight = 2 # create list of kernels basisImArrList = [] kVec = [] for row in range(kHeight): for col in range(kWidth): kernel = afwMath.DeltaFunctionKernel( kWidth, kHeight, lsst.geom.Point2I(col, row)) basisImage = afwImage.ImageD(kernel.getDimensions()) kernel.computeImage(basisImage, True) basisImArrList.append(basisImage.getArray().transpose().copy()) kVec.append(kernel) kParams = [0.0] * len(kVec) k = afwMath.LinearCombinationKernel(kVec, kParams) for ii in range(len(kVec)): kParams = [0.0] * len(kVec) kParams[ii] = 1.0 k.setKernelParameters(kParams) with lsst.utils.tests.getTempFilePath(".fits") as filename: k.writeFits(filename) k2 = afwMath.LinearCombinationKernel.readFits(filename) self.kernelCheck(k, k2) kIm = afwImage.ImageD(k2.getDimensions()) k2.computeImage(kIm, True) kImArr = kIm.getArray().transpose() if not np.allclose(kImArr, basisImArrList[ii]): self.fail( f"{k2.__class__.__name__} = {kImArr} != {basisImArrList[ii]} " f"for the {ii}'th basis kernel")
def testDeltaFunctionKernel(self): """Test DeltaFunctionKernel """ for kWidth in range(1, 4): for kHeight in range(1, 4): for activeCol in range(kWidth): for activeRow in range(kHeight): kernel = afwMath.DeltaFunctionKernel(kWidth, kHeight, lsst.geom.Point2I(activeCol, activeRow)) with lsst.utils.tests.getTempFilePath(".fits") as filename: kernel.writeFits(filename) k2 = afwMath.DeltaFunctionKernel.readFits(filename) self.kernelCheck(kernel, k2) self.assertEqual(kernel.getPixel(), k2.getPixel()) kImage = afwImage.ImageD(k2.getDimensions()) kSum = k2.computeImage(kImage, False) self.assertEqual(kSum, 1.0) kArr = kImage.getArray().transpose() self.assertEqual(kArr[activeCol, activeRow], 1.0) kArr[activeCol, activeRow] = 0.0 self.assertEqual(kArr.sum(), 0.0)