Skip to content

Commit 1269263

Browse files
authored
Merge pull request #217 from kayjan/fix-nan
Fix nan
2 parents 1e4f882 + edb11c8 commit 1269263

8 files changed

+55
-5
lines changed

.pre-commit-config.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ repos:
1010
args: ["--pytest-test-first"]
1111
- id: requirements-txt-fixer
1212
- id: trailing-whitespace
13+
- repo: https://github.com/commitizen-tools/commitizen
14+
rev: v3.18.3
15+
hooks:
16+
- id: commitizen
1317
- repo: https://github.com/pycqa/isort
1418
rev: 5.13.2
1519
hooks:

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ 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+
9+
## [0.16.3] - 2024-03-14
810
### Added
911
- BaseNode: Add diameter property.
1012
- Misc: Testing to include benchmark timings for tree creation, compare benchmark tests across commits, reject pull request if benchmark tests fails.
1113
- Misc: Documentation to include benchmark results.
1214
### Changed
1315
- Misc: Documentation to enable zooming in of images, add navigation section headers, remove some meta tags.
1416
- Misc: Split up testing into multiple conftest files.
17+
### Fixed
18+
- Tree Constructor: Tree creation from dictionary adds None for empty attributes instead of np.nan.
19+
- [#216] Tree Exporter: `attr_omit_null` to handle nan/null values in addition to checking for None.
1520

1621
## [0.16.2] - 2024-02-06
1722
### Added
@@ -502,7 +507,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
502507
- Utility Iterator: Tree traversal methods.
503508
- Workflow To Do App: Tree use case with to-do list implementation.
504509

505-
[Unreleased]: https://github.com/kayjan/bigtree/compare/0.16.2...HEAD
510+
[Unreleased]: https://github.com/kayjan/bigtree/compare/0.16.3...HEAD
511+
[0.16.3]: https://github.com/kayjan/bigtree/compare/0.16.2...0.16.3
506512
[0.16.2]: https://github.com/kayjan/bigtree/compare/0.16.1...0.16.2
507513
[0.16.1]: https://github.com/kayjan/bigtree/compare/0.16.0...0.16.1
508514
[0.16.0]: https://github.com/kayjan/bigtree/compare/0.15.7...0.16.0

bigtree/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.16.2"
1+
__version__ = "0.16.3"
22

33
from bigtree.binarytree.construct import list_to_binarytree
44
from bigtree.dag.construct import dataframe_to_dag, dict_to_dag, list_to_dag

bigtree/tree/construct.py

+2
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def add_dict_to_tree_by_name(
233233

234234
# Convert dictionary to dataframe
235235
data = pd.DataFrame(name_attrs).T.rename_axis("NAME").reset_index()
236+
data = data.replace({float("nan"): None})
236237
return add_dataframe_to_tree_by_name(tree, data=data, join_type=join_type)
237238

238239

@@ -681,6 +682,7 @@ def dict_to_tree(
681682

682683
# Convert dictionary to dataframe
683684
data = pd.DataFrame(path_attrs).T.rename_axis("PATH").reset_index()
685+
data = data.replace({float("nan"): None})
684686
return dataframe_to_tree(
685687
data,
686688
sep=sep,

bigtree/tree/export.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import collections
4+
import math
45
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, TypeVar, Union
56
from urllib.request import urlopen
67

@@ -50,6 +51,20 @@
5051
T = TypeVar("T", bound=Node)
5152

5253

54+
def _isnull(value: Any) -> bool:
55+
"""Check if value is null
56+
57+
Args:
58+
value (Any): value to check
59+
60+
Returns:
61+
(bool)
62+
"""
63+
if not value or math.isnan(value):
64+
return True
65+
return False
66+
67+
5368
def print_tree(
5469
tree: T,
5570
node_name_or_path: str = "",
@@ -195,7 +210,7 @@ def print_tree(
195210
attr_str_list = [
196211
f"{attr_name}={_node.get_attr(attr_name)}"
197212
for attr_name in attr_list
198-
if _node.get_attr(attr_name)
213+
if not _isnull(_node.get_attr(attr_name))
199214
]
200215
else:
201216
attr_str_list = [

tests/tree/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def tree_node_negative_null_attr():
9393
c = Node("c", age=0)
9494
d = Node("d", age=1)
9595
e = Node("e", age=None)
96-
f = Node("f")
96+
f = Node("f", age=float("nan"))
9797
g = Node("g")
9898
h = Node("h")
9999

tests/tree/test_construct.py

+23
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,18 @@ def test_add_dict_to_tree_by_name_custom_node_type(self):
582582
assert_tree_structure_customnode_root_attr(root)
583583
assert_tree_structure_node_root(root)
584584

585+
def test_add_dict_to_tree_by_name_inconsistent_attributes(self):
586+
name_dict = {
587+
"a": {"age": 90},
588+
"b": {},
589+
"c": {"age": 60},
590+
}
591+
root = add_dict_to_tree_by_name(self.root, name_dict)
592+
expected_root_str = "a [age=90.0]\n" "├── b\n" "└── c [age=60.0]\n"
593+
assert_print_statement(
594+
print_tree, expected_root_str, root, all_attrs=True, max_depth=2
595+
)
596+
585597

586598
class TestAddDataFrameToTreeByPath(unittest.TestCase):
587599
def setUp(self):
@@ -1683,6 +1695,17 @@ def test_dict_to_tree_different_root_error():
16831695
root1=root1, root2=root2
16841696
)
16851697

1698+
@staticmethod
1699+
def test_dict_to_tree_inconsistent_attributes():
1700+
path_dict = {
1701+
"a": {"age": 90},
1702+
"a/b": {},
1703+
"a/c": {"age": 60},
1704+
}
1705+
root = dict_to_tree(path_dict)
1706+
expected_root_str = "a [age=90.0]\n" "├── b\n" "└── c [age=60.0]\n"
1707+
assert_print_statement(print_tree, expected_root_str, root, all_attrs=True)
1708+
16861709

16871710
class TestNestedDictToTree(unittest.TestCase):
16881711
def setUp(self):

tests/tree/test_export.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_print_tree_attr_omit_null_false(tree_node_negative_null_attr):
100100
"│ ├── g\n"
101101
"│ └── h\n"
102102
"└── c [age=0]\n"
103-
" └── f\n"
103+
" └── f [age=nan]\n"
104104
)
105105
assert_print_statement(
106106
print_tree,

0 commit comments

Comments
 (0)