Skip to content

Commit 91bef1e

Browse files
committed
Correct Graph options bug; go over examples.
* Graph options need to be in System context. * Improve examples for GraphDistance and component sections * Add GraphDistance summary
1 parent 47f50bf commit 91bef1e

File tree

6 files changed

+97
-58
lines changed

6 files changed

+97
-58
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ repos:
99
stages: [commit]
1010
- id: end-of-file-fixer
1111
stages: [commit]
12-
- repo: https://github.com/pycqa/isort
13-
rev: 5.10.1
14-
hooks:
15-
- id: isort
16-
stages: [commit]
12+
# - repo: https://github.com/pycqa/isort
13+
# rev: 5.10.1
14+
# hooks:
15+
# - id: isort
16+
# stages: [commit]
1717
- repo: https://github.com/psf/black
1818
rev: 22.3.0
1919
hooks:

pymathics/graph/base.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,27 @@ def _process_graph_options(g, options: dict) -> None:
124124
# g is where it is used in format. However we should wrap this as our object.
125125
# Access in G which might be better, currently isn't used.
126126
g.G.vertex_labels = g.vertex_labels = (
127-
options["Pymathics`VertexLabels"].to_python()
128-
if "Pymathics`VertexLabels" in options
127+
options["System`VertexLabels"].to_python()
128+
if "System`VertexLabels" in options
129129
else False
130130
)
131131
shape = (
132-
options["Pymathics`VertexShape"].get_string_value()
133-
if "Pymathics`VertexShape" in options
132+
options["System`VertexShape"].get_string_value()
133+
if "System`VertexShape" in options
134134
else "Circle"
135135
)
136136

137137
g.G.node_shape = g.node_shape = WL_MARKER_TO_NETWORKX.get(shape, shape)
138138

139139
color = (
140-
options["Pymathics`VertexStyle"].get_string_value()
141-
if "Pymathics`VertexStyle" in options
140+
options["System`VertexStyle"].get_string_value()
141+
if "System`VertexStyle" in options
142142
else "Blue"
143143
)
144144

145145
g.graph_layout = (
146-
options["Pymathics`GraphLayout"].get_string_value()
147-
if "Pymathics`GraphLayout" in options
146+
options["System`GraphLayout"].get_string_value()
147+
if "System`GraphLayout" in options
148148
else ""
149149
)
150150

@@ -573,7 +573,6 @@ def __init__(self, G, **kwargs):
573573
super(Graph, self).__init__()
574574
self.G = G
575575
self.mixed = kwargs.get("mixed", False)
576-
print("creating", self.G, self.mixed)
577576

578577
def __hash__(self):
579578
return hash(("Pymathics`Graph", self.G))
@@ -1082,7 +1081,10 @@ def neighbors(v):
10821081

10831082
class BetweennessCentrality(_Centrality):
10841083
"""
1085-
>> g = Graph[{a -> b, b -> c, d -> c, d -> a, e -> c, d -> b}]; BetweennessCentrality[g]
1084+
>> g = Graph[{a -> b, b -> c, d -> c, d -> a, e -> c, d -> b}]
1085+
= -Graph-
1086+
1087+
>> BetweennessCentrality[g]
10861088
= {0., 1., 0., 0., 0.}
10871089
10881090
>> g = Graph[{a -> b, b -> c, c -> d, d -> e, e -> c, e -> a}]; BetweennessCentrality[g]
@@ -1104,11 +1106,17 @@ def eval(self, graph, expression, evaluation, options):
11041106

11051107
class ClosenessCentrality(_Centrality):
11061108
"""
1107-
>> g = Graph[{a -> b, b -> c, d -> c, d -> a, e -> c, d -> b}]; ClosenessCentrality[g]
1108-
= {0.666667, 1., 0., 1., 1.}
1109+
>> g = Graph[{a -> b, b -> c, d -> c, d -> a, e -> c, d -> b}]
1110+
= -Graph-
1111+
1112+
>> ClosenessCentrality[g]
1113+
= {0.666667, 1., 0., 1., 1.}
11091114
1110-
>> g = Graph[{a -> b, b -> c, c -> d, d -> e, e -> c, e -> a}]; ClosenessCentrality[g]
1111-
= {0.4, 0.4, 0.4, 0.5, 0.666667}
1115+
>> g = Graph[{a -> b, b -> c, c -> d, d -> e, e -> c, e -> a}]
1116+
= -Graph-
1117+
1118+
>> ClosenessCentrality[g]
1119+
= {0.4, 0.4, 0.4, 0.5, 0.666667}
11121120
"""
11131121

11141122
def eval(self, graph, expression, evaluation, options):
@@ -1127,7 +1135,9 @@ def eval(self, graph, expression, evaluation, options):
11271135

11281136
class DegreeCentrality(_Centrality):
11291137
"""
1130-
>> g = Graph[{a -> b, b <-> c, d -> c, d -> a, e <-> c, d -> b}];
1138+
>> g = Graph[{a -> b, b <-> c, d -> c, d -> a, e <-> c, d -> b}]
1139+
= -Graph-
1140+
11311141
>> DegreeCentrality[g]
11321142
= ...
11331143

