Ejemplo n.º 1
0
    def test_random_walk_edge_chain_pattern(rng, chain_length):
        """ Test subsampling a chain sub-graph """
        n_iter = max(20, 100 * chain_length)
        multiplier = 4
        graph = DirectedGraph()
        for v in range(chain_length):
            for i in range(multiplier):
                for j in range(multiplier):
                    graph.add_edge((v, i), (v + 1, j))

        subgraph = random_walk_edge_sample(
            graph,
            rng,
            n_iter,
            n_seeds=1,  # any starting point in the chain
            use_opposite=True,
            use_both_ends=True,
            max_in_degree=1,
            max_out_degree=1)
        # Assert graph is a chain of expected length
        assert len(subgraph.edges) == chain_length
        assert (0 <= d <= 1 for _, d in subgraph.out_degree())
        assert (0 <= d <= 1 for _, d in subgraph.in_degree())

        # Find vertices - sorting here should retrieve the chaining
        vertices = sorted(list(subgraph.nodes))
        assert all(v in set((i, m) for m in range(multiplier))
                   for i, v in enumerate(vertices))

        # Inspect linkage
        assert all(e in subgraph.edges for e in zip(vertices, vertices[1:]))
Ejemplo n.º 2
0
    def test_random_walk_edge_star_pattern(rng, max_degree):
        """ Test subsampling of {1, k} -> 0 and then 0 -> {1, k} """
        n_iter = max(100, max_degree * 100)
        if max_degree <= 0:
            max_degree = 10
        graph = DirectedGraph()
        for v in range(1, max_degree * 2):
            graph.add_edge(v, 0)

        subgraph = random_walk_edge_sample(graph,
                                           rng,
                                           n_iter,
                                           n_seeds=1,
                                           use_opposite=False,
                                           use_both_ends=False)
        assert len(subgraph.edges) == 1  # only the seed

        subgraph = random_walk_edge_sample(graph,
                                           rng,
                                           n_iter,
                                           n_seeds=1,
                                           use_opposite=False,
                                           use_both_ends=True)
        assert len(subgraph.edges) == 1  # only the seed

        # Here we activate matching of edges *->0
        # => we can sample almost all the graph
        subgraph = random_walk_edge_sample(graph,
                                           rng,
                                           n_iter,
                                           n_seeds=1,
                                           use_opposite=True,
                                           use_both_ends=False,
                                           max_out_degree=1,
                                           max_in_degree=max_degree)
        assert len(subgraph.edges) == min(max_degree, n_iter)
Ejemplo n.º 3
0
    def test_random_walk_edge_functional_pattern(rng):
        """
        Minimal test case for the different sampling head and direction

        Graph to use should enable precise testing of the options.

        4 -> 0 -> 1 -> 2
        5 <- 0    1 <- 3
        """
        n_iter = 100  # override fixture to stabilize the tests

        graph = DirectedGraph()
        graph.add_edge(0, 1)
        graph.add_edge(1, 2)
        graph.add_edge(3, 1)
        graph.add_edge(4, 0)
        graph.add_edge(0, 5)

        # Step 1 - use_opposite=False, use_both_ends=False
        # From (0, 1), we should sample only edges starting from 1
        expected_subgraph = DirectedGraph()
        expected_subgraph.add_edge(0, 1)
        expected_subgraph.add_edge(1, 2)
        subgraph = random_walk_edge_sample(graph,
                                           rng,
                                           n_iter,
                                           seeds=[(0, 1)],
                                           use_opposite=False,
                                           use_both_ends=False)
        assert subgraph == expected_subgraph

        # Step 2 - use_opposite=False, use_both_ends=True
        # From (0, 1), we should sample only edges starting from 0 or 1
        expected_subgraph = DirectedGraph()
        expected_subgraph.add_edge(0, 1)
        expected_subgraph.add_edge(1, 2)
        expected_subgraph.add_edge(0, 5)
        subgraph = random_walk_edge_sample(graph,
                                           rng,
                                           n_iter,
                                           seeds=[(0, 1)],
                                           use_opposite=False,
                                           use_both_ends=True)
        assert subgraph == expected_subgraph

        # Step 3 - use_opposite=True, use_both_ends=False
        expected_subgraph = DirectedGraph()
        expected_subgraph.add_edge(0, 1)
        expected_subgraph.add_edge(1, 2)
        expected_subgraph.add_edge(3, 1)
        subgraph = random_walk_edge_sample(graph,
                                           rng,
                                           n_iter,
                                           seeds=[(0, 1)],
                                           use_opposite=True,
                                           use_both_ends=False)
        assert subgraph == expected_subgraph

        # Step 4 - use_opposite=True, use_both_ends=True => catch all
        subgraph = random_walk_edge_sample(graph,
                                           rng,
                                           n_iter,
                                           seeds=[(0, 1)],
                                           use_opposite=True,
                                           use_both_ends=True)
        assert subgraph == graph