Exemplo n.º 1
0
    def test_graph(self):
        g = nx.cycle_graph(10)
        G = nx.Graph()
        G.add_nodes_from(g)
        G.add_weighted_edges_from((u, v, u) for u, v in g.edges())

        # Dict of dicts
        dod = to_dict_of_dicts(G)
        GG = from_dict_of_dicts(dod, create_using=nx.Graph)
        assert nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
        assert edges_equal(sorted(G.edges()), sorted(GG.edges()))
        GW = to_networkx_graph(dod, create_using=nx.Graph)
        assert nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
        assert edges_equal(sorted(G.edges()), sorted(GW.edges()))
        GI = nx.Graph(dod)
        assert sorted(G.nodes()) == sorted(GI.nodes())
        assert sorted(G.edges()) == sorted(GI.edges())

        # Dict of lists
        dol = to_dict_of_lists(G)
        GG = from_dict_of_lists(dol, create_using=nx.Graph)
        # dict of lists throws away edge data so set it to none
        enone = [(u, v, {}) for (u, v, d) in G.edges(data=True)]
        assert nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
        assert edges_equal(enone, sorted(GG.edges(data=True)))
        GW = to_networkx_graph(dol, create_using=nx.Graph)
        assert nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
        assert edges_equal(enone, sorted(GW.edges(data=True)))
        GI = nx.Graph(dol)
        assert nodes_equal(sorted(G.nodes()), sorted(GI.nodes()))
        assert edges_equal(enone, sorted(GI.edges(data=True)))
Exemplo n.º 2
0
    def test_from_edgelist(self):
        # Pandas DataFrame
        G = nx.cycle_graph(10)
        G.add_weighted_edges_from((u, v, u) for u, v in list(G.edges))

        edgelist = nx.to_edgelist(G)
        source = []
        target = []
        weight = []
        # N.B the iterate order of edgelist may not all the same
        for s, t, d in edgelist:
            source.append(s)
            target.append(t)
            weight.append(d["weight"])
        edges = pd.DataFrame({
            "source": source,
            "target": target,
            "weight": weight
        })

        GG = nx.from_pandas_edgelist(edges, edge_attr="weight")
        assert nodes_equal(G.nodes(), GG.nodes())
        assert edges_equal(G.edges(), GG.edges())
        GW = nx.to_networkx_graph(edges, create_using=nx.Graph)
        assert nodes_equal(G.nodes(), GW.nodes())
        assert edges_equal(G.edges(), GW.edges())
Exemplo n.º 3
0
 def test_find_cores(self):
     core = nx.find_cores(self.G)
     nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(4)]
     assert nodes_equal(nodes_by_core[0], [21])
     assert nodes_equal(nodes_by_core[1], [17, 18, 19, 20])
     assert nodes_equal(nodes_by_core[2], [9, 10, 11, 12, 13, 14, 15, 16])
     assert nodes_equal(nodes_by_core[3], [1, 2, 3, 4, 5, 6, 7, 8])
Exemplo n.º 4
0
    def test_add_star(self):
        G = self.G.copy()
        nlist = [12, 13, 14, 15]
        nx.add_star(G, nlist)
        assert edges_equal(G.edges(nlist), [(12, 13), (12, 14), (12, 15)])

        G = self.G.copy()
        nx.add_star(G, nlist, weight=2.0)
        assert edges_equal(
            G.edges(nlist, data=True),
            [
                (12, 13, {
                    "weight": 2.0
                }),
                (12, 14, {
                    "weight": 2.0
                }),
                (12, 15, {
                    "weight": 2.0
                }),
            ],
        )

        G = self.G.copy()
        nlist = [12]
        nx.add_star(G, nlist)
        assert nodes_equal(G, list(self.G) + nlist)

        G = self.G.copy()
        nlist = []
        nx.add_star(G, nlist)
        assert nodes_equal(G.nodes, self.Gnodes)
        assert edges_equal(G.edges, self.G.edges)
