🔨 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
Export DAG to list of tuples containing parent-child names
Examples:
>>> 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:
Name | Type | Description | Default |
---|---|---|---|
dag |
DAGNode
|
DAG to be exported |
required |
Returns:
Type | Description |
---|---|
List[Tuple[str, str]]
|
(List[Tuple[str, str]]) |
dag_to_dict
Export DAG to dictionary.
Exported dictionary will have key as child name, and parent names and node attributes as a nested dictionary.
Examples:
>>> 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:
Name | Type | Description | Default |
---|---|---|---|
dag |
DAGNode
|
DAG to be exported |
required |
parent_key |
str
|
dictionary key for |
'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 |
False
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
(Dict[str, Any]) |
dag_to_dataframe
Export DAG to pandas DataFrame.
Examples:
>>> 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:
Name | Type | Description | Default |
---|---|---|---|
dag |
DAGNode
|
DAG to be exported |
required |
name_col |
str
|
column name for |
'name'
|
parent_col |
str
|
column name for |
'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 |
False
|
Returns:
Type | Description |
---|---|
DataFrame
|
(pd.DataFrame) |
dag_to_dot
dag_to_dot(
dag,
rankdir="TB",
bg_colour="",
node_colour="",
node_shape="",
edge_colour="",
node_attr="",
edge_attr="",
)
Export DAG or list of DAGs to image. Note that node names must be unique. Possible node attributes include style, fillcolor, shape.
Examples:
>>> 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:
Name | Type | Description | Default |
---|---|---|---|
dag |
Union[DAGNode, List[DAGNode]]
|
DAG or list of DAGs to be exported |
required |
rankdir |
str
|
set direction of graph layout, defaults to 'TB', can be 'BT, 'LR', 'RL' |
'TB'
|
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:
Type | Description |
---|---|
Dot
|
(pydot.Dot) |