Skip to content

Commit 6c7aa26

Browse files
authored
Merge pull request #21 from Mathics3/go-over-centrality
Correct problem with PageRank..
2 parents 7e0fc9b + 8b89ba4 commit 6c7aa26

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/build
55
/dist
66
/pymathics_graph.egg-info
7+
/tmp
78
__pycache__

pymathics/graph/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def coalesced_graph(self, evaluation):
408408
new_edges = defaultdict(lambda: 0)
409409
for u, v, w in self.G.edges.data("Pymathics`EdgeWeight", default=None):
410410
if w is not None:
411-
w = w.evaluate(evaluation).to_mpmath()
411+
w = float(w.evaluate(evaluation).to_mpmath())
412412
else:
413413
w = 1
414414
new_edges[(u, v)] += w
@@ -516,7 +516,6 @@ def get_sort_key(self, pattern_sort=False) -> tuple:
516516
len(self.vertices),
517517
hash(self),
518518
]
519-
return hash(self)
520519

521520
def sort_vertices(self, vertices):
522521
return sorted(vertices)

pymathics/graph/centralities.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
<url>:Centralities:https://en.wikipedia.org/wiki/Centrality</url>
77
88
9-
Routines to evaluate centralities associated to a graph.
10-
11-
In a graph, the centrality is an estimator associated to each of its \
12-
vertices, which accounts their degree of connectivity, according \
13-
to different criteria.
9+
Routines to evaluate centralities of a graph.
1410
11+
In graph theory and network analysis, the centrality is a \
12+
ranking between pairs of node according some metric.
1513
1614
"""
1715

@@ -71,13 +69,11 @@ class BetweennessCentrality(_Centrality):
7169
:Betweenness centrality:
7270
https://en.wikipedia.org/wiki/Betweenness_centrality</url> (<url>
7371
:NetworkX:
74-
https://networkx.org/documentation/networkx-2.8.8/reference/algorithms/generated/\
75-
networkx.algorithms.centrality.betweenness_centrality.html</url>,\
72+
https://networkx.org/documentation/networkx-2.8.8/reference/algorithms/generated/networkx.algorithms.centrality.betweenness_centrality.html</url>,\
7673
<url>
7774
:WMA:
7875
https://reference.wolfram.com/language/ref/BetweennessCentrality.html</url>)
7976
80-
8177
<dl>
8278
<dt>'BetweennessCentrality'[$g$]
8379
<dd>gives a list of betweenness centralities for the vertices \
@@ -90,11 +86,14 @@ class BetweennessCentrality(_Centrality):
9086
>> BetweennessCentrality[g]
9187
= {0., 1., 0., 0., 0.}
9288
93-
>> g = Graph[{a -> b, b -> c, c -> d, d -> e, e -> c, e -> a}]; BetweennessCentrality[g]
94-
= {3., 3., 6., 6., 6.}
89+
>> g = Graph[{a -> b, b -> c, c -> d, d -> e, e -> c, e -> a}]
90+
= -Graph-
91+
92+
>> BetweennessCentrality[g]
93+
= ...
9594
"""
9695

97-
summary_text = "get the betweenness centrality"
96+
summary_text = "get Betweenness centrality"
9897

9998
def eval(self, graph, expression, evaluation, options):
10099
"%(name)s[graph_, OptionsPattern[%(name)s]]"
@@ -258,7 +257,7 @@ class EigenvectorCentrality(_ComponentwiseCentrality):
258257
= {0.333333, 0.333333, 0.333333, 0., 0.}
259258
"""
260259

261-
summary_text = "compute the eigenvector centralities"
260+
summary_text = "compute Eigenvector centralities"
262261

263262
def _centrality(self, g, weight):
264263
return nx.eigenvector_centrality(g, max_iter=10000, tol=1.0e-7, weight=weight)
@@ -297,7 +296,7 @@ class HITSCentrality(_Centrality):
297296
298297
"""
299298

300-
summary_text = "get the HITS centrality"
299+
summary_text = "get HITS centrality"
301300

302301
def eval(self, graph, expression, evaluation, options):
303302
"%(name)s[graph_, OptionsPattern[%(name)s]]"
@@ -401,9 +400,7 @@ class PageRankCentrality(_Centrality):
401400
vertices in the graph $g$ and weight $alpha$ and initial centralities $beta$.
402401
</dl>
403402
404-
## Not working, possibly because an issue in networkx
405-
406-
## >> g = Graph[{a -> d, b -> c, d -> c, d -> a, e -> c, d -> c}]; PageRankCentrality[g, 0.2]
403+
>> g = Graph[{a -> d, b -> c, d -> c, d -> a, e -> c, d -> c}]; PageRankCentrality[g, 0.2]
407404
= {0.184502, 0.207565, 0.170664, 0.266605, 0.170664}
408405
"""
409406

@@ -413,7 +410,7 @@ def eval_alpha_beta(self, graph, alpha, expression, evaluation, options):
413410
"%(name)s[graph_, alpha_, OptionsPattern[%(name)s]]"
414411
graph = self._build_graph(graph, evaluation, options, expression)
415412
if graph:
416-
py_alpha = alpha.to_mpmath()
413+
py_alpha = float(alpha.to_mpmath())
417414
if py_alpha is None:
418415
return
419416
G, weight = graph.coalesced_graph(evaluation)

0 commit comments

Comments
 (0)