def test_pickle_bad_pickle(): with assert_warns_message( UserWarning, "Returning object that is not a graphtools.base.BaseGraph"): with tempfile.TemporaryDirectory() as tempdir: path = os.path.join(tempdir, "tmp.pkl") with open(path, "wb") as f: pickle.dump("hello world", f) G = graphtools.read_pickle(path)
def test_sample_idx_unique(): with assert_raises_message( ValueError, "sample_idx must contain more than one unique value"): build_graph(data, graph_class=graphtools.graphs.MNNGraph, sample_idx=np.ones(len(data))) with assert_warns_message(UserWarning, "Only one unique sample. Not using MNNGraph"): build_graph(data, sample_idx=np.ones(len(data)), graphtype="mnn")
def test_precomputed_with_pca(): with assert_warns_message( RuntimeWarning, "n_pca cannot be given on a precomputed graph. Setting n_pca=None", ): build_graph(squareform(pdist(data)), precomputed="distance", n_pca=20, decay=10)
def test_from_igraph_invalid_attribute(): with assert_warns_message( UserWarning, "Edge attribute invalid not found. Returning unweighted graph"): n = 100 m = 500 K = np.zeros((n, n)) for _ in range(m): e = np.random.choice(n, 2, replace=False) K[e[0], e[1]] = K[e[1], e[0]] = 1 g = igraph.Graph.Adjacency(K.tolist()) G = graphtools.from_igraph(g, attribute="invalid")
def test_knn_graph_sparse_no_pca(): with assert_warns_message( UserWarning, "cannot use tree with sparse input: using brute force"): build_graph( sp.coo_matrix(data), n_pca=None, # n_pca, decay=10, knn=3, thresh=1e-4, random_state=42, use_pygsp=True, )
def test_from_igraph_invalid_precomputed(): with assert_warns_message( UserWarning, "Cannot build graph from igraph with precomputed=affinity. Use 'adjacency' instead.", ): n = 100 m = 500 K = np.zeros((n, n)) for _ in range(m): e = np.random.choice(n, 2, replace=False) K[e[0], e[1]] = K[e[1], e[0]] = 1 g = igraph.Graph.Adjacency(K.tolist()) G = graphtools.from_igraph(g, attribute=None, precomputed="affinity")
def test_mnn_with_kernel_symmm_theta_and_no_theta(): with assert_warns_message( UserWarning, "kernel_symm='mnn' but theta not given. Defaulting to theta=1."): build_graph( data, thresh=0, n_pca=20, decay=10, knn=5, random_state=42, sample_idx=digits["target"], kernel_symm="mnn", )
def test_mnn_with_gamma(): with assert_warns_message(FutureWarning, "gamma is deprecated. Setting theta=0.9"): build_graph( data, thresh=0, n_pca=20, decay=10, knn=5, random_state=42, sample_idx=digits["target"], kernel_symm="mnn", gamma=0.9, )
def test_mnn_with_kernel_symm_theta(): with assert_warns_message( FutureWarning, "kernel_symm='theta' is deprecated. Setting kernel_symm='mnn'"): build_graph( data, thresh=0, n_pca=20, decay=10, knn=5, random_state=42, sample_idx=digits["target"], kernel_symm="theta", theta=0.9, )
def test_mnn_with_theta_and_kernel_symm_not_theta(): with assert_warns_message( UserWarning, "kernel_symm='+' but theta is not None. Setting kernel_symm='mnn'." ): build_graph( data, thresh=0, n_pca=20, decay=10, knn=5, random_state=42, sample_idx=digits["target"], kernel_symm="+", theta=0.9, )
def test_mnn_adaptive_k(): with assert_warns_message( DeprecationWarning, "`adaptive_k` has been deprecated. Using fixed knn."): build_graph( data, thresh=0, n_pca=20, decay=10, knn=5, random_state=42, sample_idx=digits["target"], kernel_symm="mnn", theta=0.9, adaptive_k="sqrt", )
def test_knn_graph(): k = 3 n_pca = 20 pca = PCA(n_pca, svd_solver="randomized", random_state=42).fit(data) data_nu = pca.transform(data) pdx = squareform(pdist(data_nu, metric="euclidean")) knn_dist = np.partition(pdx, k, axis=1)[:, :k] epsilon = np.max(knn_dist, axis=1) K = np.empty_like(pdx) for i in range(len(pdx)): K[i, pdx[i, :] <= epsilon[i]] = 1 K[i, pdx[i, :] > epsilon[i]] = 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=n_pca, decay=None, 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.kNNGraph) K2 = G2.build_kernel_to_data(G2.data_nu, knn=k) K2 = (K2 + K2.T) / 2 assert (G2.K - K2).nnz == 0 assert (G2.build_kernel_to_data( G2.data_nu, knn=data.shape[0]).nnz == data.shape[0] * data.shape[0]) with assert_warns_message( UserWarning, "Cannot set knn ({}) to be greater than " "n_samples ({}). Setting knn={}".format(data.shape[0] + 1, data.shape[0], data.shape[0]), ): G2.build_kernel_to_data( Y=G2.data_nu, knn=data.shape[0] + 1, )
def test_if_sparse_deprecated(): with assert_warns_message( DeprecationWarning, "Call to deprecated function (or staticmethod) if_sparse. (Use graphtools.matrix.if_sparse instead) -- Deprecated since version 1.5.0.", ): graphtools.utils.if_sparse(lambda x: x, lambda x: x, np.zeros((4, 4)))
def test_build_landmark_with_too_few_points(): with assert_warns_message( RuntimeWarning, "n_svd (100) >= n_samples (50) Consider using kNNGraph or lower n_svd", ): build_graph(data[:50], n_landmark=25, n_svd=100)
def test_sparse_set_diagonal_deprecated(): with assert_warns_message( DeprecationWarning, "Call to deprecated function (or staticmethod) sparse_set_diagonal. (Use graphtools.matrix.sparse_set_diagonal instead) -- Deprecated since version 1.5.0.", ): graphtools.utils.sparse_set_diagonal(sparse.csr_matrix((4, 4)), 1)
def test_single_sample_idx_warning(): with assert_warns_message(UserWarning, "Only one unique sample. Not using MNNGraph"): build_graph(data, sample_idx=np.repeat(1, len(data)))
def test_to_pygsp_invalid_use_pygsp(): with assert_warns_message( UserWarning, "Cannot build PyGSPGraph with use_pygsp=False. Use True instead."): G = build_graph(data) G2 = G.to_pygsp(use_pygsp=False)
def test_threshold_ignored(): with assert_warns_message( RuntimeWarning, "n_pca = 10, therefore rank_threshold of -1 will not be used. To use rank thresholding, set n_pca = True", ): assert build_graph(data, n_pca=10, rank_threshold=-1).n_pca == 10
def test_fractional_n_pca(): with assert_warns_message( RuntimeWarning, "Cannot perform PCA to fractional 1.5 dimensions. Rounding to 2" ): build_graph(data, n_pca=1.5)
def test_knnmax_too_large(): with assert_warns_message( UserWarning, "Cannot set knn_max (9) to be less than knn (10). Setting knn_max=10", ): build_graph(data, n_pca=20, decay=10, knn=10, knn_max=9, thresh=1e-4)
def test_to_array_deprecated(): with assert_warns_message( DeprecationWarning, "Call to deprecated function (or staticmethod) to_array. (Use graphtools.matrix.to_array instead) -- Deprecated since version 1.5.0.", ): graphtools.utils.to_array([1])
def test_balltree_cosine(): with assert_warns_message( UserWarning, "Metric cosine not valid for `sklearn.neighbors.BallTree`. Graph instantiation may be slower than normal.", ): build_graph(data, n_pca=20, decay=10, distance="cosine", thresh=1e-4)
def test_precomputed_nonzero_diagonal(): with assert_warns_message(RuntimeWarning, "K should have a non-zero diagonal"): build_graph(np.zeros((10, 10)), precomputed="affinity", n_pca=None)
def test_bandwidth_no_decay(): with assert_warns_message(UserWarning, "`bandwidth` is not used when `decay=None`."): build_graph(data, n_pca=20, decay=None, bandwidth=3, thresh=1e-4)
def test_nonzero_discrete_deprecated(): with assert_warns_message( DeprecationWarning, "Call to deprecated function (or staticmethod) nonzero_discrete. (Use graphtools.matrix.nonzero_discrete instead) -- Deprecated since version 1.5.0.", ): graphtools.utils.nonzero_discrete(np.zeros((4, 4)), [1])