def geodesic_patch(self,point,r,k=7,return_mask=False): """ Computes a geodesic patch around a specified point. Parameters ---------- point : int or (1,3) float array A mesh vertex. r : float Radius used to build patch. k : int, default is 7 Number of nearest neighbors to use. return_mask : boolean, default is False If True, return the patch as a (num,verts,) boolean array Returns ------- An int array containing the patch point indices. """ if np.any(self.knn_I) is None or np.any(self.knn_J) is None or np.any(self.knn_D) is None: self.knn_I,self.knn_J,self.knn_D = gl.knnsearch(self.points,20) I = self.knn_I[:,:k] J = self.knn_J[:,:k] D = self.knn_D[:,:k] W = gl.dist_matrix(I,J,D,k) W = gl.sparse_max(W,W.transpose()) point_ind = self.get_index(point) dist = gl.cDijkstra(W,np.array([point_ind]),np.array([0])) mask = dist < r if return_mask: return mask.astype(int) else: #return indices return np.arange(self.num_verts())[mask]
def build_graph(X, k=10): #Build graph I, J, D = gl.knnsearch(X, k) W = gl.weight_matrix(I, J, D, k) W = gl.diag_multiply(W, 0) if not gl.isconnected(W): print('Warning: Graph not connected') return W
def edge_points(self,u,k=7,return_mask=False,number=None): """ Computes the edge points of the mesh. Parameters ---------- u : (num_verts,1) int array Array of labels for each point. k : int, default is 7 Number of nearest neighbors to use. return_mask : boolean, default is False If True, return edge_points as a (num,verts,) boolean array. number : int, default is None Max number of edge points to return. Returns ------- An int array containing the edge point indices. """ if np.any(self.knn_I) is None or np.any(self.knn_J) is None or np.any(self.knn_D) is None: self.knn_I,self.knn_J,self.knn_D = gl.knnsearch(self.points,20) I = self.knn_I[:,:k] J = self.knn_J[:,:k] D = self.knn_D[:,:k] W = gl.weight_matrix(I,J,D,k,f=lambda x : np.ones_like(x),symmetrize=False) d = gl.degrees(W) mask = d*u != W@u #Select a few points spaced out along edge if number is not None: edge_ind = np.arange(self.num_verts())[mask] edge_points = self.points[mask,:] num_edge_points = len(edge_points) #PCA mean = np.mean(edge_points,axis=0) cov = (edge_points-mean).T@(edge_points-mean) l,v = sparse.linalg.eigs(cov,k=1,which='LM') proj = (edge_points-mean)@v.real #Sort along princpal axis sort_ind = np.argsort(proj.flatten()) dx = (num_edge_points-1)/(number-1) spaced_edge_ind = edge_ind[sort_ind[np.arange(0,num_edge_points,dx).astype(int)]] mask = np.zeros(self.num_verts(),dtype=bool) mask[spaced_edge_ind]=True if return_mask: return mask.astype(int) else: #return indices return np.arange(self.num_verts())[mask]
#Demonstration of semi-supervised learning import numpy as np import graphlearning as gl import matplotlib.pyplot as plt import sklearn.datasets as datasets #data n = 500 X, L = datasets.make_moons(n_samples=n, noise=0.1) #Build graph k = 10 I, J, D = gl.knnsearch(X, k) W = gl.weight_matrix(I, J, D, k) #Randomly choose labels m = 5 #5 labels per class ind = gl.randomize_labels(L, m) #indices of labeled points #Semi-supervised learning l = gl.graph_ssl(W, ind, L[ind], method='laplace') #Compute accuracy acc = gl.accuracy(l, L, m) print("Accuracy=%f" % acc) #Plot result (red points are labels) plt.scatter(X[:, 0], X[:, 1], c=l) plt.scatter(X[ind, 0], X[ind, 1], c='r') plt.show()
balance = True else: balance = False #allbreak contains all the categorical data of avaliable break curves allbreak = Loaddata(xls_name, variable_name) #cleandata is all the converted(numerical) data of break curves #classtype is a list that keeps all the class names cleandata, classtype = balancing_converting(allbreak, balance) cleandata, label = numerical_Label(cleandata, classtype) cleandata = np.array(cleandata) label = np.array(label) print(cleandata[0]) print(label[500:1000]) #Build graph k = 500 I, J, D = gl.knnsearch(cleandata, k) W = gl.weight_matrix(I, J, D, k) #Randomly choose labels m = 5 #5 labels per class ind = gl.randomize_labels(label, m) #indices of labeled points #Semi-supervised learning l = gl.graph_ssl(W, ind, label[ind], method='Laplace') #Compute accuracy acc = gl.accuracy(l, label, m) print("Accuracy=%f" % acc) ''' #xtrain,ytrain is training data, xtest contains label of bones, be careful xtrain,xtest,ytrain,ytest=split(cleandata,split_percentage)
def ComputeKNN(dataset, metric='L2', k=30, knn_method='annoy', scatter_pca_dims=100): if metric == "scatter_pca": outfile = "kNNData/"+dataset+"_"+metric + "_" + str(scatter_pca_dims) + ".npz" else: outfile = "kNNData/"+dataset+"_"+metric+".npz" #For variational autoencoder the vae data, e.g., Data/MNIST_vae.npz must exist. if metric[0:3]=='vae' or metric[0:3]=='aet': dataFile = "Data/"+dataset+"_"+metric+".npz" else: dataFile = "Data/"+dataset+"_raw.npz" #Try to Load data try: M = np.load(dataFile,allow_pickle=True) except: print('Cannot find '+dataFile+'.') sys.exit(2) data = M['data'] #Apply transformations (just scatter now, but others could be included) if metric == 'scatter' or metric == 'scatter_pca': if metric == 'scatter_pca' and scatter_pca_dims <= 300: #Changed to Data filePath = "Data/" + dataset + "_" + "scatter_pca" + ".npz" try: PCAfile = np.load(filePath) savedPCA = PCAfile['savedPCA'] except: print("File not found: " + filePath) print("Recomputing " + filePath) m = int(np.sqrt(data.shape[1])) # number of pixels across image (assuming square) Y = gl.scattering_transform(data, m, m) print("Computing PCA...") pca = PCA(n_components=300) savedPCA = pca.fit_transform(Y) np.savez_compressed(filePath, savedPCA=savedPCA) pca = PCA(n_components=scatter_pca_dims) data = pca.fit_transform(savedPCA) else: print("Computing scattering transform...") m = int(np.sqrt(data.shape[1])) data = gl.scattering_transform(data, m, m) #Perform kNN search if knn_method == 'annoy': if metric in ['angular', 'manhattan', 'hamming', 'dot']: similarity = metric else: similarity = 'euclidean' if metric[0:3] == 'aet': similarity = 'angular' # Similarity can be "angular", "euclidean", "manhattan", "hamming", or "dot". I,J,D = gl.knnsearch_annoy(data,k, similarity) elif knn_method == 'exact': I,J,D = gl.knnsearch(data,k) else: print('Invalid kNN method.') return #Save kNN results to file np.savez_compressed(outfile,I=I,J=J,D=D)