示例#1
0
 def testComponentSeparation(self):
     A = generate_covsig([[10,5,2],[5,10,2],[2,2,10]], 500)
     B = generate_covsig([[10,2,2],[2,10,5],[2,5,10]], 500)
         
     X = np.dstack([A,B])
     W, V = csp(X,[1,2])        
     C1a = np.cov(X[:,:,0].dot(W).T)
     C2a = np.cov(X[:,:,1].dot(W).T)
     
     Y = np.dstack([B,A])
     W, V = csp(Y,[1,2])
     C1b = np.cov(Y[:,:,0].dot(W).T)
     C2b = np.cov(Y[:,:,1].dot(W).T)
     
     # check symmetric case
     self.assertTrue(np.allclose(C1a.diagonal(), C2a.diagonal()[::-1]))
     self.assertTrue(np.allclose(C1b.diagonal(), C2b.diagonal()[::-1]))
     
     # swapping class labels (or in this case, trials) should not change the result
     self.assertTrue(np.allclose(C1a, C1b))
     self.assertTrue(np.allclose(C2a, C2b))
     
     # variance of first component should be greatest for class 1
     self.assertTrue(C1a[0,0] > C2a[0,0])
     
     # variance of last component should be greatest for class 1
     self.assertTrue(C1a[2,2] < C2a[2,2])
     
     # variance of central component should be equal for both classes
     self.assertTrue(np.allclose(C1a[1,1], C2a[1,1]))
示例#2
0
文件: test_csp.py 项目: tntroger/scot
    def testComponentSeparation(self):
        A = generate_covsig([[10, 5, 2], [5, 10, 2], [2, 2, 10]], 500)
        B = generate_covsig([[10, 2, 2], [2, 10, 5], [2, 5, 10]], 500)

        X = np.concatenate([A[np.newaxis], B[np.newaxis]], axis=0)
        W, V = csp(X, [1, 2])
        C1a = np.cov(np.dot(W.T, X[0, :, :]))
        C2a = np.cov(np.dot(W.T, X[1, :, :]))

        Y = np.concatenate([B[np.newaxis], A[np.newaxis]], axis=0)
        W, V = csp(Y, [1, 2])
        C1b = np.cov(np.dot(W.T, Y[0, :, :]))
        C2b = np.cov(np.dot(W.T, Y[1, :, :]))

        # check symmetric case
        assert_allclose(C1a.diagonal(), C2a.diagonal()[::-1])
        assert_allclose(C1b.diagonal(), C2b.diagonal()[::-1])

        # swapping class labels (or in this case, trials) should not change the result
        assert_allclose(C1a, C1b, rtol=1e-9, atol=1e-9)
        assert_allclose(C2a, C2b, rtol=1e-9, atol=1e-9)

        # variance of first component should be greatest for class 1
        self.assertGreater(C1a[0, 0], C2a[0, 0])

        # variance of last component should be greatest for class 1
        self.assertLess(C1a[2, 2], C2a[2, 2])

        # variance of central component should be equal for both classes
        assert_allclose(C1a[1, 1], C2a[1, 1])
示例#3
0
文件: test_csp.py 项目: cbrnr/scot
    def testComponentSeparation(self):
        A = generate_covsig([[10, 5, 2], [5, 10, 2], [2, 2, 10]], 500)
        B = generate_covsig([[10, 2, 2], [2, 10, 5], [2, 5, 10]], 500)

        X = np.concatenate([A[np.newaxis], B[np.newaxis]], axis=0)
        W, V = csp(X, [1, 2])
        C1a = np.cov(np.dot(W.T, X[0, :, :]))
        C2a = np.cov(np.dot(W.T, X[1, :, :]))

        Y = np.concatenate([B[np.newaxis], A[np.newaxis]], axis=0)
        W, V = csp(Y, [1, 2])
        C1b = np.cov(np.dot(W.T, Y[0, :, :]))
        C2b = np.cov(np.dot(W.T, Y[1, :, :]))

        # check symmetric case
        assert_allclose(C1a.diagonal(), C2a.diagonal()[::-1])
        assert_allclose(C1b.diagonal(), C2b.diagonal()[::-1])

        # swapping class labels (or in this case, trials) should not change the result
        assert_allclose(C1a, C1b, rtol=1e-9, atol=1e-9)
        assert_allclose(C2a, C2b, rtol=1e-9, atol=1e-9)

        # variance of first component should be greatest for class 1
        self.assertGreater(C1a[0, 0], C2a[0, 0])

        # variance of last component should be greatest for class 1
        self.assertLess(C1a[2, 2], C2a[2, 2])

        # variance of central component should be equal for both classes
        assert_allclose(C1a[1, 1], C2a[1, 1])
示例#4
0
 def testSorting(self):
     """components should be sorted by decreasing variance
     """
     x = generate_covsig(np.diag([1, 9, 2, 6, 3, 8, 4, 5, 7]), 500)
     w, v = pca(x, reducedim=5)
     c = np.cov(np.dot(w.T, x))
     self.assertTrue(np.allclose(c, np.diag([9, 8, 7, 6, 5]), rtol=1e-1, atol=1e-2))
示例#5
0
 def testDecorrelation(self):
     """components should be decorrelated after PCA
     """
     x = generate_covsig([[3, 2, 1], [2, 3, 2], [1, 2, 3]], 500)
     w, v = pca(x)
     c = np.cov(np.dot(w.T, x))
     c -= np.diag(c.diagonal())
     self.assertTrue(np.allclose(c, np.zeros((3, 3)), rtol=1e-2, atol=1e-3))
示例#6
0
 def testIdentity(self):
     """identity covariance in -> identity covariance out
        test for up to 50 dimensions
     """
     for i in range(1, 50):
         x = generate_covsig(np.eye(i), 500)
         w, v = pca(x)
         c = np.cov(np.dot(w.T, x))
         self.assertTrue(np.allclose(c, np.eye(i)))
示例#7
0
 def testSorting(self):
     """components should be sorted by decreasing variance
     """
     x = generate_covsig(np.diag([1, 9, 2, 6, 3, 8, 4, 5, 7]), 500)
     w, v = pca(x, sort_components=True)
     c = np.cov(x.dot(w).T)
     self.assertTrue(np.allclose(c, np.diag([9, 8, 7, 6, 5, 4, 3, 2, 1]), rtol=1e-1, atol=1e-2))
     w, v = pca(x, sort_components=True)
     c = np.cov(x.dot(w).T)
     self.assertTrue(np.allclose(c, np.diag([9, 8, 7, 6, 5, 4, 3, 2, 1]), rtol=1e-1, atol=1e-2))
示例#8
0
 def testSorting(self):
     """components should be sorted by decreasing variance
     """
     x = generate_covsig(np.diag([1, 9, 2, 6, 3, 8, 4, 5, 7]), 500)
     w, v = pca(x, sort_components=True)
     c = np.cov(x.dot(w).T)
     self.assertTrue(np.allclose(c, np.diag([9, 8, 7, 6, 5, 4, 3, 2, 1]), rtol=1e-1, atol=1e-2))
     w, v = pca(x, sort_components=True)
     c = np.cov(x.dot(w).T)
     self.assertTrue(np.allclose(c, np.diag([9, 8, 7, 6, 5, 4, 3, 2, 1]), rtol=1e-1, atol=1e-2))