コード例 #1
0
def test_iteration_depth_node_edge_attr():
    """
    All nodes should have the correct number of subgraph hashes in the output when
    setting initial node labels to an attribute and also using an edge attribute when
    aggregating neighborhoods.
    Subsequent iteration depths for the same graph should be additive for each node
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G = nx.erdos_renyi_graph(n, p * i, seed=900 + i)

        for u in G.nodes():
            G.nodes[u]["node_attr1"] = f"{u}-1"

        for a, b in G.edges:
            G[a][b]["edge_attr1"] = f"{a}-{b}-1"

        depth3 = nx.weisfeiler_lehman_subgraph_hashes(
            G, edge_attr="edge_attr1", node_attr="node_attr1", iterations=3
        )
        depth4 = nx.weisfeiler_lehman_subgraph_hashes(
            G, edge_attr="edge_attr1", node_attr="node_attr1", iterations=4
        )
        depth5 = nx.weisfeiler_lehman_subgraph_hashes(
            G, edge_attr="edge_attr1", node_attr="node_attr1", iterations=5
        )

        assert all(len(hashes) == 3 for hashes in depth3.values())
        assert all(len(hashes) == 4 for hashes in depth4.values())
        assert all(len(hashes) == 5 for hashes in depth5.values())

        assert is_subiteration(depth3, depth4)
        assert is_subiteration(depth4, depth5)
        assert is_subiteration(depth3, depth5)
コード例 #2
0
def test_directed_subgraph_hash():
    """
    A directed graph with no bi-directional edges should yield different subgraph hashes
    to the same graph taken as undirected, if all hashes don't collide.
    """
    r = 10
    for i in range(r):
        G_directed = nx.gn_graph(10 + r, seed=100 + i)
        G_undirected = nx.to_undirected(G_directed)

        directed_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G_directed)
        undirected_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G_undirected)

        assert directed_subgraph_hashes != undirected_subgraph_hashes
コード例 #3
0
def test_isomorphic_subgraph_hash():
    """
    the subgraph hashes should be invariant to node-relabeling when the output is reindexed
    by the same mapping and all hashes don't collide.
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=200 + i)
        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g1_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G1)
        g2_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G2)

        assert g1_subgraph_hashes == {-1 * k: v for k, v in g2_subgraph_hashes.items()}
コード例 #4
0
def test_reversed_subgraph_hash():
    """
    A directed graph with no bi-directional edges should yield different subgraph hashes
    to the same graph taken with edge directions reversed if there are no hash collisions.
    Here we test a cycle graph which is the minimal counterexample
    """
    G = nx.cycle_graph(5, create_using=nx.DiGraph)
    nx.set_node_attributes(G, {n: str(n) for n in G.nodes()}, name="label")

    G_reversed = G.reverse()

    h = nx.weisfeiler_lehman_subgraph_hashes(G, node_attr="label")
    h_reversed = nx.weisfeiler_lehman_subgraph_hashes(G_reversed, node_attr="label")

    assert h != h_reversed
