def testInnerProducts(self): """Test inner products""" width, height = 10, 20 im1 = afwImage.ImageF(afwGeom.Extent2I(width, height)) val1 = 10 im1.set(val1) im2 = im1.Factory(im1.getDimensions()) val2 = 20 im2.set(val2) self.assertEqual(afwImage.innerProduct(im1, im1), width * height * val1 * val1) self.assertEqual(afwImage.innerProduct(im1, im2), width * height * val1 * val2) im2.set(0, 0, 0) self.assertEqual(afwImage.innerProduct(im1, im2), (width * height - 1) * val1 * val2) im2.set(0, 0, val2) # reinstate value im2.set(width - 1, height - 1, 1) self.assertEqual(afwImage.innerProduct(im1, im2), (width * height - 1) * val1 * val2 + val1)
def testPca(self): """Test calculating PCA""" random.seed(0) width, height = 200, 100 numBases = 3 numInputs = 3 bases = [] for i in range(numBases): im = afwImage.ImageF(width, height) array = im.getArray() x, y = np.indices(array.shape) period = 5 * (i + 1) fx = np.sin(2 * math.pi / period * x + 2 * math.pi / numBases * i) fy = np.sin(2 * math.pi / period * y + 2 * math.pi / numBases * i) array[x, y] = fx + fy bases.append(im) if display: mos = displayUtils.Mosaic(background=-10) ds9.mtv(mos.makeMosaic(bases), title="Basis functions", frame=1) inputs = [] for i in range(numInputs): im = afwImage.ImageF(afwGeom.Extent2I(width, height)) im.set(0) for b in bases: im.scaledPlus(random.random(), b) inputs.append(im) self.ImageSet.addImage(im, 1.0) if display: mos = displayUtils.Mosaic(background=-10) ds9.mtv(mos.makeMosaic(inputs), title="Inputs", frame=2) self.ImageSet.analyze() eImages = [] for img in self.ImageSet.getEigenImages(): eImages.append(img) if display: mos = displayUtils.Mosaic(background=-10) ds9.mtv(mos.makeMosaic(eImages), title="Eigenimages", frame=3) self.assertEqual(len(eImages), numInputs) # Test for orthogonality for i1, i2 in itertools.combinations(range(len(eImages)), 2): inner = afwImage.innerProduct(eImages[i1], eImages[i2]) norm1 = eImages[i1].getArray().sum() norm2 = eImages[i2].getArray().sum() inner /= norm1 * norm2 self.assertAlmostEqual(inner, 0)