Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (11.7 KB)

1
from nose.tools import assert_in, assert_not_in, assert_equal
2
from nose.tools import assert_is, assert_is_not
3
from nose.tools import assert_raises, assert_true, assert_false
4

    
5
import networkx as nx
6
from networkx.testing import assert_edges_equal, assert_nodes_equal
7

    
8
# Note: SubGraph views are not tested here. They have their own testing file
9

    
10

    
11
class TestReverseView(object):
12
    def setup(self):
13
        self.G = nx.path_graph(9, create_using=nx.DiGraph())
14
        self.rv = nx.reverse_view(self.G)
15

    
16
    def test_pickle(self):
17
        import pickle
18
        rv = self.rv
19
        prv = pickle.loads(pickle.dumps(rv, -1))
20
        assert_equal(rv._node, prv._node)
21
        assert_equal(rv._adj, prv._adj)
22
        assert_equal(rv.graph, prv.graph)
23

    
24
    def test_contains(self):
25
        assert_in((2, 3), self.G.edges)
26
        assert_not_in((3, 2), self.G.edges)
27
        assert_not_in((2, 3), self.rv.edges)
28
        assert_in((3, 2), self.rv.edges)
29

    
30
    def test_iter(self):
31
        expected = sorted(tuple(reversed(e)) for e in self.G.edges)
32
        assert_equal(sorted(self.rv.edges), expected)
33

    
34
    def test_exceptions(self):
35
        nxg = nx.graphviews
36
        assert_raises(nx.NetworkXNotImplemented, nxg.reverse_view, nx.Graph())
37

    
38
    def test_subclass(self):
39
        class MyGraph(nx.DiGraph):
40
            def my_method(self):
41
                return "me"
42
            def to_directed_class(self):
43
                return MyGraph()
44

    
45
        M = MyGraph()
46
        M.add_edge(1, 2)
47
        RM = nx.reverse_view(M)
48
        print("RM class",RM.__class__)
49
        RMC = RM.copy()
50
        print("RMC class",RMC.__class__)
51
        print(RMC.edges)
52
        assert_true(RMC.has_edge(2, 1))
53
        assert_equal(RMC.my_method(), "me")
54

    
55

    
56
class TestMultiReverseView(object):
57
    def setup(self):
58
        self.G = nx.path_graph(9, create_using=nx.MultiDiGraph())
59
        self.G.add_edge(4, 5)
60
        self.rv = nx.reverse_view(self.G)
61

    
62
    def test_pickle(self):
63
        import pickle
64
        rv = self.rv
65
        prv = pickle.loads(pickle.dumps(rv, -1))
66
        assert_equal(rv._node, prv._node)
67
        assert_equal(rv._adj, prv._adj)
68
        assert_equal(rv.graph, prv.graph)
69

    
70
    def test_contains(self):
71
        assert_in((2, 3, 0), self.G.edges)
72
        assert_not_in((3, 2, 0), self.G.edges)
73
        assert_not_in((2, 3, 0), self.rv.edges)
74
        assert_in((3, 2, 0), self.rv.edges)
75
        assert_in((5, 4, 1), self.rv.edges)
76
        assert_not_in((4, 5, 1), self.rv.edges)
77

    
78
    def test_iter(self):
79
        expected = sorted((v, u, k) for u, v, k in self.G.edges)
80
        assert_equal(sorted(self.rv.edges), expected)
81

    
82
    def test_exceptions(self):
83
        nxg = nx.graphviews
84
        MG = nx.MultiGraph(self.G)
85
        assert_raises(nx.NetworkXNotImplemented, nxg.reverse_view, MG)
86

    
87

    
88
class TestToDirected(object):
89
    def setup(self):
90
        self.G = nx.path_graph(9)
91
        self.dv = nx.to_directed(self.G)
92
        self.MG = nx.path_graph(9, create_using=nx.MultiGraph())
93
        self.Mdv = nx.to_directed(self.MG)
