Skip to content

Commit 758aab0

Browse files
committed
Add TreeGraph
1 parent b32477a commit 758aab0

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

pymathics/graph/__main__.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
from mathics.builtin.graphics import GraphicsBox
1111
from mathics.builtin.randomnumbers import RandomEnv
1212
from mathics.core.expression import (
13-
Expression,
14-
Symbol,
1513
Atom,
14+
Expression,
1615
Integer,
1716
Real,
17+
String,
18+
Symbol,
1819
from_python,
1920
)
2021
from mathics.builtin.patterns import Matcher
@@ -31,8 +32,9 @@
3132
"PlotLabel": "Null",
3233
}
3334

34-
DEFAULT_TREE_OPTIONS = {**DEFAULT_GRAPH_OPTIONS, **{"Directed" : "False"}}
35-
35+
DEFAULT_TREE_OPTIONS = {**DEFAULT_GRAPH_OPTIONS,
36+
**{"Directed" : "False",
37+
"GraphLayout": '"tree"'}}
3638

3739
try:
3840
import networkx as nx
@@ -445,6 +447,12 @@ def update_weights(self, evaluation):
445447
return weights
446448

447449

450+
class TreeGraph(Graph):
451+
def __init__(self, G, **kwargs):
452+
super(Graph, self).__init__()
453+
self.G = G
454+
455+
448456
def _is_connected(G):
449457
if len(G) == 0: # empty graph?
450458
return True
@@ -508,9 +516,6 @@ def _create_graph(new_edges, new_edge_properties, options, from_graph=None):
508516

509517
multigraph = [False]
510518

511-
if "System`VertexStyle" in options:
512-
vertex_options = options["System`VertexStyle"].to_python()
513-
514519
known_vertices = set(vertices)
515520
known_edges = set(edges)
516521

@@ -640,7 +645,10 @@ def full_new_edge_properties(new_edge_style):
640645
n_undirected=len(undirected_edges),
641646
)
642647

643-
return Graph(G)
648+
G.vertex_labeling = options["System`VertexLabeling"]
649+
g = Graph(G)
650+
G.title = g.title = options["System`PlotLabel"]
651+
return g
644652

645653

646654
class Property(Builtin):
@@ -739,6 +747,22 @@ def apply(self, graph, evaluation, options):
739747
return _graph_from_list(graph.leaves, options)
740748

741749

750+
class TreeGraphAtom(AtomBuiltin):
751+
"""
752+
>> TreeGraph[{1->2, 2->3, 3->1}]
753+
= -Graph-
754+
755+
"""
756+
757+
options = DEFAULT_TREE_OPTIONS
758+
759+
def apply(self, graph, evaluation, options):
760+
"TreeGraph[graph_List, OptionsPattern[%(name)s]]"
761+
g = _graph_from_list(graph.leaves, options)
762+
g.G.graph_layout = String("tree")
763+
# Compute/check/set for root?
764+
return g
765+
742766
class PathGraph(_NetworkXBuiltin):
743767
"""
744768
>> PathGraph[{1, 2, 3}]

0 commit comments

Comments
 (0)