import ibis
from ibis import _
from ibis.expr.visualize import to_graph
GraphViz + Ibis
Ibis supports visualizing an expression as a directed graph using GraphViz.
To get started, make sure you’ve installed the necessary dependencies.
$ pip install 'ibis-framework[duckdb,examples,visualization]'
Replace
duckdb
with your backend of choice
For instance, you can replace duckdb
with snowflake
if you want to use the Snowflake backend.
Let’s run through a few examples.
First we’ll import the things we need.
Now we can visualize an expression graph.
Here’s a call to select
.
= ibis.examples.penguins.fetch()
t = t.select(lowered=_.species.lower())
expr to_graph(expr)
The way to read the graph is from top to bottom.
- The top of the graph contains Ibis operations with no dependents.
- The edges encode dependencies from one node to another.
- The bold text in the rectangles is the name of the Ibis operation class.
- The bottom of the graph contains Ibis operations with no dependencies.
- If this were a data flow graph, data would flow from bottom to top.
Let’s look at a more complex example: group_by
.
= (
expr
t.group_by(_.species)
.agg(=_.bill_depth_mm.mean(),
bill_depth_avg=_.bill_length_mm.mean(),
bill_length_avg
)
) to_graph(expr)
Switching gears, let’s look at a join
and show customization of node and edge attributes.
= ibis.table(dict(a="int64", b="string"), name="left")
left = ibis.table(dict(b="string", c="int64", d="string"), name="right")
right = (
expr "b")
left.inner_join(right, =right.c, c=right.d)
.select(left.a, b=ibis.array([1, 2, 3]))
.mutate(arrays
)
to_graph(
expr,=True,
label_edges={"shape": "hexagon", "color": "green", "fontname": "Roboto Mono"},
node_attr={"fontsize": "12", "fontname": "Comic Sans MS"}
edge_attr )
Please try out to_graph
and give us feedback on Zulip or in a GitHub issue!