Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / tests / test_convert.py @ 5cef0f13

History | View | Annotate | Download (11.7 KB)

1
#!/usr/bin/env python
2
from nose.tools import (assert_equal, assert_not_equal,
3
                        assert_true, assert_false,
4
                        assert_raises, assert_is)
5

    
6
import networkx as nx
7
from networkx.testing import assert_nodes_equal, assert_edges_equal, assert_graphs_equal
8
from networkx.convert import (to_networkx_graph,
9
                              to_dict_of_dicts,
10
                              from_dict_of_dicts,
11
                              to_dict_of_lists,
12
                              from_dict_of_lists)
13
from networkx.generators.classic import barbell_graph, cycle_graph
14

    
15

    
16
class TestConvert():
17
    def edgelists_equal(self, e1, e2):
18
        return sorted(sorted(e) for e in e1) == sorted(sorted(e) for e in e2)
19

    
20
    def test_simple_graphs(self):
21
        for dest, source in [(to_dict_of_dicts, from_dict_of_dicts),
22
                             (to_dict_of_lists, from_dict_of_lists)]:
23
            G = barbell_graph(10, 3)
24
            G.graph = {}
25
            dod = dest(G)
26

    
27
            # Dict of [dicts, lists]
28
            GG = source(dod)
29
            assert_graphs_equal(G, GG)
30
            GW = to_networkx_graph(dod)
31
            assert_graphs_equal(G, GW)
32
            GI = nx.Graph(dod)
33
            assert_graphs_equal(G, GI)
34

    
35
            # With nodelist keyword
36
            P4 = nx.path_graph(4)
37
            P3 = nx.path_graph(3)
38
            P4.graph = {}
39
            P3.graph = {}
40
            dod = dest(P4, nodelist=[0, 1, 2])
41
            Gdod = nx.Graph(dod)
42
            assert_graphs_equal(Gdod, P3)
43

    
44
    def test_exceptions(self):
45
        # NX graph
46
        class G(object):
47
            adj = None
48

    
49
        assert_raises(nx.NetworkXError, to_networkx_graph, G)
50

    
51
        # pygraphviz  agraph
52
        class G(object):
53
            is_strict = None
54

    
55
        assert_raises(nx.NetworkXError, to_networkx_graph, G)
56

    
57
        # Dict of [dicts, lists]
58
        G = {"a": 0}
59
        assert_raises(TypeError, to_networkx_graph, G)
60

    
61
        # list or generator of edges
62
        class G(object):
63
            next = None
64

    
65
        assert_raises(nx.NetworkXError, to_networkx_graph, G)
66

    
67
        # no match
68
        assert_raises(nx.NetworkXError, to_networkx_graph, "a")
69

    
70
    def test_digraphs(self):
71
        for dest, source in [(to_dict_of_dicts, from_dict_of_dicts),
72
                             (to_dict_of_lists, from_dict_of_lists)]:
73
            G = cycle_graph(10)
74

    
75
            # Dict of [dicts, lists]
76
            dod = dest(G)
77
            GG = source(dod)
78
            assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
79
            assert_edges_equal(sorted(G.edges()), sorted(GG.edges()))
80
            GW = to_networkx_graph(dod)
81
            assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
82
            assert_edges_equal(sorted(G.edges()), sorted(GW.edges()))
83
            GI = nx.Graph(dod)
84
            assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes()))
85
            assert_edges_equal(sorted(G.edges()), sorted(GI.edges()))
86

    
87
            G = cycle_graph(10, create_using=nx.DiGraph)
88
            dod = dest(G)
89
            GG = source(dod, create_using=nx.DiGraph)
90
            assert_equal(sorted(G.nodes()), sorted(GG.nodes()))
91
            assert_equal(sorted(G.edges()), sorted(GG.edges()))
92
            GW = to_networkx_graph(dod, create_using=nx.DiGraph)
93
            assert_equal(sorted(G.nodes()), sorted(GW.nodes()))
94
            assert_equal(sorted(G.edges()), sorted(GW.edges()))
95
            GI = nx.DiGraph(dod)
96
            assert_equal(sorted(G.nodes()), sorted(GI.nodes()))
97
            assert_equal(sorted(G.edges()), sorted(GI.edges()))
98

    
99
    def test_graph(self):
100
        g = nx.cycle_graph(10)
101
        G = nx.Graph()
102
        G.add_nodes_from(g)
103
        G.add_weighted_edges_from((u, v, u) for u, v in g.edges())
104

    
105
        # Dict of dicts
106
        dod = to_dict_of_dicts(G)
107
        GG = from_dict_of_dicts(dod, create_using=nx.Graph)
108
        assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
109
        assert_edges_equal(sorted(G.edges()), sorted(GG.edges()))
110
        GW = to_networkx_graph(dod, create_using=nx.Graph)
111
        assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
112
        assert_edges_equal(sorted(G.edges()), sorted(GW.edges()))
