def test_grow_maximal_weight_tie(self, dim, monkeypatch):
        """Test if function grows using randomness to break ties during weight-based node
        selection. The chosen graph is a fully connected graph where the final three nodes have
        subsequently been disconnected from each other, but remain connected to all the other
        nodes. We then start from the clique composed of all but the final three nodes and seek
        to grow. In this construction, we can add just one of the final three nodes. This test
        gives every node the same weight, so we expect that they should be selected randomly with
        equal probability. This test monkeypatches the ``np.random.choice`` call to guarantee
        that one of the nodes is picked during one run of ``grow`` and the another node is picked
        during the next run."""
        graph = nx.complete_graph(dim)
        s = graph.subgraph([dim - 3, dim - 2, dim - 1])
        for e in s.edges():
            graph.remove_edge(*e)

        s = set(range(dim - 3))
        weights = [1 for _ in range(dim)]

        patch_random_choice_1 = functools.partial(patch_random_choice, element=0)
        patch_random_choice_2 = functools.partial(patch_random_choice, element=1)

        with monkeypatch.context() as m:
            m.setattr(np.random, "choice", patch_random_choice_1)
            c1 = clique.grow(s, graph, node_select=weights)

        with monkeypatch.context() as m:
            m.setattr(np.random, "choice", patch_random_choice_2)
            c2 = clique.grow(s, graph, node_select=weights)

        target1 = list(s | {dim - 3})
        target2 = list(s | {dim - 2})
        assert c1 != c2
        assert target1 == c1 and target2 == c2
 def test_bad_weights(self, dim, graph):
     """Test if function raises a ``ValueError`` when a vector of node weights input to
     ``node_select`` is not of the same dimension as the input graph."""
     s = [0, 1]
     w = np.ones(dim - 1)
     with pytest.raises(ValueError, match="Number of node weights must match number of nodes"):
         clique.grow(s, graph, node_select=w)
 def test_bad_node_select(self, dim):
     """Tests if function raises a ``ValueError`` when input an invalid ``node_select``
     argument"""
     graph = nx.barbell_graph(dim, 0)
     s = [0]
     with pytest.raises(ValueError, match="Node selection method not recognized"):
         clique.grow(s, graph, node_select="")
예제 #4
0
    def test_grow_maximal_degree_tie(self, dim, monkeypatch):
        """Test if function grows using randomness to break ties during degree-based node
        selection. The chosen graph is a fully connected graph with the ``dim - 2`` and ``dim -
        1`` nodes then disconnected. Starting from the first ``dim - 3`` nodes, one can add
        either of the ``dim - 2`` and ``dim - 1`` nodes. As they have the same degree, they should
        be selected randomly with equal probability. This function monkeypatches the
        ``np.random.choice`` call to guarantee that one of the nodes is picked during one run of
        ``grow`` and the other node is picked during the next run."""
        graph = nx.complete_graph(dim)
        graph.remove_edge(dim - 2, dim - 1)
        s = set(range(dim - 2))

        patch_random_choice_1 = functools.partial(patch_random_choice,
                                                  element=0)
        patch_random_choice_2 = functools.partial(patch_random_choice,
                                                  element=1)

        with monkeypatch.context() as m:
            m.setattr(np.random, "choice", patch_random_choice_1)
            c1 = clique.grow(s, graph, node_select="degree")

        with monkeypatch.context() as m:
            m.setattr(np.random, "choice", patch_random_choice_2)
            c2 = clique.grow(s, graph, node_select="degree")

        assert c1 != c2
 def test_grow_maximal(self, dim):
     """Test if function grows to expected maximal graph and then stops. The chosen graph is
     composed of two fully connected graphs joined together at one node. Starting from the
     first node, ``grow`` is expected to grow to be the first fully connected graph."""
     graph = nx.barbell_graph(dim, 0)
     s = [0]
     assert set(clique.grow(s, graph)) == set(range(dim))
 def test_grow_maximal_degree(self, dim):
     """Test if function grows to expected maximal graph when degree-based node selection is
     used. The chosen graph is a fully connected graph with only the final node being
     connected to an additional node. Furthermore, the ``dim - 2`` node is disconnected from
     the ``dim - 1`` node. Starting from the first ``dim - 3`` nodes, one can either add in
     the ``dim - 2`` node or the ``dim - 1`` node. The ``dim - 1`` node has a higher degree
     due to the lollipop graph structure, and hence should be selected."""
     graph = nx.lollipop_graph(dim, 1)
     graph.remove_edge(dim - 2, dim - 1)
     s = set(range(dim - 2))
     target = s | {dim - 1}
     assert set(clique.grow(s, graph, node_select="degree")) == target
    def test_grow_maximal_weight(self, dim):
        """Test if function grows to expected maximal graph when weight-based node selection is
        used. The chosen graph is a fully connected graph where the final three nodes have
        subsequently been disconnected from each other, but remain connected to all the other
        nodes. We then start from the clique composed of all but the final three nodes and seek
        to grow. In this construction, we can add just one of the final three nodes. This test
        gives the final node the largest weight, so we expect that one to be added."""
        graph = nx.complete_graph(dim)
        s = graph.subgraph([dim - 3, dim - 2, dim - 1])
        for e in s.edges():
            graph.remove_edge(*e)

        s = set(range(dim - 3))
        weights = list(range(dim))
        target = s | {dim - 1}
        assert set(clique.grow(s, graph, node_select=weights)) == target
 def test_input_not_subgraph(self, dim):
     """Test if function raises a ``ValueError`` when input is not a subgraph"""
     with pytest.raises(ValueError, match="Input is not a valid subgraph"):
         clique.grow([dim + 1], nx.empty_graph(dim))
 def test_input_not_clique(self, dim):
     """Tests if function raises a ``ValueError`` when input is not a clique"""
     with pytest.raises(ValueError, match="Input subgraph is not a clique"):
         clique.grow([0, 1], nx.empty_graph(dim))