Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (10.8 KB)

1
#!/usr/bin/env python
2

    
3
from nose.tools import assert_equal
4
from nose.tools import assert_false
5
from nose.tools import assert_true
6
from nose.tools import assert_raises
7

    
8

    
9
import networkx as nx
10
from networkx.testing import assert_nodes_equal
11
from test_graph import BaseGraphTester, BaseAttrGraphTester, TestGraph
12
from test_graph import TestEdgeSubgraph as TestGraphEdgeSubgraph
13

    
14

    
15
class BaseDiGraphTester(BaseGraphTester):
16
    def test_has_successor(self):
17
        G = self.K3
18
        assert_equal(G.has_successor(0, 1), True)
19
        assert_equal(G.has_successor(0, -1), False)
20

    
21
    def test_successors(self):
22
        G = self.K3
23
        assert_equal(sorted(G.successors(0)), [1, 2])
24
        assert_raises((KeyError, nx.NetworkXError), G.successors, -1)
25

    
26
    def test_has_predecessor(self):
27
        G = self.K3
28
        assert_equal(G.has_predecessor(0, 1), True)
29
        assert_equal(G.has_predecessor(0, -1), False)
30

    
31
    def test_predecessors(self):
32
        G = self.K3
33
        assert_equal(sorted(G.predecessors(0)), [1, 2])
34
        assert_raises((KeyError, nx.NetworkXError), G.predecessors, -1)
35

    
36
    def test_edges(self):
37
        G = self.K3
38
        assert_equal(sorted(G.edges()), [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)])
39
        assert_equal(sorted(G.edges(0)), [(0, 1), (0, 2)])
40
        assert_equal(sorted(G.edges([0, 1])), [(0, 1), (0, 2), (1, 0), (1, 2)])
41
        assert_raises((KeyError, nx.NetworkXError), G.edges, -1)
42

    
43
    def test_edges_data(self):
44
        G = self.K3
45
        all_edges = [(0, 1, {}), (0, 2, {}), (1, 0, {}), (1, 2, {}), (2, 0, {}), (2, 1, {})]
46
        assert_equal(sorted(G.edges(data=True)), all_edges)
47
        assert_equal(sorted(G.edges(0, data=True)), all_edges[:2])
48
        assert_equal(sorted(G.edges([0, 1], data=True)), all_edges[:4])
49
        assert_raises((KeyError, nx.NetworkXError), G.edges, -1, True)
50

    
51
    def test_out_edges(self):
52
        G = self.K3
53
        assert_equal(sorted(G.out_edges()), [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)])
54
        assert_equal(sorted(G.out_edges(0)), [(0, 1), (0, 2)])
55
        assert_raises((KeyError, nx.NetworkXError), G.out_edges, -1)
56

    
57
    def test_out_edges_dir(self):
58
        G = self.P3
59
        assert_equal(sorted(G.out_edges()), [(0, 1), (1, 2)])
60
        assert_equal(sorted(G.out_edges(0)), [(0, 1)])
61
        assert_equal(sorted(G.out_edges(2)), [])
62

    
63
    def test_out_edges_data(self):
64
        G = nx.DiGraph([(0, 1, {'data': 0}), (1, 0, {})])
65
        assert_equal(sorted(G.out_edges(data=True)), [(0, 1, {'data': 0}), (1, 0, {})])
66
        assert_equal(sorted(G.out_edges(0, data=True)), [(0, 1, {'data': 0})])
67
        assert_equal(sorted(G.out_edges(data='data')), [(0, 1, 0), (1, 0, None)])
68
        assert_equal(sorted(G.out_edges(0, data='data')), [(0, 1, 0)])
69

    
70
    def test_in_edges_dir(self):
71
        G = self.P3
72
        assert_equal(sorted(G.in_edges()), [(0, 1), (1, 2)])
73
        assert_equal(sorted(G.in_edges(0)), [])
