Example #1
0
    def test_update(self):
        # specify both edgees and nodes
        G = self.K3.copy()
        G.update(nodes=[3, (4, {
            'size': 2
        })],
                 edges=[(4, 5), (6, 7, {
                     'weight': 2
                 })])
        nlist = [(0, {}), (1, {}), (2, {}), (3, {}), (4, {
            'size': 2
        }), (5, {}), (6, {}), (7, {})]
        assert sorted(G.nodes.data()) == nlist
        if G.is_directed():
            elist = [(0, 1, {}), (0, 2, {}), (1, 0, {}), (1, 2, {}),
                     (2, 0, {}), (2, 1, {}), (4, 5, {}), (6, 7, {
                         'weight': 2
                     })]
        else:
            elist = [(0, 1, {}), (0, 2, {}), (1, 2, {}), (4, 5, {}),
                     (6, 7, {
                         'weight': 2
                     })]
        assert sorted(G.edges.data()) == elist
        assert G.graph == {}

        # no keywords -- order is edges, nodes
        G = self.K3.copy()
        G.update([(4, 5), (6, 7, {'weight': 2})], [3, (4, {'size': 2})])
        assert sorted(G.nodes.data()) == nlist
        assert sorted(G.edges.data()) == elist
        assert G.graph == {}

        # update using only a graph
        G = self.Graph()
        G.graph['foo'] = 'bar'
        G.add_node(2, data=4)
        G.add_edge(0, 1, weight=0.5)
        GG = G.copy()
        H = self.Graph()
        GG.update(H)
        assert_graphs_equal(G, GG)
        H.update(G)
        assert_graphs_equal(H, G)

        # update nodes only
        H = self.Graph()
        H.update(nodes=[3, 4])
        assert H.nodes ^ {3, 4} == set([])
        assert H.size() == 0

        # update edges only
        H = self.Graph()
        H.update(edges=[(3, 4)])
        assert sorted(H.edges.data()) == [(3, 4, {})]
        assert H.size() == 1

        # No inputs -> exception
        with pytest.raises(nx.NetworkXError):
            nx.Graph().update()
Example #2
0
    def test_symmetric(self):
        """Tests that a symmetric array has edges added only once to an
        undirected multigraph when using :func:`networkx.from_numpy_array`.

        """
        A = np.array([[0, 1], [1, 0]])
        G = nx.from_numpy_array(A, create_using=nx.MultiGraph())
        expected = nx.MultiGraph()
        expected.add_edge(0, 1, weight=1)
        assert_graphs_equal(G, expected)
Example #3
0
    def test_symmetric(self):
        """Tests that a symmetric matrix has edges added only once to an
        undirected multigraph when using :func:`networkx.from_numpy_matrix`.

        """
        A = np.matrix([[0, 1], [1, 0]])
        G = nx.from_numpy_matrix(A, create_using=nx.MultiGraph())
        expected = nx.MultiGraph()
        expected.add_edge(0, 1, weight=1)
        assert_graphs_equal(G, expected)
Example #4
0
 def test_protocol(self):
     for G in [self.G, self.DG, self.MG, self.MDG,
               self.fG, self.fDG, self.fMG, self.fMDG]:
         with tempfile.TemporaryFile() as f:
             nx.write_gpickle(G, f, 0)
             f.seek(0)
             Gin = nx.read_gpickle(f)
             assert_nodes_equal(list(G.nodes(data=True)),
                                list(Gin.nodes(data=True)))
             assert_edges_equal(list(G.edges(data=True)),
                                list(Gin.edges(data=True)))
             assert_graphs_equal(G, Gin)
Example #5
0
 def test_gpickle(self):
     for G in [self.G, self.DG, self.MG, self.MDG,
               self.fG, self.fDG, self.fMG, self.fMDG]:
         (fd, fname) = tempfile.mkstemp()
         nx.write_gpickle(G, fname)
         Gin = nx.read_gpickle(fname)
         assert_nodes_equal(list(G.nodes(data=True)),
                            list(Gin.nodes(data=True)))
         assert_edges_equal(list(G.edges(data=True)),
                            list(Gin.edges(data=True)))
         assert_graphs_equal(G, Gin)
         os.close(fd)
         os.unlink(fname)
Example #6
0
    def test_from_numpy_matrix_parallel_edges(self):
        """Tests that the :func:`networkx.from_numpy_matrix` function
        interprets integer weights as the number of parallel edges when
        creating a multigraph.

        """
        A = np.matrix([[1, 1], [1, 2]])
        # First, with a simple graph, each integer entry in the adjacency
        # matrix is interpreted as the weight of a single edge in the graph.
        expected = nx.DiGraph()
        edges = [(0, 0), (0, 1), (1, 0)]
        expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
        expected.add_edge(1, 1, weight=2)
        actual = nx.from_numpy_matrix(A, parallel_edges=True,
                                      create_using=nx.DiGraph())
        assert_graphs_equal(actual, expected)
        actual = nx.from_numpy_matrix(A, parallel_edges=False,
                                      create_using=nx.DiGraph())
        assert_graphs_equal(actual, expected)
        # Now each integer entry in the adjacency matrix is interpreted as the
        # number of parallel edges in the graph if the appropriate keyword
        # argument is specified.
        edges = [(0, 0), (0, 1), (1, 0), (1, 1), (1, 1)]
        expected = nx.MultiDiGraph()
        expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
        actual = nx.from_numpy_matrix(A, parallel_edges=True,
                                      create_using=nx.MultiDiGraph())
        assert_graphs_equal(actual, expected)
        expected = nx.MultiDiGraph()
        expected.add_edges_from(set(edges), weight=1)
        # The sole self-loop (edge 0) on vertex 1 should have weight 2.
        expected[1][1][0]['weight'] = 2
        actual = nx.from_numpy_matrix(A, parallel_edges=False,
                                      create_using=nx.MultiDiGraph())
        assert_graphs_equal(actual, expected)
