Skip to content

Commit cb9f847

Browse files
authored
Merge pull request #14 from Mathics3/cleaning_tests_and_adding_workflows
Cleaning tests and adding workflows
2 parents adfb2e6 + 3771e61 commit cb9f847

File tree

7 files changed

+392
-28
lines changed

7 files changed

+392
-28
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Pymathics-Graph (Consistency Checks)
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: ['3.10']
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Set up Python ${{ matrix.python-version }}
18+
uses: actions/setup-python@v2
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
- name: Install dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
python -m pip install pytest
25+
python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
26+
git clone https://github.com/Mathics3/mathics-core
27+
(cd mathics-core && make)
28+
(cd mathics-core && python -m pip install -e .[full])
29+
- name: Install Pymathics.graph with minimum dependencies
30+
run: |
31+
make develop
32+
- name: Test Mathics Consistency and Style
33+
run: |
34+
make check-consistency-and-style

.github/workflows/ubuntu.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Pymathics.graph (ubuntu)
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: '**'
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-20.04
12+
strategy:
13+
matrix:
14+
python-version: ['3.9', '3.10']
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Set up Python ${{ matrix.python-version }}
18+
uses: actions/setup-python@v2
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
- name: Install dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
python -m pip install pytest
25+
# Can comment out when next Mathics core and Mathics-scanner are released
26+
python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
27+
python -m pip install -e git+https://github.com/Mathics3/mathics-core#egg=Mathics3[full]
28+
(cd src/mathics3 && bash ./admin-tools/make-op-tables.sh)
29+
# python -m pip install Mathics3[full]
30+
python -m pip install -e .
31+
- name: install pymathics graph
32+
run: |
33+
make develop
34+
- name: Test Mathics
35+
run: |
36+
make -j3 check

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,8 @@ rmChangeLog:
7474
#: Create a ChangeLog from git via git log and git2cl
7575
ChangeLog: rmChangeLog
7676
git log --pretty --numstat --summary | $(GIT2CL) >$@
77+
78+
79+
#: Run pytest consistency and style checks
80+
check-consistency-and-style:
81+
MATHICS_LINT=t $(PYTHON) -m pytest test/consistency-and-style

pymathics/graph/base.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,8 @@ class Graph(Atom):
540540
def __init__(self, G, **kwargs):
541541
super(Graph, self).__init__()
542542
self.G = G
543+
self.mixed = kwargs.get("mixed", False)
544+
print("creating", self.G, self.mixed)
543545

544546
def __hash__(self):
545547
return hash(("Pymathics`Graph", self.G))
@@ -636,12 +638,17 @@ def empty(self):
636638
def head(self):
637639
return SymbolGraph
638640

641+
def is_directed(self):
642+
if self.G.is_directed():
643+
return not self.mixed
644+
return False
645+
639646
def is_loop_free(self):
640647
return not any(True for _ in nx.nodes_with_selfloops(self.G))
641648

642649
# networkx graphs can't be for mixed
643650
def is_mixed_graph(self):
644-
return False
651+
return self.mixed
645652
# return self.edges. ... is_mixed()
646653

647654
def is_multigraph(self):
@@ -900,6 +907,7 @@ def full_new_edge_properties(new_edge_style):
900907
return None
901908

902909
empty_dict = {}
910+
mixed = False
903911
if directed_edges:
904912
G = nx.MultiDiGraph() if multigraph[0] else nx.DiGraph()
905913
nodes_seen = set()
@@ -913,6 +921,8 @@ def full_new_edge_properties(new_edge_style):
913921
for v in unseen_vertices:
914922
G.add_node(v)
915923

924+
if undirected_edges:
925+
mixed = True
916926
for u, v, attr_dict in undirected_edges:
917927
attr_dict = attr_dict or empty_dict
918928
G.add_edge(u, v, **attr_dict)
@@ -931,7 +941,7 @@ def full_new_edge_properties(new_edge_style):
931941
n_undirected=len(undirected_edges),
932942
)
933943

934-
g = Graph(G)
944+
g = Graph(G, mixed=mixed)
935945
_process_graph_options(g, options)
936946
return g
937947

@@ -1209,6 +1219,10 @@ class DirectedEdge(Builtin):
12091219

