Exemplo n.º 1
0
 def dimreduce_test(self):
     # Check to see if intrinsically 2D data
     # can be transformed to 2D and back exactly
     p = PCA(dim=2, eps=0.)
     new = p.fit_transform(self.data)
     new = p.inv_transform(new)
     assert np.allclose(new,self.data)
Exemplo n.º 2
0
 def transform_test(self):
     # Check to see if data can 
     # be transformed and inverse transformed exactly
     p = PCA(eps=0.)
     new = p.fit_transform(self.data)
     new = p.inv_transform(new)
     assert np.allclose(new,self.data)
Exemplo n.º 3
0
 def transform_zca_whiten_test(self):
     # Check to see if data can 
     # be transformed and inverse transformed exactly
     p = PCA(whiten=True, eps=0.)
     data = self.rng.randn(100,10)
     p.fit(data)
     new = p.transform_zca(data)
     old = p.inv_transform_zca(new)
     assert np.allclose(old,data)
Exemplo n.º 4
0
def get_length_from_list( image_sequence ):
    """ Function to get PCs of given image sequence """
    N, D = len(image_sequence), len(sitk.GetArrayFromImage( image_sequence[0] ).ravel())
    X = np.zeros([D,N])
    for n in range(N):
        X[:,n] = sitk.GetArrayFromImage( image_sequence[n] ).ravel()
    ipca = PCA( X.T, np.eye(N), fraction=fraction, ptype='dual' )
    iZ = ipca.transform()
    length = 0
    for n in range(N-1):
        length += np.linalg.norm( iZ[n,:]-iZ[n+1,:] )
    del X, ipca
    return length
