def test_transition_matrix_with_supernode():
    karate_club = karate_club_graph()
    karate_club = canonical_relabel_nodes(keep_largest_component(karate_club))
    karate_club_super = add_supernode(karate_club, query=[10, 20, 30])
    P = compute_transition_matrix(karate_club_super)
    for v in [10, 20, 30]:
        assert P[-1, v - 1] == 1 / 3
def test_largest_component():
    karate_club = karate_club_graph()
    new_club = karate_club.copy()
    # add a triangle, disconnected from the rest of the graph
    new_club.add_edges_from([(100, 101), (101, 102), (102, 100)])
    largest_component = keep_largest_component(new_club)
    assert is_isomorphic(largest_component, karate_club)
def test_transition_matrix_with_supernode():
    karate_club = karate_club_graph()
    karate_club = canonical_relabel_nodes(keep_largest_component(karate_club))
    karate_club_super = add_supernode(karate_club, query=[10, 20, 30])
    P = compute_transition_matrix(karate_club_super)
    for v in [10, 20, 30]:
        assert P[-1, v - 1] == 1 / 3
def test_update_fundamental_matrix():
    prng = RandomState(20150101)
    P = compute_transition_matrix(karate_club_graph())
    n = P.shape[0]
    order = arange(P.shape[0])
    previous_index = prng.choice(order, 1)
    previous_node = order[previous_index]
    non_absorbing_nodes = chain(range(previous_index),
                                range(previous_index + 1, n))
    non_absorbing_nodes = list(non_absorbing_nodes)
    order = order[non_absorbing_nodes]
    F = compute_fundamental_matrix(P[non_absorbing_nodes, :]
                                   [:, non_absorbing_nodes])
    absorbing_nodes = [previous_node]
    P_updated = P.copy()
    F_updated = F
    while P_updated.shape[0] >= 3:
        next_node = order[prng.choice(len(order), 1)]
        (P_updated, F_updated, order, previous_index) = \
            update_fundamental_matrix(P_updated, F_updated, next=next_node,
                                      previous=previous_node,
                                      previous_index=previous_index,
                                      node_order=order)
        previous_node = next_node
        absorbing_nodes.append(next_node)
        non_absorbing_nodes = [x for x in range(n) if x not in absorbing_nodes]
        F_slow = compute_fundamental_matrix(P[non_absorbing_nodes, :]
                                            [:, non_absorbing_nodes])
        error_at_step = sum(sum(F_updated - F_slow).T)[0, 0]
        assert abs(error_at_step) < 1e-8, "Error is more than 1e-8."
def test_largest_component():
    karate_club = karate_club_graph()
    new_club = karate_club.copy()
    # add a triangle, disconnected from the rest of the graph
    new_club.add_edges_from([(100, 101), (101, 102), (102, 100)])
    largest_component = keep_largest_component(new_club)
    assert is_isomorphic(largest_component, karate_club)
def test_update_fundamental_matrix():
    prng = RandomState(20150101)
    P = compute_transition_matrix(karate_club_graph())
    n = P.shape[0]
    order = arange(P.shape[0])
    previous_index = prng.choice(order, 1)
    previous_node = order[previous_index]
    non_absorbing_nodes = chain(range(previous_index),
                                range(previous_index + 1, n))
    non_absorbing_nodes = list(non_absorbing_nodes)
    order = order[non_absorbing_nodes]
    F = compute_fundamental_matrix(
        P[non_absorbing_nodes, :][:, non_absorbing_nodes])
    absorbing_nodes = [previous_node]
    P_updated = P.copy()
    F_updated = F
    while P_updated.shape[0] >= 3:
        next_node = order[prng.choice(len(order), 1)]
        (P_updated, F_updated, order, previous_index) = \
            update_fundamental_matrix(P_updated, F_updated, next=next_node,
                                      previous=previous_node,
                                      previous_index=previous_index,
                                      node_order=order)
        previous_node = next_node
        absorbing_nodes.append(next_node)
        non_absorbing_nodes = [x for x in range(n) if x not in absorbing_nodes]
        F_slow = compute_fundamental_matrix(
            P[non_absorbing_nodes, :][:, non_absorbing_nodes])
        error_at_step = sum(sum(F_updated - F_slow).T)[0, 0]
        assert abs(error_at_step) < 1e-8, "Error is more than 1e-8."
def test_add_supernode():
    karate_club = karate_club_graph()
    n = len(karate_club)
    m = len(karate_club.edges())
    karate_club = add_supernode(karate_club, query=[10, 20, 30])
    assert len(karate_club) == n + 1, 'Supernode was not added.'
    # for v in [10, 20, 30]:
        # assert(n + 1,
               # karate_club.graph['canonical_map'][v] in karate_club.edges(),
        # 'Edge from supernode to query not added.')
    assert len(karate_club.edges()) == 2 * m + 3, \
        'Number of edges after adding supernode is incorrect.'
