Representing Julia expressions AST using a directed graph

My graph should represent the AST of Julia expressions like y = x + 10/a for instance; there is a catch though, these ys and xs are not just mere numbers; they are custom types (wrappers) that hold, among other things, numeric values.

I want my graph to also represent the label of the object x, which also holds a value, say 8.0

Maybe GraphMakie.jl can help you here. If you have the associated DAG, you could just set the labels.

could you please point out to an example or a documentation link? I looked into the docs, but didn’t find a simple use case

By the way, I expect it to look like:

a|data: 8.0 -------> b|data: 5.0

Does this help?

julia> using SimpleExpressions, AbstractTrees

julia> @symbolic x y;

julia> expression = 2*x + 3*x*y
(2 * x) + (y * (3 * x))

julia> print_tree(expression)
+
├─ *
│  ├─ 2
│  └─ x
└─ *
   ├─ y
   └─ *
      ├─ 3
      └─ x

On Slack I recommended other solutions, but now it seems that you just want a visualization of the tree?

BTW, if you take a look at the AbstractTrees.jl documentation, it points to D3Trees.jl, which can visualize the expression tree in a more graphic and interactive manner.

2 Likes

thank you, this is the closest to what I’m looking for:

image
(from Deep Neural Networks As Computational Graphs | by Tyler Elliot Bettilyon | Teb’s Lab | Medium)

I don’t care too much about the style of the graph as long as I have control over the nodes’ display (meaning, I can customize the information to be displayed on each node)… like an “AST weighted computational graph”

Hopefully, that makes sense

For really fine control over labels attached to each node and edge, a tool you may consider is GraphViz, which can allow for fairly complex types of information to be included in each node (tables, for example Node Shapes | Graphviz).There is a graphviz_jll package that you could use to link to it.

2 Likes

Thank you.

I spent a lot of time looking into different libraries; I decided to use Graphviz but through Python using PyCall, because the Julia version is not maintained anymore (for at least a couple of years).

1 Like

Personally I wouldn’t bother with any library bindings, instead I’d just generate the graph descriptions as text.

FTR, alternatives to Graphviz include:

BTW, a fun fact that I think not many people know, is that AFAIK Graphviz originated at Bell Labs together with Unix and other things.

2 Likes