Skip to content

Commit 9563ccc

Browse files
authored
First bunch of bug corrections (#7)
* first bunch of silly bugs corrections * adding specialized doctest to Makefile I am going to merge this in order to start with other more large changes.
1 parent ba69592 commit 9563ccc

8 files changed

Lines changed: 150 additions & 94 deletions

File tree

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ PYTHON ?= python
99
PIP ?= $(PYTHON) -m pip
1010
RM ?= rm
1111

12+
13+
14+
1215
.PHONY: all build \
1316
check clean \
1417
develop dist doc doc-data \
@@ -40,7 +43,7 @@ install: pypi-setup
4043
$(PYTHON) setup.py install
4144

4245
# Run tests
43-
test check: pytest
46+
test check: pytest doctest
4447

4548
#: Remove derived files
4649
clean: clean-pyc
@@ -60,7 +63,7 @@ pytest:
6063

6164
#: Run tests that appear in docstring in the code.
6265
doctest:
63-
$(PYTHON) mathics/test.py $o
66+
MATHICS_CHARACTER_ENCODING="ASCII" $(PYTHON) -m mathics.docpipeline -l pymathics.graph -c 'Graphs - Vertices and Edges' $o
6467

6568
# #: Make Mathics PDF manual
6669
# doc mathics.pdf: mathics/doc/tex/data

pymathics/graph/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
FindShortestPath,
3030
FindVertexCut,
3131
Graph,
32+
GraphAtom,
3233
GraphBox,
3334
HITSCentrality,
3435
HighlightGraph,
@@ -37,7 +38,6 @@
3738
MixedGraphQ,
3839
MultigraphQ,
3940
PageRankCentrality,
40-
PlanarGraphQ,
4141
PathGraphQ,
4242
Property,
4343
PropertyValue,

pymathics/graph/algorithms.py

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from typing import Optional
77

8+
from mathics.core.atoms import Integer1, Integer2, Integer3
89
from mathics.core.convert.expression import to_mathics_list
910
from mathics.core.convert.python import from_python
1011
from mathics.core.evaluation import Evaluation
@@ -25,14 +26,19 @@
2526

2627
class ConnectedComponents(_NetworkXBuiltin):
2728
"""
28-
## >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
29-
## = {{3, 4}, {2}, {1}}
29+
<dl>
30+
<dt>'ConnectedComponents'[$g$]
31+
<dd> gives the connected components of the graph $g$
32+
</dl>
3033
31-
## >> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
32-
## = {{1, 2, 3}}
34+
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
35+
= {{3, 4}, {2}, {1}}
36+
37+
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
38+
= {{1, 2, 3}}
3339
34-
## >> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
35-
## = {{4, 5}, {1, 2, 3}}
40+
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
41+
= {{4, 5}, {1, 2, 3}}
3642
"""
3743

3844
def eval(
@@ -72,7 +78,7 @@ def eval(
7278
class GraphDistance(_NetworkXBuiltin):
7379
"""
7480
<dl>
75-
<dt>'GraphDistance[$g$, $s$, $t$]'
81+
<dt>'GraphDistance'[$g$, $s$, $t$]
7682
<dd>returns the distance from source vertex $s$ to target vertex $t$ in the graph $g$.
7783
</dl>
7884
@@ -117,7 +123,7 @@ def eval_s(
117123
if graph:
118124
weight = graph.update_weights(evaluation)
119125
d = nx.shortest_path_length(graph.G, source=s, weight=weight)
120-
inf = Expression(SymbolDirectedInfinity, 1)
126+
inf = Expression(SymbolDirectedInfinity, Integer1)
121127
return to_mathics_list(*[d.get(v, inf) for v in graph.vertices])
122128

123129
def eval_s_t(self, graph, s, t, expression, evaluation: Evaluation, options: dict):
@@ -127,27 +133,28 @@ def eval_s_t(self, graph, s, t, expression, evaluation: Evaluation, options: dic
127133
return
128134
G = graph.G
129135
if not G.has_node(s):
130-
self._not_a_vertex(expression, 2, evaluation)
136+
self._not_a_vertex(expression, Integer2, evaluation)
131137
elif not G.has_node(t):
132-
self._not_a_vertex(expression, 3, evaluation)
138+
self._not_a_vertex(expression, Integer3, evaluation)
133139
else:
134140
try:
135141
weight = graph.update_weights(evaluation)
136142
return from_python(
137143
nx.shortest_path_length(graph.G, source=s, target=t, weight=weight)
138144
)
139145
except nx.exception.NetworkXNoPath:
140-
return Expression(SymbolDirectedInfinity, 1)
146+
return Expression(SymbolDirectedInfinity, Integer1)
141147

142148

143149
class FindSpanningTree(_NetworkXBuiltin):
144150
"""
145151
<dl>
146-
<dt>'FindSpanningTree[$g$]'
152+
<dt>'FindSpanningTree'[$g$]
147153
<dd>finds a spanning tree of the graph $g$.
148154
</dl>
149155
150156
>> FindSpanningTree[CycleGraph[4]]
157+
= -Graph-
151158
"""
152159

153160
options = DEFAULT_GRAPH_OPTIONS
@@ -160,7 +167,7 @@ def eval(self, graph, expression, evaluation: Evaluation, options: dict):
160167
SymbolDirectedEdge if graph.G.is_directed() else SymbolUndirectedEdge
161168
# FIXME: put in edge to Graph conversion function?
162169
edges = [
163-
Expression("UndirectedEdge", u, v)
170+
Expression(SymbolUndirectedEdge, from_python(u), from_python(v))
164171
for u, v in nx.minimum_spanning_edges(graph.G, data=False)
165172
]
166173
g = _create_graph(edges, [None] * len(edges), options)
@@ -174,30 +181,52 @@ def eval(self, graph, expression, evaluation: Evaluation, options: dict):
174181

175182
class PlanarGraphQ(_NetworkXBuiltin):
176183
"""
184+
See <url>https://en.wikipedia.org/wiki/Planar_graph</url>
185+
177186
<dl>
178-
<dd>PlanarGraphQ[g]
179-
<dd>Returns True if g is a planar graph and False otherwise.
187+
<dd>'PlanarGraphQ'[$g$]
188+
<dd>Returns True if $g$ is a planar graph and False otherwise.
180189
</dl>
181190
182191
>> PlanarGraphQ[CycleGraph[4]]
183192
= True
193+
184194
>> PlanarGraphQ[CompleteGraph[5]]
185195
= False
196+
197+
>> PlanarGraphQ[CompleteGraph[4]]
198+
= True
199+
200+
>> PlanarGraphQ[CompleteGraph[5]]
201+
= False
202+
203+
#> PlanarGraphQ[Graph[{}]]
204+
= False
205+
206+
207+
>> PlanarGraphQ["abc"]
208+
: Expected a graph at position 1 in PlanarGraphQ[abc].
209+
= False
186210
"""
187211

188212
options = DEFAULT_GRAPH_OPTIONS
189213

190214
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
191-
"PlanarGraphQ[graph_, OptionsPattern[PlanarGraphQ]]"
215+
"Pymathics`PlanarGraphQ[graph_, OptionsPattern[PlanarGraphQ]]"
192216
graph = self._build_graph(graph, evaluation, options, expression)
193-
if not graph:
217+
if not graph or graph.empty():
194218
return SymbolFalse
195219
is_planar, _ = nx.check_planarity(graph.G)
196220
return from_python(is_planar)
197221

198222

199223
class WeaklyConnectedComponents(_NetworkXBuiltin):
200224
"""
225+
<dl>
226+
<dt>'WeaklyConnectedComponents'[$g$]
227+
<dd> gives the weakly connected components of the graph $g$
228+
</dl>
229+
201230
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; WeaklyConnectedComponents[g]
202231
= {{1, 2, 3, 4}}
203232

0 commit comments

Comments
 (0)