1
1
import random
2
2
3
- import networkx
3
+ import networkx as nx
4
4
import pytest
5
5
6
6
numpy = pytest .importorskip ("numpy" )
17
17
class TestPageRank :
18
18
@classmethod
19
19
def setup_class (cls ):
20
- G = networkx .DiGraph ()
20
+ G = nx .DiGraph ()
21
21
edges = [
22
22
(1 , 2 ),
23
23
(1 , 3 ),
@@ -55,7 +55,7 @@ def setup_class(cls):
55
55
)
56
56
)
57
57
58
- @pytest .mark .parametrize ("alg" , (networkx .pagerank , _pagerank_python ))
58
+ @pytest .mark .parametrize ("alg" , (nx .pagerank , _pagerank_python ))
59
59
def test_pagerank (self , alg ):
60
60
G = self .G
61
61
p = alg (G , alpha = 0.9 , tol = 1.0e-08 )
@@ -67,30 +67,28 @@ def test_pagerank(self, alg):
67
67
for n in G :
68
68
assert almost_equal (p [n ], G .pagerank [n ], places = 4 )
69
69
70
- @pytest .mark .parametrize ("alg" , (networkx .pagerank , _pagerank_python ))
70
+ @pytest .mark .parametrize ("alg" , (nx .pagerank , _pagerank_python ))
71
71
def test_pagerank_max_iter (self , alg ):
72
- with pytest .raises (networkx .PowerIterationFailedConvergence ):
72
+ with pytest .raises (nx .PowerIterationFailedConvergence ):
73
73
alg (self .G , max_iter = 0 )
74
74
75
75
def test_numpy_pagerank (self ):
76
76
G = self .G
77
- p = networkx .pagerank_numpy (G , alpha = 0.9 )
77
+ p = nx .pagerank_numpy (G , alpha = 0.9 )
78
78
for n in G :
79
79
assert almost_equal (p [n ], G .pagerank [n ], places = 4 )
80
80
81
81
def test_google_matrix (self ):
82
82
G = self .G
83
- M = networkx .google_matrix (G , alpha = 0.9 , nodelist = sorted (G ))
83
+ M = nx .google_matrix (G , alpha = 0.9 , nodelist = sorted (G ))
84
84
e , ev = numpy .linalg .eig (M .T )
85
85
p = numpy .array (ev [:, 0 ] / ev [:, 0 ].sum ())[:, 0 ]
86
86
for (a , b ) in zip (p , self .G .pagerank .values ()):
87
87
assert almost_equal (a , b )
88
88
89
- @pytest .mark .parametrize (
90
- "alg" , (networkx .pagerank , _pagerank_python , networkx .pagerank_numpy )
91
- )
89
+ @pytest .mark .parametrize ("alg" , (nx .pagerank , _pagerank_python , nx .pagerank_numpy ))
92
90
def test_personalization (self , alg ):
93
- G = networkx .complete_graph (4 )
91
+ G = nx .complete_graph (4 )
94
92
personalize = {0 : 1 , 1 : 1 , 2 : 4 , 3 : 4 }
95
93
answer = {
96
94
0 : 0.23246732615667579 ,
@@ -102,17 +100,15 @@ def test_personalization(self, alg):
102
100
for n in G :
103
101
assert almost_equal (p [n ], answer [n ], places = 4 )
104
102
105
- @pytest .mark .parametrize (
106
- "alg" , (networkx .pagerank , _pagerank_python , networkx .google_matrix )
107
- )
103
+ @pytest .mark .parametrize ("alg" , (nx .pagerank , _pagerank_python , nx .google_matrix ))
108
104
def test_zero_personalization_vector (self , alg ):
109
- G = networkx .complete_graph (4 )
105
+ G = nx .complete_graph (4 )
110
106
personalize = {0 : 0 , 1 : 0 , 2 : 0 , 3 : 0 }
111
107
pytest .raises (ZeroDivisionError , alg , G , personalization = personalize )
112
108
113
- @pytest .mark .parametrize ("alg" , (networkx .pagerank , _pagerank_python ))
109
+ @pytest .mark .parametrize ("alg" , (nx .pagerank , _pagerank_python ))
114
110
def test_one_nonzero_personalization_value (self , alg ):
115
- G = networkx .complete_graph (4 )
111
+ G = nx .complete_graph (4 )
116
112
personalize = {0 : 0 , 1 : 0 , 2 : 0 , 3 : 1 }
117
113
answer = {
118
114
0 : 0.22077931820379187 ,
@@ -124,9 +120,9 @@ def test_one_nonzero_personalization_value(self, alg):
124
120
for n in G :
125
121
assert almost_equal (p [n ], answer [n ], places = 4 )
126
122
127
- @pytest .mark .parametrize ("alg" , (networkx .pagerank , _pagerank_python ))
123
+ @pytest .mark .parametrize ("alg" , (nx .pagerank , _pagerank_python ))
128
124
def test_incomplete_personalization (self , alg ):
129
- G = networkx .complete_graph (4 )
125
+ G = nx .complete_graph (4 )
130
126
personalize = {3 : 1 }
131
127
answer = {
132
128
0 : 0.22077931820379187 ,
@@ -146,8 +142,8 @@ def test_dangling_matrix(self):
146
142
G = self .G
147
143
dangling = self .dangling_edges
148
144
dangling_sum = float (sum (dangling .values ()))
149
- M1 = networkx .google_matrix (G , personalization = dangling )
150
- M2 = networkx .google_matrix (G , personalization = dangling , dangling = dangling )
145
+ M1 = nx .google_matrix (G , personalization = dangling )
146
+ M2 = nx .google_matrix (G , personalization = dangling , dangling = dangling )
151
147
for i in range (len (G )):
152
148
for j in range (len (G )):
153
149
if i == self .dangling_node_index and (j + 1 ) in dangling :
@@ -157,24 +153,22 @@ def test_dangling_matrix(self):
157
153
else :
158
154
assert almost_equal (M2 [i , j ], M1 [i , j ], places = 4 )
159
155
160
- @pytest .mark .parametrize (
161
- "alg" , (networkx .pagerank , _pagerank_python , networkx .pagerank_numpy )
162
- )
156
+ @pytest .mark .parametrize ("alg" , (nx .pagerank , _pagerank_python , nx .pagerank_numpy ))
163
157
def test_dangling_pagerank (self , alg ):
164
158
pr = alg (self .G , dangling = self .dangling_edges )
165
159
for n in self .G :
166
160
assert almost_equal (pr [n ], self .G .dangling_pagerank [n ], places = 4 )
167
161
168
162
def test_empty (self ):
169
- G = networkx .Graph ()
170
- assert networkx .pagerank (G ) == {}
163
+ G = nx .Graph ()
164
+ assert nx .pagerank (G ) == {}
171
165
assert _pagerank_python (G ) == {}
172
- assert networkx .pagerank_numpy (G ) == {}
173
- assert networkx .google_matrix (G ).shape == (0 , 0 )
166
+ assert nx .pagerank_numpy (G ) == {}
167
+ assert nx .google_matrix (G ).shape == (0 , 0 )
174
168
175
- @pytest .mark .parametrize ("alg" , (networkx .pagerank , _pagerank_python ))
169
+ @pytest .mark .parametrize ("alg" , (nx .pagerank , _pagerank_python ))
176
170
def test_multigraph (self , alg ):
177
- G = networkx .MultiGraph ()
171
+ G = nx .MultiGraph ()
178
172
G .add_edges_from ([(1 , 2 ), (1 , 2 ), (1 , 2 ), (2 , 3 ), (2 , 3 ), ("3" , 3 ), ("3" , 3 )])
179
173
answer = {
180
174
1 : 0.21066048614468322 ,
@@ -190,42 +184,40 @@ def test_multigraph(self, alg):
190
184
class TestPageRankScipy (TestPageRank ):
191
185
def test_scipy_pagerank (self ):
192
186
G = self .G
193
- p = networkx .pagerank_scipy (G , alpha = 0.9 , tol = 1.0e-08 )
187
+ p = nx .pagerank_scipy (G , alpha = 0.9 , tol = 1.0e-08 )
194
188
for n in G :
195
189
assert almost_equal (p [n ], G .pagerank [n ], places = 4 )
196
190
personalize = {n : random .random () for n in G }
197
- p = networkx .pagerank_scipy (
198
- G , alpha = 0.9 , tol = 1.0e-08 , personalization = personalize
199
- )
191
+ p = nx .pagerank_scipy (G , alpha = 0.9 , tol = 1.0e-08 , personalization = personalize )
200
192
201
193
nstart = {n : random .random () for n in G }
202
- p = networkx .pagerank_scipy (G , alpha = 0.9 , tol = 1.0e-08 , nstart = nstart )
194
+ p = nx .pagerank_scipy (G , alpha = 0.9 , tol = 1.0e-08 , nstart = nstart )
203
195
for n in G :
204
196
assert almost_equal (p [n ], G .pagerank [n ], places = 4 )
205
197
206
198
def test_scipy_pagerank_max_iter (self ):
207
- with pytest .raises (networkx .PowerIterationFailedConvergence ):
208
- networkx .pagerank_scipy (self .G , max_iter = 0 )
199
+ with pytest .raises (nx .PowerIterationFailedConvergence ):
200
+ nx .pagerank_scipy (self .G , max_iter = 0 )
209
201
210
202
def test_dangling_scipy_pagerank (self ):
211
- pr = networkx .pagerank_scipy (self .G , dangling = self .dangling_edges )
203
+ pr = nx .pagerank_scipy (self .G , dangling = self .dangling_edges )
212
204
for n in self .G :
213
205
assert almost_equal (pr [n ], self .G .dangling_pagerank [n ], places = 4 )
214
206
215
207
def test_empty_scipy (self ):
216
- G = networkx .Graph ()
217
- assert networkx .pagerank_scipy (G ) == {}
208
+ G = nx .Graph ()
209
+ assert nx .pagerank_scipy (G ) == {}
218
210
219
211
220
212
@pytest .mark .parametrize (
221
213
"pagerank_alg" ,
222
- (networkx .pagerank_numpy , networkx .pagerank_scipy ),
214
+ (nx .pagerank_numpy , nx .pagerank_scipy ),
223
215
)
224
216
def test_deprecation_warnings (pagerank_alg ):
225
217
"""Make sure deprecation warnings are raised.
226
218
227
219
To be removed when deprecations expire.
228
220
"""
229
- G = networkx .DiGraph (networkx .path_graph (4 ))
221
+ G = nx .DiGraph (nx .path_graph (4 ))
230
222
with pytest .warns (DeprecationWarning ):
231
223
pr = pagerank_alg (G , alpha = 0.9 )
0 commit comments