Exemplo n.º 5
0
    def test_digraphs(self):
        for dest, source in [
            (to_dict_of_dicts, from_dict_of_dicts),
            (to_dict_of_lists, from_dict_of_lists),
        ]:
            G = cycle_graph(10)

            # Dict of [dicts, lists]
            dod = dest(G)
            GG = source(dod)
            assert nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
            assert edges_equal(sorted(G.edges()), sorted(GG.edges()))
            GW = to_networkx_graph(dod)
            assert nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
            assert edges_equal(sorted(G.edges()), sorted(GW.edges()))
            GI = nx.Graph(dod)
            assert nodes_equal(sorted(G.nodes()), sorted(GI.nodes()))
            assert edges_equal(sorted(G.edges()), sorted(GI.edges()))

            G = cycle_graph(10, create_using=nx.DiGraph)
            dod = dest(G)
            GG = source(dod, create_using=nx.DiGraph)
            assert sorted(G.nodes()) == sorted(GG.nodes())
            assert sorted(G.edges()) == sorted(GG.edges())
            GW = to_networkx_graph(dod, create_using=nx.DiGraph)
            assert sorted(G.nodes()) == sorted(GW.nodes())
            assert sorted(G.edges()) == sorted(GW.edges())
            GI = nx.DiGraph(dod)
            assert sorted(G.nodes()) == sorted(GI.nodes())
            assert sorted(G.edges()) == sorted(GI.edges())
Exemplo n.º 6
0
 def test_find_cores2(self):
     core = nx.find_cores(self.H)
     nodes_by_core = [
         sorted([n for n in core if core[n] == val]) for val in range(3)
     ]
     assert nodes_equal(nodes_by_core[0], [0])
     assert nodes_equal(nodes_by_core[1], [1, 3])
     assert nodes_equal(nodes_by_core[2], [2, 4, 5, 6])
Exemplo n.º 7
0
 def test_path_projected_graph(self):
     G = nx.path_graph(4)
     P = bipartite.projected_graph(G, [1, 3])
     assert nodes_equal(list(P), [1, 3])
     assert edges_equal(list(P.edges()), [(1, 3)])
     P = bipartite.projected_graph(G, [0, 2])
     assert nodes_equal(list(P), [0, 2])
     assert edges_equal(list(P.edges()), [(0, 2)])
Exemplo n.º 8
0
 def test_node_attr2(self):
     G = self.K3.copy()
     a = {"foo": "bar"}
     G.add_node(3, **a)
     assert nodes_equal(G.nodes(), [0, 1, 2, 3])
     assert nodes_equal(G.nodes(data=True), [(0, {}), (1, {}), (2, {}),
                                             (3, {
                                                 "foo": "bar"
                                             })])
Exemplo n.º 9
0
 def test_relabel_selfloop(self):
     G = nx.DiGraph([(1, 1), (1, 2), (2, 3)])
     G = nx.relabel_nodes(G, {1: "One", 2: "Two", 3: "Three"}, copy=False)
     assert nodes_equal(G.nodes(), ["One", "Three", "Two"])
     G = nx.MultiDiGraph([(1, 1), (1, 2), (2, 3)])
     G = nx.relabel_nodes(G, {1: "One", 2: "Two", 3: "Three"}, copy=False)
     assert nodes_equal(G.nodes(), ["One", "Three", "Two"])
     G = nx.MultiDiGraph([(1, 1)])
     G = nx.relabel_nodes(G, {1: 0}, copy=False)
     assert nodes_equal(G.nodes(), [0])
Exemplo n.º 10
0
 def test_path_weighted_projected_graph(self):
     G = nx.path_graph(4)
     P = bipartite.weighted_projected_graph(G, [1, 3])
     assert nodes_equal(list(P), [1, 3])
     assert edges_equal(list(P.edges()), [(1, 3)])
     P[1][3]["weight"] = 1
     P = bipartite.weighted_projected_graph(G, [0, 2])
     assert nodes_equal(list(P), [0, 2])
     assert edges_equal(list(P.edges()), [(0, 2)])
     P[0][2]["weight"] = 1
Exemplo n.º 11
0
 def test_relabel_nodes_missing(self):
     G = nx.Graph([("A", "B"), ("A", "C"), ("B", "C"), ("C", "D")])
     mapping = {0: "aardvark"}
     # copy=True
     H = nx.relabel_nodes(G, mapping, copy=True)
     assert nodes_equal(H.nodes, G.nodes)
     # copy=False
     GG = G.copy()
     nx.relabel_nodes(G, mapping, copy=False)
     assert nodes_equal(G.nodes, GG.nodes)