74
        assert_equal(sorted(G.in_edges(2)), [(1, 2)])
75

    
76
    def test_in_edges_data(self):
77
        G = nx.DiGraph([(0, 1, {'data': 0}), (1, 0, {})])
78
        assert_equal(sorted(G.in_edges(data=True)), [(0, 1, {'data': 0}), (1, 0, {})])
79
        assert_equal(sorted(G.in_edges(1, data=True)), [(0, 1, {'data': 0})])
80
        assert_equal(sorted(G.in_edges(data='data')), [(0, 1, 0), (1, 0, None)])
81
        assert_equal(sorted(G.in_edges(1, data='data')), [(0, 1, 0)])
82

    
83
    def test_degree(self):
84
        G = self.K3
85
        assert_equal(sorted(G.degree()), [(0, 4), (1, 4), (2, 4)])
86
        assert_equal(dict(G.degree()), {0: 4, 1: 4, 2: 4})
87
        assert_equal(G.degree(0), 4)
88
        assert_equal(list(G.degree(iter([0]))), [
89
                     (0, 4)])  # run through iterator
90

    
91
    def test_in_degree(self):
92
        G = self.K3
93
        assert_equal(sorted(G.in_degree()), [(0, 2), (1, 2), (2, 2)])
94
        assert_equal(dict(G.in_degree()), {0: 2, 1: 2, 2: 2})
95
        assert_equal(G.in_degree(0), 2)
96
        assert_equal(list(G.in_degree(iter([0]))), [(0, 2)])  # run through iterator
97

    
98
    def test_in_degree_weighted(self):
99
        G = self.K3
100
        G.add_edge(0, 1, weight=0.3, other=1.2)
101
        assert_equal(sorted(G.in_degree(weight='weight')), [(0, 2), (1, 1.3), (2, 2)])
102
        assert_equal(dict(G.in_degree(weight='weight')), {0: 2, 1: 1.3, 2: 2})
103
        assert_equal(G.in_degree(1, weight='weight'), 1.3)
104
        assert_equal(sorted(G.in_degree(weight='other')), [(0, 2), (1, 2.2), (2, 2)])
105
        assert_equal(dict(G.in_degree(weight='other')), {0: 2, 1: 2.2, 2: 2})
106
        assert_equal(G.in_degree(1, weight='other'), 2.2)
107
        assert_equal(list(G.in_degree(iter([1]), weight='other')), [(1, 2.2)])
108

    
109
    def test_out_degree_weighted(self):
110
        G = self.K3
111
        G.add_edge(0, 1, weight=0.3, other=1.2)
112
        assert_equal(sorted(G.out_degree(weight='weight')), [(0, 1.3), (1, 2), (2, 2)])
113
        assert_equal(dict(G.out_degree(weight='weight')), {0: 1.3, 1: 2, 2: 2})
114
        assert_equal(G.out_degree(0, weight='weight'), 1.3)
115
        assert_equal(sorted(G.out_degree(weight='other')), [(0, 2.2), (1, 2), (2, 2)])
116
        assert_equal(dict(G.out_degree(weight='other')), {0: 2.2, 1: 2, 2: 2})
117
        assert_equal(G.out_degree(0, weight='other'), 2.2)
118
        assert_equal(list(G.out_degree(iter([0]), weight='other')), [(0, 2.2)])
119

    
120
    def test_out_degree(self):
121
        G = self.K3
122
        assert_equal(sorted(G.out_degree()), [(0, 2), (1, 2), (2, 2)])
123
        assert_equal(dict(G.out_degree()), {0: 2, 1: 2, 2: 2})
124
        assert_equal(G.out_degree(0), 2)
125
        assert_equal(list(G.out_degree(iter([0]))), [(0, 2)])
126

    
127
    def test_size(self):
128
        G = self.K3
129
        assert_equal(G.size(), 6)
130
        assert_equal(G.number_of_edges(), 6)
