コード例 #1
0
ファイル: test_util.py プロジェクト: thejonaslab/tinygraph
def test_permute():
    """
    Test permuting a graph to be equal to another, or permuting back and forth
    to stay equal to itself.
    """
    g1 = tg.TinyGraph(5,
                      np.int32,
                      vp_types={'color': np.int32},
                      ep_types={'color2': np.int32})
    g2 = tg.TinyGraph(5,
                      np.int32,
                      vp_types={'color': np.int32},
                      ep_types={'color2': np.int32})
    g1[0, 1] = 5
    g2[3, 4] = 5
    g1[2, 3] = 1
    g2[1, 2] = 1
    g1.v['color'][0] = 10
    g2.v['color'][4] = 10
    g1.e['color2'][2, 3] = 4
    g2.e['color2'][2, 1] = 4

    pG11 = permute(g1, [3, 4, 1, 2, 0])
    pG12 = permute(g1, [4, 3, 1, 2, 0])
    pG13 = permute(pG11, [4, 2, 3, 0, 1])

    assert not graph_equality(g2, pG11)
    assert graph_equality(g2, pG12)
    assert graph_equality(g1, pG13)
コード例 #2
0
ファイル: test_util.py プロジェクト: thejonaslab/tinygraph
def test_merge_identity():
    """Merge with an empty graph"""
    g1 = graph_test_suite.gen_random(5, np.bool, [True], 0.5)
    g2 = tg.TinyGraph(0, np.bool)

    gg = merge(g1, g2)
    gh = merge(g2, g1)

    assert graph_equality(g1, gg)
    assert graph_equality(g1, gh)
コード例 #3
0
ファイル: test_util.py プロジェクト: thejonaslab/tinygraph
def test_subgraph_empty():
    """Limiting case of an empty graph"""
    g = graph_test_suite.gen_random(5, np.bool, [True], 0.5)
    sg = subgraph(g, [])

    assert sg.vert_N == 0
    assert not graph_equality(g, sg)
コード例 #4
0
ファイル: test_util.py プロジェクト: thejonaslab/tinygraph
def test_permutation_inversion_suite(test_name):
    """Use the graph suite to permute and un-permute a graph"""
    rng = np.random.RandomState(0)

    for g in suite[test_name]:
        # Get order and a random permutation
        N = g.vert_N
        perm = rng.permutation(N)
        inv_perm = np.argsort(perm)

        # Permute the permutation
        # m e t a
        order = rng.permutation(N)
        inv_perm_dict = dict(zip(np.arange(N)[order], inv_perm[order]))

        # Use the list version
        h = permute(g, perm)
        g2 = permute(h, inv_perm)
        assert graph_equality(g, g2)

        # Use the dict version
        g3 = permute(h, inv_perm_dict)
        assert graph_equality(g, g3)
コード例 #5
0
ファイル: test_util.py プロジェクト: thejonaslab/tinygraph
def test_permute_path():
    """Another case to ensure permutations work in the expected direction"""
    g = tg.TinyGraph(4, np.bool, vp_types={'name': np.dtype('<U20')})
    perm = [2, 3, 1, 0]
    inv_perm = np.argsort(perm)  # [3, 2, 0, 1]
    assert np.array_equal(inv_perm, [3, 2, 0, 1])
    g.v['name'][:] = ['a', 'b', 'c', 'd']
    g[0, 1] = 1
    g[1, 2] = 1
    g[2, 3] = 1

    h = permute(g, perm)
    assert list(h.v['name']) == ['d', 'c', 'a', 'b']
    gp = permute(h, inv_perm)
    assert graph_equality(g, gp)
コード例 #6
0
ファイル: test_util.py プロジェクト: thejonaslab/tinygraph
def test_permute_identity():
    """Ensure that the identity permutation preserves graph equality"""
    g1 = tg.TinyGraph(5,
                      np.int32,
                      vp_types={'color': np.int32},
                      ep_types={'color2': np.int32})
    g1[0, 1] = 5
    g1[1, 3] = 3
    g1[2, 3] = 1
    g1[0, 4] = 2
    g1.v['color'][0] = 10
    g1.e['color2'][2, 3] = 4

    g2 = permute(g1, [0, 1, 2, 3, 4])

    assert graph_equality(g1, g2)
コード例 #7
0
ファイル: test_util.py プロジェクト: thejonaslab/tinygraph
def test_subgraph_suite(test_name):
    """Use the test suite to grab subgraphs"""
    rng = np.random.RandomState(0)

    for g in suite[test_name]:
        # Get order and a random permutation
        N = g.vert_N
        SN = rng.randint(N)
        print(SN)
        vert_list = rng.choice(N, SN, replace=False)
        vertices = sorted(vert_list)

        h = subgraph(g, vertices)

        # Check global properties
        assert h.props == g.props

        # Check vertex properties
        for n, _ in h.vertices():
            for k in g.v.keys():
                assert h.v[k][n] == g.v[k][vertices[n]]

        # Check edge properties and weights
        for n1, n2 in h.edges():
            assert h[n1, n2] == g[vertices[n1], vertices[n2]]
            for k in g.e.keys():
                assert h.e[k][n1, n2] == g.e[k][vertices[n1], vertices[n2]]

        # Check behavior with vertices
        g.add_vert_prop('old_vertex_id', np.int)
        g.v['old_vertex_id'][:] = np.arange(N)

        # Unclear what ordering will result from iterating over vertset
        # so just check that we can reconstruct the original
        vertset = rng.permutation(vert_list)
        h_set = subgraph(g, vertset)

        # Attempt to reconstruct the subgraph `h` made with `vertices`.
        # Note that h_set.v['old_vertex_id'] has indices that lie outside
        # the normal range. So, we perform an argsort and a reverse argsort
        # which brings all the indices in range and preserves the order.
        order = np.argsort(np.argsort(h_set.v['old_vertex_id']))
        h_rec = permute(h_set, order)
        h_rec.remove_vert_prop('old_vertex_id')
        g.remove_vert_prop('old_vertex_id')

        assert graph_equality(h, h_rec)
コード例 #8
0
ファイル: test_util.py プロジェクト: thejonaslab/tinygraph
def test_subgraph_complete():
    """Limiting case of a full graph"""
    g = graph_test_suite.gen_random(5, np.bool, [True], 0.5)
    sg = subgraph(g, range(5))

    assert graph_equality(g, sg)