Exemplo n.º 12
0
 def test_directed_path_collaboration_projected_graph(self):
     G = nx.DiGraph()
     nx.add_path(G, range(4))
     P = bipartite.collaboration_weighted_projected_graph(G, [1, 3])
     assert nodes_equal(list(P), [1, 3])
     assert edges_equal(list(P.edges()), [(1, 3)])
     P[1][3]["weight"] = 1
     P = bipartite.collaboration_weighted_projected_graph(G, [0, 2])
     assert nodes_equal(list(P), [0, 2])
     assert edges_equal(list(P.edges()), [(0, 2)])
     P[0][2]["weight"] = 1
Exemplo n.º 13
0
    def test_quotient_graph_incomplete_partition(self):
        G = nx.path_graph(6)
        partition = []
        H = nx.quotient_graph(G, partition, relabel=True)
        assert nodes_equal(H.nodes(), [])
        assert edges_equal(H.edges(), [])

        partition = [[0, 1], [2, 3], [5]]
        H = nx.quotient_graph(G, partition, relabel=True)
        assert nodes_equal(H.nodes(), [0, 1, 2])
        assert edges_equal(H.edges(), [(0, 1)])
Exemplo n.º 14
0
 def test_create_empty_copy(self):
     G = nx.create_empty_copy(self.G, with_data=False)
     assert nodes_equal(G, list(self.G))
     assert G.graph == {}
     assert G._node == {}.fromkeys(self.G.nodes(), {})
     assert G._adj == {}.fromkeys(self.G.nodes(), {})
     G = nx.create_empty_copy(self.G)
     assert nodes_equal(G, list(self.G))
     assert G.graph == self.G.graph
     assert G._node == self.G._node
     assert G._adj == {}.fromkeys(self.G.nodes(), {})
Exemplo n.º 15
0
    def test_star_projected_graph(self):
        G = nx.star_graph(3)
        P = bipartite.projected_graph(G, [1, 2, 3])
        assert nodes_equal(list(P), [1, 2, 3])
        assert edges_equal(list(P.edges()), [(1, 2), (1, 3), (2, 3)])
        P = bipartite.weighted_projected_graph(G, [1, 2, 3])
        assert nodes_equal(list(P), [1, 2, 3])
        assert edges_equal(list(P.edges()), [(1, 2), (1, 3), (2, 3)])

        P = bipartite.projected_graph(G, [0])
        assert nodes_equal(list(P), [0])
        assert edges_equal(list(P.edges()), [])
Exemplo n.º 16
0
    def test_add_cycle(self):
        G = self.G.copy()
        nlist = [12, 13, 14, 15]
        oklists = [
            [(12, 13), (12, 15), (13, 14), (14, 15)],
            [(12, 13), (13, 14), (14, 15), (15, 12)],
        ]
        nx.add_cycle(G, nlist)
        assert sorted(G.edges(nlist)) in oklists
        G = self.G.copy()
        oklists = [
            [
                (12, 13, {
                    "weight": 1.0
                }),
                (12, 15, {
                    "weight": 1.0
                }),
                (13, 14, {
                    "weight": 1.0
                }),
                (14, 15, {
                    "weight": 1.0
                }),
            ],
            [
                (12, 13, {
                    "weight": 1.0
                }),
                (13, 14, {
                    "weight": 1.0
                }),
                (14, 15, {
                    "weight": 1.0
                }),
                (15, 12, {
                    "weight": 1.0
                }),
            ],
        ]
        nx.add_cycle(G, nlist, weight=1.0)
        assert sorted(G.edges(nlist, data=True)) in oklists

        G = self.G.copy()
        nlist = [12]
        nx.add_cycle(G, nlist)
        assert nodes_equal(G, list(self.G) + nlist)

        G = self.G.copy()
        nlist = []
        nx.add_cycle(G, nlist)
        assert nodes_equal(G.nodes, self.Gnodes)
        assert edges_equal(G.edges, self.G.edges)
