Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
*pytest_cache/
*__pycache__*

tests/*png
tests/*dot
dist/
*/build/
*.DS_Store
assets/docstr
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.14.8] - 2023-12-25
### Changed
- Tree Modifier: Enhance documentation examples.
- Workflow To Do App: Change import and export logic.
- Misc: Organize assets folder based on whether it originated from README, docstrings, or sphinx documentation.
- Misc: Rename functions in `plot.py` utils file for coverage report.
### Fixed
- Misc: Fixed doctests and automate doctest checks.

## [0.14.7] - 2023-12-22
### Changed
- Tree Helper: `get_tree_diff` enhanced to compare tree attributes by taking in `attr_list` parameter, and indicates difference with `(~)`.
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ root.show()
# └── c

graph = tree_to_dot(root, node_colour="gold")
graph.write_png("assets/demo_tree.png")
graph.write_png("assets/docs/demo_tree.png")
```

![Sample Tree Output](https://github.com/kayjan/bigtree/raw/master/assets/demo_tree.png)
![Sample Tree Output](https://github.com/kayjan/bigtree/raw/master/assets/docs/demo_tree.png)

{emphasize-lines="8-10"}
```python
Expand Down Expand Up @@ -897,22 +897,22 @@ tree_to_dataframe(
# 4 /a/c c a 60

graph = tree_to_dot(root, node_colour="gold")
graph.write_png("assets/demo.png")
graph.write_png("assets/docs/demo_dot.png")

pillow_image = tree_to_pillow(root)
pillow_image.save("assets/demo_pillow.png")
pillow_image.save("assets/docs/demo_pillow.png")

mermaid_md = tree_to_mermaid(root)
print(mermaid_md)
```

- demo.png
- demo_dot.png

![Sample Dot Image Output](https://github.com/kayjan/bigtree/raw/master/assets/demo.png)
![Sample Dot Image Output](https://github.com/kayjan/bigtree/raw/master/assets/docs/demo_dot.png)

- demo_pillow.png

![Sample Pillow Image Output](https://github.com/kayjan/bigtree/raw/master/assets/demo_pillow.png)
![Sample Pillow Image Output](https://github.com/kayjan/bigtree/raw/master/assets/docs/demo_pillow.png)

- Mermaid flowchart
```mermaid
Expand Down Expand Up @@ -954,10 +954,10 @@ g = BinaryNode(7, parent=c)
h = BinaryNode(8, parent=d)

graph = tree_to_dot(a, node_colour="gold")
graph.write_png("assets/demo_binarytree.png")
graph.write_png("assets/docs/demo_binarytree.png")
```

![Sample DAG Output](https://github.com/kayjan/bigtree/raw/master/assets/demo_binarytree.png)
![Sample DAG Output](https://github.com/kayjan/bigtree/raw/master/assets/docs/demo_binarytree.png)

2. **From *list***

Expand Down Expand Up @@ -1058,10 +1058,10 @@ h = DAGNode("h")
g = DAGNode("g", parents=[c], children=[h])

graph = dag_to_dot(a, node_colour="gold")
graph.write_png("assets/demo_dag.png")
graph.write_png("assets/docs/demo_dag.png")
```

![Sample DAG Output](https://github.com/kayjan/bigtree/raw/master/assets/demo_dag.png)
![Sample DAG Output](https://github.com/kayjan/bigtree/raw/master/assets/docs/demo_dag.png)

2. **From *list***

Expand Down
Binary file removed assets/binarytree.png
Binary file not shown.
Binary file added assets/construct_binarytree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/demo.png
Binary file not shown.
Binary file removed assets/demo_binarytree.png
Binary file not shown.
Binary file removed assets/demo_dag.png
Binary file not shown.
Binary file removed assets/demo_tree.png
Binary file not shown.
Binary file added assets/docs/demo_binarytree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/docs/demo_dag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/docs/demo_dot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added assets/docs/demo_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
2 changes: 1 addition & 1 deletion bigtree/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.14.7"
__version__ = "0.14.8"

from bigtree.binarytree.construct import list_to_binarytree
from bigtree.dag.construct import dataframe_to_dag, dict_to_dag, list_to_dag
Expand Down
4 changes: 2 additions & 2 deletions bigtree/binarytree/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def list_to_binarytree(
├── 6
└── 7
>>> graph = tree_to_dot(root, node_colour="gold")
>>> graph.write_png("assets/binarytree.png")
>>> graph.write_png("assets/construct_binarytree.png")

.. image:: https://github.com/kayjan/bigtree/raw/master/assets/binarytree.png
.. image:: https://github.com/kayjan/bigtree/raw/master/assets/construct_binarytree.png

Args:
heapq_list (List[int]): list containing integer node names, ordered in heapq fashion
Expand Down
5 changes: 3 additions & 2 deletions bigtree/dag/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,12 @@ def dag_to_dot(
>>> 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("tree_dag.png")
>>> dag_graph.write_dot("tree_dag.dot")
>>> dag_graph.write_png("assets/docstr/tree_dag.png")
>>> dag_graph.write_dot("assets/docstr/tree_dag.dot")

Export to string

Expand Down
6 changes: 3 additions & 3 deletions bigtree/tree/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def add_path_to_tree(
All paths should start from the same root node.
- For example: Path strings should be "a/b", "a/c", "a/b/d" etc., and should not start with another root node.

>>> from bigtree import add_path_to_tree
>>> from bigtree import add_path_to_tree, Node
>>> root = Node("a")
>>> add_path_to_tree(root, "a/b/c")
Node(/a/b/c, )
Expand Down Expand Up @@ -249,7 +249,7 @@ def add_dataframe_to_tree_by_path(
- For example: Path strings should be "a/b", "a/c", "a/b/d" etc. and should not start with another root node.

>>> import pandas as pd
>>> from bigtree import add_dataframe_to_tree_by_path
>>> from bigtree import add_dataframe_to_tree_by_path, Node
>>> root = Node("a")
>>> path_data = pd.DataFrame([
... ["a", 90],
Expand Down Expand Up @@ -348,7 +348,7 @@ def add_dataframe_to_tree_by_name(
Note that if multiple nodes have the same name, attributes will be added to all nodes sharing same name.

>>> import pandas as pd
>>> from bigtree import add_dataframe_to_tree_by_name
>>> from bigtree import add_dataframe_to_tree_by_name, Node
>>> root = Node("a")
>>> b = Node("b", parent=root)
>>> name_data = pd.DataFrame([
Expand Down
19 changes: 10 additions & 9 deletions bigtree/tree/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def yield_tree(

**Printing tree**

>>> from bigtree import Node, print_tree
>>> from bigtree import Node, yield_tree
>>> root = Node("a", age=90)
>>> b = Node("b", age=65, parent=root)
>>> c = Node("c", age=60, parent=root)
Expand Down Expand Up @@ -638,11 +638,12 @@ def tree_to_dot(
>>> from IPython.display import Image, display
>>> plt = Image(graph.create_png())
>>> display(plt)
<IPython.core.display.Image object>

Export to image, dot file, etc.

>>> graph.write_png("tree.png")
>>> graph.write_dot("tree.dot")
>>> graph.write_png("assets/docstr/tree.png")
>>> graph.write_dot("assets/docstr/tree.dot")

Export to string

Expand Down Expand Up @@ -676,9 +677,9 @@ def tree_to_dot(
>>> d = CustomNode("d", node_shape="square", edge_label="child", parent=b)
>>> e = CustomNode("e", node_shape="square", edge_label="child", parent=b)
>>> graph = tree_to_dot(root, node_colour="gold", node_shape="diamond", node_attr="node_attr", edge_attr="edge_attr")
>>> graph.write_png("assets/custom_tree.png")
>>> graph.write_png("assets/export_tree_dot.png")

.. image:: https://github.com/kayjan/bigtree/raw/master/assets/custom_tree.png
.. image:: https://github.com/kayjan/bigtree/raw/master/assets/export_tree_dot.png

Alternative way to define node and edge attributes (using callable function)

Expand All @@ -694,9 +695,9 @@ def tree_to_dot(
>>> d = CustomNode("d", parent=b)
>>> e = CustomNode("e", parent=b)
>>> graph = tree_to_dot(root, node_colour="gold", node_attr=get_node_attribute)
>>> graph.write_png("assets/custom_tree_callable.png")
>>> graph.write_png("assets/export_tree_dot_callable.png")

.. image:: https://github.com/kayjan/bigtree/raw/master/assets/custom_tree_callable.png
.. image:: https://github.com/kayjan/bigtree/raw/master/assets/export_tree_dot_callable.png

Args:
tree (Node/List[Node]): tree or list of trees to be exported
Expand Down Expand Up @@ -803,8 +804,8 @@ def tree_to_pillow(

Export to image (PNG, JPG) file, etc.

>>> pillow_image.save("tree_pillow.png")
>>> pillow_image.save("tree_pillow.jpg")
>>> pillow_image.save("assets/docstr/tree_pillow.png")
>>> pillow_image.save("assets/docstr/tree_pillow.jpg")

Args:
tree (Node): tree to be exported
Expand Down
18 changes: 9 additions & 9 deletions bigtree/utils/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ def reingold_tilford(
x_offset (float): graph offset of x-coordinates
y_offset (float): graph offset of y-coordinates
"""
first_pass(tree_node, sibling_separation, subtree_separation)
x_adjustment = second_pass(tree_node, level_separation, x_offset, y_offset)
third_pass(tree_node, x_adjustment)
_first_pass(tree_node, sibling_separation, subtree_separation)
x_adjustment = _second_pass(tree_node, level_separation, x_offset, y_offset)
_third_pass(tree_node, x_adjustment)


def first_pass(
def _first_pass(
tree_node: T, sibling_separation: float, subtree_separation: float
) -> None:
"""
Expand Down Expand Up @@ -116,7 +116,7 @@ def first_pass(
"""
# Post-order iteration (LRN)
for child in tree_node.children:
first_pass(child, sibling_separation, subtree_separation)
_first_pass(child, sibling_separation, subtree_separation)

_x = 0.0
_mod = 0.0
Expand Down Expand Up @@ -274,7 +274,7 @@ def _get_subtree_shift(
return cum_shift + new_shift


def second_pass(
def _second_pass(
tree_node: T,
level_separation: float,
x_offset: float,
Expand Down Expand Up @@ -322,7 +322,7 @@ def second_pass(
if tree_node.children:
return max(
[
second_pass(
_second_pass(
child,
level_separation,
x_offset,
Expand All @@ -337,7 +337,7 @@ def second_pass(
return max(x_adjustment, -final_x)


def third_pass(tree_node: BaseNode, x_adjustment: float) -> None:
def _third_pass(tree_node: BaseNode, x_adjustment: float) -> None:
"""Adjust all x-coordinates by an adjustment value so that every x-coordinate is greater than or equal to 0.
Modifies tree in-place.

Expand All @@ -350,4 +350,4 @@ def third_pass(tree_node: BaseNode, x_adjustment: float) -> None:

# Pre-order iteration (NLR)
for child in tree_node.children:
third_pass(child, x_adjustment)
_third_pass(child, x_adjustment)
12 changes: 6 additions & 6 deletions bigtree/workflows/app_todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from typing import Any, List, Union

from bigtree.node.node import Node
from bigtree.tree.construct import dict_to_tree
from bigtree.tree.export import print_tree, tree_to_dict
from bigtree.tree.construct import nested_dict_to_tree
from bigtree.tree.export import print_tree, tree_to_nested_dict
from bigtree.tree.search import find_child_by_name, find_name

logging.getLogger(__name__).addHandler(logging.NullHandler())
Expand Down Expand Up @@ -72,8 +72,8 @@ class AppToDo:

*Exporting and Importing List*

>>> app.save("list.json")
>>> app2 = AppToDo.load("list.json")
>>> app.save("assets/docstr/list.json")
>>> app2 = AppToDo.load("assets/docstr/list.json")
>>> app2.show()
To Do App
├── General
Expand Down Expand Up @@ -242,7 +242,7 @@ def load(json_path: str) -> AppToDo:
with open(json_path, "r") as fp:
app_dict = json.load(fp)
_app = AppToDo("dummy")
AppToDo.__setattr__(_app, "_root", dict_to_tree(app_dict["root"]))
AppToDo.__setattr__(_app, "_root", nested_dict_to_tree(app_dict["root"]))
return _app

def save(self, json_path: str) -> None:
Expand All @@ -254,7 +254,7 @@ def save(self, json_path: str) -> None:
if not json_path.endswith(".json"):
raise ValueError("Path should end with .json")

node_dict = tree_to_dict(self._root, all_attrs=True)
node_dict = tree_to_nested_dict(self._root, all_attrs=True)
app_dict = {"root": node_dict}
with open(json_path, "w") as fp:
json.dump(app_dict, fp)
4 changes: 2 additions & 2 deletions docs/source/bigtree/dag/construct.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Construct Directed Acyclic Graph (DAG) from list, dictionary, and pandas DataFra
These functions are not standalone functions.
Under the hood, they have the following dependency,

.. image:: https://github.com/kayjan/bigtree/raw/master/assets/dag_construct.png
:target: https://github.com/kayjan/bigtree/raw/master/assets/dag_construct.png
.. image:: https://github.com/kayjan/bigtree/raw/master/assets/sphinx/dag_construct.png
:target: https://github.com/kayjan/bigtree/raw/master/assets/sphinx/dag_construct.png
:alt: DAG Constructor Dependency Diagram

.. automodule:: bigtree.dag.construct
Expand Down
4 changes: 2 additions & 2 deletions docs/source/bigtree/tree/construct.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ To add attributes to existing tree,
These functions are not standalone functions.
Under the hood, they have the following dependency,

.. image:: https://github.com/kayjan/bigtree/raw/master/assets/tree_construct.png
:target: https://github.com/kayjan/bigtree/raw/master/assets/tree_construct.png
.. image:: https://github.com/kayjan/bigtree/raw/master/assets/sphinx/tree_construct.png
:target: https://github.com/kayjan/bigtree/raw/master/assets/sphinx/tree_construct.png
:alt: Tree Constructor Dependency Diagram

.. automodule:: bigtree.tree.construct
Expand Down
8 changes: 4 additions & 4 deletions docs/source/bigtree/tree/modify.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ All other methods calls these 2 methods directly.
Tree Modification Illustration
----------------------------------

.. image:: https://github.com/kayjan/bigtree/raw/master/assets/modify_shift_and_copy.png
:target: https://github.com/kayjan/bigtree/raw/master/assets/modify_shift_and_copy.png
.. image:: https://github.com/kayjan/bigtree/raw/master/assets/sphinx/modify_shift_and_copy.png
:target: https://github.com/kayjan/bigtree/raw/master/assets/sphinx/modify_shift_and_copy.png
:width: 80%
:align: center
:alt: Shift and Copy Example
Expand Down Expand Up @@ -126,8 +126,8 @@ Tree Modification Illustration
- "/a/b/c"
- Shift/copy node `c`, skip if "/a/c" cannot be found

.. image:: https://github.com/kayjan/bigtree/raw/master/assets/modify_advanced.png
:target: https://github.com/kayjan/bigtree/raw/master/assets/modify_advanced.png
.. image:: https://github.com/kayjan/bigtree/raw/master/assets/sphinx/modify_advanced.png
:target: https://github.com/kayjan/bigtree/raw/master/assets/sphinx/modify_advanced.png
:alt: Advanced Shift Example

.. list-table:: Sample Tree Modification (Advanced)
Expand Down
8 changes: 8 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@
"myst_parser",
"sphinxemoji.sphinxemoji",
"sphinx.ext.mathjax",
"sphinx.ext.doctest",
]
autodoc_default_options = {"autosummary": True}
sphinxemoji_style = "twemoji"
myst_enable_extensions = [
"attrs_block",
]
base_dir = "/".join(os.getcwd().split("/")[:-2])
doctest_global_setup = f"""
import os
os.chdir("{base_dir}")
if not os.path.exists("assets/docstr"):
os.mkdir("assets/docstr")
"""

templates_path = ["_templates"]
exclude_patterns = []
Expand Down
Loading