def plot_roll(W, X, colors, embed_dim, image_name): fig, (ax1, ax2) = pyplot.subplots(ncols=2) show_neighbor_graph(X[:, (0, 2)], W, ax=ax1, vertex_colors=colors, vertex_edgecolor='k', edge_style='k-') # show the embedding D = pairwise_distances(X, metric='euclidean') D[~W.astype(bool)] = 0 embed = isomap(D, embed_dim) scatterplot(embed, ax=ax2, c=colors, marker='o', edgecolor='k') for ax in (ax1, ax2): ax.tick_params(which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off') fig.tight_layout() savefig(image_name)
def plot_roll(W, X, colors, embed_dim, image_name): fig, (ax1,ax2) = pyplot.subplots(ncols=2) show_neighbor_graph(X[:,(0,2)], W, ax=ax1, vertex_colors=colors, vertex_edgecolor='k', edge_style='k-') # show the embedding D = pairwise_distances(X, metric='euclidean') D[~W.astype(bool)] = 0 embed = isomap(D, embed_dim) scatterplot(embed, ax=ax2, c=colors, marker='o', edgecolor='k') for ax in (ax1,ax2): ax.tick_params(which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off') fig.tight_layout() savefig(image_name)
def show_skeleton_issue(): t = np.linspace(0,4,25)[:,None] X = np.hstack((np.cos(t), np.random.uniform(-1,1,t.shape), np.sin(t))) GT = np.hstack((t, X[:,1:2])) W = neighbor_graph(X, k=1, symmetrize=False) W = grow_trees(X, W, 2) labels = join_CCs_simple(X, W) # switch up the CC order for better contrast between groups order = np.arange(labels.max()+1) np.random.shuffle(order) labels = order[labels] show_neighbor_graph(GT, W, vertex_style=None, edge_style='k-') ax = pyplot.gca() for l,marker in zip(np.unique(labels), "osD^v><"): scatterplot(GT[labels==l], marker, ax=ax, edgecolor='k', c='white') ax.tick_params(which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off') savefig('skeleton.png')
def show_skeleton_issue(): t = np.linspace(0, 4, 25)[:, None] X = np.hstack((np.cos(t), np.random.uniform(-1, 1, t.shape), np.sin(t))) GT = np.hstack((t, X[:, 1:2])) W = neighbor_graph(X, k=1, symmetrize=False) W = grow_trees(X, W, 2) labels = join_CCs_simple(X, W) # switch up the CC order for better contrast between groups order = np.arange(labels.max() + 1) np.random.shuffle(order) labels = order[labels] show_neighbor_graph(GT, W, vertex_style=None, edge_style='k-') ax = pyplot.gca() for l, marker in zip(np.unique(labels), "osD^v><"): scatterplot(GT[labels == l], marker, ax=ax, edgecolor='k', c='white') ax.tick_params(which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off') savefig('skeleton.png')
W = neighbor_graph(X=X, k=knn) corr = Correspondence(matrix=W) with Timer('LapEig'): le_embed = lapeig(W=W, num_vecs=out_dim) with Timer('Linear LapEig'): # lapeig_linear returns a projector, not an embedding lel_embed = np.dot(X, lapeig_linear(X=X, W=W, num_vecs=out_dim, k=knn)) with Timer('Isomap'): im_embed = isomap(X=X, num_vecs=out_dim, k=knn) with Timer('LLE'): lle_embed = lle(X=X, num_vecs=out_dim, k=knn) with Timer('SFA'): sfa_embed = np.dot(X, slow_features(X=X, num_vecs=out_dim)) show_neighbor_graph(X, corr, 'Original space') fig, axes = pyplot.subplots(nrows=3, ncols=2) fig.tight_layout() # spaces the subplots better show_neighbor_graph(le_embed, corr, 'Laplacian Eigenmaps', ax=axes[0, 0]) show_neighbor_graph(lel_embed, corr, 'Linear Laplacian Eigenmaps', ax=axes[0, 1]) show_neighbor_graph(im_embed, corr, 'Isomap', ax=axes[1, 0]) show_neighbor_graph(lle_embed, corr, 'Locally Linear Embedding', ax=axes[1, 1]) show_neighbor_graph(sfa_embed, corr,
warp_inds[i] = P[j, 1] return A[warp_inds] def _bound_row(self): P = self.pairs() n = P.shape[0] B = np.zeros((P[-1, 0] + 1, 2), dtype=np.int) head = 0 while head < n: i = P[head, 0] tail = head + 1 while tail < n and P[tail, 0] == i: tail += 1 B[i, :] = P[(head, tail - 1), 1] head = tail return B if __name__ == '__main__': # simple sanity-check tests from neighborhood import neighbor_graph from viz import show_neighbor_graph, pyplot n = 500 data = np.random.uniform(-1, 1, (n, 2)) corr_k = Correspondence(matrix=neighbor_graph(data, k=3)) corr_eps = Correspondence(matrix=neighbor_graph(data, epsilon=0.01)) pyplot.subplot(1, 2, 1) show_neighbor_graph(data, corr_k, 'kNN graph, k = 3') pyplot.subplot(1, 2, 2) show_neighbor_graph(data, corr_eps, '$\epsilon$-ball graph, $\epsilon$ = 0.1')()
warp_inds[i] = P[j,1] return A[warp_inds] def _bound_row(self): P = self.pairs() n = P.shape[0] B = np.zeros((P[-1,0]+1,2),dtype=np.int) head = 0 while head < n: i = P[head,0] tail = head+1 while tail < n and P[tail,0] == i: tail += 1 B[i,:] = P[(head,tail-1),1] head = tail return B if __name__ == '__main__': # simple sanity-check tests from neighborhood import neighbor_graph from viz import show_neighbor_graph, pyplot n = 500 data = np.random.uniform(-1,1,(n,2)) corr_k = Correspondence(matrix=neighbor_graph(data,k=3)) corr_eps = Correspondence(matrix=neighbor_graph(data,epsilon=0.01)) pyplot.subplot(1,2,1) show_neighbor_graph(data,corr_k,'kNN graph, k = 3') pyplot.subplot(1,2,2) show_neighbor_graph(data, corr_eps, '$\epsilon$-ball graph, $\epsilon$ = 0.1')()
n = 300 knn = 5 out_dim = 2 X = cylinder(np.linspace(0, 4, n)) W = neighbor_graph(X=X, k=knn) corr = Correspondence(matrix=W) with Timer("LapEig"): le_embed = lapeig(W=W, num_vecs=out_dim) with Timer("Linear LapEig"): # lapeig_linear returns a projector, not an embedding lel_embed = np.dot(X, lapeig_linear(X=X, W=W, num_vecs=out_dim, k=knn)) with Timer("Isomap"): im_embed = isomap(X=X, num_vecs=out_dim, k=knn) with Timer("LLE"): lle_embed = lle(X=X, num_vecs=out_dim, k=knn) with Timer("SFA"): sfa_embed = np.dot(X, slow_features(X=X, num_vecs=out_dim)) show_neighbor_graph(X, corr, "Original space") fig, axes = pyplot.subplots(nrows=3, ncols=2) fig.tight_layout() # spaces the subplots better show_neighbor_graph(le_embed, corr, "Laplacian Eigenmaps", ax=axes[0, 0]) show_neighbor_graph(lel_embed, corr, "Linear Laplacian Eigenmaps", ax=axes[0, 1]) show_neighbor_graph(im_embed, corr, "Isomap", ax=axes[1, 0]) show_neighbor_graph(lle_embed, corr, "Locally Linear Embedding", ax=axes[1, 1]) show_neighbor_graph(sfa_embed, corr, "Slow Features Embedding", ax=axes[2, 0]) pyplot.show()