Ejemplo n.º 1
0
def test_bad_inputs():
    np.random.seed(1)
    single_graph = er_np(100, 0.2)
    different_size_graphs = [er_np(100, 0.2)] + [er_np(200, 0.2)]

    with pytest.raises(TypeError):
        "Invalid unscaled"
        mase = MultipleASE(scaled="1")

    with pytest.raises(ValueError):
        "Test single graph input"
        MultipleASE().fit(single_graph)

    with pytest.raises(ValueError):
        "Test 3-d tensor with 1 graph"
        single_graph_tensor = single_graph.reshape(1, 100, -1)
        MultipleASE().fit(single_graph_tensor)

    with pytest.raises(ValueError):
        "Empty list"
        MultipleASE().fit([])

    with pytest.raises(ValueError):
        "Test graphs with different sizes"
        MultipleASE().fit(different_size_graphs)
Ejemplo n.º 2
0
def test_diag_aug():
    np.random.seed(5)
    n = 100
    p = 0.25

    graphs_list = [er_np(n, p) for _ in range(2)]
    graphs_arr = np.array(graphs_list)

    # Test that array and list inputs results in same embeddings
    mase_arr = MultipleASE(diag_aug=True).fit_transform(graphs_arr)
    mase_list = MultipleASE(diag_aug=True).fit_transform(graphs_list)

    assert array_equal(mase_list, mase_arr)
Ejemplo n.º 3
0
    def run(diag_aug, scaled):
        # undirected case
        np.random.seed(4 + diag_aug + scaled)
        X = make_train_undirected(n, m)

        res = MultipleASE(n_components=2).fit(X).latent_left_
        gmm = GaussianCluster(10, covariance_type="all").fit(res)
        assert gmm.n_components_ == 2

        # directed case
        np.random.seed(5 + diag_aug + scaled)
        X = make_train_directed(n, m)

        mase = MultipleASE(n_components=2).fit(X)
        res = np.hstack([mase.latent_left_, mase.latent_right_])
        gmm = GaussianCluster(10, covariance_type="all").fit(res)
        assert gmm.n_components_ == 2
Ejemplo n.º 4
0
    def run(diag_aug, scaled):
        # undirected case
        np.random.seed(2 + diag_aug + scaled)
        X = make_train_undirected(n, m)

        res = (MultipleASE(2, diag_aug=diag_aug,
                           scaled=scaled).fit(X).scores_.reshape((m * 4, -1)))
        gmm = GaussianCluster(10, covariance_type="all").fit(res)
        assert gmm.n_components_ == 4

        # directed case
        np.random.seed(3 + diag_aug + scaled)
        X = make_train_directed(n, m)

        res = MultipleASE(2, diag_aug=diag_aug).fit(X).scores_.reshape(
            (m * 4, -1))
        gmm = GaussianCluster(10, covariance_type="all").fit(res)
        assert gmm.n_components_ == 4
Ejemplo n.º 5
0
def test_vertex():
    """
    There should be 2 clusters since each graph is a 2 block model
    """
    # undirected case
    np.random.seed(4)
    n = [128, 128]
    m = 10
    X = make_train_undirected(n, m)

    # undirected case
    res = MultipleASE(n_components=2).fit(X).latent_left_
    gmm = GaussianCluster(10, covariance_type="all").fit(res)
    assert gmm.n_components_ == 2

    # directed case
    np.random.seed(5)
    X = make_train_directed(n, m)

    mase = MultipleASE(n_components=2).fit(X)
    res = np.hstack([mase.latent_left_, mase.latent_right_])
    gmm = GaussianCluster(10, covariance_type="all").fit(res)
    assert gmm.n_components_ == 2

    # Scaled and undirected case
    np.random.seed(4)
    X = make_train_undirected(n, m)

    res = MultipleASE(n_components=2, scaled=True).fit_transform(X)
    gmm = GaussianCluster(10, covariance_type="all").fit(res)
    assert gmm.n_components_ == 2

    # Scaled and directed case
    np.random.seed(5)
    X = make_train_directed(n, m)

    left, right = MultipleASE(n_components=2, scaled=True).fit_transform(X)
    res = np.hstack([left, right])
    gmm = GaussianCluster(10, covariance_type="all").fit(res)
    assert gmm.n_components_ == 2
Ejemplo n.º 6
0
def test_graph_clustering():
    """
    There should be 4 total clusters since 4 class problem.
    n_components = 2
    """
    # undirected case
    np.random.seed(2)
    n = [128, 128]
    m = 10
    X = make_train_undirected(n, m)

    res = MultipleASE(2).fit(X).scores_.reshape((m * 4, -1))
    gmm = GaussianCluster(10, covariance_type="all").fit(res)
    assert gmm.n_components_ == 4

    # directed case
    np.random.seed(3)
    X = make_train_directed(n, m)

    res = MultipleASE(2).fit(X).scores_.reshape((m * 4, -1))
    gmm = GaussianCluster(10, covariance_type="all").fit(res)
    assert gmm.n_components_ == 4

    # Scaled cases
    # undirected case
    np.random.seed(12)
    X = make_train_undirected(n, m)

    res = MultipleASE(2, scaled=True).fit(X).scores_.reshape((m * 4, -1))
    gmm = GaussianCluster(10, covariance_type="all").fit(res)
    assert gmm.n_components_ == 4

    # directed case
    np.random.seed(13)
    X = make_train_directed(n, m)

    res = MultipleASE(2, scaled=True).fit(X).scores_.reshape((m * 4, -1))
    gmm = GaussianCluster(10, covariance_type="all").fit(res)
    assert gmm.n_components_ == 4