113
        GI = nx.Graph(dod)
114
        assert_equal(sorted(G.nodes()), sorted(GI.nodes()))
115
        assert_equal(sorted(G.edges()), sorted(GI.edges()))
116

    
117
        # Dict of lists
118
        dol = to_dict_of_lists(G)
119
        GG = from_dict_of_lists(dol, create_using=nx.Graph)
120
        # dict of lists throws away edge data so set it to none
121
        enone = [(u, v, {}) for (u, v, d) in G.edges(data=True)]
122
        assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
123
        assert_edges_equal(enone, sorted(GG.edges(data=True)))
124
        GW = to_networkx_graph(dol, create_using=nx.Graph)
125
        assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
126
        assert_edges_equal(enone, sorted(GW.edges(data=True)))
127
        GI = nx.Graph(dol)
128
        assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes()))
129
        assert_edges_equal(enone, sorted(GI.edges(data=True)))
130

    
131
    def test_with_multiedges_self_loops(self):
132
        G = cycle_graph(10)
133
        XG = nx.Graph()
134
        XG.add_nodes_from(G)
135
        XG.add_weighted_edges_from((u, v, u) for u, v in G.edges())
136
        XGM = nx.MultiGraph()
137
        XGM.add_nodes_from(G)
138
        XGM.add_weighted_edges_from((u, v, u) for u, v in G.edges())
139
        XGM.add_edge(0, 1, weight=2)  # multiedge
140
        XGS = nx.Graph()
141
        XGS.add_nodes_from(G)
142
        XGS.add_weighted_edges_from((u, v, u) for u, v in G.edges())
143
        XGS.add_edge(0, 0, weight=100)  # self loop
144

    
145
        # Dict of dicts
146
        # with self loops, OK
147
        dod = to_dict_of_dicts(XGS)
148
        GG = from_dict_of_dicts(dod, create_using=nx.Graph)
149
        assert_nodes_equal(XGS.nodes(), GG.nodes())
150
        assert_edges_equal(XGS.edges(), GG.edges())
151
        GW = to_networkx_graph(dod, create_using=nx.Graph)
152
        assert_nodes_equal(XGS.nodes(), GW.nodes())
153
        assert_edges_equal(XGS.edges(), GW.edges())
154
        GI = nx.Graph(dod)
155
        assert_nodes_equal(XGS.nodes(), GI.nodes())
156
        assert_edges_equal(XGS.edges(), GI.edges())
157

    
158
        # Dict of lists
159
        # with self loops, OK
160
        dol = to_dict_of_lists(XGS)
161
        GG = from_dict_of_lists(dol, create_using=nx.Graph)
162
        # dict of lists throws away edge data so set it to none
163
        enone = [(u, v, {}) for (u, v, d) in XGS.edges(data=True)]
164
        assert_nodes_equal(sorted(XGS.nodes()), sorted(GG.nodes()))
165
        assert_edges_equal(enone, sorted(GG.edges(data=True)))
166
        GW = to_networkx_graph(dol, create_using=nx.Graph)
167
        assert_nodes_equal(sorted(XGS.nodes()), sorted(GW.nodes()))
168
        assert_edges_equal(enone, sorted(GW.edges(data=True)))
169
        GI = nx.Graph(dol)
170
        assert_nodes_equal(sorted(XGS.nodes()), sorted(GI.nodes()))
171
        assert_edges_equal(enone, sorted(GI.edges(data=True)))
172

    
173
        # Dict of dicts
174
        # with multiedges, OK
175
        dod = to_dict_of_dicts(XGM)
176
        GG = from_dict_of_dicts(dod, create_using=nx.MultiGraph,
177
                                multigraph_input=True)
178
        assert_nodes_equal(sorted(XGM.nodes()), sorted(GG.nodes()))
179
        assert_edges_equal(sorted(XGM.edges()), sorted(GG.edges()))
180
        GW = to_networkx_graph(dod, create_using=nx.MultiGraph, multigraph_input=True)
181
        assert_nodes_equal(sorted(XGM.nodes()), sorted(GW.nodes()))
182
        assert_edges_equal(sorted(XGM.edges()), sorted(GW.edges()))
183
        GI = nx.MultiGraph(dod)  # convert can't tell whether to duplicate edges!
184
        assert_nodes_equal(sorted(XGM.nodes()), sorted(GI.nodes()))
185
        #assert_not_equal(sorted(XGM.edges()), sorted(GI.edges()))
186
        assert_false(sorted(XGM.edges()) == sorted(GI.edges()))
187
        GE = from_dict_of_dicts(dod, create_using=nx.MultiGraph,
188
                                multigraph_input=False)
189
        assert_nodes_equal(sorted(XGM.nodes()), sorted(GE.nodes()))
190
        assert_not_equal(sorted(XGM.edges()), sorted(GE.edges()))
191
        GI = nx.MultiGraph(XGM)
192
        assert_nodes_equal(sorted(XGM.nodes()), sorted(GI.nodes()))
