Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / operators / tests / test_all.py @ 5cef0f13

History | View | Annotate | Download (5.69 KB)

1
from nose.tools import *
2
import networkx as nx
3
from networkx.testing import *
4

    
5

    
6
def test_union_all_attributes():
7
    g = nx.Graph()
8
    g.add_node(0, x=4)
9
    g.add_node(1, x=5)
10
    g.add_edge(0, 1, size=5)
11
    g.graph['name'] = 'g'
12

    
13
    h = g.copy()
14
    h.graph['name'] = 'h'
15
    h.graph['attr'] = 'attr'
16
    h.nodes[0]['x'] = 7
17

    
18
    j = g.copy()
19
    j.graph['name'] = 'j'
20
    j.graph['attr'] = 'attr'
21
    j.nodes[0]['x'] = 7
22

    
23
    ghj = nx.union_all([g, h, j], rename=('g', 'h', 'j'))
24
    assert_equal(set(ghj.nodes()), set(['h0', 'h1', 'g0', 'g1', 'j0', 'j1']))
25
    for n in ghj:
26
        graph, node = n
27
        assert_equal(ghj.nodes[n], eval(graph).nodes[int(node)])
28

    
29
    assert_equal(ghj.graph['attr'], 'attr')
30
    assert_equal(ghj.graph['name'], 'j')  # j graph attributes take precendent
31

    
32

    
33
def test_intersection_all():
34
    G = nx.Graph()
35
    H = nx.Graph()
36
    R = nx.Graph()
37
    G.add_nodes_from([1, 2, 3, 4])
38
    G.add_edge(1, 2)
39
    G.add_edge(2, 3)
40
    H.add_nodes_from([1, 2, 3, 4])
41
    H.add_edge(2, 3)
42
    H.add_edge(3, 4)
43
    R.add_nodes_from([1, 2, 3, 4])
44
    R.add_edge(2, 3)
45
    R.add_edge(4, 1)
46
    I = nx.intersection_all([G, H, R])
47
    assert_equal(set(I.nodes()), set([1, 2, 3, 4]))
48
    assert_equal(sorted(I.edges()), [(2, 3)])
49

    
50

    
51
def test_intersection_all_attributes():
52
    g = nx.Graph()
53
    g.add_node(0, x=4)
54
    g.add_node(1, x=5)
55
    g.add_edge(0, 1, size=5)
56
    g.graph['name'] = 'g'
57

    
58
    h = g.copy()
59
    h.graph['name'] = 'h'
60
    h.graph['attr'] = 'attr'
61
    h.nodes[0]['x'] = 7
62

    
63
    gh = nx.intersection_all([g, h])
64
    assert_equal(set(gh.nodes()), set(g.nodes()))
65
    assert_equal(set(gh.nodes()), set(h.nodes()))
66
    assert_equal(sorted(gh.edges()), sorted(g.edges()))
67

    
68
    h.remove_node(0)
69
    assert_raises(nx.NetworkXError, nx.intersection, g, h)
70

    
71

    
72
def test_intersection_all_multigraph_attributes():
73
    g = nx.MultiGraph()
74
    g.add_edge(0, 1, key=0)
75
    g.add_edge(0, 1, key=1)
76
    g.add_edge(0, 1, key=2)
77
    h = nx.MultiGraph()
78
    h.add_edge(0, 1, key=0)
79
    h.add_edge(0, 1, key=3)
80
    gh = nx.intersection_all([g, h])
81
    assert_equal(set(gh.nodes()), set(g.nodes()))
82
    assert_equal(set(gh.nodes()), set(h.nodes()))
83
    assert_equal(sorted(gh.edges()), [(0, 1)])
84
    assert_equal(sorted(gh.edges(keys=True)), [(0, 1, 0)])
85

    
86

    
87
def test_union_all_and_compose_all():
88
    K3 = nx.complete_graph(3)
89
    P3 = nx.path_graph(3)
90

    
91
    G1 = nx.DiGraph()
92
    G1.add_edge('A', 'B')
93
    G1.add_edge('A', 'C')
94
    G1.add_edge('A', 'D')
95
    G2 = nx.DiGraph()
96
    G2.add_edge('1', '2')
97
    G2.add_edge('1', '3')
98
    G2.add_edge('1', '4')
99

    
100
    G = nx.union_all([G1, G2])
101
    H = nx.compose_all([G1, G2])
102
    assert_edges_equal(G.edges(), H.edges())
103
    assert_false(G.has_edge('A', '1'))
104
    assert_raises(nx.NetworkXError, nx.union, K3, P3)