94

    
95
    def test_directed(self):
96
        assert_false(self.G.is_directed())
97
        assert_true(self.dv.is_directed())
98

    
99
    def test_already_directed(self):
100
        dd = nx.to_directed(self.dv)
101
        Mdd = nx.to_directed(self.Mdv)
102
        assert_edges_equal(dd.edges, self.dv.edges)
103
        assert_edges_equal(Mdd.edges, self.Mdv.edges)
104

    
105
    def test_pickle(self):
106
        import pickle
107
        dv = self.dv
108
        pdv = pickle.loads(pickle.dumps(dv, -1))
109
        assert_equal(dv._node, pdv._node)
110
        assert_equal(dv._succ, pdv._succ)
111
        assert_equal(dv._pred, pdv._pred)
112
        assert_equal(dv.graph, pdv.graph)
113

    
114
    def test_contains(self):
115
        assert_in((2, 3), self.G.edges)
116
        assert_in((3, 2), self.G.edges)
117
        assert_in((2, 3), self.dv.edges)
118
        assert_in((3, 2), self.dv.edges)
119

    
120
    def test_iter(self):
121
        revd = [tuple(reversed(e)) for e in self.G.edges]
122
        expected = sorted(list(self.G.edges) + revd)
123
        assert_equal(sorted(self.dv.edges), expected)
124

    
125
    def test_exceptions(self):
126
        nxg = nx.graphviews
127
        assert_raises(nx.NetworkXError, nxg.DiGraphView, self.MG)
128
        assert_raises(nx.NetworkXError, nxg.MultiDiGraphView, self.G)
129

    
130

    
131
class TestToUndirected(object):
132
    def setup(self):
133
        self.DG = nx.path_graph(9, create_using=nx.DiGraph())
134
        self.uv = nx.to_undirected(self.DG)
135
        self.MDG = nx.path_graph(9, create_using=nx.MultiDiGraph())
136
        self.Muv = nx.to_undirected(self.MDG)
137

    
138
    def test_directed(self):
139
        assert_true(self.DG.is_directed())
140
        assert_false(self.uv.is_directed())
141

    
142
    def test_already_directed(self):
143
        uu = nx.to_undirected(self.uv)
144
        Muu = nx.to_undirected(self.Muv)
145
        assert_edges_equal(uu.edges, self.uv.edges)
146
        assert_edges_equal(Muu.edges, self.Muv.edges)
147

    
148
    def test_pickle(self):
149
        import pickle
150
        uv = self.uv
151
        puv = pickle.loads(pickle.dumps(uv, -1))
152
        assert_equal(uv._node, puv._node)
153
        assert_equal(uv._adj, puv._adj)
154
        assert_equal(uv.graph, puv.graph)
155
        assert_true(hasattr(uv, '_graph'))
156

    
157
    def test_contains(self):
158
        assert_in((2, 3), self.DG.edges)
159
        assert_not_in((3, 2), self.DG.edges)
160
        assert_in((2, 3), self.uv.edges)
161
        assert_in((3, 2), self.uv.edges)
162

    
163
    def test_iter(self):
164
        expected = sorted(self.DG.edges)
165
        assert_equal(sorted(self.uv.edges), expected)
166

    
167
    def test_exceptions(self):
168
        nxg = nx.graphviews
169
        assert_raises(nx.NetworkXError, nxg.GraphView, self.MDG)
170
        assert_raises(nx.NetworkXError, nxg.MultiGraphView, self.DG)
171

    
172

    
173
class TestChainsOfViews(object):
174
    def setUp(self):
175
        self.G = nx.path_graph(9)
176
        self.DG = nx.path_graph(9, create_using=nx.DiGraph())
177
        self.MG = nx.path_graph(9, create_using=nx.MultiGraph())
178
        self.MDG = nx.path_graph(9, create_using=nx.MultiDiGraph())
179
        self.Gv = nx.to_undirected(self.DG)
180
        self.DGv = nx.to_directed(self.G)