pymathics/graph/components.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,25 @@ class ConnectedComponents(_NetworkXBuiltin):
1717
"""
1818
<dl>
1919
<dt>'ConnectedComponents'[$g$]
20-
<dd> gives the connected components of the graph $g$
20+
<dd> gives the connected components of the graph $g$.
2121
</dl>
2222
23-
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
23+
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}, VertexLabels->True]
24+
= -Graph-
25+
26+
>> ConnectedComponents[g]
2427
= ...
2528
26-
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
29+
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexLabels->True]
30+
= -Graph-
31+
32+
>> ConnectedComponents[g]
2733
= ...
2834
29-
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
35+
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}, VertexLabels->True]
36+
= -Graph-
37+
38+
>> ConnectedComponents[g]
3039
= ...
3140
"""
3241

@@ -69,16 +78,25 @@ class WeaklyConnectedComponents(_NetworkXBuiltin):
6978
"""
7079
<dl>
7180
<dt>'WeaklyConnectedComponents'[$g$]
72-
<dd> gives the weakly connected components of the graph $g$
81+
<dd> gives the weakly connected components of the graph $g$.
7382
</dl>
7483
75-
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; WeaklyConnectedComponents[g]
84+
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}, VertexLabels->True]
85+
= -Graph-
86+
87+
>> WeaklyConnectedComponents[g]
7688
= {{1, 2, 3, 4}}
7789
78-
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; WeaklyConnectedComponents[g]
90+
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexLabels->True]
91+
= -Graph-
92+
93+
>> WeaklyConnectedComponents[g]
7994
= {{1, 2, 3}}
8095
81-
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5, 6 <-> 7, 7 <-> 8}]; WeaklyConnectedComponents[g]
96+
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5, 6 <-> 7, 7 <-> 8}, VertexLabels->True]
97+
= -Graph-
98+
99+
>> WeaklyConnectedComponents[g]
82100
= {{1, 2, 3, 4, 5}, {6, 7, 8}}
83101
"""
84102

