Skip to content

Commit

Permalink
Merge pull request #84 from kayjan/v0.12.0
Browse files Browse the repository at this point in the history
V0.12.0
  • Loading branch information
Kay Jan authored Sep 9, 2023
2 parents 6ae4805 + 9b0fc0b commit a8911d6
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 81 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ 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.12.0] - 2023-09-09
### Changed
- Tree/DAG Constructor, Tree/DAG Exporter: Make `pandas` optional dependency.
### Fixed
- Misc: Fixed Calendar workflow to throw error when `to_dataframe` method is called on empty calendar.
- Tree/DAGNode Exporter, Tree Helper, Tree Search: Relax type hinting using TypeVar.

## [0.11.0] - 2023-09-08
### Added
- Tree Helper: Pruning tree to allow pruning by `prune_path` and `max_depth`.
Expand Down Expand Up @@ -303,6 +310,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Utility Iterator: Tree traversal methods.
- Workflow To Do App: Tree use case with to-do list implementation.

[0.12.0]: https://github.com/kayjan/bigtree/compare/0.11.0...0.12.0
[0.11.0]: https://github.com/kayjan/bigtree/compare/0.10.3...0.11.0
[0.10.3]: https://github.com/kayjan/bigtree/compare/0.10.2...0.10.3
[0.10.2]: https://github.com/kayjan/bigtree/compare/0.10.1...0.10.2
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ To install `bigtree`, run the following line in command prompt:
$ pip install bigtree
```

If tree needs to use pandas methods, it requires additional dependencies.
Run the following line in command prompt:

```shell
$ pip install 'bigtree[pandas]'
```

If tree needs to be exported to image, it requires additional dependencies.
Run the following lines in command prompt:

Expand All @@ -110,6 +117,12 @@ $ brew install gprof2dot # for MacOS
$ conda install graphviz # for Windows
```

Alternatively, install all optional dependencies with the following line in command prompt:

```shell
$ pip install 'bigtree[all]'
```

----

## Tree Demonstration
Expand Down
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.11.0"
__version__ = "0.12.0"

from bigtree.binarytree.construct import list_to_binarytree
from bigtree.dag.construct import dataframe_to_dag, dict_to_dag, list_to_dag
Expand Down
12 changes: 10 additions & 2 deletions bigtree/dag/construct.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from typing import Any, Dict, List, Tuple, Type
from __future__ import annotations

import pandas as pd
from typing import Any, Dict, List, Tuple, Type

from bigtree.node.dagnode import DAGNode
from bigtree.utils.exceptions import optional_dependencies_pandas

try:
import pandas as pd
except ImportError: # pragma: no cover
pd = None

__all__ = ["list_to_dag", "dict_to_dag", "dataframe_to_dag"]


@optional_dependencies_pandas
def list_to_dag(
relations: List[Tuple[str, str]],
node_type: Type[DAGNode] = DAGNode,
Expand Down Expand Up @@ -85,6 +92,7 @@ def dict_to_dag(
)


@optional_dependencies_pandas
def dataframe_to_dag(
data: pd.DataFrame,
child_col: str = "",
Expand Down
42 changes: 27 additions & 15 deletions bigtree/dag/export.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
from typing import Any, Dict, List, Tuple, Union
from __future__ import annotations

import pandas as pd
from typing import Any, Dict, List, Tuple, TypeVar, Union

from bigtree.node.dagnode import DAGNode
from bigtree.utils.exceptions import (
optional_dependencies_image,
optional_dependencies_pandas,
)
from bigtree.utils.iterators import dag_iterator

try:
import pandas as pd
except ImportError: # pragma: no cover
pd = None

try:
import pydot
except ImportError: # pragma: no cover
pydot = None

__all__ = ["dag_to_list", "dag_to_dict", "dag_to_dataframe", "dag_to_dot"]


T = TypeVar("T", bound=DAGNode)


def dag_to_list(
dag: DAGNode,
dag: T,
) -> List[Tuple[str, str]]:
"""Export DAG to list of tuple containing parent-child names
Expand All @@ -35,7 +52,7 @@ def dag_to_list(


def dag_to_dict(
dag: DAGNode,
dag: T,
parent_key: str = "parents",
attr_dict: Dict[str, str] = {},
all_attrs: bool = False,
Expand Down Expand Up @@ -95,8 +112,9 @@ def dag_to_dict(
return data_dict


@optional_dependencies_pandas
def dag_to_dataframe(
dag: DAGNode,
dag: T,
name_col: str = "name",
parent_col: str = "parent",
attr_dict: Dict[str, str] = {},
Expand Down Expand Up @@ -160,16 +178,17 @@ def dag_to_dataframe(
return pd.DataFrame(data_list).drop_duplicates().reset_index(drop=True)


def dag_to_dot( # type: ignore[no-untyped-def]
dag: Union[DAGNode, List[DAGNode]],
@optional_dependencies_image("pydot")
def dag_to_dot(
dag: Union[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:
r"""Export DAG tree or list of DAG trees to image.
Note that node names must be unique.
Posible node attributes include style, fillcolor, shape.
Expand Down Expand Up @@ -208,13 +227,6 @@ def dag_to_dot( # type: ignore[no-untyped-def]
Returns:
(pydot.Dot)
"""
try:
import pydot
except ImportError: # pragma: no cover
raise ImportError(
"pydot not available. Please perform a\n\npip install 'bigtree[image]'\n\nto install required dependencies"
)

# Get style
if bg_colour:
graph_style = dict(bgcolor=bg_colour)
Expand Down
19 changes: 16 additions & 3 deletions bigtree/tree/construct.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from __future__ import annotations

import re
from collections import OrderedDict
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type

import pandas as pd

from bigtree.node.node import Node
from bigtree.tree.export import tree_to_dataframe
from bigtree.tree.search import find_child_by_name, find_name
from bigtree.utils.exceptions import DuplicatedNodeError, TreeError
from bigtree.utils.exceptions import (
DuplicatedNodeError,
TreeError,
optional_dependencies_pandas,
)

try:
import pandas as pd
except ImportError: # pragma: no cover
pd = None

__all__ = [
"add_path_to_tree",
Expand Down Expand Up @@ -172,6 +181,7 @@ def add_dict_to_tree_by_path(
return tree_root


@optional_dependencies_pandas
def add_dict_to_tree_by_name(
tree: Node, name_attrs: Dict[str, Dict[str, Any]], join_type: str = "left"
) -> Node:
Expand Down Expand Up @@ -320,6 +330,7 @@ def add_dataframe_to_tree_by_path(
return tree_root


@optional_dependencies_pandas
def add_dataframe_to_tree_by_name(
tree: Node,
data: pd.DataFrame,
Expand Down Expand Up @@ -541,6 +552,7 @@ def list_to_tree(
return root_node


@optional_dependencies_pandas
def list_to_tree_by_relation(
relations: Iterable[Tuple[str, str]],
allow_duplicates: bool = False,
Expand Down Expand Up @@ -587,6 +599,7 @@ def list_to_tree_by_relation(
)


@optional_dependencies_pandas
def dict_to_tree(
path_attrs: Dict[str, Any],
sep: str = "/",
Expand Down
Loading

0 comments on commit a8911d6

Please sign in to comment.