Skip to content

Commit 0cd9adb

Browse files
committed
feat: make tree diff about adding suffixes for path changes
1 parent 8755db2 commit 0cd9adb

File tree

2 files changed

+17
-55
lines changed

2 files changed

+17
-55
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Changed:
9-
- Tree Helper: Get tree diff logic to be faster to compare all attribute list and data at once.
9+
- Tree Helper: Get tree diff logic to be faster to compare all attribute list and data at once (for attr diff).
10+
- Tree Helper: Get tree diff logic to be faster to add suffix at the end (for path diff).
1011

1112
## [0.22.2] - 2024-11-11
1213
### Added:

bigtree/tree/helper.py

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -490,60 +490,14 @@ def get_tree_diff(
490490
moved_from_ind = [name in names_added for name in names_removed]
491491
moved_to_ind = [name in names_removed for name in names_added]
492492

493-
def add_suffix_to_path(
494-
_data: pd.DataFrame, _condition: pd.Series, _original_name: str, _suffix: str
495-
) -> None:
496-
"""Add suffix to path string, in-place
497-
498-
Args:
499-
_data (pd.DataFrame): original data with path column
500-
_condition (pd.Series): whether to add suffix, contains True/False values
501-
_original_name (str): path prefix to add suffix to
502-
_suffix (str): suffix to add to path column
503-
504-
Returns:
505-
(pd.DataFrame)
506-
"""
507-
_data.iloc[_condition.values, _data.columns.get_loc(path_col)] = _data.iloc[
508-
_condition.values, _data.columns.get_loc(path_col)
509-
].str.replace(_original_name, f"{_original_name} ({_suffix})", regex=True)
510-
511-
def add_suffix_to_data(
512-
_data: pd.DataFrame,
513-
paths_diff: List[str],
514-
move_ind: List[bool],
515-
suffix_general: str,
516-
suffix_move: str,
517-
suffix_not_moved: str,
518-
) -> None:
519-
"""Add suffix to data, in-place
520-
521-
Args:
522-
_data (pd.DataFrame): original data with path column
523-
paths_diff (List[str]): list of paths that were modified (e.g., added/removed)
524-
move_ind (List[bool]): move indicator to indicate path was moved instead of added/removed
525-
suffix_general (str): path suffix for general case
526-
suffix_move (str): path suffix if path was moved
527-
suffix_not_moved (str): path suffix if path is not moved (e.g., added/removed)
528-
"""
529-
for _path_diff, _move_ind in zip(paths_diff, move_ind):
530-
if not detail:
531-
suffix = suffix_general
532-
else:
533-
suffix = suffix_move if _move_ind else suffix_not_moved
534-
condition_node_modified = data_compare[path_col].str.endswith(
535-
_path_diff
536-
) | data_compare[path_col].str.contains(_path_diff + tree_sep)
537-
add_suffix_to_path(
538-
data_compare, condition_node_modified, _path_diff, suffix
539-
)
540-
541-
add_suffix_to_data(
542-
data_compare, paths_removed, moved_from_ind, "-", "moved from", "removed"
543-
)
544-
add_suffix_to_data(
545-
data_compare, paths_added, moved_to_ind, "+", "moved to", "added"
546-
)
493+
path_removed_to_name = {
494+
k: "-" if not detail else ("moved from" if v else "removed")
495+
for k, v in zip(paths_removed, moved_from_ind)
496+
}
497+
path_added_to_name = {
498+
k: "+" if not detail else ("moved to" if v else "added")
499+
for k, v in zip(paths_added, moved_to_ind)
500+
}
547501

548502
# Check tree attribute difference
549503
dict_attr_diff: Dict[str, Dict[str, Any]] = {}
@@ -584,6 +538,13 @@ def add_suffix_to_data(
584538
tree_diff = construct.dataframe_to_tree(
585539
data_compare, node_type=tree.__class__, sep=tree.sep
586540
)
541+
for path in sorted(path_removed_to_name, reverse=True):
542+
_node = search.find_full_path(tree_diff, path)
543+
_node.name += f""" ({path_removed_to_name[path]})"""
544+
for path in sorted(path_added_to_name, reverse=True):
545+
_node = search.find_full_path(tree_diff, path)
546+
_node.name += f""" ({path_added_to_name[path]})"""
547+
587548
# Handle tree attribute difference
588549
if dict_attr_diff:
589550
tree_diff = construct.add_dict_to_tree_by_path(tree_diff, dict_attr_diff)

0 commit comments

Comments
 (0)