Skip to content

Commit 23391dd

Browse files
Pagerank followup (networkx#4399)
1 parent a1cad29 commit 23391dd

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

networkx/algorithms/link_analysis/pagerank_alg.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def google_matrix(
236236
if nodelist is None:
237237
nodelist = list(G)
238238

239-
M = nx.to_numpy_matrix(G, nodelist=nodelist, weight=weight)
239+
M = np.asmatrix(nx.to_numpy_array(G, nodelist=nodelist, weight=weight))
240240
N = len(G)
241241
if N == 0:
242242
return M
@@ -336,7 +336,7 @@ def pagerank_numpy(G, alpha=0.85, personalization=None, weight="weight", danglin
336336
The PageRank citation ranking: Bringing order to the Web. 1999
337337
http://dbpubs.stanford.edu:8090/pub/showDoc.Fulltext?lang=en&doc=1999-66&format=pdf
338338
"""
339-
msg = "networkx.pagerank_numpy will be deprecated in NetworkX 3.0, use networkx.pagerank instead."
339+
msg = "networkx.pagerank_numpy is deprecated and will be removed in NetworkX 3.0, use networkx.pagerank instead."
340340
warn(msg, DeprecationWarning, stacklevel=2)
341341
import numpy as np
342342

@@ -446,7 +446,7 @@ def pagerank_scipy(
446446
The PageRank citation ranking: Bringing order to the Web. 1999
447447
http://dbpubs.stanford.edu:8090/pub/showDoc.Fulltext?lang=en&doc=1999-66&format=pdf
448448
"""
449-
msg = "networkx.pagerank_scipy will be deprecated in NetworkX 3.0, use networkx.pagerank instead."
449+
msg = "networkx.pagerank_scipy is deprecated and will be removed in NetworkX 3.0, use networkx.pagerank instead."
450450
warn(msg, DeprecationWarning, stacklevel=2)
451451
import numpy as np
452452
import scipy.sparse

networkx/algorithms/link_analysis/tests/test_pagerank.py

+34-42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import random
22

3-
import networkx
3+
import networkx as nx
44
import pytest
55

66
numpy = pytest.importorskip("numpy")
@@ -17,7 +17,7 @@
1717
class TestPageRank:
1818
@classmethod
1919
def setup_class(cls):
20-
G = networkx.DiGraph()
20+
G = nx.DiGraph()
2121
edges = [
2222
(1, 2),
2323
(1, 3),
@@ -55,7 +55,7 @@ def setup_class(cls):
5555
)
5656
)
5757

58-
@pytest.mark.parametrize("alg", (networkx.pagerank, _pagerank_python))
58+
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python))
5959
def test_pagerank(self, alg):
6060
G = self.G
6161
p = alg(G, alpha=0.9, tol=1.0e-08)
@@ -67,30 +67,28 @@ def test_pagerank(self, alg):
6767
for n in G:
6868
assert almost_equal(p[n], G.pagerank[n], places=4)
6969

70-
@pytest.mark.parametrize("alg", (networkx.pagerank, _pagerank_python))
70+
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python))
7171
def test_pagerank_max_iter(self, alg):
72-
with pytest.raises(networkx.PowerIterationFailedConvergence):
72+
with pytest.raises(nx.PowerIterationFailedConvergence):
7373
alg(self.G, max_iter=0)
7474

7575
def test_numpy_pagerank(self):
7676
G = self.G
77-
p = networkx.pagerank_numpy(G, alpha=0.9)
77+
p = nx.pagerank_numpy(G, alpha=0.9)
7878
for n in G:
7979
assert almost_equal(p[n], G.pagerank[n], places=4)
8080

8181
def test_google_matrix(self):
8282
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))
8484
e, ev = numpy.linalg.eig(M.T)
8585
p = numpy.array(ev[:, 0] / ev[:, 0].sum())[:, 0]
8686
for (a, b) in zip(p, self.G.pagerank.values()):
8787
assert almost_equal(a, b)
8888

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))
9290
def test_personalization(self, alg):
93-
G = networkx.complete_graph(4)
91+
G = nx.complete_graph(4)
9492
personalize = {0: 1, 1: 1, 2: 4, 3: 4}
9593
answer = {
9694
0: 0.23246732615667579,
@@ -102,17 +100,15 @@ def test_personalization(self, alg):
102100
for n in G:
103101
assert almost_equal(p[n], answer[n], places=4)
104102

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))
108104
def test_zero_personalization_vector(self, alg):
109-
G = networkx.complete_graph(4)
105+
G = nx.complete_graph(4)
110106
personalize = {0: 0, 1: 0, 2: 0, 3: 0}
111107
pytest.raises(ZeroDivisionError, alg, G, personalization=personalize)
112108

