Skip to content

Commit e0b9351

Browse files
committed
start GraphData
1 parent 4e21c1e commit e0b9351

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

pymathics/graph/__main__.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,19 @@
4141
# And many others. Is there a list somewhere?
4242
}
4343

44+
WL_LAYOUT_TO_MATPLOTLIB = {
45+
"CircularEmbedding": "circular",
46+
"SpiralEmbedding": "spiral",
47+
"SpectralEmbedding": "spectral",
48+
"SpringEmbedding": "spring",
49+
# And many others. Is there a list somewhere?
50+
}
51+
4452
DEFAULT_GRAPH_OPTIONS = {
4553
"DirectedEdges": "False",
4654
"EdgeStyle": "{}",
4755
"EdgeWeight": "{}",
48-
"GraphLayout": "{}",
56+
"GraphLayout": "Null",
4957
"PlotLabel": "Null",
5058
"VertexLabels": "False",
5159
"VertexSize": "{}",
@@ -59,6 +67,7 @@
5967
def has_directed_option(options: dict) -> bool:
6068
return options.get("System`DirectedEdges", False).to_python()
6169

70+
6271
def _process_graph_options(g, options: dict) -> None:
6372
"""
6473
Handle common graph-related options like VertexLabels, PlotLabel, VertexShape, etc.
@@ -85,6 +94,14 @@ def _process_graph_options(g, options: dict) -> None:
8594
else "Blue"
8695
)
8796

97+
g.graph_layout = (
98+
options["System`GraphLayout"].get_string_value()
99+
if "System`GraphLayout" in options
100+
else ""
101+
)
102+
103+
g.G.graph_layout = g.graph_layout = WL_LAYOUT_TO_MATPLOTLIB.get(g.graph_layout, g.graph_layout)
104+
88105
g.G.node_color = g.node_color = WL_COLOR_TO_MATPLOTLIB.get(color, color)
89106

90107
g.G.title = g.title = (
@@ -834,6 +851,7 @@ def apply_1(self, vertices, edges, evaluation, options):
834851
edges.leaves, options=options, new_vertices=vertices.leaves
835852
)
836853

854+
837855
class PathGraphQ(_NetworkXBuiltin):
838856
"""
839857
>> PathGraphQ[Graph[{1 -> 2, 2 -> 3}]]
@@ -1502,7 +1520,7 @@ def apply(self, graph, expression, evaluation, options):
15021520
)
15031521
return Expression(
15041522
"List",
1505-
*[Real(centrality.get(v, 0.0)) for v in graph.vertices.expressions]
1523+
*[Real(centrality.get(v, 0.0)) for v in graph.vertices.expressions],
15061524
)
15071525

15081526

@@ -1526,7 +1544,7 @@ def apply(self, graph, expression, evaluation, options):
15261544
centrality = nx.closeness_centrality(G, distance=weight, wf_improved=False)
15271545
return Expression(
15281546
"List",
1529-
*[Real(centrality.get(v, 0.0)) for v in graph.vertices.expressions]
1547+
*[Real(centrality.get(v, 0.0)) for v in graph.vertices.expressions],
15301548
)
15311549

15321550

@@ -1546,7 +1564,7 @@ def _from_dict(self, graph, centrality):
15461564
s = len(graph.G) - 1 # undo networkx's normalization
15471565
return Expression(
15481566
"List",
1549-
*[Integer(s * centrality.get(v, 0)) for v in graph.vertices.expressions]
1567+
*[Integer(s * centrality.get(v, 0)) for v in graph.vertices.expressions],
15501568
)
15511569

15521570
def apply(self, graph, expression, evaluation, options):
@@ -1685,7 +1703,7 @@ def apply_alpha_beta(self, graph, alpha, expression, evaluation, options):
16851703
centrality = nx.pagerank(G, alpha=py_alpha, weight=weight, tol=1.0e-7)
16861704
return Expression(
16871705
"List",
1688-
*[Real(centrality.get(v, 0)) for v in graph.vertices.expressions]
1706+
*[Real(centrality.get(v, 0)) for v in graph.vertices.expressions],
16891707
)
16901708

16911709

@@ -1775,7 +1793,7 @@ def apply_s_t(self, graph, s, t, expression, evaluation, options):
17751793
weight = graph.update_weights(evaluation)
17761794
return Expression(
17771795
"List",
1778-
*list(nx.shortest_path(G, source=s, target=t, weight=weight))
1796+
*list(nx.shortest_path(G, source=s, target=t, weight=weight)),
17791797
)
17801798
except nx.exception.NetworkXNoPath:
17811799
return Expression("List")

pymathics/graph/generators.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def graph_helper(
4545
except MemoryError:
4646
evaluation.message(self.get_name(), "mem", expression)
4747
return None
48-
G.graph_layout = options["System`GraphLayout"].get_string_value() or graph_layout
48+
if graph_layout and not options["System`GraphLayout"].get_string_value():
49+
options["System`GraphLayout"] = String(graph_layout)
50+
4951
g = Graph(G)
5052
_process_graph_options(g, options)
5153

@@ -619,3 +621,29 @@ def apply(self, n, expression, evaluation, options):
619621
return None
620622
g.G.n = n
621623
return g
624+
625+
WL_TO_NETWORKX_FN = {
626+
"DodecahedralGraph": (nx.dodecahedral_graph, None),
627+
"DiamondGraph": (nx.diamond_graph, "spring"),
628+
"PappusGraph": (nx.pappus_graph, "circular"),
629+
"IsohedralGraph": (nx.icosahedral_graph, "spring"),
630+
"PetersenGraph": (nx.petersen_graph, None),
631+
}
632+
633+
class GraphData(_NetworkXBuiltin):
634+
"""
635+
<dl>
636+
<dt>'GraphData[$name$]'
637+
<dd>Returns a graph with the specified name.
638+
</dl>
639+
640+
>> GraphData["PappusGraph"]
641+
"""
642+
def apply(self, name, expression, evaluation, options):
643+
"%(name)s[name_String, OptionsPattern[%(name)s]]"
644+
py_name = name.get_string_value()
645+
fn, layout = WL_TO_NETWORKX_FN.get(py_name, (None, None))
646+
if fn:
647+
g = graph_helper(fn, options, False, layout)
648+
g.G.name = py_name
649+
return g

0 commit comments

Comments
 (0)