Exemplo n.º 5
0
    def train(self):
        self.construct_graph()
        tf.global_variables_initializer().run()

        for _ in range(self.epoch):
            x, y = self.shuffle_data(self.x_train, self.y_train)
            for i in range(self.x_train.shape[0] // self.batch_size):
                x_batch = x[i * self.batch_size:(i + 1) * self.batch_size]
                y_batch = y[i * self.batch_size:(i + 1) * self.batch_size]
                feed_dict = {self.x: x_batch, self.y: y_batch}
                self.sess.run(self.opt, feed_dict=feed_dict)

        b, w = self.sess.run([self.b, self.W])
        self.coeff = np.concatenate((b.reshape((-1, 1)), w), axis=0)

        PCA.save(self)
Exemplo n.º 6
0
def get_pca_distance( tseq, wseq, mask, perimeter, vis ):
    """ Function to compute the distance between PCA manifolds """
#==============================================================================
#   Create matrices to store the sequences  
#==============================================================================
    n_t, n_w, D = len(tseq), len(wseq), len(sitk.GetArrayFromImage( tseq[0] ).ravel())
    tX, wX = np.zeros([D,n_t]), np.zeros([D,n_w])
    for n in range(n_t):
        tX[:,n] = sitk.GetArrayFromImage( tseq[n] ).ravel()
    for n in range(n_w):
        wX[:,n] = sitk.GetArrayFromImage( wseq[n] ).ravel()
#==============================================================================
#   Reduce dimensionality of target sequence  
#==============================================================================
    tpca = PCA( tX[mask,:].T, np.eye(n_t), fraction=fraction, ptype='dual' )
    tZ = tpca.transform()
    U = np.dot( np.dot( tpca.psi, tpca.eigVec ), \
        np.linalg.inv(np.diag(np.sqrt( tpca.eigVal )) ) )
#==============================================================================
#   Project warped sequence onto reduced subspace of target  
#==============================================================================
    wpca = PCA( wX[mask,:].T, np.eye(n_w), fraction=fraction, ptype='dual' )
    wtZ = np.real( np.dot( U.T, wpca.A ).T )
    length = 0
    for n in range(n_w-1):
        length += np.linalg.norm( wtZ[n,:]-wtZ[n+1,:] )
#==============================================================================
#   Find correspondences if sequences have different number of frames  
#==============================================================================
    n_vertices = np.max( [n_t, n_w])
    idx_t, idx_w = np.zeros( n_vertices, dtype = np.uint ),\
        np.zeros( n_vertices, dtype = np.uint )
    at, aw = np.linspace(0,1,num=n_t), np.linspace(0,1,num=n_w)
    if n_t>n_w:
        idx_t = np.arange( n_vertices )
        for ni in np.arange( n_vertices ):
            idx_w[ni] = find_nearest( aw, at[ni] )
    elif n_t<n_w:
        idx_w = np.arange( n_vertices )
        for ni in np.arange(n_vertices):
            idx_t[ni] = find_nearest( at, aw[ni] )
    else:
        idx_t = np.arange( n_vertices )
        idx_w = np.arange( n_vertices )
    return _GAMMA*np.sum( np.linalg.norm(wtZ[idx_w,:]-tZ[idx_t,:], axis=1) ) +\
        (1-_GAMMA)*(perimeter-length)
Exemplo n.º 7
0
 def change_test(self):
     # Check to see if functions touch original data
     # They should not
     data_init = np.copy(self.data)
     p = PCA(eps=0.)
     p.fit(self.data)
     assert np.allclose(self.data,data_init)
     p.fit_transform(self.data)
     assert np.allclose(self.data,data_init)
     p.inv_transform(self.data)
     assert np.allclose(self.data,data_init)
Exemplo n.º 8
0
def plot_in_2d(X, y=None, title=None, accuracy=None, legend_labels=None):

    cmap = plt.get_cmap('viridis')
    X_transformed = PCA().transform(X, 2)

    x1 = X_transformed[:, 0]
    x2 = X_transformed[:, 1]
    class_distr = []
    y = np.array(y).astype(int)

    colors = [cmap(i) for i in np.linspace(0, 1, len(np.unique(y)))]

    # Plot the different class distributions
    for i, l in enumerate(np.unique(y)):
        _x1 = x1[y == l]
        _x2 = x2[y == l]
        _y = y[y == l]
        class_distr.append(plt.scatter(_x1, _x2, color=colors[i]))

    # Plot legend
    if not legend_labels is None:
        plt.legend(class_distr, legend_labels, loc=1)

    # Plot title
    if title:
        if accuracy:
            perc = 100 * accuracy
            plt.suptitle(title)
            plt.title("Accuracy: %.1f%%" % perc, fontsize=10)
        else:
            plt.title(title)

    # Axis labels
    plt.xlabel('Principal Component 1')
    plt.ylabel('Principal Component 2')

    plt.show()
Exemplo n.º 9
0
@author: Eric
"""

from os import chdir
chdir("..")

import numpy as np
import matplotlib.pyplot as plt
import scipy.io
from pca.pca import PCA
import SAILnet

imfile = "patches.mat"
imname = "patches"

nullpca = PCA()
normalpca = PCA()
whiteningpca = PCA(whiten=True)

patches = scipy.io.loadmat(imfile)[imname]
origshape = patches.shape
veclength = origshape[0] * origshape[1]
nimages = origshape[2]

# unroll images
patches = patches.reshape((veclength, nimages))

scipy.io.savemat("unrolledpatches.mat", {"patches": patches})

# the PCA object wants each row to be a data point by default
patches = np.transpose(patches)
Exemplo n.º 10
0
 def ready_test(self):
     p = PCA(dim=2, eps=0.)
     assert p.ready == False
     new = p.fit(self.data)
     assert p.ready == True
Exemplo n.º 11
0
 def whiten_test(self):
     data = self.data+self.rng.rand(*self.data.shape)
     p = PCA(whiten=True, eps=0.)
     new = p.fit_transform(data)
     cov = new.T.dot(new)
     assert np.allclose(cov,np.eye(data.shape[1]))
import numpy as np
from pca.pca import PCA


def sd_normalize(mat: np.ndarray):
    sd = np.sqrt(np.var(mat, 0))
    return mat / sd


def eigen_normalize(mat, eigen, ep):
    return mat / np.array(np.sqrt(eigen[0:2] + ep))


if __name__ == '__main__':
    data = np.array([[5, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1], [0, 0, 1],
                     [1, 2, 1], [2, 3, 4], [0, 0, 1]])

    pca = PCA(data)
    out, eigen, vector = pca.fit()
    pca_whitening_mat = sd_normalize(out)
    pca_whitening_mat2 = eigen_normalize(out, eigen, 0.01)
    print(pca_whitening_mat)
    a = 1