pymathics/graph/curated.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class GraphData(_NetworkXBuiltin):
1616
</dl>
1717
1818
>> GraphData["PappusGraph"]
19+
= -Graph-
1920
"""
2021

2122
def eval(self, name, expression, evaluation: Evaluation, options: dict) -> Graph:

pymathics/graph/measures_and_metrics.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,34 @@ class GraphDistance(_NetworkXBuiltin):
9797
<dd>use rules $v$->$w$ to specify the graph $g$.
9898
</dl>
9999
100-
>> GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 2 <-> 4, 4 -> 5}, 1, 5]
100+
>> g = Graph[{1 -> 2, 2 <-> 3, 4 -> 3, 2 <-> 4, 4 -> 5}, VertexLabels->True]
101+
= -Graph-
102+
103+
>> GraphDistance[g, 1, 5]
101104
= 3
102105
103-
>> GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 -> 2, 4 -> 5}, 1, 5]
104-
= 4
106+
>> GraphDistance[g, 4, 2]
107+
= 1
105108
106-
>> GraphDistance[{1 <-> 2, 2 <-> 3, 4 -> 3, 4 -> 2, 4 -> 5}, 1, 5]
109+
>> GraphDistance[g, 5, 4]
107110
= Infinity
108111
109-
>> GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 2 <-> 4, 4 -> 5}, 3]
110-
= ...
112+
>> GraphDistance[g, 5]
113+
= [Infinity, Infinity, Infinity, 0]
114+
115+
>> GraphDistance[g, 3]
116+
= {Infinity, 1, 2, 0, 3}
111117
112-
>> GraphDistance[{1 <-> 2, 3 <-> 4}, 3]
113-
= {Infinity, Infinity, 0, 1}
118+
>> GraphDistance[g, 4]
119+
= {1, 2, 1, 0, 2}
114120
115121
#> GraphDistance[{1 -> 2}, 3, 4]
116122
: The vertex at position 2 in GraphDistance[{1 -> 2}, 3, 4] does not belong to the graph at position 1.
117123
= GraphDistance[{1 -> 2}, 3, 4]
118124
"""
119125

126+
summary_text = "get path distance"
127+
120128
def eval_s(
121129
self, graph, s, expression, evaluation: Evaluation, options: dict
122130
) -> Optional[ListExpression]:

pymathics/graph/parametric.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class BalancedTree(_NetworkXBuiltin):
3434
<dt>'BalancedTree[$r$, $h$]'
3535
<dd>Returns the perfectly balanced $r$-ary tree of height $h$.
3636
37-
In this tree produced, all non-leaf nodes will have $r$ children and the height of
38-
the path from root $r$ to any leaf will be $h$.
37+
In this tree produced, all non-leaf nodes will have $r$ children and \
38+
the height of the path from root $r$ to any leaf will be $h$.
3939
</dl>
4040
4141
>> BalancedTree[2, 3]
@@ -82,8 +82,8 @@ class BarbellGraph(_NetworkXBuiltin):
8282
<dd>Barbell Graph: two complete graphs connected by a path.
8383
</dl>
8484
85-
## >> BarbellGraph[4, 1]
86-
## = -Graph-
85+
>> BarbellGraph[4, 1]
86+
= -Graph-
8787
8888
"""
8989

@@ -132,11 +132,14 @@ class BinomialTree(_NetworkXBuiltin):
132132
133133
The binomial tree of order $n$ with root $R$ is defined as:
134134
135-
If $k$=0, $B[k]$ = $B[0]$ = {$R$}. i.e., the binomial tree of order zero consists of a single node, $R$.
135+
If $k$=0, $B[k]$ = $B[0]$ = {$R$}. i.e., the binomial tree of order \
136+
zero consists of a single node, $R$.
136137
137-
If $k>0$, B[k] = {$R$, $B[0$], $B[1]$ .. $B[k]$, i.e., the binomial tree of order $k$>0 comprises the root $R$, and $k$ binomial subtrees, $B[0] to $B[k].
138+
If $k>0$, B[k] = {$R$, $B[0$], $B[1]$ .. $B[k]$, i.e., the binomial tree \
139+
of order $k$>0 comprises the root $R$, and $k$ binomial subtrees, \
140+
$B[0] to $B[k].
138141
139-
Binomial trees the underlying datastructre in Binomial Heaps.
142+
Binomial trees are the underlying datastructre in Binomial Heaps.
140143
</dl>
141144
142145
>> BinomialTree[3]
@@ -171,15 +174,11 @@ class CompleteGraph(_NetworkXBuiltin):
171174
"""
172175
<dl>
173176
<dt>'CompleteGraph[$n$]'
174-
<dd>Returns the complete graph with $n$ vertices, $K_n$
177+
<dd>Returns the complete graph with $n$ vertices, $K_n$.
175178
</dl>
176179
177180
>> CompleteGraph[8]
178181
= -Graph-
179-
180-
#> CompleteGraph[0]
181-
: Expected a positive integer at position 1 in CompleteGraph[0].
182-
= CompleteGraph[0]
183182
"""
184183

185184
messages = {
@@ -192,9 +191,9 @@ def eval(self, n: Integer, expression, evaluation: Evaluation, options: dict):
192191

193192
def eval_multipartite(self, n, evaluation: Evaluation, options: dict):
194193
"%(name)s[n_List, OptionsPattern[%(name)s]]"
195-
if all(isinstance(i, Integer) for i in n.leaves):
194+
if all(isinstance(i, Integer) for i in n.elements):
196195
return Graph(
197-
nx.complete_multipartite_graph(*[i.get_int_value() for i in n.leaves])
196+
nx.complete_multipartite_graph(*[i.get_int_value() for i in n.elements])
198197
)
199198

200199

@@ -204,7 +203,7 @@ class CompleteKaryTree(_NetworkXBuiltin):
204203
<dd>Creates a complete $k$-ary tree of $n$ levels.
205204
</dl>
206205
207-
In the returned tree, with $n$ nodes, the from root $R$ to any
206+
In the returned tree, with $n$ nodes, the from root $R$ to any \
208207
leaf be $k$.
209208
210209
>> CompleteKaryTree[2, 3]
@@ -246,7 +245,7 @@ class CycleGraph(_NetworkXBuiltin):
246245
<dd>Returns the cycle graph with $n$ vertices $C_n$.
247246
</dl>
248247
249-
>> CycleGraph[3, PlotLabel -> "C_i"]
248+
>> CycleGraph[5, PlotLabel -> "C_i"]
250249
= -Graph-
251250
"""
252251

