Skip to content

Commit 2f42381

Browse files
committed
Add GraphQ; lint some
1 parent 2cc4a4b commit 2f42381

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

.github/workflows/ubuntu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
2727
python -m pip install -e git+https://github.com/Mathics3/mathics-core#egg=Mathics3[full]
2828
python -m pip install -e .
29-
(cd src/mathics3 && bash ./admin-tools/make-op-tables.sh)
29+
(cd src/mathics3 && bash ./admin-tools/make-JSON-tables.sh)
3030
- name: install pymathics graph
3131
run: |
3232
make develop

.pre-commit-config.yaml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@ default_language_version:
22
python: python
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v4.0.1
5+
rev: v4.5.0
66
hooks:
77
- id: check-merge-conflict
88
- id: debug-statements
9-
stages: [commit]
9+
stages: [pre-commit]
1010
- id: end-of-file-fixer
11-
stages: [commit]
12-
# - repo: https://github.com/pycqa/isort
13-
# rev: 5.10.1
14-
# hooks:
15-
# - id: isort
16-
# stages: [commit]
11+
stages: [pre-commit]
12+
- repo: https://github.com/pycqa/isort
13+
rev: 5.13.2
14+
hooks:
15+
- id: isort
16+
stages: [pre-commit]
1717
- repo: https://github.com/psf/black
18-
rev: 22.3.0
18+
rev: 24.10.0
1919
hooks:
2020
- id: black
2121
language_version: python3
2222
exclude: 'pymathics/graph/version.py'
23-
stages: [commit]
24-
- repo: https://github.com/pycqa/flake8
25-
rev: 3.9.2
26-
hooks:
27-
- id: flake8
28-
stages: [commit]
23+
stages: [pre-commit]