181
        self.MGv = nx.to_undirected(self.MDG)
182
        self.MDGv = nx.to_directed(self.MG)
183
        self.Rv = self.DG.reverse()
184
        self.MRv = self.MDG.reverse()
185
        self.graphs = [self.G, self.DG, self.MG, self.MDG,
186
                       self.Gv, self.DGv, self.MGv, self.MDGv,
187
                       self.Rv, self.MRv]
188
        for G in self.graphs:
189
            G.edges, G.nodes, G.degree
190

    
191
    def test_pickle(self):
192
        import pickle
193
        for G in self.graphs:
194
            H = pickle.loads(pickle.dumps(G, -1))
195
            assert_edges_equal(H.edges, G.edges)
196
            assert_nodes_equal(H.nodes, G.nodes)
197

    
198
    def test_subgraph_of_subgraph(self):
199
        SGv = nx.subgraph(self.G, range(3, 7))
200
        SDGv = nx.subgraph(self.DG, range(3, 7))
201
        SMGv = nx.subgraph(self.MG, range(3, 7))
202
        SMDGv = nx.subgraph(self.MDG, range(3, 7))
203
        for G in self.graphs + [SGv, SDGv, SMGv, SMDGv]:
204
            SG = nx.induced_subgraph(G, [4, 5, 6])
205
            assert_equal(list(SG), [4, 5, 6])
206
            SSG = SG.subgraph([6, 7])
207
            assert_equal(list(SSG), [6])
208
            # subgraph-subgraph chain is short-cut in base class method
209
            assert_is(SSG._graph, G)
210

    
211
    def test_restricted_induced_subgraph_chains(self):
212
        """ Test subgraph chains that both restrict and show nodes/edges.
213

214
        A restricted_view subgraph should allow induced subgraphs using
215
        G.subgraph that automagically without a chain (meaning the result
216
        is a subgraph view of the original graph not a subgraph-of-subgraph.
217
        """
218
        hide_nodes = [3, 4, 5]
219
        hide_edges = [(6, 7)]
220
        RG = nx.restricted_view(self.G, hide_nodes, hide_edges)
221
        nodes = [4, 5, 6, 7, 8]
222
        SG = nx.induced_subgraph(RG, nodes)
223
        SSG = RG.subgraph(nodes)
224
        assert_is(RG._graph, self.G)
225
        assert_is(SSG._graph, self.G)
226
        assert_is(SG._graph, RG)
227
        assert_edges_equal(SG.edges, SSG.edges)
228
        # should be same as morphing the graph
229
        CG = self.G.copy()
230
        CG.remove_nodes_from(hide_nodes)
231
        CG.remove_edges_from(hide_edges)
232
        assert_edges_equal(CG.edges(nodes), SSG.edges)
233
        CG.remove_nodes_from([0, 1, 2, 3])
234
        assert_edges_equal(CG.edges, SSG.edges)
235
        # switch order: subgraph first, then restricted view
236
        SSSG = self.G.subgraph(nodes)
237
        RSG = nx.restricted_view(SSSG, hide_nodes, hide_edges)
238
        assert_is_not(RSG._graph, self.G)
239
        assert_edges_equal(RSG.edges, CG.edges)
240

    
241
    def test_subgraph_copy(self):
242
        for origG in self.graphs:
243
            G = nx.OrderedGraph(origG)
244
            SG = G.subgraph([4, 5, 6])
245
            H = SG.copy()
246
            assert_equal(type(G), type(H))
247

    
248
    def test_subgraph_todirected(self):
249
        SG = nx.induced_subgraph(self.G, [4, 5, 6])
250
        SSG = SG.to_directed()
251
        assert_equal(sorted(SSG), [4, 5, 6])
252
        assert_equal(sorted(SSG.edges), [(4, 5), (5, 4), (5, 6), (6, 5)])
253

    
254
    def test_subgraph_toundirected(self):
255
        SG = nx.induced_subgraph(self.G, [4, 5, 6])
