Skip to content

Commit 5d87c3f

Browse files
authored
Merge pull request #184 from kayjan/update-changelog
Update changelog
2 parents 8826ddb + a99a0a1 commit 5d87c3f

File tree

10 files changed

+233
-58
lines changed

10 files changed

+233
-58
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Misc: Documentation plugin Termynal for code animation.
10+
- Misc: Usage of `docstr-coverage`.
11+
- Misc: Docstrings for nested functions to pass `docstr-coverage`.
12+
### Changed
13+
- Misc: Documentation CSS for h1 display for windows compatibility, modify the related links on main page.
814

915
## [0.16.1] - 2023-01-29
1016
### Fixed

bigtree/node/dagnode.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,20 @@ def ancestors(self: T) -> Iterable[T]:
363363
if not len(list(self.parents)):
364364
return ()
365365

366-
def recursive_parent(node: T) -> Iterable[T]:
366+
def _recursive_parent(node: T) -> Iterable[T]:
367+
"""Recursively yield parent of current node, returns earliest to latest ancestor
368+
369+
Args:
370+
node (DAGNode): current node
371+
372+
Returns:
373+
(Iterable[DAGNode])
374+
"""
367375
for _node in node.parents:
368-
yield from recursive_parent(_node)
376+
yield from _recursive_parent(_node)
369377
yield _node
370378

371-
ancestors = list(recursive_parent(self))
379+
ancestors = list(_recursive_parent(self))
372380
return list(dict.fromkeys(ancestors))
373381

374382
@property
@@ -531,18 +539,27 @@ def go_to(self: T, node: T) -> List[List[T]]:
531539

532540
self.__path: List[List[T]] = []
533541

534-
def recursive_path(_node: T, _path: List[T]) -> Optional[List[T]]:
542+
def _recursive_path(_node: T, _path: List[T]) -> Optional[List[T]]:
543+
"""Get path to specified node
544+
545+
Args:
546+
_node (DAGNode): current node
547+
_path (List[DAGNode]): current path, from start node to current node, excluding current node
548+
549+
Returns:
550+
(List[DAGNode])
551+
"""
535552
if _node: # pragma: no cover
536553
_path.append(_node)
537554
if _node == node:
538555
return _path
539556
for _child in _node.children:
540-
ans = recursive_path(_child, _path.copy())
557+
ans = _recursive_path(_child, _path.copy())
541558
if ans:
542559
self.__path.append(ans)
543560
return None
544561

545-
recursive_path(self, [])
562+
_recursive_path(self, [])
546563
return self.__path
547564

548565
def copy(self: T) -> T:

