Skip to content

✨ Construct

Construct Directed Acyclic Graph (DAG) from list, dictionary, and pandas DataFrame.

DAG Construct Methods

Construct DAG from Using parent-child relation Add node attributes
List list_to_dag No
Dictionary dict_to_dag Yes
DataFrame dataframe_to_dag Yes

bigtree.dag.construct

list_to_dag

list_to_dag(relations, node_type=DAGNode)

Construct DAG from list of tuples containing parent-child names. Note that node names must be unique.

Examples:

>>> from bigtree import DAG, dag_iterator
>>> relations_list = [("a", "c"), ("a", "d"), ("b", "c"), ("c", "d"), ("d", "e")]
>>> dag = DAG.from_list(relations_list)
>>> [(parent.node_name, child.node_name) for parent, child in dag.iterate()]
[('a', 'd'), ('c', 'd'), ('d', 'e'), ('a', 'c'), ('b', 'c')]

Parameters:

Name Type Description Default
relations Collection[tuple[str, str]]

contains pairs of parent-child names

required
node_type type[T]

node type of DAG to be created

DAGNode

Returns:

Type Description
T

DAG node

dict_to_dag

dict_to_dag(
    relation_attrs, parent_key="parents", node_type=DAGNode
)

Construct DAG from nested dictionary, key: child name, value: dictionary of parent names and attributes. Note that node names must be unique.

Examples:

>>> from bigtree import DAG
>>> relation_dict = {
...     "a": {"step": 1},
...     "b": {"step": 1},
...     "c": {"parents": ["a", "b"], "step": 2},
...     "d": {"parents": ["a", "c"], "step": 2},
...     "e": {"parents": ["d"], "step": 3},
... }
>>> dag = DAG.from_dict(relation_dict, parent_key="parents")
>>> [(parent.node_name, child.node_name) for parent, child in dag.iterate()]
[('a', 'd'), ('c', 'd'), ('d', 'e'), ('a', 'c'), ('b', 'c')]

Parameters:

Name Type Description Default
relation_attrs Mapping[str, Any]

node, node parents, and node attribute information, key: child name, value: dictionary of parent names and attributes

required
parent_key str

key of dictionary to retrieve list of parents name

'parents'
node_type type[T]

node type of DAG to be created

DAGNode

Returns:

Type Description
T

DAG node

dataframe_to_dag

dataframe_to_dag(
    data,
    child_col=None,
    parent_col=None,
    attribute_cols=None,
    node_type=DAGNode,
)

Construct DAG from pandas DataFrame. Note that node names must be unique.

  • child_col and parent_col specify columns for child name and parent name to construct DAG.
  • attribute_cols specify columns for node attribute for child name.
  • If columns are not specified, child_col takes first column, parent_col takes second column, and all other columns are attribute_cols.

Only attributes in attribute_cols with non-null values will be added to the tree.

Examples:

>>> import pandas as pd
>>> from bigtree import DAG
>>> relation_data = pd.DataFrame([
...     ["a", None, 1],
...     ["b", None, 1],
...     ["c", "a", 2],
...     ["c", "b", 2],
...     ["d", "a", 2],
...     ["d", "c", 2],
...     ["e", "d", 3],
... ],
...     columns=["child", "parent", "step"]
... )
>>> dag = DAG.from_dataframe(relation_data)
>>> [(parent.node_name, child.node_name) for parent, child in dag.iterate()]
[('a', 'd'), ('c', 'd'), ('d', 'e'), ('a', 'c'), ('b', 'c')]

Parameters:

Name Type Description Default
data DataFrame

data containing path and node attribute information

required
child_col str | None

column of data containing child name information, if not set, it will take the first column of data

None
parent_col str | None

column of data containing parent name information, if not set, it will take the second column of data

None
attribute_cols list[str] | None

columns of data containing child node attribute information, if not set, it will take all columns of data except child_col and parent_col

None
node_type type[T]

node type of DAG to be created

DAGNode

Returns:

Type Description
T

DAG node