Example #7
0
    def test_from_numpy_array_parallel_edges(self):
        """Tests that the :func:`networkx.from_numpy_array` function
        interprets integer weights as the number of parallel edges when
        creating a multigraph.

        """
        A = np.array([[1, 1], [1, 2]])
        # First, with a simple graph, each integer entry in the adjacency
        # matrix is interpreted as the weight of a single edge in the graph.
        expected = nx.DiGraph()
        edges = [(0, 0), (0, 1), (1, 0)]
        expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
        expected.add_edge(1, 1, weight=2)
        actual = nx.from_numpy_array(A, parallel_edges=True, create_using=nx.DiGraph)
        assert_graphs_equal(actual, expected)
        actual = nx.from_numpy_array(A, parallel_edges=False, create_using=nx.DiGraph)
        assert_graphs_equal(actual, expected)
        # Now each integer entry in the adjacency matrix is interpreted as the
        # number of parallel edges in the graph if the appropriate keyword
        # argument is specified.
        edges = [(0, 0), (0, 1), (1, 0), (1, 1), (1, 1)]
        expected = nx.MultiDiGraph()
        expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
        actual = nx.from_numpy_array(
            A, parallel_edges=True, create_using=nx.MultiDiGraph
        )
        assert_graphs_equal(actual, expected)
        expected = nx.MultiDiGraph()
        expected.add_edges_from(set(edges), weight=1)
        # The sole self-loop (edge 0) on vertex 1 should have weight 2.
        expected[1][1][0]["weight"] = 2
        actual = nx.from_numpy_array(
            A, parallel_edges=False, create_using=nx.MultiDiGraph
        )
        assert_graphs_equal(actual, expected)
Example #8
0
    def test_update(self):
        # specify both edges and nodes
        G = self.K3.copy()
        G.update(nodes=[3, (4, {
            "size": 2
        })],
                 edges=[(4, 5), (6, 7, {
                     "weight": 2
                 })])
        nlist = [
            (0, {}),
            (1, {}),
            (2, {}),
            (3, {}),
            (4, {
                "size": 2
            }),
            (5, {}),
            (6, {}),
            (7, {}),
        ]
        assert sorted(G.nodes.data()) == nlist
        if G.is_directed():
            elist = [
                (0, 1, {}),
                (0, 2, {}),
                (1, 0, {}),
                (1, 2, {}),
                (2, 0, {}),
                (2, 1, {}),
                (4, 5, {}),
                (6, 7, {
                    "weight": 2
                }),
            ]
        else:
            if os.environ.get("DEPLOYMENT", None) == "standalone":
                elist = [
                    (0, 1, {}),
                    (0, 2, {}),
                    (1, 2, {}),
                    (4, 5, {}),
                    (6, 7, {
                        "weight": 2
                    }),
                ]
            else:  # num_workers=2, N.B: diff with _TestGraph, update the order of id
                elist = [
                    (1, 0, {}),
                    (1, 2, {}),
                    (2, 0, {}),
                    (5, 4, {}),
                    (7, 6, {
                        "weight": 2
                    }),
                ]
        assert sorted(G.edges.data()) == elist
        assert G.graph == {}

        # no keywords -- order is edges, nodes
        G = self.K3.copy()
        G.update([(4, 5), (6, 7, {"weight": 2})], [3, (4, {"size": 2})])
        assert sorted(G.nodes.data()) == nlist
        assert sorted(G.edges.data()) == elist
        assert G.graph == {}

        # update using only a graph
        G = self.Graph()
        G.graph["foo"] = "bar"
        G.add_node(2, data=4)
        G.add_edge(0, 1, weight=0.5)
        GG = G.copy()
        H = self.Graph()
        GG.update(H)
        assert_graphs_equal(G, GG)
        H.update(G)
        assert_graphs_equal(H, G)

        # update nodes only
        H = self.Graph()
        H.update(nodes=[3, 4])
        assert H.nodes ^ {3, 4} == set()
        assert H.size() == 0

        # update edges only
        H = self.Graph()
        H.update(edges=[(3, 4)])
        if H.is_directed():
            assert sorted(H.edges.data()) == [(3, 4, {})]
        else:
            assert sorted(H.edges.data()) in ([(3, 4, {})], [(4, 3, {})])
        assert H.size() == 1

        # No inputs -> exception
        with pytest.raises(nx.NetworkXError):
            nx.Graph().update()