def plot_bumps_on_data(X, bumps, palette='Set1'): plot_data_embedded(X, palette='w') colors = sns.color_palette(palette, n_colors=len(bumps)) colors = [mpl_colors.to_hex(c) for c in colors] np.random.shuffle(colors) for i, (b, c) in enumerate(zip(bumps, colors)): alpha = np.maximum(b, 0) / b.max() plot_data_embedded(X, palette=c, alpha=alpha)
def process_curves(): with open('bunny_processed.pickle', mode='r+b') as f: bunny_dict = pickle.load(f) circle_clustered_dict = pickle.load(f) plt.figure(figsize=(10, 6), tight_layout=True) gs = gridspec.GridSpec(3, 6) bunny_idx = [0, 40, 80, 174, 524, 699] # bunny_idx = np.linspace(0, len(bunny_dict) - 1, num=5, endpoint=True, # dtype=np.int) for i, idx in enumerate(bunny_idx): print(idx) curve = bunny_dict[idx] Y = sdp_km_burer_monteiro(curve, 20, rank=len(curve), tol=1e-6, maxiter=5000, verbose=True) Q = Y.dot(Y.T) labels = np.arange(len(curve)) ax = plt.subplot(gs[0, i]) plot_data_embedded(curve, s=2, ax=ax) ax = plt.subplot(gs[1, i]) plot_matrix(Q, labels=labels, labels_palette='hls', ax=ax) ax = plt.subplot(gs[2, i]) plot_bumps_1d(Y, subsampling=10, labels=labels, labels_palette='hls', ax=ax) plt.savefig(dir_name + 'bunny_deformation.pdf', dpi=300) plt.show() plt.figure(figsize=(10, 6), tight_layout=True) gs = gridspec.GridSpec(3, 6) circle_idx = [1, 3, 5, 7, 10, 199] # circle_idx = np.linspace(1, len(circle_clustered_dict), num=5, # endpoint=True, dtype=np.int) for i, idx in enumerate(circle_idx): print(idx) curve = circle_clustered_dict[idx] Y = sdp_km_burer_monteiro(curve, 4, rank=len(curve), tol=1e-6, maxiter=5000, verbose=True) Q = Y.dot(Y.T) labels = np.arange(len(curve)) ax = plt.subplot(gs[0, i]) plot_data_embedded(curve, s=2, ax=ax) ax = plt.subplot(gs[1, i]) plot_matrix(Q, labels=labels, labels_palette='hls', ax=ax) ax = plt.subplot(gs[2, i]) plot_bumps_1d(Y, subsampling=15, labels=labels, labels_palette='hls', ax=ax) plt.savefig(dir_name + 'circle_deformation.pdf', dpi=300) plt.show()
def plot_bumps_on_data(X, bumps, palette='Set1', ax=None): if ax is None: ax = plt.gca() plot_data_embedded(X, palette='w', edgecolors='k', ax=ax) colors = sns.color_palette(palette, n_colors=len(bumps)) colors = [mpl_colors.to_hex(c) for c in colors] np.random.shuffle(colors) for i, (b, c) in enumerate(zip(bumps, colors)): alpha = np.maximum(b, 0) / b.max() plot_data_embedded(X, palette=c, alpha=alpha, edgecolors='none', ax=ax)
def plot_bumps_on_data(X, bumps, palette='Set1', ax=None): if ax is None: ax = plt.gca() # Plot the sphere u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) ax.plot_surface(0.99 * np.outer(np.cos(u), np.sin(v)), 0.99 * np.outer(np.sin(u), np.sin(v)), 0.99 * np.outer(np.ones(np.size(u)), np.cos(v)), color='w', edgecolors='#AAAAAA', alpha=1) plot_data_embedded(X, palette='w', edgecolors='k', ax=ax) colors = sns.color_palette(palette, n_colors=len(bumps)) colors = [mpl_colors.to_hex(c) for c in colors] for i, (b, c) in enumerate(zip(bumps, colors)): alpha = np.maximum(b, 0) / b.max() plot_data_embedded(X, palette=c, alpha=alpha, edgecolors='none', ax=ax)
def test_grid(n_clusters=16, use_copositive=False): X = np.mgrid[0:16, 0:16] X = X.reshape((len(X), -1)).T labels = np.arange(len(X)) # X_norm = X - np.mean(X, axis=0) # cov = X_norm.T.dot(X_norm) # X_norm /= np.trace(cov.dot(cov)) ** 0.25 # # alpha = 0.001 # plt.matshow(np.maximum(X_norm.dot(X_norm.T) - alpha, 0), cmap='gray_r') # # from scipy.spatial.distance import pdist, squareform # plt.matshow(squareform(pdist(X)), cmap='gray_r') # # return rank = len(X) print(rank) if use_copositive: beta = n_clusters / len(X) Y = copositive_burer_monteiro(X, alpha=0.003, beta=beta, rank=rank, tol=1e-5, constraint_tol=1e-5, verbose=True) name = 'grid_copositive_bm' else: Y = sdp_km_burer_monteiro(X, n_clusters, rank=rank, tol=1e-6, verbose=True) name = 'grid_sdpkm_bm' Q = Y.dot(Y.T) idx = np.argsort(np.argmax(Y, axis=0)) Y = Y[:, idx] sns.set_style('white') plt.figure(figsize=(12, 4.7), tight_layout=True) gs = gridspec.GridSpec(1, 3) ax = plt.subplot(gs[0]) plot_data_embedded(X, palette='hls', ax=ax) plt_title = ax.set_title('Input dataset', fontsize='xx-large') # plt_title.set_position((0.5, 1.07)) ax = plt.subplot(gs[1]) plot_matrix(Q, ax=ax, labels=labels, which_labels='both', labels_palette='hls') plt_title = ax.set_title(r'$\mathbf{Q}$', fontsize='xx-large') plt_title.set_position((0.5, 1.07)) ax = plt.subplot(gs[2]) plot_matrix(Y, ax=ax, labels=labels, which_labels='vertical', labels_palette='hls') plt_title = ax.set_title(r'$\mathbf{Y}^\top$', fontsize='xx-large') plt_title.set_position((0.5, 1.07)) plt.savefig('{}{}.pdf'.format(dir_name, name)) pdf_file_name = '{}{}_plot_{}_on_data_{}{}' for i in range(Y.shape[1]): plt.figure() plot_bumps_on_data(X, [Y[:, i]]) plt.savefig(pdf_file_name.format(dir_name, name, 'Y', i, '.png'), dpi=300, bbox_inches='tight') plt.close() pdf_file_name = '{}{}_plot_{}_on_data_{}' plt.figure() bumps_locs = np.random.random_integers(Y.shape[1], size=6) plot_bumps_on_data(X, [Y[:, i] for i in bumps_locs], palette='Set1') plt.savefig(pdf_file_name.format(dir_name, name, 'Y', 'multiple.png'), dpi=300, bbox_inches='tight') Y_aligned = align_bumps(Y, Y.shape[1] // 2) _, ax = plt.subplots(1, 1) plot_matrix(Y_aligned, ax=ax) plt_title = ax.set_title(r'Aligned $\mathbf{Y}^\top$', fontsize='xx-large') plt_title.set_position((0.5, 1.07)) plt.savefig('{}{}_Y_aligned_2d.pdf'.format(dir_name, name)) _, ax = plt.subplots(1, 1) ax.plot(Y_aligned) ax.set_xticks([]) ax.set_yticks([]) ax.set_title(r'Receptive fields', fontsize='xx-large') plt.savefig('{}{}Y_aligned_1d.pdf'.format(dir_name, name)) pos = np.arange(len(Y)) median = np.median(Y_aligned, axis=1) mu = np.mean(Y_aligned, axis=1) sigma = np.std(Y_aligned, axis=1) _, ax = plt.subplots(1, 1) plt_mean = ax.plot(pos, mu, color='#377eb8') ax.fill_between(pos, np.maximum(mu - 3 * sigma, 0), mu + 3 * sigma, alpha=0.3, color='#377eb8') plt_median = ax.plot(pos, median, '-.', color='#e41a1c') ax.set_xticks([]) ax.set_yticks([]) plt_aux = ax.fill(np.NaN, np.NaN, '#377eb8', alpha=0.3, linewidth=0) ax.legend([(plt_mean[0], plt_aux[0]), plt_median[0]], [r'Mean $\pm$ 3 STD', 'Median'], loc='upper left', fontsize='xx-large') ax.set_title(r'Receptive fields summary', fontsize='xx-large') plt.savefig('{}{}Y_aligned_1d_summary.pdf'.format(dir_name, name))
def test_toy_embedding(X, Y, target_dim, filename, palette='hls', elev_azim=None): print('--------\n', filename) embedding = spectral_embedding(Y, target_dim=target_dim, gramian=False, discard_first=False) D = dot_matrix(X) Q = Y.dot(Y.T) labels = np.arange(len(X)) sns.set_style('white') plt.figure(figsize=(20, 5), tight_layout=True) gs = gridspec.GridSpec(1, 5) if X.shape[1] == 3: ax = plt.subplot(gs[0], projection='3d') else: ax = plt.subplot(gs[0]) plot_data_embedded(X, ax=ax, palette=palette, elev_azim=elev_azim) ax.set_title('Input dataset', fontsize='xx-large') titles = [ r'Input Gramian $\mathbf{X}^\top \mathbf{X}$', r'$\mathbf{Y}^\top \mathbf{Y}$' ] for i, (M, t) in enumerate(zip([D, Q], titles)): ax = plt.subplot(gs[i + 1]) plot_matrix(M, ax=ax, labels=labels, which_labels='both', labels_palette=palette, colorbar_labelsize=15) plt_title = ax.set_title(t, fontsize=25) plt_title.set_position((0.5, 1.07)) ax = plt.subplot(gs[3]) plot_matrix(Y, ax=ax, labels=labels, which_labels='vertical', labels_palette=palette, colorbar_labelsize=15) title_y = r'$\mathbf{Y}^\top$ ($' plt_title = ax.set_title(title_y, fontsize=25) plt_title.set_position((0.5, 1.07)) if target_dim == 2: ax = plt.subplot(gs[4]) if target_dim == 3: ax = plt.subplot(gs[4], projection='3d') plot_data_embedded(embedding, ax=ax, palette=palette) ax.set_title('2D embedding', fontsize=25) plt.savefig('{}{}.pdf'.format(dir_name, filename), dpi=300) fig = plt.figure() if target_dim == 2: ax = fig.add_subplot(111) if target_dim == 3: ax = fig.add_subplot(111, projection='3d') plot_data_embedded(embedding, ax=ax, palette=palette) plt.savefig('{}{}_embedding.pdf'.format(dir_name, filename), dpi=300) # pdf_file_name = '{}{}_plot_{}_on_data_{}{}' # for i in range(Y.shape[1]): # plt.figure() # plot_bumps_on_data(embedding, [Y[:, i]]) # plt.savefig(pdf_file_name.format(dir_name, filename, 'Y', i, '.png'), # dpi=300) # plt.close() pdf_file_name = '{}{}_plot_{}_on_data_{}' plt.figure() if target_dim == 2: ax = fig.add_subplot(111) if target_dim == 3: ax = fig.add_subplot(111, projection='3d') plot_bumps_on_data(embedding, [Y[:, i] for i in range(0, Y.shape[1], 10)], ax=ax) plt.savefig(pdf_file_name.format(dir_name, filename, 'Y', 'multiple.pdf'), dpi=300) pdf_file_name = '{}{}_plot_{}_1d{}' _, ax = plt.subplots(1, 1) plot_bumps_1d(Y, subsampling=10, labels=labels, ax=ax) ax.set_yticks([]) ax.set_title(r'Rows of $\mathbf{Y}$', fontsize=25) plt.savefig(pdf_file_name.format(dir_name, filename, 'Y', '.pdf'), dpi=300)
def test_one_circle(n_clusters=25, use_copositive=False): np.random.seed(10) X = sphere.generate_sphere_grid() print(X.shape) labels = np.arange(len(X)) rank = 4 * n_clusters print(rank) if use_copositive: beta = n_clusters / len(X) Y = copositive_burer_monteiro(X, alpha=0.0048, beta=beta, rank=rank, tol=1e-6, constraint_tol=1e-6, verbose=True) name = 'sphere_copositive_bm' else: Y = sdp_km_burer_monteiro(X, n_clusters, rank=rank, tol=1e-5, verbose=True) name = 'sphere_sdpkm_bm' Q = Y.dot(Y.T) idx = np.argsort(np.argmax(Y, axis=0)) Y = Y[:, idx] Y_aligned = align_bumps(Y, Y.shape[1] // 2) scipy.io.savemat(name + '.mat', {'X': X, 'Y': Y}) sns.set_style('white') plt.figure(figsize=(12, 5), tight_layout=True) gs = gridspec.GridSpec(1, 3) ax = plt.subplot(gs[0], projection='3d') plot_data_embedded(X, palette='hls', ax=ax) ax.set_title('Input dataset', fontsize='xx-large') ax = plt.subplot(gs[1]) plot_matrix(Q, ax=ax, labels=labels, which_labels='both', labels_palette='hls') plt_title = ax.set_title(r'$\mathbf{Q}$', fontsize='xx-large') plt_title.set_position((0.5, 1.07)) ax = plt.subplot(gs[2]) plot_matrix(Y, ax=ax, labels=labels, which_labels='vertical', labels_palette='hls') plt_title = ax.set_title(r'$\mathbf{Y}^\top$', fontsize='xx-large') plt_title.set_position((0.5, 1.07)) plt.savefig('{}{}.pdf'.format(dir_name, name), dpi=300)
def test_one_circle(X, Y, name, bump_subsampling=20, cos_freq=0.13, cos_loc=None): labels = np.arange(len(X)) Q = Y.dot(Y.T) idx = np.argsort(np.argmax(Y, axis=0)) Y = Y[:, idx] Y_aligned = align_bumps(Y, Y.shape[1] // 2) sns.set_style('white') plt.figure(figsize=(12, 5), tight_layout=True) gs = gridspec.GridSpec(1, 3) ax = plt.subplot(gs[0]) plot_data_embedded(X, palette='hls', ax=ax) ax.set_title('Input dataset', fontsize=30) ax = plt.subplot(gs[1]) plot_matrix(Q, ax=ax, labels=labels, which_labels='both', labels_palette='hls', colorbar_labelsize=15) plt_title = ax.set_title(r'$\mathbf{Y}^\top \mathbf{Y}$', fontsize=25) plt_title.set_position((0.5, 1.07)) ax = plt.subplot(gs[2]) plot_matrix(Y, ax=ax, labels=labels, which_labels='vertical', labels_palette='hls', colorbar_labelsize=15) plt_title = ax.set_title(r'$\mathbf{Y}^\top$', fontsize=25) plt_title.set_position((0.5, 1.07)) plt.savefig('{}{}.pdf'.format(dir_name, name), dpi=300) # pdf_file_name = '{}{}_plot_{}_on_data_{}{}' # for i in range(Y.shape[1]): # plt.figure() # plot_bumps_on_data(X, [Y[:, i]]) # plt.savefig(pdf_file_name.format(dir_name, name, 'Y', i, '.png'), # dpi=300) # plt.close() pdf_file_name = '{}{}_plot_{}_on_data_{}' plt.figure() bumps_locs = np.linspace(0, Y.shape[1], num=6, endpoint=False, dtype=np.int) plot_bumps_on_data(X, [Y[:, i] for i in bumps_locs], palette='Set1') plt.savefig(pdf_file_name.format(dir_name, name, 'Y', 'multiple.png'), dpi=300) _, ax = plt.subplots(1, 1) plot_matrix(Y_aligned, ax=ax) plt_title = ax.set_title('Aligned receptive fields', fontsize=25) plt_title.set_position((0.5, 1.07)) plt.savefig('{}{}_Y_aligned_2d.pdf'.format(dir_name, name), dpi=300) _, ax = plt.subplots(1, 1) plot_bumps_1d(Y, labels=labels, ax=ax, subsampling=bump_subsampling) ax.set_title('Receptive fields', fontsize=25) plt.savefig('{}{}Y_1d.pdf'.format(dir_name, name), dpi=300) _, ax = plt.subplots(1, 1) ax.plot(Y_aligned) ax.set_xticks([]) ax.set_yticks([]) ax.set_title('Aligned receptive fields', fontsize=25) plt.savefig('{}{}Y_aligned_1d.pdf'.format(dir_name, name), dpi=300) pos = np.arange(len(Y)) # median = np.median(Y_aligned, axis=1) mu = np.mean(Y_aligned, axis=1) sigma = np.std(Y_aligned, axis=1) def cosine(x): peak = np.argmax(mu) if cos_loc is None: cloc = np.mean(np.where(mu == mu[peak])[0]) else: cloc = cos_loc cos_bump = mu[peak] * np.cos(cos_freq * (x - cloc)) cos_bump[mu == 0] = 0 cos_bump[cos_bump < 0] = 0 return cos_bump _, ax = plt.subplots(1, 1) plt_mean = ax.plot(pos, mu, color='#377eb8') ax.fill_between(pos, np.maximum(mu - 3 * sigma, 0), mu + 3 * sigma, alpha=0.3, color='#377eb8') plt_cosine = ax.plot(pos, cosine(pos), '-.', color='#e41a1c') ax.set_xticks([]) ax.set_yticks([]) plt_aux = ax.fill(np.NaN, np.NaN, '#377eb8', alpha=0.3, linewidth=0) ax.legend([(plt_mean[0], plt_aux[0]), plt_cosine[0]], [r'Mean $\pm$ 3 STD', 'Truncated cosine'], loc='center left', fontsize=25) ax.set_title(r'Receptive fields summary', fontsize=25) plt.savefig('{}{}Y_aligned_1d_summary.pdf'.format(dir_name, name), dpi=300)