Ejemplo n.º 1
0
def test_precomputed_interpolate():
    with assert_raises_message(ValueError,
                               "Cannot extend kernel on precomputed graph"):
        G = build_graph(squareform(pdist(data)),
                        n_pca=None,
                        precomputed="distance")
        G.build_kernel_to_data(data)
Ejemplo n.º 2
0
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, random_state=42,
                     use_pygsp=True)
    assert(G.N == G2.N)
    assert(np.all(G.d == G2.d))
    assert((G.W != G2.W).nnz == 0)
    assert((G2.W != G.W).sum() == 0)
    assert(isinstance(G2, graphtools.graphs.kNNGraph))
Ejemplo n.º 3
0
def test_sample_idx_and_precomputed():
    build_graph(
        squareform(pdist(data)),
        n_pca=None,
        sample_idx=np.arange(10),
        precomputed="distance",
        decay=10,
    )
Ejemplo n.º 4
0
def test_exact_graph_callable_bandwidth():
    decay = 2
    knn = 5

    def bandwidth(x):
        return 2

    n_pca = 20
    thresh = 1e-4
    pca = PCA(n_pca, svd_solver="randomized", random_state=42).fit(data)
    data_nu = pca.transform(data)
    pdx = squareform(pdist(data_nu, metric="euclidean"))
    K = np.exp(-1 * (pdx / bandwidth(pdx))**decay)
    K[K < thresh] = 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,
        knn=knn - 1,
        decay=decay,
        bandwidth=bandwidth,
        random_state=42,
        thresh=thresh,
        use_pygsp=True,
    )
    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

    def bandwidth(x):
        return np.percentile(x, 10, axis=1)

    K = np.exp(-1 * (pdx / bandwidth(pdx))**decay)
    K[K < thresh] = 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,
        knn=knn - 1,
        decay=decay,
        bandwidth=bandwidth,
        random_state=42,
        thresh=thresh,
        use_pygsp=True,
    )
    assert isinstance(G2, graphtools.graphs.TraditionalGraph)
    assert G.N == G2.N
    np.testing.assert_allclose(G.dw, G2.dw)
    np.testing.assert_allclose((G2.W - G.W).data, 0, atol=1e-14)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
def test_sample_idx_and_precomputed():
    with assert_raises_message(
            ValueError,
            "MNNGraph does not support precomputed values. Use `graphtype='exact'` and `sample_idx=None` or `precomputed=None`",
    ):
        build_graph(
            squareform(pdist(data)),
            n_pca=None,
            sample_idx=np.arange(10),
            precomputed="distance",
            decay=10,
        )
Ejemplo n.º 7
0
def test_exact_graph_fixed_bandwidth():
    decay = 2
    knn = None
    bandwidth = 2
    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"))
    K = np.exp(-1 * (pdx / bandwidth)**decay)
    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,
        graphtype="exact",
        knn=knn,
        decay=decay,
        bandwidth=bandwidth,
        random_state=42,
        thresh=0,
        use_pygsp=True,
    )
    assert isinstance(G2, graphtools.graphs.TraditionalGraph)
    assert G.N == G2.N
    np.testing.assert_allclose(G.dw, G2.dw)
    np.testing.assert_allclose((G2.W - G.W).data, 0, atol=1e-14)
    bandwidth = np.random.gamma(5, 0.5, len(data))
    K = np.exp(-1 * (pdx.T / bandwidth).T**decay)
    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,
        graphtype="exact",
        knn=knn,
        decay=decay,
        bandwidth=bandwidth,
        random_state=42,
        thresh=0,
        use_pygsp=True,
    )
    assert isinstance(G2, graphtools.graphs.TraditionalGraph)
    assert G.N == G2.N
    np.testing.assert_allclose(G.dw, G2.dw)
    np.testing.assert_allclose((G2.W - G.W).data, 0, atol=1e-14)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
def test_sparse_alpha_knn_graph():
    data = datasets.make_swiss_roll()[0]
    k = 5
    a = 0.45
    thresh = 0.01
    pdx = squareform(pdist(data, metric='euclidean'))
    knn_dist = np.partition(pdx, k, axis=1)[:, :k]
    epsilon = np.max(knn_dist, axis=1)
    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, thresh=thresh,
                     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))
