ioftools / networkxMiCe / networkxmaster / networkx / algorithms / assortativity / tests / test_connectivity.py @ 5cef0f13
History  View  Annotate  Download (5.02 KB)
1 
from itertools import permutations 

2  
3 
from nose.tools import assert_almost_equal 
4 
from nose.tools import assert_equal 
5 
from nose.tools import raises 
6  
7 
import networkx as nx 
8  
9  
10 
class TestNeighborConnectivity(object): 
11  
12 
def test_degree_p4(self): 
13 
G = nx.path_graph(4)

14 
answer = {1: 2.0, 2: 1.5} 
15 
nd = nx.average_degree_connectivity(G) 
16 
assert_equal(nd, answer) 
17  
18 
D = G.to_directed() 
19 
answer = {2: 2.0, 4: 1.5} 
20 
nd = nx.average_degree_connectivity(D) 
21 
assert_equal(nd, answer) 
22  
23 
answer = {1: 2.0, 2: 1.5} 
24 
D = G.to_directed() 
25 
nd = nx.average_degree_connectivity(D, source='in', target='in') 
26 
assert_equal(nd, answer) 
27  
28 
D = G.to_directed() 
29 
nd = nx.average_degree_connectivity(D, source='in', target='in') 
30 
assert_equal(nd, answer) 
31  
32 
def test_degree_p4_weighted(self): 
33 
G = nx.path_graph(4)

34 
G[1][2]['weight'] = 4 
35 
answer = {1: 2.0, 2: 1.8} 
36 
nd = nx.average_degree_connectivity(G, weight='weight')

37 
assert_equal(nd, answer) 
38 
answer = {1: 2.0, 2: 1.5} 
39 
nd = nx.average_degree_connectivity(G) 
40 
assert_equal(nd, answer) 
41  
42 
D = G.to_directed() 
43 
answer = {2: 2.0, 4: 1.8} 
44 
nd = nx.average_degree_connectivity(D, weight='weight')

45 
assert_equal(nd, answer) 
46  
47 
answer = {1: 2.0, 2: 1.8} 
48 
D = G.to_directed() 
49 
nd = nx.average_degree_connectivity(D, weight='weight', source='in', 
50 
target='in')

51 
assert_equal(nd, answer) 
52  
53 
D = G.to_directed() 
54 
nd = nx.average_degree_connectivity(D, source='in', target='out', 
55 
weight='weight')

56 
assert_equal(nd, answer) 
57  
58 
def test_weight_keyword(self): 
59 
G = nx.path_graph(4)

60 
G[1][2]['other'] = 4 
61 
answer = {1: 2.0, 2: 1.8} 
62 
nd = nx.average_degree_connectivity(G, weight='other')

63 
assert_equal(nd, answer) 
64 
answer = {1: 2.0, 2: 1.5} 
65 
nd = nx.average_degree_connectivity(G, weight=None)

66 
assert_equal(nd, answer) 
67  
68 
D = G.to_directed() 
69 
answer = {2: 2.0, 4: 1.8} 
70 
nd = nx.average_degree_connectivity(D, weight='other')

71 
assert_equal(nd, answer) 
72  
73 
answer = {1: 2.0, 2: 1.8} 
74 
D = G.to_directed() 
75 
nd = nx.average_degree_connectivity(D, weight='other', source='in', 
76 
target='in')

77 
assert_equal(nd, answer) 
78  
79 
D = G.to_directed() 
80 
nd = nx.average_degree_connectivity(D, weight='other', source='in', 
81 
target='in')

82 
assert_equal(nd, answer) 
83  
84 
def test_degree_barrat(self): 
85 
G = nx.star_graph(5)

86 
G.add_edges_from([(5, 6), (5, 7), (5, 8), (5, 9)]) 
87 
G[0][5]['weight'] = 5 
88 
nd = nx.average_degree_connectivity(G)[5]

89 
assert_equal(nd, 1.8)

90 
nd = nx.average_degree_connectivity(G, weight='weight')[5] 
91 
assert_almost_equal(nd, 3.222222, places=5) 
92 
nd = nx.k_nearest_neighbors(G, weight='weight')[5] 
93 
assert_almost_equal(nd, 3.222222, places=5) 
94  
95 
def test_zero_deg(self): 
96 
G = nx.DiGraph() 
97 
G.add_edge(1, 2) 
98 
G.add_edge(1, 3) 
99 
G.add_edge(1, 4) 
100 
c = nx.average_degree_connectivity(G) 
101 
assert_equal(c, {1: 0, 3: 1}) 
102 
c = nx.average_degree_connectivity(G, source='in', target='in') 
103 
assert_equal(c, {0: 0, 1: 0}) 
104 
c = nx.average_degree_connectivity(G, source='in', target='out') 
105 
assert_equal(c, {0: 0, 1: 3}) 
106 
c = nx.average_degree_connectivity(G, source='in', target='in+out') 
107 
assert_equal(c, {0: 0, 1: 3}) 
108 
c = nx.average_degree_connectivity(G, source='out', target='out') 
109 
assert_equal(c, {0: 0, 3: 0}) 
110 
c = nx.average_degree_connectivity(G, source='out', target='in') 
111 
assert_equal(c, {0: 0, 3: 1}) 
112 
c = nx.average_degree_connectivity(G, source='out', target='in+out') 
113 
assert_equal(c, {0: 0, 3: 1}) 
114  
115 
def test_in_out_weight(self): 
116 
G = nx.DiGraph() 
117 
G.add_edge(1, 2, weight=1) 
118 
G.add_edge(1, 3, weight=1) 
119 
G.add_edge(3, 1, weight=1) 
120 
for s, t in permutations(['in', 'out', 'in+out'], 2): 
121 
c = nx.average_degree_connectivity(G, source=s, target=t) 
122 
cw = nx.average_degree_connectivity(G, source=s, target=t, 
123 
weight='weight')

124 
assert_equal(c, cw) 
125  
126 
@raises(ValueError) 
127 
def test_invalid_source(self): 
128 
G = nx.DiGraph() 
129 
nx.average_degree_connectivity(G, source='bogus')

130  
131 
@raises(ValueError) 
132 
def test_invalid_target(self): 
133 
G = nx.DiGraph() 
134 
nx.average_degree_connectivity(G, target='bogus')

135  
136 
def test_single_node(self): 
137 
# TODO Is this really the intended behavior for providing a

138 
# single node as the argument `nodes`? Shouldn't the function

139 
# just return the connectivity value itself?

140 
G = nx.trivial_graph() 
141 
conn = nx.average_degree_connectivity(G, nodes=0)

142 
assert_equal(conn, {0: 0}) 