コード例 #5
0
def test_digest_size_subgraph_hash():
    """
    The hash string lengths should be as expected for a variety of graphs and
    digest sizes
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G = nx.erdos_renyi_graph(n, p * i, seed=1000 + i)

        digest_size16_hashes = nx.weisfeiler_lehman_subgraph_hashes(G)
        digest_size32_hashes = nx.weisfeiler_lehman_subgraph_hashes(G, digest_size=32)

        assert digest_size16_hashes != digest_size32_hashes

        assert hexdigest_sizes_correct(digest_size16_hashes, 16)
        assert hexdigest_sizes_correct(digest_size32_hashes, 32)
コード例 #6
0
def test_empty_graph_subgraph_hash():
    """ "
    empty graphs should give empty dict subgraph hashes regardless of other params
    """
    G = nx.empty_graph()

    subgraph_hashes1 = nx.weisfeiler_lehman_subgraph_hashes(G)
    subgraph_hashes2 = nx.weisfeiler_lehman_subgraph_hashes(G, edge_attr="edge_attr")
    subgraph_hashes3 = nx.weisfeiler_lehman_subgraph_hashes(G, node_attr="edge_attr")
    subgraph_hashes4 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=2)
    subgraph_hashes5 = nx.weisfeiler_lehman_subgraph_hashes(G, digest_size=64)

    assert subgraph_hashes1 == {}
    assert subgraph_hashes2 == {}
    assert subgraph_hashes3 == {}
    assert subgraph_hashes4 == {}
    assert subgraph_hashes5 == {}
コード例 #7
0
def test_isomorphic_node_attr_subgraph_hash():
    """
    Isomorphic graphs with differing node attributes should yield different subgraph
    hashes if the 'node_attr' argument is supplied and populated in the graph, and
    all hashes don't collide.
    The output should still be invariant to node-relabeling
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=400 + i)

        for u in G1.nodes():
            G1.nodes[u]["node_attr1"] = f"{u}-1"
            G1.nodes[u]["node_attr2"] = f"{u}-2"

        g1_hash_with_node_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, node_attr="node_attr1"
        )
        g1_hash_with_node_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, node_attr="node_attr2"
        )
        g1_hash_no_node_attr = nx.weisfeiler_lehman_subgraph_hashes(G1, node_attr=None)

        assert g1_hash_with_node_attr1 != g1_hash_no_node_attr
        assert g1_hash_with_node_attr2 != g1_hash_no_node_attr
        assert g1_hash_with_node_attr1 != g1_hash_with_node_attr2

        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g2_hash_with_node_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, node_attr="node_attr1"
        )
        g2_hash_with_node_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, node_attr="node_attr2"
        )

        assert g1_hash_with_node_attr1 == {
            -1 * k: v for k, v in g2_hash_with_node_attr1.items()
        }
        assert g1_hash_with_node_attr2 == {
            -1 * k: v for k, v in g2_hash_with_node_attr2.items()
        }
コード例 #8
0
def test_iteration_depth():
    """
    All nodes should have the correct number of subgraph hashes in the output when
    using degree as initial node labels
    Subsequent iteration depths for the same graph should be additive for each node
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G = nx.erdos_renyi_graph(n, p * i, seed=600 + i)

        depth3 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=3)
        depth4 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=4)
        depth5 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=5)

        assert all(len(hashes) == 3 for hashes in depth3.values())
        assert all(len(hashes) == 4 for hashes in depth4.values())
        assert all(len(hashes) == 5 for hashes in depth5.values())

        assert is_subiteration(depth3, depth4)
        assert is_subiteration(depth4, depth5)
        assert is_subiteration(depth3, depth5)
コード例 #9
0
def test_isomorphic_edge_attr_and_node_attr():
    """
    Isomorphic graphs with differing node attributes should yield different subgraph
    hashes if the 'node_attr' and 'edge_attr' argument is supplied and populated in
    the graph, and all hashes don't collide
    The output should still be invariant to node-relabeling
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=500 + i)

        for u in G1.nodes():
            G1.nodes[u]["node_attr1"] = f"{u}-1"
            G1.nodes[u]["node_attr2"] = f"{u}-2"

        for a, b in G1.edges:
            G1[a][b]["edge_attr1"] = f"{a}-{b}-1"
            G1[a][b]["edge_attr2"] = f"{a}-{b}-2"

        g1_hash_edge1_node1 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, edge_attr="edge_attr1", node_attr="node_attr1"
        )
        g1_hash_edge2_node2 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, edge_attr="edge_attr2", node_attr="node_attr2"
        )
        g1_hash_edge1_node2 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, edge_attr="edge_attr1", node_attr="node_attr2"
        )
        g1_hash_no_attr = nx.weisfeiler_lehman_subgraph_hashes(G1)

        assert g1_hash_edge1_node1 != g1_hash_no_attr
        assert g1_hash_edge2_node2 != g1_hash_no_attr
        assert g1_hash_edge1_node1 != g1_hash_edge2_node2
        assert g1_hash_edge1_node2 != g1_hash_edge2_node2
        assert g1_hash_edge1_node2 != g1_hash_edge1_node1

        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g2_hash_edge1_node1 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, edge_attr="edge_attr1", node_attr="node_attr1"
        )
        g2_hash_edge2_node2 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, edge_attr="edge_attr2", node_attr="node_attr2"
        )

        assert g1_hash_edge1_node1 == {
            -1 * k: v for k, v in g2_hash_edge1_node1.items()
        }
        assert g1_hash_edge2_node2 == {
            -1 * k: v for k, v in g2_hash_edge2_node2.items()
        }