def assertRegionCorrect(self, region): """Assert that a region has correct corner images This test is only relevant for operations that try to reuse the image array data """ regionCopy = mathDetail.KernelImagesForRegion(region.getKernel(), region.getBBox(), region.getXY0(), region.getDoNormalize()) for location in ( region.BOTTOM_LEFT, region.BOTTOM_RIGHT, region.TOP_LEFT, region.TOP_RIGHT, ): actImage = region.getImage(location) actImArr = actImage.getArray().transpose().copy() desImage = regionCopy.getImage(location) desImArr = desImage.getArray().transpose().copy() actImArr -= desImArr if not np.allclose(actImArr, 0): actImage.writeFits(f"actImage{location}.fits") desImage.writeFits(f"desImage{location}.fits") self.fail(f"failed on location {location}")
def testDoNormalize(self): """Test getDoNormalize """ kernel = self.makeKernel() for doNormalize in (False, True): region = mathDetail.KernelImagesForRegion(kernel, self.bbox, self.xy0, doNormalize) self.assertEqual(region.getDoNormalize(), doNormalize)
def testExactImages(self): """Confirm that kernel image at each location is correct """ desImage = afwImage.ImageD( afwGeom.Extent2I(self.kernel.getWidth(), self.kernel.getHeight())) for doNormalize in (False, True): region = mathDetail.KernelImagesForRegion(self.kernel, self.bbox, self.xy0, doNormalize) for location in ( region.BOTTOM_LEFT, region.BOTTOM_RIGHT, region.TOP_LEFT, region.TOP_RIGHT, ): pixelIndex = region.getPixelIndex(location) xPos = afwImage.indexToPosition(pixelIndex[0] + self.xy0[0]) yPos = afwImage.indexToPosition(pixelIndex[1] + self.xy0[1]) self.kernel.computeImage(desImage, doNormalize, xPos, yPos) desImArr = desImage.getArray().transpose().copy() actImage = region.getImage(location) actImArr = actImage.getArray().transpose().copy() errStr = imTestUtils.imagesDiffer(actImArr, desImArr) if errStr: self.fail("exact image(%s) incorrect:\n%s" % (LocNameDict[location], errStr))
def testComputeNextRow(self): """Test computeNextRow method and the resulting RowOfKernelImagesForRegion """ nx = 6 ny = 5 regionRow = mathDetail.RowOfKernelImagesForRegion(nx, ny) self.assert_(not regionRow.hasData()) self.assert_(not regionRow.isLastRow()) self.assert_(regionRow.getYInd() == -1) region = mathDetail.KernelImagesForRegion(self.kernel, self.bbox, self.xy0, False) floatWidth = self.bbox.getWidth() / float(nx) validWidths = (int(math.floor(floatWidth)), int(math.ceil(floatWidth))) floatHeight = self.bbox.getHeight() / float(ny) validHeights = (int(math.floor(floatHeight)), int(math.ceil(floatHeight))) totalHeight = 0 for yInd in range(ny): rowWidth = 0 isOK = region.computeNextRow(regionRow) self.assert_(isOK) self.assert_(regionRow.hasData()) self.assert_(regionRow.isLastRow() == (yInd + 1 >= ny)) self.assert_(regionRow.getYInd() == yInd) firstBBox = regionRow.getRegion(0).getBBox() self.assert_(firstBBox.getMinX() == self.bbox.getMinX()) if yInd == 0: self.assert_(firstBBox.getMinY() == self.bbox.getMinY()) firstBBoxHeight = firstBBox.getHeight() self.assert_(firstBBoxHeight in validHeights) totalHeight += firstBBoxHeight if yInd > 0: self.assert_(firstBBox.getMinY() == prevFirstBBox.getMaxY() + 1) if yInd == ny - 1: self.assert_(firstBBox.getMaxY() == self.bbox.getMaxY()) prevFirstBBox = firstBBox for xInd in range(nx): subregion = regionRow.getRegion(xInd) try: self.assertRegionCorrect(subregion) except: print "failed on xInd=%s, yInd=%s" % (xInd, yInd) raise bbox = subregion.getBBox() rowWidth += bbox.getWidth() self.assert_(bbox.getWidth() in validWidths) self.assert_(bbox.getHeight() == firstBBoxHeight) if xInd > 0: self.assert_(bbox.getMinX() == prevBBox.getMaxX() + 1) self.assert_(bbox.getMinY() == prevBBox.getMinY()) self.assert_(bbox.getMaxY() == prevBBox.getMaxY()) if xInd == nx - 1: self.assert_(bbox.getMaxX() == self.bbox.getMaxX()) prevBBox = bbox self.assert_(rowWidth == self.bbox.getWidth()) self.assert_(totalHeight == self.bbox.getHeight()) self.assert_(not region.computeNextRow(regionRow))
def testGetPixelIndex(self): """Test getPixelIndex method """ region = mathDetail.KernelImagesForRegion(self.kernel, self.bbox, self.xy0, False) leftInd = self.bbox.getMinX() rightInd = self.bbox.getMaxX() + 1 bottomInd = self.bbox.getMinY() topInd = self.bbox.getMaxY() + 1 int(round((leftInd + rightInd) / 2.0)) int(round((bottomInd + topInd) / 2.0)) for location, desIndex in ( (region.BOTTOM_LEFT, (leftInd, bottomInd)), (region.BOTTOM_RIGHT, (rightInd, bottomInd)), (region.TOP_LEFT, (leftInd, topInd)), (region.TOP_RIGHT, (rightInd, topInd)), ): desPixIndex = afwGeom.Point2I(desIndex[0], desIndex[1]) self.assertEqual(region.getPixelIndex(location), desPixIndex, "getPixelIndex(%s) = %s != %s" % (LocNameDict[location], region.getPixelIndex(location), desPixIndex))
def testExactImages(self): """Confirm that kernel image at each location is correct """ desImage = afwImage.ImageD(afwGeom.Extent2I(self.kernel.getWidth(), self.kernel.getHeight())) for doNormalize in (False, True): region = mathDetail.KernelImagesForRegion(self.kernel, self.bbox, self.xy0, doNormalize) for location in ( region.BOTTOM_LEFT, region.BOTTOM_RIGHT, region.TOP_LEFT, region.TOP_RIGHT, ): pixelIndex = region.getPixelIndex(location) xPos = afwImage.indexToPosition(pixelIndex[0] + self.xy0[0]) yPos = afwImage.indexToPosition(pixelIndex[1] + self.xy0[1]) self.kernel.computeImage(desImage, doNormalize, xPos, yPos) actImage = region.getImage(location) msg = "exact image(%s) incorrect" % (LocNameDict[location],) self.assertImagesNearlyEqual(actImage, desImage, msg=msg)