def test_add_supernode():
    karate_club = karate_club_graph()
    n = len(karate_club)
    m = len(karate_club.edges())
    karate_club = add_supernode(karate_club, query=[10, 20, 30])
    assert len(karate_club) == n + 1, 'Supernode was not added.'
    # for v in [10, 20, 30]:
    # assert(n + 1,
    # karate_club.graph['canonical_map'][v] in karate_club.edges(),
    # 'Edge from supernode to query not added.')
    assert len(karate_club.edges()) == 2 * m + 3, \
        'Number of edges after adding supernode is incorrect.'
Esempio n. 9
0
def test_greedy_team():
    karate = karate_club_graph()
    solutions = {
        1: [33],
        3: [33, 0, 32],
        5: [33, 0, 32, 2, 6],
        8: [33, 0, 32, 2, 6, 25, 1, 5],
        9: [33, 0, 32, 2, 6, 25, 1, 5, 24],
        10: [33, 0, 32, 2, 6, 25, 1, 5, 24, 3]
    }
    for k in solutions:
        score_k, team_k, times_k = greedy_team(karate, k, return_times=True)
        assert sorted(team_k[-1]) == sorted(solutions[k])
def test_greedy_team():
    karate = karate_club_graph()
    solutions = {
        1: [33],
        3: [33, 0, 32],
        5: [33, 0, 32, 2, 6],
        8: [33, 0, 32, 2, 6, 25, 1, 5],
        9: [33, 0, 32, 2, 6, 25, 1, 5, 24],
        10: [33, 0, 32, 2, 6, 25, 1, 5, 24, 3]
    }
    for k in solutions:
        score_k, team_k, times_k = greedy_team(karate, k, return_times=True)
        assert sorted(team_k[-1]) == sorted(solutions[k])
def test_spectral_graph_forge():
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
    scipy = 1

    try:
        import numpy
    except ImportError:
        raise SkipTest('NumPy not available.')
    try:
        import scipy
    except ImportError:
        raise SkipTest("SciPy not available")

    G = karate_club_graph()

    seed = 54321

    # common cases, just checking node number preserving and difference
    # between identity and modularity cases
    H = spectral_graph_forge(G, 0.1, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)

    I = spectral_graph_forge(G, 0.1, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)
    assert_true(is_isomorphic(I, H))

    I = spectral_graph_forge(G, 0.1, transformation='modularity', seed=seed)
    assert_nodes_equal(G, I)

    assert_false(is_isomorphic(I, H))

    # with all the eigenvectors, output graph is identical to the input one
    H = spectral_graph_forge(G, 1, transformation='modularity', seed=seed)
    assert_nodes_equal(G, H)
    assert_true(is_isomorphic(G, H))

    # invalid alpha input value, it is silently truncated in [0,1]
    H = spectral_graph_forge(G, -1, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)

    H = spectral_graph_forge(G, 10, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)
    assert_true(is_isomorphic(G, H))

    # invalid transformation mode, checking the error raising
    assert_raises(NetworkXError,
                  spectral_graph_forge,
                  G,
                  0.1,
                  transformation='unknown',
                  seed=seed)
def test_spectral_graph_forge():
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
    scipy = 1

    try:
        import numpy
    except ImportError:
        raise SkipTest('NumPy not available.')
    try:
        import scipy
    except ImportError:
        raise SkipTest("SciPy not available")

    G = karate_club_graph()

    seed = 54321

    # common cases, just checking node number preserving and difference
    # between identity and modularity cases
    H = spectral_graph_forge(G, 0.1, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)

    I = spectral_graph_forge(G, 0.1, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)
    assert_true(is_isomorphic(I, H))

    I = spectral_graph_forge(G, 0.1, transformation='modularity', seed=seed)
    assert_nodes_equal(G, I)

    assert_false(is_isomorphic(I, H))

    # with all the eigenvectors, output graph is identical to the input one
    H = spectral_graph_forge(G, 1, transformation='modularity', seed=seed)
    assert_nodes_equal(G, H)
    assert_true(is_isomorphic(G, H))

    # invalid alpha input value, it is silently truncated in [0,1]
    H = spectral_graph_forge(G, -1, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)

    H = spectral_graph_forge(G, 10, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)
    assert_true(is_isomorphic(G, H))

    # invalid transformation mode, checking the error raising
    assert_raises(NetworkXError,
                  spectral_graph_forge, G, 0.1, transformation='unknown',
                  seed=seed)
def test_spectral_graph_forge():
    numpy = pytest.importorskip('numpy')
    scipy = pytest.importorskip('scipy')

    G = karate_club_graph()

    seed = 54321

    # common cases, just checking node number preserving and difference
    # between identity and modularity cases
    H = spectral_graph_forge(G, 0.1, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)

    I = spectral_graph_forge(G, 0.1, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)
    assert is_isomorphic(I, H)

    I = spectral_graph_forge(G, 0.1, transformation='modularity', seed=seed)
    assert_nodes_equal(G, I)

    assert not is_isomorphic(I, H)

    # with all the eigenvectors, output graph is identical to the input one
    H = spectral_graph_forge(G, 1, transformation='modularity', seed=seed)
    assert_nodes_equal(G, H)
    assert is_isomorphic(G, H)

    # invalid alpha input value, it is silently truncated in [0,1]
    H = spectral_graph_forge(G, -1, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)

    H = spectral_graph_forge(G, 10, transformation='identity', seed=seed)
    assert_nodes_equal(G, H)
    assert is_isomorphic(G, H)

    # invalid transformation mode, checking the error raising
    pytest.raises(NetworkXError,
                  spectral_graph_forge,
                  G,
                  0.1,
                  transformation='unknown',
                  seed=seed)