Exemplo n.º 17
0
 def test_weight_attribute(self):
     G = nx.Graph()
     G.add_edge(0, 1, weight=1, distance=7)
     G.add_edge(0, 2, weight=30, distance=1)
     G.add_edge(1, 2, weight=1, distance=1)
     G.add_node(3)
     T = nx.minimum_spanning_tree(G, algorithm=self.algo, weight="distance")
     assert nodes_equal(sorted(T), list(range(4)))
     assert edges_equal(sorted(T.edges()), [(0, 2), (1, 2)])
     T = nx.maximum_spanning_tree(G, algorithm=self.algo, weight="distance")
     assert nodes_equal(sorted(T), list(range(4)))
     assert edges_equal(sorted(T.edges()), [(0, 1), (0, 2)])
Exemplo n.º 18
0
 def test_ego_distance(self):
     G = nx.Graph()
     G.add_edge(0, 1, weight=2, distance=1)
     G.add_edge(1, 2, weight=2, distance=2)
     G.add_edge(2, 3, weight=2, distance=1)
     assert nodes_equal(nx.ego_graph(G, 0, radius=3).nodes(), [0, 1, 2, 3])
     eg = nx.ego_graph(G, 0, radius=3, distance="weight")
     assert nodes_equal(eg.nodes(), [0, 1])
     eg = nx.ego_graph(G, 0, radius=3, distance="weight", undirected=True)
     assert nodes_equal(eg.nodes(), [0, 1])
     eg = nx.ego_graph(G, 0, radius=3, distance="distance")
     assert nodes_equal(eg.nodes(), [0, 1, 2])
Exemplo n.º 19
0
 def test_path_projected_properties_graph(self):
     G = nx.path_graph(4)
     G.add_node(1, name="one")
     G.add_node(2, name="two")
     P = bipartite.projected_graph(G, [1, 3])
     assert nodes_equal(list(P), [1, 3])
     assert edges_equal(list(P.edges()), [(1, 3)])
     assert P.nodes[1]["name"] == G.nodes[1]["name"]
     P = bipartite.projected_graph(G, [0, 2])
     assert nodes_equal(list(P), [0, 2])
     assert edges_equal(list(P.edges()), [(0, 2)])
     assert P.nodes[2]["name"] == G.nodes[2]["name"]
Exemplo n.º 20
0
    def test_weighted_degree(self):
        G = self.Graph()
        G.add_edge(1, 2, weight=2, other=3)
        G.add_edge(2, 3, weight=3, other=4)
        assert sorted(d for n, d in G.degree(weight="weight")) == [2, 3, 5]
        assert dict(G.degree(weight="weight")) == {1: 2, 2: 5, 3: 3}
        assert G.degree(1, weight="weight") == 2
        assert nodes_equal((G.degree([1], weight="weight")), [(1, 2)])

        assert nodes_equal((d for n, d in G.degree(weight="other")), [3, 7, 4])
        assert dict(G.degree(weight="other")) == {1: 3, 2: 7, 3: 4}
        assert G.degree(1, weight="other") == 3
        assert edges_equal((G.degree([1], weight="other")), [(1, 3)])
Exemplo n.º 21
0
 def test_subgraph(self):
     G = self.G()
     G.add_edges_from([("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"),
                       ("C", "D")])
     SG = G.subgraph(["A", "B", "D"])
     assert nodes_equal(list(SG), ["A", "B", "D"])
     assert edges_equal(list(SG.edges()), [("A", "B"), ("B", "D")])
Exemplo n.º 22
0
    def test_edges_nbunch(self):
        # Test G.edges(nbunch) with various forms of nbunch
        G = self.G()
        G.add_edges_from([("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"),
                          ("C", "D")])
        # node not in nbunch should be quietly ignored
        pytest.raises(nx.NetworkXError, G.edges, 6)
        assert list(G.edges("Z")) == []  # iterable non-node
        # nbunch can be an empty list
        assert list(G.edges([])) == []
        if G.is_directed():
            elist = [("A", "B"), ("A", "C"), ("B", "D")]
        else:
            elist = [("A", "B"), ("A", "C"), ("B", "C"), ("B", "D")]
        # nbunch can be a list
        assert edges_equal(list(G.edges(["A", "B"])), elist)
        # nbunch can be a set
        assert edges_equal(G.edges({"A", "B"}), elist)
        # nbunch can be a graph
        G1 = self.G()
        G1.add_nodes_from("AB")
        assert edges_equal(G.edges(G1), elist)
        # nbunch can be a dict with nodes as keys
        ndict = {"A": "thing1", "B": "thing2"}
        assert edges_equal(G.edges(ndict), elist)
        # nbunch can be a single node
        assert edges_equal(list(G.edges("A")), [("A", "B"), ("A", "C")])
        assert nodes_equal(sorted(G), ["A", "B", "C", "D"])

        # nbunch can be nothing (whole graph)
        assert edges_equal(
            list(G.edges()),
            [("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")],
        )
