def testInnerProducts(self): """Test inner products""" width, height = 10, 20 im1 = afwImage.ImageF(lsst.geom.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[0, 0, afwImage.LOCAL] = 0 self.assertEqual(afwImage.innerProduct(im1, im2), (width*height - 1)*val1*val2) im2[0, 0, afwImage.LOCAL] = val2 # reinstate value im2[width - 1, height - 1, afwImage.LOCAL] = 1 self.assertEqual(afwImage.innerProduct(im1, im2), (width*height - 1)*val1*val2 + val1)
def testPca(self): """Test calculating PCA""" 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 = afwDisplay.utils.Mosaic(background=-10) afwDisplay.Display(frame=1).mtv(mos.makeMosaic(bases), title="Basis functions") inputs = [] for i in range(numInputs): im = afwImage.ImageF(lsst.geom.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 = afwDisplay.utils.Mosaic(background=-10) afwDisplay.Display(frame=2).mtv(mos.makeMosaic(inputs), title="Inputs") self.ImageSet.analyze() eImages = [] for img in self.ImageSet.getEigenImages(): eImages.append(img) if display: mos = afwDisplay.utils.Mosaic(background=-10) afwDisplay.Display(frame=3).mtv(mos.makeMosaic(eImages), title="EigenImages") self.assertEqual(len(eImages), numInputs) # Test for orthogonality for i1, i2 in itertools.combinations(list(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)
def testImagePca(self): # Test out the ImagePca behavior kc1 = self.makeCandidate(1, 0.0, 0.0) kc1.build(self.kList) kc2 = self.makeCandidate(2, 0.0, 0.0) kc2.build(self.kList) kc3 = self.makeCandidate(3, 0.0, 0.0) kc3.build(self.kList) imagePca = ipDiffim.KernelPcaD() kpv = ipDiffim.KernelPcaVisitorF(imagePca) kpv.processCandidate(kc1) kpv.processCandidate(kc2) kpv.processCandidate(kc3) imagePca.analyze() eigenImages = imagePca.getEigenImages() # NOTE : this needs to be changed once ticket #1649 is resolved for i in range(len(eigenImages)): for j in range(i, len(eigenImages)): print i, j, afwImage.innerProduct(eigenImages[i], eigenImages[j])