bigtree/tree/construct.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,18 @@ def nested_dict_to_tree(
740740
if not node_attrs:
741741
raise ValueError("Dictionary does not contain any data, check `node_attrs`")
742742

743-
def recursive_add_child(
743+
def _recursive_add_child(
744744
child_dict: Dict[str, Any], parent_node: Optional[Node] = None
745745
) -> Node:
746+
"""Recursively add child to tree, given child attributes and parent node.
747+
748+
Args:
749+
child_dict (Dict[str, Any]): child to be added to tree, from dictionary
750+
parent_node (Node): parent node to be assigned to child node, defaults to None
751+
752+
Returns:
753+
(Node)
754+
"""
746755
child_dict = child_dict.copy()
747756
node_name = child_dict.pop(name_key)
748757
node_children = child_dict.pop(child_key, [])
@@ -752,10 +761,10 @@ def recursive_add_child(
752761
)
753762
node = node_type(node_name, parent=parent_node, **child_dict)
754763
for _child in node_children:
755-
recursive_add_child(_child, parent_node=node)
764+
_recursive_add_child(_child, parent_node=node)
756765
return node
757766

758-
root_node = recursive_add_child(node_attrs)
767+
root_node = _recursive_add_child(node_attrs)
759768
return root_node
760769

761770

@@ -988,27 +997,40 @@ def dataframe_to_tree_by_relation(
988997
else:
989998
root_node = node_type(root_name)
990999

991-
def retrieve_attr(_row: Dict[str, Any]) -> Dict[str, Any]:
1000+
def _retrieve_attr(_row: Dict[str, Any]) -> Dict[str, Any]:
1001+
"""Retrieve node attributes from dictionary, remove parent and child column from dictionary.
1002+
1003+
Args:
1004+
_row (Dict[str, Any]): node attributes
1005+
1006+
Returns:
1007+
(Dict[str, Any])
1008+
"""
9921009
node_attrs = _row.copy()
9931010
node_attrs["name"] = node_attrs[child_col]
9941011
del node_attrs[child_col]
9951012
del node_attrs[parent_col]
9961013
_node_attrs = {k: v for k, v in node_attrs.items() if v is not None}
9971014
return _node_attrs
9981015

999-
def recursive_create_child(parent_node: Node) -> None:
1016+
def _recursive_add_child(parent_node: Node) -> None:
1017+
"""Recursive add child to tree, given current node.
1018+
1019+
Args:
1020+
parent_node (Node): parent node
1021+
"""
10001022
child_rows = data[data[parent_col] == parent_node.node_name]
10011023

10021024
for row in child_rows.to_dict(orient="index").values():
1003-
child_node = node_type(**retrieve_attr(row))
1025+
child_node = node_type(**_retrieve_attr(row))
10041026
child_node.parent = parent_node
1005-
recursive_create_child(child_node)
1027+
_recursive_add_child(child_node)
10061028

10071029
# Create root node attributes
10081030
if len(root_row):
10091031
row = list(root_row.to_dict(orient="index").values())[0]
1010-
root_node.set_attrs(retrieve_attr(row))
1011-
recursive_create_child(root_node)
1032+
root_node.set_attrs(_retrieve_attr(row))
1033+
_recursive_add_child(root_node)
10121034
return root_node
10131035

10141036

@@ -1093,6 +1115,18 @@ def _create_node(
10931115
_depth_nodes: Dict[int, List[Node]],
10941116
_current_depth: int,
10951117
) -> Tuple[Node, int]:
1118+
"""Create node at checkpoint.
1119+
1120+
Args:
1121+
_new_node (Optional[Node]): existing node (to add length attribute), or nothing (to create a node)
1122+
_cumulative_string (str): cumulative string, contains either node name or length attribute
1123+
_unlabelled_node_counter (int): number of unlabelled nodes, updates and returns counter
1124+
_depth_nodes (Dict[int, List[Node]]): list of nodes at each depth
1125+
_current_depth (int): depth of current node or node to be created
1126+
1127+
Returns:
1128+
(Tuple[Node, int])
1129+
"""
10961130
if not _new_node:
10971131
if not _cumulative_string:
10981132
_cumulative_string = f"node{_unlabelled_node_counter}"
@@ -1116,6 +1150,11 @@ def _create_node(
11161150
return _new_node, _unlabelled_node_counter
11171151

11181152
def _raise_value_error(tree_idx: int) -> None:
1153+
"""Raise value error.
1154+
1155+
Raises:
1156+
ValueError
1157+
"""
11191158
raise ValueError(
11201159
f"String not properly closed, check `tree_string` at index {tree_idx}"
11211160
)

bigtree/tree/export.py

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ def tree_to_newick(
789789
attr_sep = attr_sep.value
790790

791791
def _serialize(item: Any) -> Any:
792-
"""Serialize item if it contains special Newick characters
792+
"""Serialize item if it contains special Newick characters.
793793
794794
Args:
795795
item (Any): item to serialize
@@ -887,7 +887,12 @@ def tree_to_dict(
887887
tree = tree.copy()
888888
data_dict = {}
889889

890-
def recursive_append(node: T) -> None:
890+
def _recursive_append(node: T) -> None:
891+
"""Recursively iterate through node and its children to export to dictionary.
892+
893+
Args:
894+
node (Node): current node
895+
"""
891896
if node:
892897
if (
893898
(not max_depth or node.depth <= max_depth)
@@ -915,9 +920,9 @@ def recursive_append(node: T) -> None:
915920
data_child[v] = node.get_attr(k)
916921
data_dict[node.path_name] = data_child
917922
for _node in node.children:
918-
recursive_append(_node)
923+
_recursive_append(_node)
919924

920-
recursive_append(tree)
925+
_recursive_append(tree)
921926
return data_dict
922927

923928

@@ -960,7 +965,13 @@ def tree_to_nested_dict(
960965
tree = tree.copy()
961966
data_dict: Dict[str, List[Dict[str, Any]]] = {}
962967

963-
def recursive_append(node: T, parent_dict: Dict[str, Any]) -> None:
968+
def _recursive_append(node: T, parent_dict: Dict[str, Any]) -> None:
969+
"""Recursively iterate through node and its children to export to nested dictionary.
970+
971+
Args:
972+
node (Node): current node
973+
parent_dict (Dict[str, Any]): parent dictionary
974+
"""
964975
if node:
965976
if not max_depth or node.depth <= max_depth:
966977
data_child = {name_key: node.node_name}
@@ -981,9 +992,9 @@ def recursive_append(node: T, parent_dict: Dict[str, Any]) -> None:
981992
parent_dict[child_key] = [data_child]
982993

983994
for _node in node.children:
984-
recursive_append(_node, data_child)
995+
_recursive_append(_node, data_child)
985996

986-
recursive_append(tree, data_dict)
997+
_recursive_append(tree, data_dict)
987998
return data_dict[child_key][0]
988999

9891000

@@ -1044,7 +1055,12 @@ def tree_to_dataframe(
10441055
tree = tree.copy()
10451056
data_list = []
10461057

1047-
def recursive_append(node: T) -> None:
1058+
def _recursive_append(node: T) -> None:
1059+
"""Recursively iterate through node and its children to export to dataframe.
1060+
1061+
Args:
1062+
node (Node): current node
1063+
"""
10481064
if node:
10491065
if (
10501066
(not max_depth or node.depth <= max_depth)
@@ -1071,9 +1087,9 @@ def recursive_append(node: T) -> None:
10711087
data_child[v] = node.get_attr(k)
10721088
data_list.append(data_child)
10731089
for _node in node.children:
1074-
recursive_append(_node)
1090+
_recursive_append(_node)
10751091

1076-
recursive_append(tree)
1092+
_recursive_append(tree)
10771093
return pd.DataFrame(data_list)
10781094

10791095

@@ -1212,9 +1228,13 @@ def tree_to_dot(
12121228

12131229
name_dict: Dict[str, List[str]] = collections.defaultdict(list)
12141230

1215-
def recursive_create_node_and_edges(
1216-
parent_name: Optional[str], child_node: T
1217-
) -> None:
1231+
def _recursive_append(parent_name: Optional[str], child_node: T) -> None:
1232+
"""Recursively iterate through node and its children to export to dot by creating node and edges.
1233+
1234+
Args:
1235+
parent_name (Optional[str]): parent name
1236+
child_node (Node): current node
1237+
"""
12181238
_node_style = node_style.copy()
12191239
_edge_style = edge_style.copy()
12201240

@@ -1241,9 +1261,9 @@ def recursive_create_node_and_edges(
12411261
_graph.add_edge(edge)
12421262
for child in child_node.children:
12431263
if child:
1244-
recursive_create_node_and_edges(child_name, child)
1264+
_recursive_append(child_name, child)
12451265

1246-
recursive_create_node_and_edges(None, _tree.root)
1266+
_recursive_append(None, _tree.root)
12471267
return _graph
12481268

12491269

@@ -1522,6 +1542,17 @@ def _construct_style(
15221542
_node_border_colour: str,
15231543
_node_border_width: float,
15241544
) -> str:
1545+
"""Construct style for Mermaid.
1546+
1547+
Args:
1548+
_style_name (str): style name
1549+
_node_colour (str): node colour
1550+
_node_border_colour (str): node border colour
1551+
_node_border_width (float): node border width
1552+
1553+
Returns:
1554+
(str)
1555+
"""
15251556
style = []
15261557
if _node_colour:
15271558
style.append(f"fill:{_node_colour}")
@@ -1539,6 +1570,8 @@ def _construct_style(
15391570
styles.append(default_style)
15401571

15411572
class MermaidNode(Node):
1573+
"""Mermaid Node, adds property `mermaid_name`"""
1574+
15421575
@property
15431576
def mermaid_name(self) -> str:
15441577
"""Reference name for MermaidNode, must be unique for each node.
@@ -1550,7 +1583,7 @@ def mermaid_name(self) -> str:
15501583
return "0"
15511584
return f"{self.parent.mermaid_name}-{self.parent.children.index(self)}"
15521585

1553-
def get_attr(
1586+
def _get_attr(
15541587
_node: MermaidNode,
15551588
attr_parameter: str | Callable[[MermaidNode], str],
15561589
default_parameter: str,
@@ -1579,24 +1612,24 @@ def get_attr(
15791612
# Get custom style (node_shape_attr)
15801613
_parent_node_name = ""
15811614
if node.parent.is_root:
1582-
_parent_node_shape_choice = get_attr(
1615+
_parent_node_shape_choice = _get_attr(
15831616
node.parent, node_shape_attr, node_shape # type: ignore
15841617
)
15851618
_parent_node_shape = node_shapes[_parent_node_shape_choice]
15861619
_parent_node_name = _parent_node_shape.format(label=node.parent.name)
1587-
_node_shape_choice = get_attr(node, node_shape_attr, node_shape) # type: ignore
1620+
_node_shape_choice = _get_attr(node, node_shape_attr, node_shape) # type: ignore
15881621
_node_shape = node_shapes[_node_shape_choice]
15891622
_node_name = _node_shape.format(label=node.name)
15901623

15911624
# Get custom style (edge_arrow_attr, edge_label)
1592-
_arrow_choice = get_attr(node, edge_arrow_attr, edge_arrow) # type: ignore
1625+
_arrow_choice = _get_attr(node, edge_arrow_attr, edge_arrow) # type: ignore
15931626
_arrow = edge_arrows[_arrow_choice]
15941627
_arrow_label = (
15951628
f"|{node.get_attr(edge_label)}|" if node.get_attr(edge_label) else ""
15961629
)
15971630

15981631
# Get custom style (node_attr)
1599-
_flow_style = get_attr(node, node_attr, "") # type: ignore
1632+
_flow_style = _get_attr(node, node_attr, "") # type: ignore
16001633
if _flow_style:
16011634
_flow_style_class = f"""class{node.get_attr("mermaid_name")}"""
16021635
styles.append(

bigtree/tree/helper.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,23 @@ def clone_tree(tree: BaseNode, node_type: Type[BaseNodeT]) -> BaseNodeT:
4141
root_info = dict(tree.root.describe(exclude_prefix="_"))
4242
root_node = node_type(**root_info)
4343

44-
def recursive_add_child(
44+
def _recursive_add_child(
4545
_new_parent_node: BaseNodeT, _parent_node: BaseNode
4646
) -> None:
47+
"""Recursively clone current node
48+
49+
Args:
50+
_new_parent_node (BaseNode): cloned parent node
51+
_parent_node (BaseNode): parent node to be cloned
52+
"""
4753
for _child in _parent_node.children:
4854
if _child:
4955
child_info = dict(_child.describe(exclude_prefix="_"))
5056
child_node = node_type(**child_info)
5157
child_node.parent = _new_parent_node
52-
recursive_add_child(child_node, _child)
58+
_recursive_add_child(child_node, _child)
5359

54-
recursive_add_child(root_node, tree.root)
60+
_recursive_add_child(root_node, tree.root)
5561
return root_node
5662

5763

0 commit comments

Comments
 (0)