12101220
class DirectedGraphQ(_NetworkXBuiltin):
12111221
"""
1222+
<dl>
1223+
<dt>'DirectedGraphQ'[$graph$]
1224+
<dd>True if $graph$ is a 'Graph' and all the edges are directed.
1225+
</dl>
12121226
>> g = Graph[{1 -> 2, 2 -> 3}]; DirectedGraphQ[g]
12131227
= True
12141228
@@ -1226,8 +1240,7 @@ def eval(self, graph, expression, evaluation, options):
12261240
"%(name)s[graph_, OptionsPattern[%(name)s]]"
12271241
graph = self._build_graph(graph, evaluation, options, expression, quiet=True)
12281242
if graph:
1229-
directed = graph.G.is_directed() and not graph.is_mixed_graph()
1230-
return from_python(directed)
1243+
return from_python(graph.is_directed())
12311244
else:
12321245
return SymbolFalse
12331246

@@ -1268,8 +1281,11 @@ def eval_st(self, graph, s, t, expression, evaluation, options):
12681281

12691282
class EdgeIndex(_NetworkXBuiltin):
12701283
"""
1271-
>> EdgeIndex[{c <-> d, d <-> a, a -> e}, d <-> a]
1272-
= 2
1284+
<dl>
1285+
<dt>'EdgeIndex['graph', 'edge']'
1286+
<dd>gives the position of the 'edge' in the list of edges associated \
1287+
to the graph.
1288+
</dl>
12731289
"""
12741290

12751291
def eval(self, graph, v, expression, evaluation, options):
@@ -1287,8 +1303,10 @@ def eval(self, graph, v, expression, evaluation, options):
12871303

12881304
class EdgeList(_PatternList):
12891305
"""
1290-
>> EdgeList[{1 -> 2, 2 <-> 3}]
1291-
= {DirectedEdge[1, 2], UndirectedEdge[2, 3]}
1306+
<dl>
1307+
<dt>'EdgeList'[$g$]
1308+
<dd>gives the list of edges that defines $g$
1309+
</dl>
12921310
"""
12931311

12941312
def _items(self, graph):
@@ -1301,17 +1319,13 @@ class EdgeRules(_NetworkXBuiltin):
13011319
<dt>'EdgeRules'[$g$]
13021320
<dd> gives the list of edge rules for the graph $g$.
13031321
</dl>
1304-
1305-
>> EdgeRules[{1 <-> 2, 2 -> 3, 3 <-> 4}]
1306-
= {1 -> 2, 2 -> 3, 3 -> 4}
13071322
"""
13081323
summary_text = "list the edge rules"
13091324

13101325
def eval(self, graph, expression, evaluation, options):
13111326
"%(name)s[graph_, OptionsPattern[%(name)s]]"
13121327
graph = self._build_graph(graph, evaluation, options, expression)
13131328
if graph:
1314-
13151329
def rules():
13161330
for edge in graph.edges:
13171331
u, v = edge
@@ -1396,11 +1410,6 @@ class FindShortestPath(_NetworkXBuiltin):
13961410
= {}
13971411
13981412
>> g = Graph[{1 -> 2, 2 -> 3, 1 -> 3}, EdgeWeight -> {0.5, a, 3}];
1399-
>> a = 0.5; FindShortestPath[g, 1, 3]
1400-
= {1, 2, 3}
1401-
>> a = 10; FindShortestPath[g, 1, 3]
1402-
= {1, 3}
1403-
>> a=.;
14041413
14051414
#> FindShortestPath[{}, 1, 2]
14061415
: The vertex at position 2 in FindShortestPath[{}, 1, 2] does not belong to the graph at position 1.
@@ -1519,14 +1528,6 @@ class GraphAtom(AtomBuiltin):
15191528
>> Graph[{{1 -> 2}}]
15201529
= Graph[{{1 -> 2}}]
15211530
1522-
>> g = Graph[{1 -> 2, 2 -> 3}, DirectedEdges -> True];
1523-
>> EdgeCount[g, _DirectedEdge]
1524-
= 2
1525-
>> g = Graph[{1 -> 2, 2 -> 3}, DirectedEdges -> False];
1526-
>> EdgeCount[g, _DirectedEdge]
1527-
= 0
1528-
>> EdgeCount[g, _UndirectedEdge]
1529-
= 2
15301531
"""
15311532

15321533
# requires = ("networkx",)
@@ -1583,8 +1584,6 @@ class HITSCentrality(_Centrality):
15831584
the vertices in the graph $g$.
15841585
</dl>
15851586
1586-
>> g = Graph[{a -> d, b -> c, d -> c, d -> a, e -> c}]; HITSCentrality[g]
1587-
= {{0.292893, 0., 0., 0.707107, 0.}, {0., 1., 0.707107, 0., 0.707107}}
15881587
"""
15891588

15901589
summary_text = "HITS centrality"

0 commit comments

Comments
 (0)