256
        SSG = SG.to_undirected()
257
        assert_equal(list(SSG), [4, 5, 6])
258
        assert_equal(sorted(SSG.edges), [(4, 5), (5, 6)])
259

    
260
    def test_reverse_subgraph_toundirected(self):
261
        G = self.DG.reverse(copy=False)
262
        SG = G.subgraph([4, 5, 6])
263
        SSG = SG.to_undirected()
264
        assert_equal(list(SSG), [4, 5, 6])
265
        assert_equal(sorted(SSG.edges), [(4, 5), (5, 6)])
266

    
267
    def test_reverse_reverse_copy(self):
268
        G = self.DG.reverse(copy=False)
269
        H = G.reverse(copy=True)
270
        assert_equal(H.nodes, self.DG.nodes)
271
        assert_equal(H.edges, self.DG.edges)
272
        G = self.MDG.reverse(copy=False)
273
        H = G.reverse(copy=True)
274
        assert_equal(H.nodes, self.MDG.nodes)
275
        assert_equal(H.edges, self.MDG.edges)
276

    
277
    def test_subgraph_edgesubgraph_toundirected(self):
278
        G = self.G.copy()
279
        SG = G.subgraph([4, 5, 6])
280
        SSG = SG.edge_subgraph([(4, 5), (5, 4)])
281
        USSG = SSG.to_undirected()
282
        assert_equal(list(USSG), [4, 5])
283
        assert_equal(sorted(USSG.edges), [(4, 5)])
284

    
285
    def test_copy_subgraph(self):
286
        G = self.G.copy()
287
        SG = G.subgraph([4, 5, 6])
288
        CSG = SG.copy(as_view=True)
289
        DCSG = SG.copy(as_view=False)
290
        assert_true(hasattr(CSG, '_graph'))  # is a view
291
        assert_false(hasattr(DCSG, '_graph'))  # not a view
292

    
293
    def test_copy_disubgraph(self):
294
        G = self.DG.copy()
295
        SG = G.subgraph([4, 5, 6])
296
        CSG = SG.copy(as_view=True)
297
        DCSG = SG.copy(as_view=False)
298
        assert_true(hasattr(CSG, '_graph'))  # is a view
299
        assert_false(hasattr(DCSG, '_graph'))  # not a view
300

    
301
    def test_copy_multidisubgraph(self):
302
        G = self.MDG.copy()
303
        SG = G.subgraph([4, 5, 6])
304
        CSG = SG.copy(as_view=True)
305
        DCSG = SG.copy(as_view=False)
306
        assert_true(hasattr(CSG, '_graph'))  # is a view
307
        assert_false(hasattr(DCSG, '_graph'))  # not a view
308

    
309
    def test_copy_multisubgraph(self):
310
        G = self.MG.copy()
311
        SG = G.subgraph([4, 5, 6])
312
        CSG = SG.copy(as_view=True)
313
        DCSG = SG.copy(as_view=False)
314
        assert_true(hasattr(CSG, '_graph'))  # is a view
315
        assert_false(hasattr(DCSG, '_graph'))  # not a view
316

    
317
    def test_copy_of_view(self):
318
        G = nx.OrderedMultiGraph(self.MGv)
319
        assert_equal(G.__class__.__name__, 'OrderedMultiGraph')
320
        G = G.copy(as_view=True)
321
        assert_equal(G.__class__.__name__, 'OrderedMultiGraph')
322

    
323
    def test_subclass(self):
324
        class MyGraph(nx.DiGraph):
325
            def my_method(self):
326
                return "me"
327
            def to_directed_class(self):
328
                return MyGraph()
329

    
330
        for origG in self.graphs:
331
            G = MyGraph(origG)
332
            SG = G.subgraph([4, 5, 6])
333
            H = SG.copy()
334
            assert_equal(SG.my_method(), "me")
335
            assert_equal(H.my_method(), "me")
336
            assert_false(3 in H or 3 in SG)