Skip to content

Commit

Permalink
Add GraphQ; lint some
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Jan 13, 2025
1 parent 2cc4a4b commit 2f42381
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
python -m pip install -e git+https://github.com/Mathics3/mathics-core#egg=Mathics3[full]
python -m pip install -e .
(cd src/mathics3 && bash ./admin-tools/make-op-tables.sh)
(cd src/mathics3 && bash ./admin-tools/make-JSON-tables.sh)
- name: install pymathics graph
run: |
make develop
Expand Down
25 changes: 10 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,22 @@ default_language_version:
python: python
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.5.0
hooks:
- id: check-merge-conflict
- id: debug-statements
stages: [commit]
stages: [pre-commit]
- id: end-of-file-fixer
stages: [commit]
# - repo: https://github.com/pycqa/isort
# rev: 5.10.1
# hooks:
# - id: isort
# stages: [commit]
stages: [pre-commit]
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
stages: [pre-commit]
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 24.10.0
hooks:
- id: black
language_version: python3
exclude: 'pymathics/graph/version.py'
stages: [commit]
- repo: https://github.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
stages: [commit]
stages: [pre-commit]
4 changes: 2 additions & 2 deletions pymathics/graph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
VertexIndex,
VertexList,
)

from pymathics.graph.centralities import (
BetweennessCentrality,
ClosenessCentrality,
Expand All @@ -70,7 +69,6 @@
KatzCentrality,
PageRankCentrality,
)

from pymathics.graph.components import ConnectedComponents, WeaklyConnectedComponents
from pymathics.graph.curated import GraphData
from pymathics.graph.measures_and_metrics import (
Expand Down Expand Up @@ -99,6 +97,7 @@
AcyclicGraphQ,
ConnectedGraphQ,
DirectedGraphQ,
GraphQ,
LoopFreeGraphQ,
MixedGraphQ,
MultigraphQ,
Expand Down Expand Up @@ -151,6 +150,7 @@
"GraphBox",
"GraphData",
"GraphDistance",
"GraphQ",
"HITSCentrality",
"HighlightGraph",
"HknHararyGraph",
Expand Down
51 changes: 38 additions & 13 deletions pymathics/graph/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AcyclicGraphQ(_NetworkXBuiltin):

summary_text = "test if is a graph is acyclic"

def eval(self, graph, expression, evaluation, options):
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
"AcyclicGraphQ[graph_, OptionsPattern[AcyclicGraphQ]]"
graph = self._build_graph(graph, evaluation, options, expression, quiet=False)
if not graph or graph.empty():
Expand All @@ -73,7 +73,7 @@ class ConnectedGraphQ(_NetworkXBuiltin):
#Connected_vertices_and_graphs
</url> test (<url>
:NetworkX:
https://networkx.org/documentation/networkx-2.8.8/reference/algorithms\
https://networkx.org/documentation/stable/reference/algorithms\
/generated/networkx.algorithms.components.is_connected.html
</url>, <url>
:WMA:
Expand Down Expand Up @@ -111,7 +111,7 @@ class ConnectedGraphQ(_NetworkXBuiltin):

summary_text = "test if a graph is a connected"

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

summary_text = "test if a graph is directed"

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


class GraphQ(_NetworkXBuiltin):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/GraphQ.html</url>
<dl>
<dt>'GraphQ'[$graph$]
<dd>True if $graph$ is a 'Graph'.
</dl>
A graph with one one node and one self-looping edge:
>> GraphQ[{1 -> 2, 2 -> 3, 3 -> 1}]
= True
>> GraphQ[{1, 2, 3}]
= False
"""

summary_text = "test object is a graph"

def eval(self, graph, expression, evaluation: Evaluation, options: dict):
"GraphQ[graph_, OptionsPattern[GraphQ]]"
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
return SymbolTrue if graph else SymbolFalse


class LoopFreeGraphQ(_NetworkXBuiltin):
"""
<url>
:Loop-Free graph:
https://en.wikipedia.org/wiki/Loop_(graph_theory)
</url> test (<url>
:NetworkX:
https://networkx.org/documentation/networkx-2.8.8/reference/\
https://networkx.org/documentation/stable/reference/\
generated/networkx.classes.function.nodes_with_selfloops.html
</url>, <url>
:WMA:
Expand All @@ -195,7 +220,7 @@ class LoopFreeGraphQ(_NetworkXBuiltin):

summary_text = "test if a graph is loop free"

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

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

def eval(self, graph, expression, evaluation, options):
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
"MixedGraphQ[graph_, OptionsPattern[MixedGraphQ]]"
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
if graph:
Expand All @@ -254,7 +279,7 @@ class MultigraphQ(_NetworkXBuiltin):
:Multigraph:
https://en.wikipedia.org/wiki/Multigraph</url> test (<url>
:NetworkX:
https://networkx.org/documentation/networkx-2.8.8/reference/classes/multigraph.html</url>, \
https://networkx.org/documentation/stable/reference/classes/multigraph.html</url>, \
<url>
:WMA:
https://reference.wolfram.com/language/ref/MulitGraphQ.html</url>)
Expand All @@ -281,7 +306,7 @@ class MultigraphQ(_NetworkXBuiltin):

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

def eval(self, graph, expression, evaluation, options):
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
"MultigraphQ[graph_, OptionsPattern[MultigraphQ]]"
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
if graph:
Expand Down Expand Up @@ -339,7 +364,7 @@ class PathGraphQ(_NetworkXBuiltin):

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

def eval(self, graph, expression, evaluation, options):
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
"PathGraphQ[graph_, OptionsPattern[PathGraphQ]]"
if not isinstance(graph, Graph) or graph.empty():
return SymbolFalse
Expand All @@ -365,7 +390,7 @@ class PlanarGraphQ(_NetworkXBuiltin):
:Planar Graph:
https://en.wikipedia.org/wiki/Planar_graph</url> test (<url>
:NetworkX:
https://networkx.org/documentation/networkx-2.8.8/reference/algorithms/
https://networkx.org/documentation/stable/reference/algorithms/
generated/networkx.algorithms.planarity.check_planarity.html</url>, <url>
:WMA:
https://reference.wolfram.com/language/ref/PlanaGraphQ.html</url>)
Expand Down Expand Up @@ -436,7 +461,7 @@ class SimpleGraphQ(_NetworkXBuiltin):

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

def eval(self, graph, expression, evaluation, options):
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
"SimpleGraphQ[graph_, OptionsPattern[LoopFreeGraphQ]]"
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
if graph:
Expand Down
6 changes: 4 additions & 2 deletions pymathics/graph/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Random Graphs
"""

from typing import Any, Generator

import networkx as nx
from mathics.builtin.numbers.randomnumbers import RandomEnv
from mathics.core.atoms import Integer, Integer1
Expand Down Expand Up @@ -36,7 +38,7 @@ class RandomGraph(_NetworkXBuiltin):

def _generate(
self, n: Integer, m: Integer, k: Integer, evaluation: Evaluation, options: dict
) -> Graph:
) -> Generator[Any, Any, Any]:
py_n = n.value
py_m = m.value
py_k = k.value
Expand Down Expand Up @@ -64,6 +66,6 @@ def eval_nmk(
expression,
evaluation: Evaluation,
options: dict,
) -> Graph:
) -> ListExpression:
"RandomGraph[{n_Integer, m_Integer}, k_Integer, OptionsPattern[RandomGraph]]"
return ListExpression(*self._generate(n, m, k, evaluation, options))

0 comments on commit 2f42381

Please sign in to comment.