131

    
132
    def test_to_undirected_reciprocal(self):
133
        G = self.Graph()
134
        G.add_edge(1, 2)
135
        assert_true(G.to_undirected().has_edge(1, 2))
136
        assert_false(G.to_undirected(reciprocal=True).has_edge(1, 2))
137
        G.add_edge(2, 1)
138
        assert_true(G.to_undirected(reciprocal=True).has_edge(1, 2))
139

    
140
    def test_reverse_copy(self):
141
        G = nx.DiGraph([(0, 1), (1, 2)])
142
        R = G.reverse()
143
        assert_equal(sorted(R.edges()), [(1, 0), (2, 1)])
144
        R.remove_edge(1, 0)
145
        assert_equal(sorted(R.edges()), [(2, 1)])
146
        assert_equal(sorted(G.edges()), [(0, 1), (1, 2)])
147

    
148
    def test_reverse_nocopy(self):
149
        G = nx.DiGraph([(0, 1), (1, 2)])
150
        R = G.reverse(copy=False)
151
        assert_equal(sorted(R.edges()), [(1, 0), (2, 1)])
152
        assert_raises(nx.NetworkXError, R.remove_edge, 1, 0)
153

    
154
    def test_reverse_hashable(self):
155
        class Foo(object):
156
            pass
157
        x = Foo()
158
        y = Foo()
159
        G = nx.DiGraph()
160
        G.add_edge(x, y)
161
        assert_nodes_equal(G.nodes(), G.reverse().nodes())
162
        assert_equal([(y, x)], list(G.reverse().edges()))
163

    
164

    
165
class BaseAttrDiGraphTester(BaseDiGraphTester, BaseAttrGraphTester):
166
    pass
167

    
168

    
169
class TestDiGraph(BaseAttrDiGraphTester, TestGraph):
170
    """Tests specific to dict-of-dict-of-dict digraph data structure"""
171

    
172
    def setUp(self):
173
        self.Graph = nx.DiGraph
174
        # build dict-of-dict-of-dict K3
175
        ed1, ed2, ed3, ed4, ed5, ed6 = ({}, {}, {}, {}, {}, {})
176
        self.k3adj = {0: {1: ed1, 2: ed2}, 1: {0: ed3, 2: ed4}, 2: {0: ed5, 1: ed6}}
177
        self.k3edges = [(0, 1), (0, 2), (1, 2)]
178
        self.k3nodes = [0, 1, 2]
179
        self.K3 = self.Graph()
180
        self.K3._adj = self.K3._succ = self.k3adj
181
        self.K3._pred = {0: {1: ed3, 2: ed5}, 1: {0: ed1, 2: ed6}, 2: {0: ed2, 1: ed4}}
182
        self.K3._node = {}
183
        self.K3._node[0] = {}
184
        self.K3._node[1] = {}
185
        self.K3._node[2] = {}
186

    
187
        ed1, ed2 = ({}, {})
188
        self.P3 = self.Graph()
189
        self.P3._adj = {0: {1: ed1}, 1: {2: ed2}, 2: {}}
190
        self.P3._succ = self.P3._adj
191
        self.P3._pred = {0: {}, 1: {0: ed1}, 2: {1: ed2}}
192
        self.P3._node = {}
193
        self.P3._node[0] = {}
194
        self.P3._node[1] = {}
195
        self.P3._node[2] = {}
196

    
197
    def test_data_input(self):
198
        G = self.Graph({1: [2], 2: [1]}, name="test")
199
        assert_equal(G.name, "test")
200
        assert_equal(sorted(G.adj.items()), [(1, {2: {}}), (2, {1: {}})])
201
        assert_equal(sorted(G.succ.items()), [(1, {2: {}}), (2, {1: {}})])
202
        assert_equal(sorted(G.pred.items()), [(1, {2: {}}), (2, {1: {}})])
203

    
204
    def test_add_edge(self):