193
        assert_edges_equal(sorted(XGM.edges()), sorted(GI.edges()))
194
        GM = nx.MultiGraph(G)
195
        assert_nodes_equal(sorted(GM.nodes()), sorted(G.nodes()))
196
        assert_edges_equal(sorted(GM.edges()), sorted(G.edges()))
197

    
198
        # Dict of lists
199
        # with multiedges, OK, but better write as DiGraph else you'll
200
        # get double edges
201
        dol = to_dict_of_lists(G)
202
        GG = from_dict_of_lists(dol, create_using=nx.MultiGraph)
203
        assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes()))
204
        assert_edges_equal(sorted(G.edges()), sorted(GG.edges()))
205
        GW = to_networkx_graph(dol, create_using=nx.MultiGraph)
206
        assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes()))
207
        assert_edges_equal(sorted(G.edges()), sorted(GW.edges()))
208
        GI = nx.MultiGraph(dol)
209
        assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes()))
210
        assert_edges_equal(sorted(G.edges()), sorted(GI.edges()))
211

    
212
    def test_edgelists(self):
213
        P = nx.path_graph(4)
214
        e = [(0, 1), (1, 2), (2, 3)]
215
        G = nx.Graph(e)
216
        assert_nodes_equal(sorted(G.nodes()), sorted(P.nodes()))
217
        assert_edges_equal(sorted(G.edges()), sorted(P.edges()))
218
        assert_edges_equal(sorted(G.edges(data=True)), sorted(P.edges(data=True)))
219

    
220
        e = [(0, 1, {}), (1, 2, {}), (2, 3, {})]
221
        G = nx.Graph(e)
222
        assert_nodes_equal(sorted(G.nodes()), sorted(P.nodes()))
223
        assert_edges_equal(sorted(G.edges()), sorted(P.edges()))
224
        assert_edges_equal(sorted(G.edges(data=True)), sorted(P.edges(data=True)))
225

    
226
        e = ((n, n + 1) for n in range(3))
227
        G = nx.Graph(e)
228
        assert_nodes_equal(sorted(G.nodes()), sorted(P.nodes()))
229
        assert_edges_equal(sorted(G.edges()), sorted(P.edges()))
230
        assert_edges_equal(sorted(G.edges(data=True)), sorted(P.edges(data=True)))
231

    
232
    def test_directed_to_undirected(self):
233
        edges1 = [(0, 1), (1, 2), (2, 0)]
234
        edges2 = [(0, 1), (1, 2), (0, 2)]
235
        assert_true(self.edgelists_equal(nx.Graph(nx.DiGraph(edges1)).edges(), edges1))
236
        assert_true(self.edgelists_equal(nx.Graph(nx.DiGraph(edges2)).edges(), edges1))
237
        assert_true(self.edgelists_equal(nx.MultiGraph(nx.DiGraph(edges1)).edges(), edges1))
238
        assert_true(self.edgelists_equal(nx.MultiGraph(nx.DiGraph(edges2)).edges(), edges1))
239

    
240
        assert_true(self.edgelists_equal(nx.MultiGraph(nx.MultiDiGraph(edges1)).edges(),
241
                                         edges1))
242
        assert_true(self.edgelists_equal(nx.MultiGraph(nx.MultiDiGraph(edges2)).edges(),
243
                                         edges1))
244

    
245
        assert_true(self.edgelists_equal(nx.Graph(nx.MultiDiGraph(edges1)).edges(), edges1))
246
        assert_true(self.edgelists_equal(nx.Graph(nx.MultiDiGraph(edges2)).edges(), edges1))
247

    
248
    def test_attribute_dict_integrity(self):
249
        # we must not replace dict-like graph data structures with dicts
250
        G = nx.OrderedGraph()
251
        G.add_nodes_from("abc")
252
        H = to_networkx_graph(G, create_using=nx.OrderedGraph)
253
        assert_equal(list(H.nodes), list(G.nodes))
254
        H = nx.OrderedDiGraph(G)
255
        assert_equal(list(H.nodes), list(G.nodes))
256

    
257
    def test_to_edgelist(self):
258
        G = nx.Graph([(1, 1)])
259
        elist = nx.to_edgelist(G, nodelist=list(G))
260
        assert_edges_equal(G.edges(data=True), elist)
261

    
262
    def test_custom_node_attr_dict_safekeeping(self):
263
        class custom_dict(dict):
264
            pass
265

    
266
        class Custom(nx.Graph):
267
            node_attr_dict_factory = custom_dict
268

    
269
        g = nx.Graph()
270
        g.add_node(1, weight=1)
271

    
272
        h = Custom(g)
273
        assert isinstance(g._node[1], dict)
274
        assert isinstance(h._node[1], custom_dict)
275

    
276
        # this raise exception
277
        # h._node.update((n, dd.copy()) for n, dd in g.nodes.items())
278
        # assert isinstance(h._node[1], custom_dict)