def testShrink(self): width, height = 5, 10 # Size of footprint x0, y0 = 50, 50 # Position of footprint imwidth, imheight = 100, 100 # Size of image foot = afwDetect.Footprint(afwGeom.Box2I(afwGeom.Point2I(x0, y0), afwGeom.Extent2I(width, height)), afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(imwidth, imheight))) self.assertEqual(foot.getNpix(), width*height) # Add some peaks to the original footprint and check that those lying outside # the shrunken footprint are omitted from the returned shrunken footprint. foot.addPeak(50, 50, 1) # should be omitted in shrunken footprint foot.addPeak(52, 52, 2) # should be kept in shrunken footprint foot.addPeak(50, 59, 3) # should be omitted in shrunken footprint self.assertEqual(len(foot.getPeaks()), 3) # check that all three peaks were added # Shrinking by one pixel makes each dimension *two* pixels shorter. shrunk = afwDetect.shrinkFootprint(foot, 1, True) self.assertEqual(3*8, shrunk.getNpix()) # Shrunken footprint should now only contain one peak at (52, 52) self.assertEqual(len(shrunk.getPeaks()), 1) peak = shrunk.getPeaks()[0] self.assertEqual((peak.getIx(), peak.getIy()), (52, 52)) # Without shifting the centroid self.assertEqual(shrunk.getCentroid(), foot.getCentroid()) # Get the same result from a Manhattan shrink shrunk = afwDetect.shrinkFootprint(foot, 1, False) self.assertEqual(3*8, shrunk.getNpix()) self.assertEqual(shrunk.getCentroid(), foot.getCentroid()) # Shrinking by a large amount leaves nothing. self.assertEqual(afwDetect.shrinkFootprint(foot, 100, True).getNpix(), 0)
def testShrinkIsoVsManhattan(self): # Demonstrate that isotropic and Manhattan shrinks are different. radius = 8 imwidth, imheight = 100, 100 x0, y0 = imwidth//2, imheight//2 nshrink = 4 ellipse = afwGeomEllipses.Ellipse(afwGeomEllipses.Axes(1.5*radius, 2*radius, 0), afwGeom.Point2D(x0,y0)) foot = afwDetect.Footprint(ellipse, afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(imwidth, imheight))) self.assertNotEqual(afwDetect.shrinkFootprint(foot, nshrink, False), afwDetect.shrinkFootprint(foot, nshrink, True))
def _fig8Test(self, x1, y1, x2, y2): # Construct a "figure of 8" consisting of two circles touching at the # centre of an image, then demonstrate that it shrinks correctly. # (Helper method for tests below.) radius = 3 imwidth, imheight = 100, 100 nshrink = 1 # These are the correct values for footprint sizes given the paramters # above. circle_npix = 29 initial_npix = circle_npix * 2 - 1 # touch at one pixel shrunk_npix = 26 box = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(imwidth, imheight)) e1 = afwGeomEllipses.Ellipse(afwGeomEllipses.Axes(radius, radius, 0), afwGeom.Point2D(x1, y1)) f1 = afwDetect.Footprint(e1, box) self.assertEqual(f1.getNpix(), circle_npix) e2 = afwGeomEllipses.Ellipse(afwGeomEllipses.Axes(radius, radius, 0), afwGeom.Point2D(x2, y2)) f2 = afwDetect.Footprint(e2, box) self.assertEqual(f2.getNpix(), circle_npix) initial = afwDetect.mergeFootprints(f1, f2) initial.setRegion( f2.getRegion()) # merge does not propagate the region self.assertEqual(initial_npix, initial.getNpix()) shrunk = afwDetect.shrinkFootprint(initial, nshrink, True) self.assertEqual(shrunk_npix, shrunk.getNpix()) if display: idImage = afwImage.ImageU(imwidth, imheight) for i, foot in enumerate([initial, shrunk]): print(foot.getNpix()) foot.insertIntoImage(idImage, i + 1) ds9.mtv(idImage)
def _fig8Test(self, x1, y1, x2, y2): # Construct a "figure of 8" consisting of two circles touching at the # centre of an image, then demonstrate that it shrinks correctly. # (Helper method for tests below.) radius = 3 imwidth, imheight = 100, 100 nshrink = 1 # These are the correct values for footprint sizes given the paramters # above. circle_npix = 29 initial_npix = circle_npix * 2 - 1 # touch at one pixel shrunk_npix = 26 box = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(imwidth, imheight)) e1 = afwGeomEllipses.Ellipse(afwGeomEllipses.Axes(radius, radius, 0), afwGeom.Point2D(x1, y1)) f1 = afwDetect.Footprint(e1,box) self.assertEqual(f1.getNpix(), circle_npix) e2 = afwGeomEllipses.Ellipse(afwGeomEllipses.Axes(radius, radius, 0), afwGeom.Point2D(x2, y2)) f2 = afwDetect.Footprint(e2,box) self.assertEqual(f2.getNpix(), circle_npix) initial = afwDetect.mergeFootprints(f1, f2) initial.setRegion(f2.getRegion()) # merge does not propagate the region self.assertEqual(initial_npix, initial.getNpix()) shrunk = afwDetect.shrinkFootprint(initial, nshrink, True) self.assertEqual(shrunk_npix, shrunk.getNpix()) if display: idImage = afwImage.ImageU(imwidth, imheight) for i, foot in enumerate([initial, shrunk]): print foot.getNpix() foot.insertIntoImage(idImage, i+1); ds9.mtv(idImage)