Exemplo n.º 23
0
    def test_pickle(self):
        import pickle

        for G in self.graphs:
            H = pickle.loads(pickle.dumps(G, -1))
            assert edges_equal(H.edges, G.edges)
            assert nodes_equal(H.nodes, G.nodes)
Exemplo n.º 24
0
def test_parse_edgelist_with_data_list(example_graph):
    G = example_graph
    H = nx.parse_edgelist(
        ["1 2 3", "2 3 27", "3 4 3.0"], nodetype=int, data=(("weight", float),)
    )
    assert nodes_equal(G.nodes, H.nodes)
    assert edges_equal(G.edges(data=True), H.edges(data=True))
Exemplo n.º 25
0
def test_parse_edgelist_with_data_dict(example_graph):
    G = example_graph
    H = nx.parse_edgelist(
        ["1 2 {'weight': 3}", "2 3 {'weight': 27}", "3 4 {'weight': 3.0}"], nodetype=int
    )
    assert nodes_equal(G.nodes, H.nodes)
    assert edges_equal(G.edges(data=True), H.edges(data=True))
Exemplo n.º 26
0
 def test_node_attr(self):
     G = self.K3.copy()
     G.add_node(1, foo="bar")
     assert nodes_equal(G.nodes(), [0, 1, 2])
     assert nodes_equal(G.nodes(data=True), [(0, {}), (1, {
         "foo": "bar"
     }), (2, {})])
     G.nodes[1]["foo"] = "baz"
     assert nodes_equal(G.nodes(data=True), [(0, {}), (1, {
         "foo": "baz"
     }), (2, {})])
     assert nodes_equal(G.nodes(data="foo"), [(0, None), (1, "baz"),
                                              (2, None)])
     assert nodes_equal(G.nodes(data="foo", default="bar"), [(0, "bar"),
                                                             (1, "baz"),
                                                             (2, "bar")])
Exemplo n.º 27
0
 def test_reverse_hashable(self):
     x = True
     y = False
     G = nx.DiGraph()
     G.add_edge(x, y)
     assert nodes_equal(G.nodes(), G.reverse().nodes())
     assert [(y, x)] == list(G.reverse().edges())
Exemplo n.º 28
0
def test_selfloops(graph_type):
    G = nx.complete_graph(3, create_using=graph_type)
    G.add_edge(0, 0)
    assert nodes_equal(nx.nodes_with_selfloops(G), [0])
    assert edges_equal(nx.selfloop_edges(G), [(0, 0)])
    assert edges_equal(nx.selfloop_edges(G, data=True), [(0, 0, {})])
    assert nx.number_of_selfloops(G) == 1
Exemplo n.º 29
0
 def test_read_equals_from_bytes(self):
     data = b"DF{"
     G = nx.from_graph6_bytes(data)
     fh = BytesIO(data)
     Gin = nx.read_graph6(fh)
     assert nodes_equal(G.nodes(), Gin.nodes())
     assert edges_equal(G.edges(), Gin.edges())
Exemplo n.º 30
0
 def test_relabel_nodes_multidigraph(self):
     G = nx.MultiDiGraph([("a", "b"), ("a", "b")])
     mapping = {"a": "aardvark", "b": "bear"}
     G = nx.relabel_nodes(G, mapping, copy=False)
     assert nodes_equal(G.nodes(), ["aardvark", "bear"])
     assert edges_equal(G.edges(), [("aardvark", "bear"),
                                    ("aardvark", "bear")])