Esempio n. 14
0
def graph_selector(graph):
    nx_graph = None

    if (graph == ''):
        return None

    # Small Networks
    elif (graph == 'bull_graph'):
        nx_graph = generators.bull_graph()
    elif (graph == 'chvatal_graph'):
        nx_graph = generators.chvatal_graph()
    elif (graph == 'cubical_graph'):
        nx_graph = generators.cubical_graph()
    elif (graph == 'desargues_graph'):
        nx_graph = generators.desargues_graph()
    elif (graph == 'diamond_graph'):
        nx_graph = generators.diamond_graph()
    elif (graph == 'dodecahedral_graph'):
        nx_graph = generators.dodecahedral_graph()
    elif (graph == 'frucht_graph'):
        nx_graph = generators.frucht_graph()
    elif (graph == 'heawood_graph'):
        nx_graph = generators.heawood_graph()
    elif (graph == 'hoffman_singleton_graph'):
        nx_graph = generators.hoffman_singleton_graph()
    elif (graph == 'house_x_graph'):
        nx_graph = generators.house_x_graph()
    elif (graph == 'icosahedral_graph'):
        nx_graph = generators.icosahedral_graph()
    elif (graph == 'krackhardt_kite_graph'):
        nx_graph = generators.krackhardt_kite_graph()
    elif (graph == 'moebius_kantor_graph'):
        nx_graph = generators.moebius_kantor_graph()
    elif (graph == 'octahedral_graph'):
        nx_graph = generators.octahedral_graph()
    elif (graph == 'pappus_graph'):
        nx_graph = generators.pappus_graph()
    elif (graph == 'sedgewick_maze_graph'):
        nx_graph = generators.sedgewick_maze_graph()
    elif (graph == 'tetrahedral_graph'):
        nx_graph = generators.tetrahedral_graph()
    elif (graph == 'truncated_cube_graph'):
        nx_graph = generators.truncated_cube_graph()
    elif (graph == 'truncated_tetrahedron_graph'):
        nx_graph = generators.truncated_tetrahedron_graph()
    elif (graph == 'tutte_graph'):
        nx_graph = generators.tutte_graph()

    # Social Networks
    elif (graph == 'karate_club_graph'):
        nx_graph = generators.karate_club_graph()
    elif (graph == 'davis_southern_women_graph'):
        nx_graph = generators.davis_southern_women_graph()
    elif (graph == 'florentine_families_graph'):
        nx_graph = generators.florentine_families_graph()
    elif (graph == 'les_miserables_graph'):
        nx_graph = generators.les_miserables_graph()

    # else
    else:
        nx_graph = None

    print(nx_graph)

    return nx_graph
def test_supernode_last():
    karate_club = karate_club_graph()
    karate_club_super = add_supernode(karate_club, query=[])
    for i, v in enumerate(karate_club_super):
        if i == len(karate_club_super) - 1:
            assert karate_club_super.graph['label_map'][v] == SUPER_NODE
def test_label_is_permutation():
    karate_club = karate_club_graph()
    G = canonical_relabel_nodes(karate_club)
    assert set(G.nodes()) == set(range(1, len(G) + 1))
    assert set(G.graph['canonical_map']) ==  set(karate_club.nodes())
def test_is_canonical():
    karate_club = karate_club_graph()
    G = canonical_relabel_nodes(karate_club)
    assert is_canonical(G)
    assert not is_canonical(karate_club)
def test_order_is_consistent():
    karate_club = karate_club_graph()
    G = canonical_relabel_nodes(karate_club)
    for i, v in enumerate(G):
        assert i == v - 1, 'Order of nodes changed after canonicalization.'
def test_order_is_consistent():
    karate_club = karate_club_graph()
    G = canonical_relabel_nodes(karate_club)
    for i, v in enumerate(G):
        assert i == v - 1, 'Order of nodes changed after canonicalization.'
def test_is_canonical():
    karate_club = karate_club_graph()
    G = canonical_relabel_nodes(karate_club)
    assert is_canonical(G)
    assert not is_canonical(karate_club)
def test_label_is_permutation():
    karate_club = karate_club_graph()
    G = canonical_relabel_nodes(karate_club)
    assert set(G.nodes()) == set(range(1, len(G) + 1))
    assert set(G.graph['canonical_map']) == set(karate_club.nodes())
def test_supernode_last():
    karate_club = karate_club_graph()
    karate_club_super = add_supernode(karate_club, query=[])
    for i, v in enumerate(karate_club_super):
        if i == len(karate_club_super) - 1:
            assert karate_club_super.graph['label_map'][v] == SUPER_NODE