ioftools / networkxMiCe / networkxmaster / networkx / algorithms / assortativity / tests / test_mixing.py @ 5cef0f13
History  View  Annotate  Download (6.44 KB)
1 
#!/usr/bin/env python


2 
from nose.tools import * 
3 
from nose import SkipTest 
4 
import networkx as nx 
5 
from base_test import BaseTestAttributeMixing, BaseTestDegreeMixing 
6  
7  
8 
class TestDegreeMixingDict(BaseTestDegreeMixing): 
9  
10 
def test_degree_mixing_dict_undirected(self): 
11 
d = nx.degree_mixing_dict(self.P4)

12 
d_result = {1: {2: 2}, 
13 
2: {1: 2, 2: 2}, 
14 
} 
15 
assert_equal(d, d_result) 
16  
17 
def test_degree_mixing_dict_undirected_normalized(self): 
18 
d = nx.degree_mixing_dict(self.P4, normalized=True) 
19 
d_result = {1: {2: 1.0 / 3}, 
20 
2: {1: 1.0 / 3, 2: 1.0 / 3}, 
21 
} 
22 
assert_equal(d, d_result) 
23  
24 
def test_degree_mixing_dict_directed(self): 
25 
d = nx.degree_mixing_dict(self.D)

26 
print(d) 
27 
d_result = {1: {3: 2}, 
28 
2: {1: 1, 3: 1}, 
29 
3: {}

30 
} 
31 
assert_equal(d, d_result) 
32  
33 
def test_degree_mixing_dict_multigraph(self): 
34 
d = nx.degree_mixing_dict(self.M)

35 
d_result = {1: {2: 1}, 
36 
2: {1: 1, 3: 3}, 
37 
3: {2: 3} 
38 
} 
39 
assert_equal(d, d_result) 
40  
41  
42 
class TestDegreeMixingMatrix(BaseTestDegreeMixing): 
43  
44 
@classmethod

45 
def setupClass(cls): 
46 
global np

47 
global npt

48 
try:

49 
import numpy as np 
50 
import numpy.testing as npt 
51  
52 
except ImportError: 
53 
raise SkipTest('NumPy not available.') 
54  
55 
def test_degree_mixing_matrix_undirected(self): 
56 
a_result = np.array([[0, 0, 0], 
57 
[0, 0, 2], 
58 
[0, 2, 2]] 
59 
) 
60 
a = nx.degree_mixing_matrix(self.P4, normalized=False) 
61 
npt.assert_equal(a, a_result) 
62 
a = nx.degree_mixing_matrix(self.P4)

63 
npt.assert_equal(a, a_result / float(a_result.sum()))

64  
65 
def test_degree_mixing_matrix_directed(self): 
66 
a_result = np.array([[0, 0, 0, 0], 
67 
[0, 0, 0, 2], 
68 
[0, 1, 0, 1], 
69 
[0, 0, 0, 0]] 
70 
) 
71 
a = nx.degree_mixing_matrix(self.D, normalized=False) 
72 
npt.assert_equal(a, a_result) 
73 
a = nx.degree_mixing_matrix(self.D)

74 
npt.assert_equal(a, a_result / float(a_result.sum()))

75  
76 
def test_degree_mixing_matrix_multigraph(self): 
77 
a_result = np.array([[0, 0, 0, 0], 
78 
[0, 0, 1, 0], 
79 
[0, 1, 0, 3], 
80 
[0, 0, 3, 0]] 
81 
) 
82 
a = nx.degree_mixing_matrix(self.M, normalized=False) 
83 
npt.assert_equal(a, a_result) 
84 
a = nx.degree_mixing_matrix(self.M)

85 
npt.assert_equal(a, a_result / float(a_result.sum()))

86  
87 
def test_degree_mixing_matrix_selfloop(self): 
88 
a_result = np.array([[0, 0, 0], 
89 
[0, 0, 0], 
90 
[0, 0, 2]] 
91 
) 
92 
a = nx.degree_mixing_matrix(self.S, normalized=False) 
93 
npt.assert_equal(a, a_result) 
94 
a = nx.degree_mixing_matrix(self.S)