113-
@pytest.mark.parametrize("alg", (networkx.pagerank, _pagerank_python))
109+
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python))
114110
def test_one_nonzero_personalization_value(self, alg):
115-
G = networkx.complete_graph(4)
111+
G = nx.complete_graph(4)
116112
personalize = {0: 0, 1: 0, 2: 0, 3: 1}
117113
answer = {
118114
0: 0.22077931820379187,
@@ -124,9 +120,9 @@ def test_one_nonzero_personalization_value(self, alg):
124120
for n in G:
125121
assert almost_equal(p[n], answer[n], places=4)
126122

127-
@pytest.mark.parametrize("alg", (networkx.pagerank, _pagerank_python))
123+
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python))
128124
def test_incomplete_personalization(self, alg):
129-
G = networkx.complete_graph(4)
125+
G = nx.complete_graph(4)
130126
personalize = {3: 1}
131127
answer = {
132128
0: 0.22077931820379187,
@@ -146,8 +142,8 @@ def test_dangling_matrix(self):
146142
G = self.G
147143
dangling = self.dangling_edges
148144
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)
151147
for i in range(len(G)):
152148
for j in range(len(G)):
153149
if i == self.dangling_node_index and (j + 1) in dangling:
@@ -157,24 +153,22 @@ def test_dangling_matrix(self):
157153
else:
158154
assert almost_equal(M2[i, j], M1[i, j], places=4)
159155

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))
163157
def test_dangling_pagerank(self, alg):
164158
pr = alg(self.G, dangling=self.dangling_edges)
165159
for n in self.G:
166160
assert almost_equal(pr[n], self.G.dangling_pagerank[n], places=4)
167161

168162
def test_empty(self):
169-
G = networkx.Graph()
170-
assert networkx.pagerank(G) == {}
163+
G = nx.Graph()
164+
assert nx.pagerank(G) == {}
171165
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)
174168

175-
@pytest.mark.parametrize("alg", (networkx.pagerank, _pagerank_python))
169+
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python))
176170
def test_multigraph(self, alg):
177-
G = networkx.MultiGraph()
171+
G = nx.MultiGraph()
178172
G.add_edges_from([(1, 2), (1, 2), (1, 2), (2, 3), (2, 3), ("3", 3), ("3", 3)])
179173
answer = {
180174
1: 0.21066048614468322,
@@ -190,42 +184,40 @@ def test_multigraph(self, alg):
190184
class TestPageRankScipy(TestPageRank):
191185
def test_scipy_pagerank(self):
192186
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)
194188
for n in G:
195189
assert almost_equal(p[n], G.pagerank[n], places=4)
196190
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)
200192

201193
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)
203195
for n in G:
204196
assert almost_equal(p[n], G.pagerank[n], places=4)
205197

206198
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)
209201

210202
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)
212204
for n in self.G:
213205
assert almost_equal(pr[n], self.G.dangling_pagerank[n], places=4)
214206

215207
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) == {}
218210

219211

220212
@pytest.mark.parametrize(
221213
"pagerank_alg",
222-
(networkx.pagerank_numpy, networkx.pagerank_scipy),
214+
(nx.pagerank_numpy, nx.pagerank_scipy),
223215
)
224216
def test_deprecation_warnings(pagerank_alg):
225217
"""Make sure deprecation warnings are raised.
226218
227219
To be removed when deprecations expire.
228220
"""
229-
G = networkx.DiGraph(networkx.path_graph(4))
221+
G = nx.DiGraph(nx.path_graph(4))
230222
with pytest.warns(DeprecationWarning):
231223
pr = pagerank_alg(G, alpha=0.9)

0 commit comments

Comments
 (0)