Ejemplo n.º 10
0
def test_knn_graph_sparse():
    k = 3
    n_pca = 20
    pca = TruncatedSVD(n_pca, 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(sp.coo_matrix(data), n_pca=n_pca,
                     decay=None, knn=k, random_state=42,
                     use_pygsp=True)
    assert(G.N == G2.N)
    np.testing.assert_allclose(G2.W.toarray(), G.W.toarray())
    assert(isinstance(G2, graphtools.graphs.kNNGraph))
Ejemplo n.º 11
0
def test_precomputed_interpolate():
    G = build_graph(squareform(pdist(data)),
                    n_pca=None,
                    precomputed='distance')
    G.build_kernel_to_data(data)
Ejemplo n.º 12
0
def test_exact_graph():
    k = 3
    a = 13
    n_pca = 20
    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)
    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=0,
                     n_pca=n_pca,
                     decay=a,
                     knn=k,
                     random_state=42,
                     use_pygsp=True)
    assert (G.N == G2.N)
    assert (np.all(G.d == G2.d))
    assert ((G.W != G2.W).nnz == 0)
    assert ((G2.W != G.W).sum() == 0)
    assert (isinstance(G2, graphtools.graphs.TraditionalGraph))
    G2 = build_graph(pdx,
                     n_pca=None,
                     precomputed='distance',
                     decay=a,
                     knn=k,
                     random_state=42,
                     use_pygsp=True)
    assert (G.N == G2.N)
    assert (np.all(G.d == G2.d))
    assert ((G.W != G2.W).nnz == 0)
    assert ((G2.W != G.W).sum() == 0)
    assert (isinstance(G2, graphtools.graphs.TraditionalGraph))
    G2 = build_graph(sp.coo_matrix(K),
                     n_pca=None,
                     precomputed='affinity',
                     random_state=42,
                     use_pygsp=True)
    assert (G.N == G2.N)
    assert (np.all(G.d == G2.d))
    assert ((G.W != G2.W).nnz == 0)
    assert ((G2.W != G.W).sum() == 0)
    assert (isinstance(G2, graphtools.graphs.TraditionalGraph))
    G2 = build_graph(K,
                     n_pca=None,
                     precomputed='affinity',
                     random_state=42,
                     use_pygsp=True)
    assert (G.N == G2.N)
    assert (np.all(G.d == G2.d))
    assert ((G.W != G2.W).nnz == 0)
    assert ((G2.W != G.W).sum() == 0)
    assert (isinstance(G2, graphtools.graphs.TraditionalGraph))
    G2 = build_graph(W,
                     n_pca=None,
                     precomputed='adjacency',
                     random_state=42,
                     use_pygsp=True)
    assert (G.N == G2.N)
    assert (np.all(G.d == G2.d))
    assert ((G.W != G2.W).nnz == 0)
    assert ((G2.W != G.W).sum() == 0)
    assert (isinstance(G2, graphtools.graphs.TraditionalGraph))
Ejemplo n.º 13
0
def test_precomputed_with_pca():
    build_graph(squareform(pdist(data)),
                precomputed='distance',
                n_pca=20,
                decay=10)
Ejemplo n.º 14
0
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
    with assert_raises_message(ValueError,
                               "Expected 0 <= anisotropy <= 1. Got -1"):
        build_graph(
            data_small,
            thresh=0,
            n_pca=n_pca,
            decay=a,
            knn=k - 1,
            random_state=42,
            use_pygsp=True,
            anisotropy=-1,
        )
    with assert_raises_message(ValueError,
                               "Expected 0 <= anisotropy <= 1. Got 2"):
        build_graph(
            data_small,
            thresh=0,
            n_pca=n_pca,
            decay=a,
            knn=k - 1,
            random_state=42,
            use_pygsp=True,
            anisotropy=2,
        )
    with assert_raises_message(ValueError,
                               "Expected 0 <= anisotropy <= 1. Got invalid"):
        build_graph(
            data_small,
            thresh=0,
            n_pca=n_pca,
            decay=a,
            knn=k - 1,
            random_state=42,
            use_pygsp=True,
            anisotropy="invalid",
        )
Ejemplo n.º 15
0
def test_precomputed_with_pca():
    build_graph(squareform(pdist(data)), precomputed="distance", n_pca=20)
Ejemplo n.º 16
0
def test_exact_graph():
    k = 3
    a = 13
    n_pca = 20
    bandwidth_scale = 1.3
    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) * bandwidth_scale
    weighted_pdx = (pdx.T / epsilon).T
    K = np.exp(-1 * weighted_pdx**a)
    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=0,
        n_pca=n_pca,
        decay=a,
        knn=k - 1,
        random_state=42,
        bandwidth_scale=bandwidth_scale,
        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(
        pdx,
        n_pca=None,
        precomputed="distance",
        bandwidth_scale=bandwidth_scale,
        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.coo_matrix(K),
        n_pca=None,
        precomputed="affinity",
        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(K,
                     n_pca=None,
                     precomputed="affinity",
                     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(W,
                     n_pca=None,
                     precomputed="adjacency",
                     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)
Ejemplo n.º 17
0
def test_invalid_precomputed():
    build_graph(squareform(pdist(data)),
                n_pca=None,
                precomputed='hello world',
                decay=10)
Ejemplo n.º 18
0
def test_truncated_exact_graph_sparse():
    k = 3
    a = 13
    n_pca = 20
    thresh = 1e-4
    data_small = data[np.random.choice(len(data),
                                       len(data) // 2,
                                       replace=False)]
    pca = TruncatedSVD(n_pca, 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 < thresh] = 0
    W = K + K.T
    W = np.divide(W, 2)
    np.fill_diagonal(W, 0)
    G = pygsp.graphs.Graph(W)
    G2 = build_graph(
        sp.coo_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_allclose(G2.W.toarray(), G.W.toarray())
    assert isinstance(G2, graphtools.graphs.TraditionalGraph)
    G2 = build_graph(
        sp.bsr_matrix(pdx),
        n_pca=None,
        precomputed="distance",
        thresh=thresh,
        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.lil_matrix(K),
        n_pca=None,
        precomputed="affinity",
        thresh=thresh,
        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.dok_matrix(W),
        n_pca=None,
        precomputed="adjacency",
        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)