205
        G = self.Graph()
206
        G.add_edge(0, 1)
207
        assert_equal(G.adj, {0: {1: {}}, 1: {}})
208
        assert_equal(G.succ, {0: {1: {}}, 1: {}})
209
        assert_equal(G.pred, {0: {}, 1: {0: {}}})
210
        G = self.Graph()
211
        G.add_edge(*(0, 1))
212
        assert_equal(G.adj, {0: {1: {}}, 1: {}})
213
        assert_equal(G.succ, {0: {1: {}}, 1: {}})
214
        assert_equal(G.pred, {0: {}, 1: {0: {}}})
215

    
216
    def test_add_edges_from(self):
217
        G = self.Graph()
218
        G.add_edges_from([(0, 1), (0, 2, {'data': 3})], data=2)
219
        assert_equal(G.adj, {0: {1: {'data': 2}, 2: {'data': 3}}, 1: {}, 2: {}})
220
        assert_equal(G.succ, {0: {1: {'data': 2}, 2: {'data': 3}}, 1: {}, 2: {}})
221
        assert_equal(G.pred, {0: {}, 1: {0: {'data': 2}}, 2: {0: {'data': 3}}})
222

    
223
        assert_raises(nx.NetworkXError, G.add_edges_from, [(0,)])  # too few in tuple
224
        assert_raises(nx.NetworkXError, G.add_edges_from, [(0, 1, 2, 3)])  # too many in tuple
225
        assert_raises(TypeError, G.add_edges_from, [0])  # not a tuple
226

    
227
    def test_remove_edge(self):
228
        G = self.K3
229
        G.remove_edge(0, 1)
230
        assert_equal(G.succ, {0: {2: {}}, 1: {0: {}, 2: {}}, 2: {0: {}, 1: {}}})
231
        assert_equal(G.pred, {0: {1: {}, 2: {}}, 1: {2: {}}, 2: {0: {}, 1: {}}})
232
        assert_raises((KeyError, nx.NetworkXError), G.remove_edge, -1, 0)
233

    
234
    def test_remove_edges_from(self):
235
        G = self.K3
236
        G.remove_edges_from([(0, 1)])
237
        assert_equal(G.succ, {0: {2: {}}, 1: {0: {}, 2: {}}, 2: {0: {}, 1: {}}})
238
        assert_equal(G.pred, {0: {1: {}, 2: {}}, 1: {2: {}}, 2: {0: {}, 1: {}}})
239
        G.remove_edges_from([(0, 0)])  # silent fail
240

    
241

    
242
class TestEdgeSubgraph(TestGraphEdgeSubgraph):
243
    """Unit tests for the :meth:`DiGraph.edge_subgraph` method."""
244

    
245
    def setup(self):
246
        # Create a doubly-linked path graph on five nodes.
247
        G = nx.DiGraph(nx.path_graph(5))
248
        # Add some node, edge, and graph attributes.
249
        for i in range(5):
250
            G.nodes[i]['name'] = 'node{}'.format(i)
251
        G.edges[0, 1]['name'] = 'edge01'
252
        G.edges[3, 4]['name'] = 'edge34'
253
        G.graph['name'] = 'graph'
254
        # Get the subgraph induced by the first and last edges.
255
        self.G = G
256
        self.H = G.edge_subgraph([(0, 1), (3, 4)])
257

    
258
    def test_pred_succ(self):
259
        """Test that nodes are added to predecessors and successors.
260

261
        For more information, see GitHub issue #2370.
262

263
        """
264
        G = nx.DiGraph()
265
        G.add_edge(0, 1)
266
        H = G.edge_subgraph([(0, 1)])
267
        assert_equal(list(H.predecessors(0)), [])
268
        assert_equal(list(H.successors(0)), [1])
269
        assert_equal(list(H.predecessors(1)), [0])
270
        assert_equal(list(H.successors(1)), [])