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)) with assert_raises_message( ValueError, "data of shape ({0}, 1) cannot be transformed to graph built on data of shape ({0}, {1}). Expected shape ({0}, {1})".format( G.data.shape[0], G.data.shape[1] ), ): G.transform(sp.csr_matrix(G.data)[:, 0]) with assert_raises_message( ValueError, "data of shape ({0}, 15) cannot be transformed to graph built on data of shape ({0}, {1}). Expected shape ({0}, {1})".format( G.data.shape[0], G.data.shape[1] ), ): 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_transform_sparse_pca(): G = build_graph(data, sparse=True, n_pca=20) assert np.all(G.data_nu == G.transform(G.data)) with assert_raises_message( ValueError, "data of shape ({0}, 1) cannot be transformed to graph built on data of shape ({0}, {1}). Expected shape ({0}, {1})".format( G.data.shape[0], G.data.shape[1] ), ): G.transform(sp.csr_matrix(G.data)[:, 0]) with assert_raises_message( ValueError, "data of shape ({0}, 15) cannot be transformed to graph built on data of shape ({0}, {1}). Expected shape ({0}, {1})".format( G.data.shape[0], G.data.shape[1] ), ): G.transform(sp.csr_matrix(G.data)[:, :15])
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 with assert_raises_message( ValueError, "data of shape ({0}, 1) cannot be inverse transformed from graph built on reduced data of shape ({0}, {1})".format( G.data.shape[0], G.data.shape[1] ), ): G.inverse_transform(sp.csr_matrix(G.data)[:, 0]) with assert_raises_message( ValueError, "data of shape ({0}, 15) cannot be inverse transformed from graph built on reduced data of shape ({0}, {1})".format( G.data.shape[0], G.data.shape[1] ), ): G.inverse_transform(sp.csr_matrix(G.data)[:, :15])
def test_transform_sparse_no_pca(): G = build_graph(data, sparse=True, n_pca=None) assert np.sum(G.data_nu != G.transform(G.data)) == 0 with assert_raises_message( ValueError, "data of shape {} cannot be transformed to graph built on data of shape {}".format( G.data.tocsr()[:, 0].shape, G.data.shape ), ): G.transform(sp.csr_matrix(G.data)[:, 0]) with assert_raises_message( ValueError, "data of shape {} cannot be transformed to graph built on data of shape {}".format( G.data.tocsr()[:, :15].shape, G.data.shape ), ): G.transform(sp.csr_matrix(G.data)[:, :15])
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_anndata_sparse(): try: anndata except NameError: # not installed return G = build_graph(anndata.AnnData(sp.csr_matrix(data))) assert isinstance(G, graphtools.base.BaseGraph) assert isinstance(G.data, sp.csr_matrix)
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_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 ) with assert_raises_message( IndexError, "index 64 is out of bounds for axis 1 with size 64" ): G.inverse_transform(G.data_nu, columns=data.shape[1]) with assert_raises_message( TypeError, "A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.", ): G.inverse_transform(sp.csr_matrix(G.data)[:, 0]) with assert_raises_message( TypeError, "A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.", ): G.inverse_transform(sp.csr_matrix(G.data)[:, :15]) with assert_raises_message( ValueError, "data of shape ({0},) cannot be inverse transformed from graph built on reduced data of shape ({0}, {1}). Expected shape ({0}, {1})".format( data.shape[0], G.n_pca ), ): G.inverse_transform(data[:, 0]) with assert_raises_message( ValueError, "data of shape ({0}, 15) cannot be inverse transformed from graph built on reduced data of shape ({0}, {1}). Expected shape ({0}, {1})".format( data.shape[0], G.n_pca ), ): G.inverse_transform(data[:, :15])
def test_truncated_exact_graph_no_pca(): k = 3 a = 13 n_pca = None thresh = 1e-4 data_small = data[np.random.choice(len(data), len(data) // 10, replace=False)] pdx = squareform(pdist(data_small, 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 < thresh] = 0 W = K + K.T W = np.divide(W, 2) np.fill_diagonal(W, 0) G = pygsp.graphs.Graph(W) G2 = build_graph( data_small, thresh=thresh, graphtype="exact", n_pca=n_pca, decay=a, knn=k - 1, random_state=42, use_pygsp=True, ) assert G.N == G2.N np.testing.assert_equal(G.dw, G2.dw) assert (G.W != G2.W).nnz == 0 assert (G2.W != G.W).sum() == 0 assert isinstance(G2, graphtools.graphs.TraditionalGraph) G2 = build_graph( sp.csr_matrix(data_small), thresh=thresh, graphtype="exact", n_pca=n_pca, decay=a, knn=k - 1, random_state=42, use_pygsp=True, ) assert G.N == G2.N np.testing.assert_equal(G.dw, G2.dw) assert (G.W != G2.W).nnz == 0 assert (G2.W != G.W).sum() == 0 assert isinstance(G2, graphtools.graphs.TraditionalGraph)
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])