def MVU_slack(datafile, dim=3): # takes in a pickled matrix of points - outputs a MVU embedding fp = open(datafile) pts = pickle.load(fp) ans = pickle.load(fp) # latent space coordinates size = len(pts) k = len(ans[0]) # the number of latent dimensions # mean center coordinates m = np.mean(pts, axis=0) pts = pts - m # TODO: move graph cluster algorithm to own file - write in C? # compute the distance matrix and cluster Y = hc.squareform(hc.pdist(pts, 'euclidean')) res = cluster_graph(Y, fnc='k', size=8) x, y = np.nonzero(res & (Y != 0)) # indices of nearest neighbors # generate data to write problem in SPDA format # TODO: add slack variable block indx = [] for (i, j) in zip(x, y): if i <= j: indx.append((i, j)) m = len(indx) + 1 nblocks = 2 c = [0.0] for (i, j) in indx: c.append(Y[i, j]**2) write_spda_file_slack("../ds/sdp.dat", m, nblocks, size, c, indx, .01) # TODO: add some error checking os.system("csdp ../ds/sdp.dat ../ds/sdp.sol") y, Z, X = read_sol_file_slack("../ds/sdp.sol", size) # spectral decomposition of the dual solution (X) u, s, v = la.svd(X) results = [] for i in range(dim): results.append(np.sqrt(s[i]) * u[:, i]) # returns the neighborhood graph for proper plotting return results, pts, res
def MVU_slack(datafile, dim = 3): # takes in a pickled matrix of points - outputs a MVU embedding fp = open(datafile) pts = pickle.load(fp) ans = pickle.load(fp) # latent space coordinates size = len(pts) k = len(ans[0]) # the number of latent dimensions # mean center coordinates m = np.mean(pts, axis=0) pts = pts - m # TODO: move graph cluster algorithm to own file - write in C? # compute the distance matrix and cluster Y = hc.squareform(hc.pdist(pts,'euclidean')) res = cluster_graph(Y, fnc = 'k', size = 8) x,y = np.nonzero(res & (Y != 0)) # indices of nearest neighbors # generate data to write problem in SPDA format # TODO: add slack variable block indx = [] for (i,j) in zip(x,y): if i <= j: indx.append((i,j)) m = len(indx) + 1 nblocks = 2 c = [0.0] for (i,j) in indx: c.append(Y[i,j]**2) write_spda_file_slack("../ds/sdp.dat", m, nblocks, size, c, indx, .01) # TODO: add some error checking os.system("csdp ../ds/sdp.dat ../ds/sdp.sol") y,Z,X = read_sol_file_slack("../ds/sdp.sol", size) # spectral decomposition of the dual solution (X) u,s,v = la.svd(X) results = [] for i in range(dim): results.append(np.sqrt(s[i]) * u[:,i]) # returns the neighborhood graph for proper plotting return results, pts, res
def expand(N, k=None): X = np.zeros(N.shape) X[:, :] = np.inf X[N.nonzero()] = N[N.nonzero()] for i in range(X.shape[0]): X[i, i] = 0 X = shortest_paths(X) if k: I = cluster_graph(X, fnc='k', size=k) J = np.zeros(N.shape) J[I] = X[I] return J else: return X