def testOnlyOneGridCell(self): """Test how the program handles nxSample,nySample being 1x1.""" # try a ramping image ... has an easy analytic solution nx = 64 ny = 64 img = afwImage.ImageD(afwGeom.Extent2I(nx, ny), 10) dzdx, dzdy, z0 = 0.1, 0.2, 10000.0 mean = z0 + dzdx * (nx - 1) / 2 + dzdy * ( ny - 1) / 2 # the analytic solution for x in range(nx): for y in range(ny): img.set(x, y, dzdx * x + dzdy * y + z0) # make a background control object bctrl = afwMath.BackgroundControl(10, 10) bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT) bctrl.setNxSample(1) bctrl.setNySample(1) bctrl.setUndersampleStyle(afwMath.THROW_EXCEPTION) backobj = afwMath.makeBackground(img, bctrl) xpixels = [0, nx // 2, nx - 1] ypixels = [0, ny // 2, ny - 1] for xpix in xpixels: for ypix in ypixels: testval = afwMath.cast_BackgroundMI(backobj).getPixel( bctrl.getInterpStyle(), xpix, ypix) self.assertAlmostEqual(testval / mean, 1)
def getParabolaImage(self, nx, ny): parabimg = afwImage.ImageD(afwGeom.Extent2I(nx, ny)) d2zdx2, d2zdy2, dzdx, dzdy, z0 = -1.0e-4, -1.0e-4, 0.1, 0.2, 10000.0 # no cross-terms for x in range(nx): for y in range(ny): parabimg.set( x, y, d2zdx2 * x * x + d2zdy2 * y * y + dzdx * x + dzdy * y + z0) return parabimg
def testUndersample(self): """Test how the program handles nx,ny being too small for requested interp style.""" # make an image nx = 64 ny = 64 img = afwImage.ImageD(afwGeom.Extent2I(nx, ny)) # make a background control object bctrl = afwMath.BackgroundControl(10, 10) bctrl.setInterpStyle(afwMath.Interpolate.CUBIC_SPLINE) bctrl.setNxSample(3) bctrl.setNySample(3) if False: # INCREASE_NXNYSAMPLE is no longer supported post #2074 bctrl.setNxSample(2) bctrl.setNySample(2) # see if it adjusts the nx,ny values up to 3x3 bctrl.setUndersampleStyle(afwMath.INCREASE_NXNYSAMPLE) backobj = afwMath.makeBackground(img, bctrl) self.assertEqual(backobj.getBackgroundControl().getNxSample(), 3) self.assertEqual(backobj.getBackgroundControl().getNySample(), 3) # put nx,ny back to 2 and see if it adjusts the interp style down to linear bctrl.setNxSample(2) bctrl.setNySample(2) bctrl.setUndersampleStyle("REDUCE_INTERP_ORDER") backobj = afwMath.makeBackground(img, bctrl) backobj.getImageF( ) # Need to interpolate background to discover what we actually needed self.assertEqual(backobj.getAsUsedInterpStyle(), afwMath.Interpolate.LINEAR) # put interp style back up to cspline and see if it throws an exception bctrl.setUndersampleStyle("THROW_EXCEPTION") def tst(img, bctrl): backobj = afwMath.makeBackground(img, bctrl) backobj.getImageF( "CUBIC_SPLINE" ) # only now do we see that we have too few points self.assertRaises(lsst.pex.exceptions.InvalidParameterError, tst, img, bctrl)
def testRamp(self): # make a ramping image (spline should be exact for linear increasing image nx = 512 ny = 512 rampimg = afwImage.ImageD(afwGeom.Extent2I(nx, ny)) dzdx, dzdy, z0 = 0.1, 0.2, 10000.0 for x in range(nx): for y in range(ny): rampimg.set(x, y, dzdx * x + dzdy * y + z0) # check corner, edge, and center pixels bctrl = afwMath.BackgroundControl(10, 10) bctrl.setInterpStyle(afwMath.Interpolate.CUBIC_SPLINE) bctrl.setNxSample(6) bctrl.setNySample(6) bctrl.getStatisticsControl().setNumSigmaClip( 20.0) # something large enough to avoid clipping entirely bctrl.getStatisticsControl().setNumIter(1) backobj = afwMath.makeBackground(rampimg, bctrl) xpixels = [0, nx // 2, nx - 1] ypixels = [0, ny // 2, ny - 1] for xpix in xpixels: for ypix in ypixels: testval = afwMath.cast_BackgroundMI(backobj).getPixel( xpix, ypix) self.assertAlmostEqual(testval / rampimg.get(xpix, ypix), 1, 6) # Test pickle bg = afwMath.cast_BackgroundMI(backobj) new = pickle.loads(pickle.dumps(bg)) self.assertBackgroundEqual(bg, new) # Check creation of sub-image box = afwGeom.Box2I(afwGeom.Point2I(123, 45), afwGeom.Extent2I(45, 123)) bgImage = bg.getImageF("AKIMA_SPLINE") bgSubImage = afwImage.ImageF(bgImage, box) testImage = bg.getImageF(box, "AKIMA_SPLINE") self.assertEqual(testImage.getXY0(), bgSubImage.getXY0()) self.assertEqual(testImage.getDimensions(), bgSubImage.getDimensions()) self.assertTrue(np.all(testImage.getArray() == bgSubImage.getArray()))
scienceMaskedImage, "subtractMaskedImages", candidateList=footprints) diffim3 = results3.subtractedImage spatialKernel3 = results3.psfMatchingKernel spatialBg3 = results3.backgroundModel kernelCellSet3 = results3.kernelCellSet basisList1 = spatialKernel1.getKernelList() basisList2 = spatialKernel2.getKernelList() basisList3 = spatialKernel3.getKernelList() frame = 1 for idx in range(min(5, len(basisList1))): kernel = basisList1[idx] im = afwImage.ImageD(spatialKernel1.getDimensions()) ksum = kernel.computeImage(im, False) ds9.mtv(im, frame=frame) frame += 1 for idx in range(min(5, len(basisList2))): kernel = basisList2[idx] im = afwImage.ImageD(spatialKernel2.getDimensions()) ksum = kernel.computeImage(im, False) ds9.mtv(im, frame=frame) frame += 1 for idx in range(min(5, len(basisList3))): kernel = basisList3[idx] im = afwImage.ImageD(spatialKernel3.getDimensions()) ksum = kernel.computeImage(im, False)