Skip to content

๐Ÿ”จ Export

Export Directed Acyclic Graph (DAG) to list, dictionary, and pandas DataFrame.

DAG Export Methods

Export DAG to Method Extract node attributes
List dag_to_list No
Dictionary dag_to_dict Yes with attr_dict or all_attrs
DataFrame dag_to_dataframe Yes with attr_dict or all_attrs
Dot (for .dot, .png, .svg, .jpeg, etc.) dag_to_dot No

bigtree.dag.export

dag_to_list

dag_to_list(dag)

Export DAG to list of tuples containing parent-child names.

Examples:

>>> from bigtree import DAGNode, DAG
>>> a = DAGNode("a", step=1)
>>> b = DAGNode("b", step=1)
>>> c = DAGNode("c", step=2, parents=[a, b])
>>> d = DAGNode("d", step=2, parents=[a, c])
>>> e = DAGNode("e", step=3, parents=[d])
>>> DAG(a).to_list()
[('a', 'c'), ('a', 'd'), ('b', 'c'), ('c', 'd'), ('d', 'e')]

Parameters:

Name Type Description Default
dag T

DAG to be exported

required

Returns:

Type Description
list[tuple[str, str]]

List of tuples containing parent-child names

dag_to_dict

dag_to_dict(
    dag,
    parent_key="parents",
    attr_dict=None,
    all_attrs=False,
)

Export DAG to dictionary. Exported dictionary will have key as child name, and values as a dictionary of parent names and node attributes.

Examples:

>>> from bigtree import DAGNode, DAG
>>> a = DAGNode("a", step=1)
>>> b = DAGNode("b", step=1)
>>> c = DAGNode("c", step=2, parents=[a, b])
>>> d = DAGNode("d", step=2, parents=[a, c])
>>> e = DAGNode("e", step=3, parents=[d])
>>> DAG(a).to_dict(parent_key="parent", attr_dict={"step": "step no."})
{'a': {'step no.': 1}, 'c': {'parent': ['a', 'b'], 'step no.': 2}, 'd': {'parent': ['a', 'c'], 'step no.': 2}, 'b': {'step no.': 1}, 'e': {'parent': ['d'], 'step no.': 3}}

Parameters:

Name Type Description Default
dag T

DAG to be exported

required
parent_key str

dictionary key for node.parent.node_name

'parents'
attr_dict Mapping[str, str] | None

node attributes mapped to dictionary key, key: node attributes, value: corresponding dictionary key

None
all_attrs bool

indicator whether to retrieve all Node attributes

False

Returns:

Type Description
dict[str, Any]

Dictionary of node names to their attributes

dag_to_dataframe

dag_to_dataframe(
    dag,
    name_col="name",
    parent_col="parent",
    attr_dict=None,
    all_attrs=False,
)

Export DAG to pandas DataFrame.

Examples:

>>> from bigtree import DAGNode, DAG
>>> a = DAGNode("a", step=1)
>>> b = DAGNode("b", step=1)
>>> c = DAGNode("c", step=2, parents=[a, b])
>>> d = DAGNode("d", step=2, parents=[a, c])
>>> e = DAGNode("e", step=3, parents=[d])
>>> DAG(a).to_dataframe(name_col="name", parent_col="parent", attr_dict={"step": "step no."})
  name parent  step no.
0    a   None         1
1    c      a         2
2    d      a         2
3    b   None         1
4    c      b         2
5    d      c         2
6    e      d         3

Parameters:

Name Type Description Default
dag T

DAG to be exported

required
name_col str

column name for node.node_name

'name'
parent_col str

column name for node.parent.node_name

'parent'
attr_dict Mapping[str, str] | None

node attributes mapped to column name, key: node attributes, value: corresponding column in dataframe

None
all_attrs bool

indicator whether to retrieve all Node attributes

False

Returns:

Type Description
DataFrame

pandas DataFrame of DAG information

dag_to_dot

dag_to_dot(
    dag,
    rankdir="TB",
    bg_colour=None,
    node_colour=None,
    node_shape=None,
    edge_colour=None,
    node_attr=None,
    edge_attr=None,
)

Export DAG or list of DAGs to image. Note that node names must be unique. Possible node attributes include style, fillcolor, or shape.

Examples:

>>> from bigtree import DAGNode, DAG
>>> a = DAGNode("a", step=1)
>>> b = DAGNode("b", step=1)
>>> c = DAGNode("c", step=2, parents=[a, b])
>>> d = DAGNode("d", step=2, parents=[a, c])
>>> e = DAGNode("e", step=3, parents=[d])
>>> dag_graph = DAG(a).to_dot()

Display image directly without saving (requires IPython)

>>> from IPython.display import Image, display
>>> plt = Image(dag_graph.create_png())
>>> display(plt)
<IPython.core.display.Image object>

Export to image, dot file, etc.

>>> dag_graph.write_png("assets/tree_dag.png")
>>> dag_graph.write_dot("assets/tree_dag.dot")

Export to Dot

Export to string

>>> dag_graph.to_string()
'strict digraph G {\nrankdir=TB;\nc [label=c];\na [label=a];\na -> c;\nd [label=d];\na [label=a];\na -> d;\nc [label=c];\nb [label=b];\nb -> c;\nd [label=d];\nc [label=c];\nc -> d;\ne [label=e];\nd [label=d];\nd -> e;\n}\n'

Parameters:

Name Type Description Default
dag T | list[T]

DAG(s) to be exported

required
rankdir str

set direction of graph layout, accepts 'TB', 'BT, 'LR', or 'RL'

'TB'
bg_colour str | None

background color of image

None
node_colour str | None

fill colour of nodes

None
node_shape str | None

shape of nodes. Possible node_shape include "circle", "square", "diamond", "triangle"

None
edge_colour str | None

colour of edges

None
node_attr str | None

node attribute for style, overrides node_colour. Possible node attributes include {"style": "filled", "fillcolor": "gold"}

None
edge_attr str | None

edge attribute for style, overrides edge_colour. Possible edge attributes include {"style": "bold", "label": "edge label", "color": "black"}

None

Returns:

Type Description
Dot

pydot object of DAG