bigtree

🔨 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

Functions:

dag_to_dataframe(dag[, name_col, ...])

Export DAG to pandas DataFrame.

dag_to_dict(dag[, parent_key, attr_dict, ...])

Export DAG to dictionary.

dag_to_dot(dag[, rankdir, bg_colour, ...])

Export DAG or list of DAGs to image.

dag_to_list(dag)

Export DAG to list of tuples containing parent-child names

bigtree.dag.export.dag_to_dataframe(dag: T, name_col: str = 'name', parent_col: str = 'parent', attr_dict: Dict[str, str] = {}, all_attrs: bool = False) DataFrame

Export DAG to pandas DataFrame.

>>> from bigtree import DAGNode, dag_to_dataframe
>>> 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_to_dataframe(a, 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:
  • dag (DAGNode) – DAG to be exported

  • name_col (str) – column name for node.node_name, defaults to ‘name’

  • parent_col (str) – column name for node.parent.node_name, defaults to ‘parent’

  • attr_dict (Dict[str, str]) – dictionary mapping node attributes to column name, key: node attributes, value: corresponding column in dataframe, optional

  • all_attrs (bool) – indicator whether to retrieve all Node attributes, defaults to False

Returns:

(pd.DataFrame)

bigtree.dag.export.dag_to_dict(dag: T, parent_key: str = 'parents', attr_dict: Dict[str, str] = {}, all_attrs: bool = False) Dict[str, Any]

Export DAG to dictionary.

Exported dictionary will have key as child name, and parent names and node attributes as a nested dictionary.

>>> from bigtree import DAGNode, dag_to_dict
>>> 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_to_dict(a, 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:
  • dag (DAGNode) – DAG to be exported

  • parent_key (str) – dictionary key for node.parent.node_name, defaults to parents

  • attr_dict (Dict[str, str]) – dictionary mapping node attributes to dictionary key, key: node attributes, value: corresponding dictionary key, optional

  • all_attrs (bool) – indicator whether to retrieve all Node attributes, defaults to False

Returns:

(Dict[str, Any])

bigtree.dag.export.dag_to_dot(dag: T | List[T], rankdir: str = 'TB', bg_colour: str = '', node_colour: str = '', node_shape: str = '', edge_colour: str = '', node_attr: str = '', edge_attr: str = '') pydot.Dot

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

>>> from bigtree import DAGNode, dag_to_dot
>>> 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_to_dot(a)

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/docstr/tree_dag.png")
>>> dag_graph.write_dot("assets/docstr/tree_dag.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:
  • dag (Union[DAGNode, List[DAGNode]]) – DAG or list of DAGs to be exported

  • rankdir (str) – set direction of graph layout, defaults to ‘TB’, can be ‘BT, ‘LR’, ‘RL’

  • bg_colour (str) – background color of image, defaults to ‘’

  • node_colour (str) – fill colour of nodes, defaults to ‘’

  • node_shape (str) – shape of nodes, defaults to None Possible node_shape include “circle”, “square”, “diamond”, “triangle”

  • edge_colour (str) – colour of edges, defaults to ‘’

  • node_attr (str) – node attribute for style, overrides node_colour, defaults to ‘’ Possible node attributes include {“style”: “filled”, “fillcolor”: “gold”}

  • edge_attr (str) – edge attribute for style, overrides edge_colour, defaults to ‘’ Possible edge attributes include {“style”: “bold”, “label”: “edge label”, “color”: “black”}

Returns:

(pydot.Dot)

bigtree.dag.export.dag_to_list(dag: T) List[Tuple[str, str]]

Export DAG to list of tuples containing parent-child names

>>> from bigtree import DAGNode, dag_to_list
>>> 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_to_list(a)
[('a', 'c'), ('a', 'd'), ('b', 'c'), ('c', 'd'), ('d', 'e')]
Parameters:

dag (DAGNode) – DAG to be exported

Returns:

(List[Tuple[str, str]])