def test_sparse_alpha_knn_graph(): data = datasets.make_swiss_roll()[0] k = 5 a = 0.45 thresh = 0.01 bandwidth_scale = 1.3 pdx = squareform(pdist(data, metric="euclidean")) knn_dist = np.partition(pdx, k, axis=1)[:, :k] epsilon = np.max(knn_dist, axis=1) * bandwidth_scale pdx = (pdx.T / epsilon).T K = np.exp(-1 * pdx**a) K = K + K.T W = np.divide(K, 2) np.fill_diagonal(W, 0) G = pygsp.graphs.Graph(W) G2 = build_graph( data, n_pca=None, # n_pca, decay=a, knn=k - 1, thresh=thresh, bandwidth_scale=bandwidth_scale, random_state=42, use_pygsp=True, ) assert np.abs(G.W - G2.W).max() < thresh assert G.N == G2.N assert isinstance(G2, graphtools.graphs.kNNGraph)
def test_knnmax(): data = datasets.make_swiss_roll()[0] k = 5 k_max = 10 a = 0.45 thresh = 0 with warnings.catch_warnings(): warnings.filterwarnings("ignore", "K should be symmetric", RuntimeWarning) G = build_graph( data, n_pca=None, # n_pca, decay=a, knn=k - 1, knn_max=k_max - 1, thresh=0, random_state=42, kernel_symm=None, ) assert np.all((G.K > 0).sum(axis=1) == k_max) pdx = squareform(pdist(data, metric="euclidean")) knn_dist = np.partition(pdx, k, axis=1)[:, :k] knn_max_dist = np.max(np.partition(pdx, k_max, axis=1)[:, :k_max], axis=1) epsilon = np.max(knn_dist, axis=1) pdx_scale = (pdx.T / epsilon).T K = np.where(pdx <= knn_max_dist[:, None], np.exp(-1 * pdx_scale**a), 0) K = K + K.T W = np.divide(K, 2) np.fill_diagonal(W, 0) G = pygsp.graphs.Graph(W) G2 = build_graph( data, n_pca=None, # n_pca, decay=a, knn=k - 1, knn_max=k_max - 1, thresh=0, random_state=42, use_pygsp=True, ) assert isinstance(G2, graphtools.graphs.kNNGraph) assert G.N == G2.N assert np.all(G.dw == G2.dw) assert (G.W - G2.W).nnz == 0
def test_thresh_small(): data = datasets.make_swiss_roll()[0] G = graphtools.Graph(data, thresh=1e-30) assert G.thresh == np.finfo("float").eps