@@ -290,8 +289,8 @@ def eval(self, r, n, expression, evaluation: Evaluation, options: dict):
290289
class GraphAtlas(_NetworkXBuiltin):
291290
"""<dl>
292291
<dt>'GraphAtlas[$n$]'
293-
<dd>Returns graph number $i$ from the Networkx's Graph
294-
Atlas. There are about 1200 of them and get large as $i$
292+
<dd>Returns graph number $i$ from the Networkx's Graph \
293+
Atlas. There are about 1200 of them and get large as $i$ \
295294
increases.
296295
</dl>
297296
@@ -331,7 +330,8 @@ class HknHararyGraph(_NetworkXBuiltin):
331330
number of edges in the graph with given node connectivity and \
332331
number of nodes.
333332
334-
Harary, F. The Maximum Connectivity of a Graph. Proc. Nat. Acad. Sci. USA 48, 1142-1146, 1962.
333+
Harary, F. The Maximum Connectivity of a Graph. \
334+
Proc. Nat. Acad. Sci. USA 48, 1142-1146, 1962.
335335
</dl>
336336
337337
>> HknHararyGraph[3, 10]
@@ -359,7 +359,8 @@ class HmnHararyGraph(_NetworkXBuiltin):
359359
connectivity with given number of nodes and given number of \
360360
edges.
361361
362-
Harary, F. The Maximum Connectivity of a Graph. Proc. Nat. Acad. Sci. USA 48, 1142-1146, 1962.
362+
Harary, F. The Maximum Connectivity of a Graph.\
363+
Proc. Nat. Acad. Sci. USA 48, 1142-1146, 1962.
363364
</dl>
364365
365366
>> HmnHararyGraph[5, 10]
@@ -475,7 +476,8 @@ class PathGraph(_NetworkXBuiltin):
475476
"""
476477
<dl>
477478
<dt>'PathGraph[{$v_1$, $v_2$, ...}]'
478-
<dd>Returns a Graph with a path with vertices $v_i$ and edges between $v-i$ and $v_i+1$ .
479+
<dd>Returns a Graph with a path with vertices $v_i$ and \
480+
edges between $v-i$ and $v_i+1$ .
479481
</dl>
480482
>> PathGraph[{1, 2, 3}]
481483
= -Graph-
@@ -534,7 +536,7 @@ class StarGraph(_NetworkXBuiltin):
534536
"""
535537
<dl>
536538
<dt>'StarGraph[$n$]'
537-
<dd>Returns a star graph with $n$ vertices
539+
<dd>Returns a star graph with $n$ vertices.
538540
</dl>
539541
540542
>> StarGraph[8]

0 commit comments

Comments
 (0)