pymathics/graph/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
VertexIndex,
6161
VertexList,
6262
)
63-
6463
from pymathics.graph.centralities import (
6564
BetweennessCentrality,
6665
ClosenessCentrality,
@@ -70,7 +69,6 @@
7069
KatzCentrality,
7170
PageRankCentrality,
7271
)
73-
7472
from pymathics.graph.components import ConnectedComponents, WeaklyConnectedComponents
7573
from pymathics.graph.curated import GraphData
7674
from pymathics.graph.measures_and_metrics import (
@@ -99,6 +97,7 @@
9997
AcyclicGraphQ,
10098
ConnectedGraphQ,
10199
DirectedGraphQ,
100+
GraphQ,
102101
LoopFreeGraphQ,
103102
MixedGraphQ,
104103
MultigraphQ,
@@ -151,6 +150,7 @@
151150
"GraphBox",
152151
"GraphData",
153152
"GraphDistance",
153+
"GraphQ",
154154
"HITSCentrality",
155155
"HighlightGraph",
156156
"HknHararyGraph",

pymathics/graph/properties.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class AcyclicGraphQ(_NetworkXBuiltin):
5252

5353
summary_text = "test if is a graph is acyclic"
5454

55-
def eval(self, graph, expression, evaluation, options):
55+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
5656
"AcyclicGraphQ[graph_, OptionsPattern[AcyclicGraphQ]]"
5757
graph = self._build_graph(graph, evaluation, options, expression, quiet=False)
5858
if not graph or graph.empty():
@@ -73,7 +73,7 @@ class ConnectedGraphQ(_NetworkXBuiltin):
7373
#Connected_vertices_and_graphs
7474
</url> test (<url>
7575
:NetworkX:
76-
https://networkx.org/documentation/networkx-2.8.8/reference/algorithms\
76+
https://networkx.org/documentation/stable/reference/algorithms\
7777
/generated/networkx.algorithms.components.is_connected.html
7878
</url>, <url>
7979
:WMA:
@@ -111,7 +111,7 @@ class ConnectedGraphQ(_NetworkXBuiltin):
111111

112112
summary_text = "test if a graph is a connected"
113113

114-
def eval(self, graph, expression, evaluation, options):
114+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
115115
"%(name)s[graph_, OptionsPattern[%(name)s]]"
116116
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
117117
if graph:
@@ -127,7 +127,7 @@ class DirectedGraphQ(_NetworkXBuiltin):
127127
https://en.wikipedia.org/wiki/Directed_graph
128128
</url> test (<url>
129129
:NetworkX:
130-
https://networkx.org/documentation/networkx-2.8.8/reference\
130+
https://networkx.org/documentation/stable/reference\
131131
/generated/networkx.classes.function.is_directed.html
132132
</url>, <url>
133133
:WMA:
@@ -153,7 +153,7 @@ class DirectedGraphQ(_NetworkXBuiltin):
153153

154154
summary_text = "test if a graph is directed"
155155

156-
def eval(self, graph, expression, evaluation, options):
156+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
157157
"DirectedGraphQ[graph_, OptionsPattern[DirectedGraphQ]]"
158158
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
159159
if graph:
@@ -162,14 +162,39 @@ def eval(self, graph, expression, evaluation, options):
162162
return SymbolFalse
163163

164164

165+
class GraphQ(_NetworkXBuiltin):
166+
"""
167+
<url>:WMA link:
168+
https://reference.wolfram.com/language/ref/GraphQ.html</url>
169+
<dl>
170+
<dt>'GraphQ'[$graph$]
171+
<dd>True if $graph$ is a 'Graph'.
172+
</dl>
173+
174+
A graph with one one node and one self-looping edge:
175+
>> GraphQ[{1 -> 2, 2 -> 3, 3 -> 1}]
176+
= True
177+
178+
>> GraphQ[{1, 2, 3}]
179+
= False
180+
"""
181+
182+
summary_text = "test object is a graph"
183+
184+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
185+
"GraphQ[graph_, OptionsPattern[GraphQ]]"
186+
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
187+
return SymbolTrue if graph else SymbolFalse
188+
189+
165190
class LoopFreeGraphQ(_NetworkXBuiltin):
166191
"""
167192
<url>
168193
:Loop-Free graph:
169194
https://en.wikipedia.org/wiki/Loop_(graph_theory)
170195
</url> test (<url>
171196
:NetworkX:
172-
https://networkx.org/documentation/networkx-2.8.8/reference/\
197+
https://networkx.org/documentation/stable/reference/\
173198
generated/networkx.classes.function.nodes_with_selfloops.html
174199
</url>, <url>
175200
:WMA:
@@ -195,7 +220,7 @@ class LoopFreeGraphQ(_NetworkXBuiltin):
195220

196221
summary_text = "test if a graph is loop free"
197222

198-
def eval(self, graph, expression, evaluation, options):
223+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
199224
"LoopFreeGraphQ[graph_, OptionsPattern[LoopFreeGraphQ]]"
200225
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
201226
if not graph or graph.empty():
@@ -240,7 +265,7 @@ class MixedGraphQ(_NetworkXBuiltin):
240265

241266
summary_text = "test if a graph has directed and undirected edges"
242267

243-
def eval(self, graph, expression, evaluation, options):
268+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
244269
"MixedGraphQ[graph_, OptionsPattern[MixedGraphQ]]"
245270
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
246271
if graph:
@@ -254,7 +279,7 @@ class MultigraphQ(_NetworkXBuiltin):
254279
:Multigraph:
255280
https://en.wikipedia.org/wiki/Multigraph</url> test (<url>
256281
:NetworkX:
257-
https://networkx.org/documentation/networkx-2.8.8/reference/classes/multigraph.html</url>, \
282+
https://networkx.org/documentation/stable/reference/classes/multigraph.html</url>, \
258283
<url>
259284
:WMA:
260285
https://reference.wolfram.com/language/ref/MulitGraphQ.html</url>)
@@ -281,7 +306,7 @@ class MultigraphQ(_NetworkXBuiltin):
281306

282307
summary_text = "test if a graph is a multi graph"
283308

284-
def eval(self, graph, expression, evaluation, options):
309+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
285310
"MultigraphQ[graph_, OptionsPattern[MultigraphQ]]"
286311
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
287312
if graph:
@@ -339,7 +364,7 @@ class PathGraphQ(_NetworkXBuiltin):
339364

340365
summary_text = "test if a graph is a path-like graph"
341366

342-
def eval(self, graph, expression, evaluation, options):
367+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
343368
"PathGraphQ[graph_, OptionsPattern[PathGraphQ]]"
344369
if not isinstance(graph, Graph) or graph.empty():
345370
return SymbolFalse
@@ -365,7 +390,7 @@ class PlanarGraphQ(_NetworkXBuiltin):
365390
:Planar Graph:
366391
https://en.wikipedia.org/wiki/Planar_graph</url> test (<url>
367392
:NetworkX:
368-
https://networkx.org/documentation/networkx-2.8.8/reference/algorithms/
393+
https://networkx.org/documentation/stable/reference/algorithms/
369394
generated/networkx.algorithms.planarity.check_planarity.html</url>, <url>
370395
:WMA:
371396
https://reference.wolfram.com/language/ref/PlanaGraphQ.html</url>)
@@ -436,7 +461,7 @@ class SimpleGraphQ(_NetworkXBuiltin):
436461

437462
summary_text = "test if a graph is simple (not multigraph)"
438463

439-
def eval(self, graph, expression, evaluation, options):
464+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
440465
"SimpleGraphQ[graph_, OptionsPattern[LoopFreeGraphQ]]"
441466
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
442467
if graph:

pymathics/graph/random.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Random Graphs
33
"""
44

5+
from typing import Any, Generator
6+
57
import networkx as nx
68
from mathics.builtin.numbers.randomnumbers import RandomEnv
79
from mathics.core.atoms import Integer, Integer1
@@ -36,7 +38,7 @@ class RandomGraph(_NetworkXBuiltin):
3638

3739
def _generate(
3840
self, n: Integer, m: Integer, k: Integer, evaluation: Evaluation, options: dict
39-
) -> Graph:
41+
) -> Generator[Any, Any, Any]:
4042
py_n = n.value
4143
py_m = m.value
4244
py_k = k.value
@@ -64,6 +66,6 @@ def eval_nmk(
6466
expression,
6567
evaluation: Evaluation,
6668
options: dict,
67-
) -> Graph:
69+
) -> ListExpression:
6870
"RandomGraph[{n_Integer, m_Integer}, k_Integer, OptionsPattern[RandomGraph]]"
6971
return ListExpression(*self._generate(n, m, k, evaluation, options))

0 commit comments

Comments
 (0)