Skip to content

Commit b464dbe

Browse files
committed
Split off tree into its own thing.
1 parent 56b18ff commit b464dbe

File tree

4 files changed

+61
-54
lines changed

4 files changed

+61
-54
lines changed

pymathics/graph/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
from pymathics.graph.__main__ import *
9+
from pymathics.graph.tree import *
910
from pymathics.graph.graph_generators import *
1011
from pymathics.graph.version import __version__
1112

pymathics/graph/__main__.py

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,7 @@
3232
"PlotLabel": "Null",
3333
}
3434

35-
DEFAULT_TREE_OPTIONS = {**DEFAULT_GRAPH_OPTIONS,
36-
**{"Directed" : "False",
37-
"GraphLayout": '"tree"'}}
38-
39-
try:
40-
import networkx as nx
41-
except ImportError:
42-
nx = {}
43-
35+
import networkx as nx
4436

4537
def _circular_layout(G):
4638
return nx.drawing.circular_layout(G, scale=1.5)
@@ -453,12 +445,6 @@ def update_weights(self, evaluation):
453445
return weights
454446

455447

456-
class TreeGraph(Graph):
457-
def __init__(self, G, **kwargs):
458-
super(Graph, self).__init__()
459-
self.G = G
460-
461-
462448
def _is_connected(G):
463449
if len(G) == 0: # empty graph?
464450
return True
@@ -776,44 +762,6 @@ def apply_1(self, vertices, edges, evaluation, options):
776762
return _graph_from_list(edges.leaves, options=options, new_vertices=vertices.leaves)
777763

778764

779-
class TreeGraphAtom(AtomBuiltin):
780-
"""
781-
>> TreeGraph[{1->2, 2->3, 3->1}]
782-
= -Graph-
783-
784-
"""
785-
786-
options = DEFAULT_TREE_OPTIONS
787-
788-
messages = {
789-
"v": "Expected first parameter vertices to be a list of vertices",
790-
"notree": "Graph is not a tree.",
791-
}
792-
793-
def apply(self, rules, evaluation, options):
794-
"TreeGraph[rules_List, OptionsPattern[%(name)s]]"
795-
g = _graph_from_list(rules.leaves, options)
796-
if not nx.is_tree(g.G):
797-
evaluation.message(self.get_name(), "notree")
798-
799-
g.G.graph_layout = String("tree")
800-
# Compute/check/set for root?
801-
return g
802-
803-
def apply_1(self, vertices, edges, evaluation, options):
804-
"TreeGraph[vertices_List, edges_List, OptionsPattern[%(name)s]]"
805-
if not all(isinstance(v, Atom) for v in vertices.leaves):
806-
evaluation.message(self.get_name(), "v")
807-
808-
g = _graph_from_list(edges.leaves, options=options, new_vertices=vertices.leaves)
809-
if not nx.is_tree(g.G):
810-
evaluation.message(self.get_name(), "notree")
811-
812-
g.G.graph_layout = String("tree")
813-
# Compute/check/set for root?
814-
return g
815-
816-
817765
class PathGraph(_NetworkXBuiltin):
818766
"""
819767
>> PathGraph[{1, 2, 3}]

pymathics/graph/graph_generators.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from pymathics.graph.__main__ import (
2-
DEFAULT_TREE_OPTIONS,
32
Graph,
43
_NetworkXBuiltin,
54
nx,
65
)
6+
7+
from pymathics.graph.tree import DEFAULT_TREE_OPTIONS
8+
79
from mathics.core.expression import Integer, String
810
from typing import Callable, Optional
911

pymathics/graph/tree.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import networkx as nx
2+
from pymathics.graph.__main__ import Graph, _graph_from_list, DEFAULT_GRAPH_OPTIONS
3+
from mathics.core.expression import String
4+
5+
DEFAULT_TREE_OPTIONS = {
6+
**DEFAULT_GRAPH_OPTIONS,
7+
**{"Directed": "False", "GraphLayout": '"tree"'},
8+
}
9+
10+
from mathics.builtin.base import Builtin, AtomBuiltin
11+
12+
13+
class TreeGraphAtom(AtomBuiltin):
14+
"""
15+
>> TreeGraph[{1->2, 2->3, 3->1}]
16+
= -Graph-
17+
18+
"""
19+
20+
options = DEFAULT_TREE_OPTIONS
21+
22+
messages = {
23+
"v": "Expected first parameter vertices to be a list of vertices",
24+
"notree": "Graph is not a tree.",
25+
}
26+
27+
def apply(self, rules, evaluation, options):
28+
"TreeGraph[rules_List, OptionsPattern[%(name)s]]"
29+
g = _graph_from_list(rules.leaves, options)
30+
if not nx.is_tree(g.G):
31+
evaluation.message(self.get_name(), "notree")
32+
33+
g.G.graph_layout = String("tree")
34+
# Compute/check/set for root?
35+
return g
36+
37+
def apply_1(self, vertices, edges, evaluation, options):
38+
"TreeGraph[vertices_List, edges_List, OptionsPattern[%(name)s]]"
39+
if not all(isinstance(v, Atom) for v in vertices.leaves):
40+
evaluation.message(self.get_name(), "v")
41+
42+
g = _graph_from_list(
43+
edges.leaves, options=options, new_vertices=vertices.leaves
44+
)
45+
if not nx.is_tree(g.G):
46+
evaluation.message(self.get_name(), "notree")
47+
48+
g.G.graph_layout = String("tree")
49+
# Compute/check/set for root?
50+
return g
51+
52+
53+
class TreeGraph(Graph):
54+
def __init__(self, G, **kwargs):
55+
super(Graph, self).__init__()
56+
self.G = G

0 commit comments

Comments
 (0)