def test_knn_interpolate(): G = build_graph(data, decay=None) assert_raises(ValueError, G.interpolate, data) pca_data = PCA(2).fit_transform(data) transitions = G.extend_to_data(data) assert(np.all(G.interpolate(pca_data, Y=data) == G.interpolate(pca_data, transitions=transitions)))
def test_set_params(): G = graphtools.base.Data(data, n_pca=20) assert G.get_params() == {"n_pca": 20, "random_state": None} G.set_params(random_state=13) assert G.random_state == 13 assert_raises(ValueError, G.set_params, n_pca=10) G.set_params(n_pca=G.n_pca)
def test_transform_sparse_adaptive_pca(): G = build_graph(data, sparse=True, n_pca=True, random_state=42) assert np.all(G.data_nu == G.transform(G.data)) assert_raises(ValueError, G.transform, sp.csr_matrix(G.data)[:, 0]) assert_raises(ValueError, G.transform, sp.csr_matrix(G.data)[:, :15]) G2 = build_graph(data, sparse=True, n_pca=True, rank_threshold=G.rank_threshold, random_state=42) assert np.allclose(G2.data_nu, G2.transform(G2.data)) assert np.allclose(G2.data_nu, G.transform(G.data)) G3 = build_graph(data, sparse=True, n_pca=G2.n_pca, random_state=42) assert np.allclose(G3.data_nu, G3.transform(G3.data)) assert np.allclose(G3.data_nu, G2.transform(G2.data))
def test_inverse_transform_sparse_svd(): G = build_graph(data, sparse=True, n_pca=data.shape[1] - 1) np.testing.assert_allclose(data, G.inverse_transform(G.data_nu), atol=1e-12) np.testing.assert_allclose(data[:, -1, None], G.inverse_transform(G.data_nu, columns=-1), atol=1e-12) np.testing.assert_allclose(data[:, 5:7], G.inverse_transform(G.data_nu, columns=[5, 6]), atol=1e-12) assert_raises(IndexError, G.inverse_transform, G.data_nu, columns=data.shape[1]) assert_raises(TypeError, G.inverse_transform, sp.csr_matrix(G.data)[:, 0]) assert_raises(TypeError, G.inverse_transform, sp.csr_matrix(G.data)[:, :15]) assert_raises(ValueError, G.inverse_transform, data[:, 0]) assert_raises(ValueError, G.inverse_transform, data[:, :15])
def test_set_params(): X, sample_idx = generate_swiss_roll() G = build_graph( X, sample_idx=sample_idx, kernel_symm="mnn", theta=0.5, n_pca=None, thresh=1e-4 ) assert G.get_params() == { "n_pca": None, "random_state": 42, "kernel_symm": "mnn", "theta": 0.5, "anisotropy": 0, "beta": 1, "knn": 3, "decay": 10, "bandwidth": None, "distance": "euclidean", "thresh": 1e-4, "n_jobs": 1, } G.set_params(n_jobs=4) assert G.n_jobs == 4 for graph in G.subgraphs: assert graph.n_jobs == 4 assert graph.knn_tree.n_jobs == 4 G.set_params(random_state=13) assert G.random_state == 13 for graph in G.subgraphs: assert graph.random_state == 13 G.set_params(verbose=2) assert G.verbose == 2 for graph in G.subgraphs: assert graph.verbose == 2 G.set_params(verbose=0) assert_raises(ValueError, G.set_params, knn=15) assert_raises(ValueError, G.set_params, decay=15) assert_raises(ValueError, G.set_params, distance="manhattan") assert_raises(ValueError, G.set_params, thresh=1e-3) assert_raises(ValueError, G.set_params, beta=0.2) G.set_params( knn=G.knn, decay=G.decay, thresh=G.thresh, distance=G.distance, beta=G.beta )
def test_inverse_transform_dense_no_pca(): G = build_graph(data, n_pca=None) np.testing.assert_allclose(data[:, 5:7], G.inverse_transform(G.data_nu, columns=[5, 6]), atol=1e-12) assert np.all(G.data == G.inverse_transform(G.data_nu)) assert_raises(ValueError, G.inverse_transform, G.data[:, 0]) assert_raises(ValueError, G.inverse_transform, G.data[:, None, :15]) assert_raises(ValueError, G.inverse_transform, G.data[:, :15])
def test_inverse_transform_dense_pca(): G = build_graph(data, n_pca=data.shape[1] - 1) np.testing.assert_allclose(G.data, G.inverse_transform(G.data_nu), atol=1e-12) np.testing.assert_allclose(G.data[:, -1, None], G.inverse_transform(G.data_nu, columns=-1), atol=1e-12) np.testing.assert_allclose(G.data[:, 5:7], G.inverse_transform(G.data_nu, columns=[5, 6]), atol=1e-12) assert_raises(IndexError, G.inverse_transform, G.data_nu, columns=data.shape[1]) assert_raises(ValueError, G.inverse_transform, G.data[:, 0]) assert_raises(ValueError, G.inverse_transform, G.data[:, None, :15]) assert_raises(ValueError, G.inverse_transform, G.data[:, :15])
def test_set_params(): G = build_graph(data, decay=10, thresh=0) assert G.get_params() == { 'n_pca': 20, 'random_state': 42, 'kernel_symm': '+', 'gamma': None, 'knn': 3, 'decay': 10, 'distance': 'euclidean', 'precomputed': None } assert_raises(ValueError, G.set_params, knn=15) assert_raises(ValueError, G.set_params, decay=15) assert_raises(ValueError, G.set_params, distance='manhattan') assert_raises(ValueError, G.set_params, precomputed='distance') G.set_params(knn=G.knn, decay=G.decay, distance=G.distance, precomputed=G.precomputed)
def test_set_params(): G = build_graph(data, decay=None) assert G.get_params() == { 'n_pca': 20, 'random_state': 42, 'kernel_symm': '+', 'gamma': None, 'knn': 3, 'decay': None, 'distance': 'euclidean', 'thresh': 0, 'n_jobs': -1, 'verbose': 0 } G.set_params(n_jobs=4) assert G.n_jobs == 4 assert G.knn_tree.n_jobs == 4 G.set_params(random_state=13) assert G.random_state == 13 G.set_params(verbose=2) assert G.verbose == 2 G.set_params(verbose=0) assert_raises(ValueError, G.set_params, knn=15) assert_raises(ValueError, G.set_params, decay=10) assert_raises(ValueError, G.set_params, distance='manhattan') assert_raises(ValueError, G.set_params, thresh=1e-3) assert_raises(ValueError, G.set_params, gamma=0.99) assert_raises(ValueError, G.set_params, kernel_symm='*') G.set_params(knn=G.knn, decay=G.decay, thresh=G.thresh, distance=G.distance, gamma=G.gamma, kernel_symm=G.kernel_symm)
def test_set_params(): X, sample_idx = generate_swiss_roll() G = build_graph(X, sample_idx=sample_idx, kernel_symm='gamma', gamma=0.5, n_pca=None, thresh=1e-4) assert G.get_params() == { 'n_pca': None, 'random_state': 42, 'kernel_symm': 'gamma', 'gamma': 0.5, 'beta': 1, 'adaptive_k': 'sqrt', 'knn': 3, 'decay': 10, 'distance': 'euclidean', 'thresh': 1e-4, 'n_jobs': 1 } G.set_params(n_jobs=4) assert G.n_jobs == 4 for graph in G.subgraphs: assert graph.n_jobs == 4 assert graph.knn_tree.n_jobs == 4 G.set_params(random_state=13) assert G.random_state == 13 for graph in G.subgraphs: assert graph.random_state == 13 G.set_params(verbose=2) assert G.verbose == 2 for graph in G.subgraphs: assert graph.verbose == 2 G.set_params(verbose=0) assert_raises(ValueError, G.set_params, knn=15) assert_raises(ValueError, G.set_params, decay=15) assert_raises(ValueError, G.set_params, distance='manhattan') assert_raises(ValueError, G.set_params, thresh=1e-3) assert_raises(ValueError, G.set_params, beta=0.2) assert_raises(ValueError, G.set_params, adaptive_k='min') G.set_params(knn=G.knn, decay=G.decay, thresh=G.thresh, distance=G.distance, beta=G.beta, adaptive_k=G.adaptive_k)
def test_inverse_transform_sparse_no_pca(): G = build_graph(data, sparse=True, n_pca=None) assert np.sum(G.data != G.inverse_transform(G.data_nu)) == 0 assert_raises(ValueError, G.inverse_transform, sp.csr_matrix(G.data)[:, 0]) assert_raises(ValueError, G.inverse_transform, sp.csr_matrix(G.data)[:, :15])
def test_transform_sparse_pca(): G = build_graph(data, sparse=True, n_pca=20) assert np.all(G.data_nu == G.transform(G.data)) assert_raises(ValueError, G.transform, sp.csr_matrix(G.data)[:, 0]) assert_raises(ValueError, G.transform, sp.csr_matrix(G.data)[:, :15])
def test_transform_dense_no_pca(): G = build_graph(data, n_pca=None) assert np.all(G.data_nu == G.transform(G.data)) assert_raises(ValueError, G.transform, G.data[:, 0]) assert_raises(ValueError, G.transform, G.data[:, None, :15]) assert_raises(ValueError, G.transform, G.data[:, :15])
def test_set_params(): G = build_graph(data, decay=None) assert G.get_params() == { "n_pca": 20, "random_state": 42, "kernel_symm": "+", "theta": None, "anisotropy": 0, "knn": 3, "knn_max": None, "decay": None, "bandwidth": None, "bandwidth_scale": 1, "distance": "euclidean", "thresh": 0, "n_jobs": -1, "verbose": 0, }, G.get_params() G.set_params(n_jobs=4) assert G.n_jobs == 4 assert G.knn_tree.n_jobs == 4 G.set_params(random_state=13) assert G.random_state == 13 G.set_params(verbose=2) assert G.verbose == 2 G.set_params(verbose=0) assert_raises(ValueError, G.set_params, knn=15) assert_raises(ValueError, G.set_params, knn_max=15) assert_raises(ValueError, G.set_params, decay=10) assert_raises(ValueError, G.set_params, distance="manhattan") assert_raises(ValueError, G.set_params, thresh=1e-3) assert_raises(ValueError, G.set_params, theta=0.99) assert_raises(ValueError, G.set_params, kernel_symm="*") assert_raises(ValueError, G.set_params, anisotropy=0.7) assert_raises(ValueError, G.set_params, bandwidth=5) assert_raises(ValueError, G.set_params, bandwidth_scale=5) G.set_params( knn=G.knn, decay=G.decay, thresh=G.thresh, distance=G.distance, theta=G.theta, anisotropy=G.anisotropy, kernel_symm=G.kernel_symm, )
def test_set_params(): G = build_graph(data, decay=10, thresh=0) assert G.get_params() == { "n_pca": 20, "random_state": 42, "kernel_symm": "+", "theta": None, "knn": 3, "anisotropy": 0, "decay": 10, "bandwidth": None, "bandwidth_scale": 1, "distance": "euclidean", "precomputed": None, } assert_raises(ValueError, G.set_params, knn=15) assert_raises(ValueError, G.set_params, decay=15) assert_raises(ValueError, G.set_params, distance="manhattan") assert_raises(ValueError, G.set_params, precomputed="distance") assert_raises(ValueError, G.set_params, bandwidth=5) assert_raises(ValueError, G.set_params, bandwidth_scale=5) G.set_params(knn=G.knn, decay=G.decay, distance=G.distance, precomputed=G.precomputed)
def test_exact_graph_anisotropy(): k = 3 a = 13 n_pca = 20 anisotropy = 0.9 data_small = data[np.random.choice(len(data), len(data) // 2, replace=False)] pca = PCA(n_pca, svd_solver="randomized", random_state=42).fit(data_small) data_small_nu = pca.transform(data_small) pdx = squareform(pdist(data_small_nu, metric="euclidean")) knn_dist = np.partition(pdx, k, axis=1)[:, :k] epsilon = np.max(knn_dist, axis=1) weighted_pdx = (pdx.T / epsilon).T K = np.exp(-1 * weighted_pdx**a) K = K + K.T K = np.divide(K, 2) d = K.sum(1) W = K / (np.outer(d, d)**anisotropy) np.fill_diagonal(W, 0) G = pygsp.graphs.Graph(W) G2 = build_graph( data_small, thresh=0, n_pca=n_pca, decay=a, knn=k - 1, random_state=42, use_pygsp=True, anisotropy=anisotropy, ) assert isinstance(G2, graphtools.graphs.TraditionalGraph) assert G.N == G2.N np.testing.assert_equal(G.dw, G2.dw) assert (G2.W != G.W).sum() == 0 assert (G.W != G2.W).nnz == 0 assert_raises( ValueError, build_graph, data_small, thresh=0, n_pca=n_pca, decay=a, knn=k - 1, random_state=42, use_pygsp=True, anisotropy=-1, ) assert_raises( ValueError, build_graph, data_small, thresh=0, n_pca=n_pca, decay=a, knn=k - 1, random_state=42, use_pygsp=True, anisotropy=2, ) assert_raises( ValueError, build_graph, data_small, thresh=0, n_pca=n_pca, decay=a, knn=k - 1, random_state=42, use_pygsp=True, anisotropy="invalid", )