95 
npt.assert_equal(a, a_result / float(a_result.sum()))

96  
97  
98 
class TestAttributeMixingDict(BaseTestAttributeMixing): 
99  
100 
def test_attribute_mixing_dict_undirected(self): 
101 
d = nx.attribute_mixing_dict(self.G, 'fish') 
102 
d_result = {'one': {'one': 2, 'red': 1}, 
103 
'two': {'two': 2, 'blue': 1}, 
104 
'red': {'one': 1}, 
105 
'blue': {'two': 1} 
106 
} 
107 
assert_equal(d, d_result) 
108  
109 
def test_attribute_mixing_dict_directed(self): 
110 
d = nx.attribute_mixing_dict(self.D, 'fish') 
111 
d_result = {'one': {'one': 1, 'red': 1}, 
112 
'two': {'two': 1, 'blue': 1}, 
113 
'red': {},

114 
'blue': {}

115 
} 
116 
assert_equal(d, d_result) 
117  
118 
def test_attribute_mixing_dict_multigraph(self): 
119 
d = nx.attribute_mixing_dict(self.M, 'fish') 
120 
d_result = {'one': {'one': 4}, 
121 
'two': {'two': 2}, 
122 
} 
123 
assert_equal(d, d_result) 
124  
125  
126 
class TestAttributeMixingMatrix(BaseTestAttributeMixing): 
127 
@classmethod

128 
def setupClass(cls): 
129 
global np

130 
global npt

131 
try:

132 
import numpy as np 
133 
import numpy.testing as npt 
134  
135 
except ImportError: 
136 
raise SkipTest('NumPy not available.') 
137  
138 
def test_attribute_mixing_matrix_undirected(self): 
139 
mapping = {'one': 0, 'two': 1, 'red': 2, 'blue': 3} 
140 
a_result = np.array([[2, 0, 1, 0], 
141 
[0, 2, 0, 1], 
142 
[1, 0, 0, 0], 
143 
[0, 1, 0, 0]] 
144 
) 
145 
a = nx.attribute_mixing_matrix(self.G, 'fish', 
146 
mapping=mapping, 
147 
normalized=False)

148 
npt.assert_equal(a, a_result) 
149 
a = nx.attribute_mixing_matrix(self.G, 'fish', 
150 
mapping=mapping) 
151 
npt.assert_equal(a, a_result / float(a_result.sum()))

152  
153 
def test_attribute_mixing_matrix_directed(self): 
154 
mapping = {'one': 0, 'two': 1, 'red': 2, 'blue': 3} 
155 
a_result = np.array([[1, 0, 1, 0], 
156 
[0, 1, 0, 1], 
157 
[0, 0, 0, 0], 
158 
[0, 0, 0, 0]] 
159 
) 
160 
a = nx.attribute_mixing_matrix(self.D, 'fish', 
161 
mapping=mapping, 
162 
normalized=False)

163 
npt.assert_equal(a, a_result) 
164 
a = nx.attribute_mixing_matrix(self.D, 'fish', 
165 
mapping=mapping) 
166 
npt.assert_equal(a, a_result / float(a_result.sum()))

167  
168 
def test_attribute_mixing_matrix_multigraph(self): 
169 
mapping = {'one': 0, 'two': 1, 'red': 2, 'blue': 3} 
170 
a_result = np.array([[4, 0, 0, 0], 
171 
[0, 2, 0, 0], 
172 
[0, 0, 0, 0], 
173 
[0, 0, 0, 0]] 
174 
) 
175 
a = nx.attribute_mixing_matrix(self.M, 'fish', 
176 
mapping=mapping, 
177 
normalized=False)

178 
npt.assert_equal(a, a_result) 
179 
a = nx.attribute_mixing_matrix(self.M, 'fish', 
180 
mapping=mapping) 
181 
npt.assert_equal(a, a_result / float(a_result.sum()))