105
    H1 = nx.union_all([H, G1], rename=('H', 'G1'))
106
    assert_equal(sorted(H1.nodes()),
107
                 ['G1A', 'G1B', 'G1C', 'G1D',
108
                  'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])
109

    
110
    H2 = nx.union_all([H, G2], rename=("H", ""))
111
    assert_equal(sorted(H2.nodes()),
112
                 ['1', '2', '3', '4',
113
                  'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])
114

    
115
    assert_false(H1.has_edge('NB', 'NA'))
116

    
117
    G = nx.compose_all([G, G])
118
    assert_edges_equal(G.edges(), H.edges())
119

    
120
    G2 = nx.union_all([G2, G2], rename=('', 'copy'))
121
    assert_equal(sorted(G2.nodes()),
122
                 ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4'])
123

    
124
    assert_equal(sorted(G2.neighbors('copy4')), [])
125
    assert_equal(sorted(G2.neighbors('copy1')), ['copy2', 'copy3', 'copy4'])
126
    assert_equal(len(G), 8)
127
    assert_equal(nx.number_of_edges(G), 6)
128

    
129
    E = nx.disjoint_union_all([G, G])
130
    assert_equal(len(E), 16)
131
    assert_equal(nx.number_of_edges(E), 12)
132

    
133
    E = nx.disjoint_union_all([G1, G2])
134
    assert_equal(sorted(E.nodes()), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
135

    
136
    G1 = nx.DiGraph()
137
    G1.add_edge('A', 'B')
138
    G2 = nx.DiGraph()
139
    G2.add_edge(1, 2)
140
    G3 = nx.DiGraph()
141
    G3.add_edge(11, 22)
142
    G4 = nx.union_all([G1, G2, G3], rename=("G1", "G2", "G3"))
143
    assert_equal(sorted(G4.nodes()),
144
                 ['G1A', 'G1B', 'G21', 'G22',
145
                  'G311', 'G322'])
146

    
147

    
148
def test_union_all_multigraph():
149
    G = nx.MultiGraph()
150
    G.add_edge(1, 2, key=0)
151
    G.add_edge(1, 2, key=1)
152
    H = nx.MultiGraph()
153
    H.add_edge(3, 4, key=0)
154
    H.add_edge(3, 4, key=1)
155
    GH = nx.union_all([G, H])
156
    assert_equal(set(GH), set(G) | set(H))
157
    assert_equal(set(GH.edges(keys=True)),
158
                 set(G.edges(keys=True)) | set(H.edges(keys=True)))
159

    
160

    
161
def test_input_output():
162
    l = [nx.Graph([(1, 2)]), nx.Graph([(3, 4)])]
163
    U = nx.disjoint_union_all(l)
164
    assert_equal(len(l), 2)
165
    C = nx.compose_all(l)
166
    assert_equal(len(l), 2)
167
    l = [nx.Graph([(1, 2)]), nx.Graph([(1, 2)])]
168
    R = nx.intersection_all(l)
169
    assert_equal(len(l), 2)
170

    
171

    
172
@raises(nx.NetworkXError)
173
def test_mixed_type_union():
174
    G = nx.Graph()
175
    H = nx.MultiGraph()
176
    I = nx.Graph()
177
    U = nx.union_all([G, H, I])
178

    
179

    
180
@raises(nx.NetworkXError)
181
def test_mixed_type_disjoint_union():
182
    G = nx.Graph()
183
    H = nx.MultiGraph()
184
    I = nx.Graph()
185
    U = nx.disjoint_union_all([G, H, I])
186

    
187

    
188
@raises(nx.NetworkXError)
189
def test_mixed_type_intersection():
190
    G = nx.Graph()
191
    H = nx.MultiGraph()
192
    I = nx.Graph()
193
    U = nx.intersection_all([G, H, I])
194

    
195

    
196
@raises(nx.NetworkXError)
197
def test_mixed_type_compose():
198
    G = nx.Graph()
199
    H = nx.MultiGraph()
200
    I = nx.Graph()
201
    U = nx.compose_all([G, H, I])
202

    
203

    
204
@raises(ValueError)
205
def test_empty_union():
206
    nx.union_all([])
207

    
208

    
209
@raises(ValueError)
210
def test_empty_disjoint_union():
211
    nx.disjoint_union_all([])
212

    
213

    
214
@raises(ValueError)
215
def test_empty_compose_all():
216
    nx.compose_all([])
217

    
218

    
219
@raises(ValueError)
220
def test_empty_intersection_all():
221
    nx.intersection_all([])