Skip to content

effector.tree

Summary

The Tree classes provide a way to construct and manage a hierarchical tree structure. The subregions detected per feature are stored in a Tree object.

API Reference

effector.tree.Tree()

A class to represent a tree structure.

Methods:

Name Description
show_full_tree

Print the full tree structure.

show_level_stats

Print the heterogeneity drop at each level of the tree.

Source code in effector/tree.py
 7
 8
 9
10
def __init__(self):
    self.nodes = []
    self.node_dict = {}  # Fast lookup by name
    self.idx = 0

show_full_tree(scale_x_list=None)

Print the full tree structure.

Examples:

>>> tree.show_full_tree()
🌳 Full Tree Structure:
───────────────────────
x1 πŸ”Ή [id: 0 | heter: 0.50 | inst: 100 | w: 1.00]
    x2 β‰₯ 3.00 πŸ”Ή [id: 1 | heter: 0.30 | inst: 50 | w: 0.50]
    x2 < 3.00 πŸ”Ή [id: 2 | heter: 0.20 | inst: 50 | w: 0.50]
>>> tree.show_full_tree({"mean": 3, "std":2}, {"mean": 3, "std":3}, {"mean": 3, "std":2})
🌳 Full Tree Structure:
───────────────────────
x1 πŸ”Ή [id: 0 | heter: 0.50 | inst: 100 | w: 1.00]
    x2 β‰₯ 12.00 πŸ”Ή [id: 1 | heter: 0.30 | inst: 50 | w: 0.50]
    x2 < 12.00 πŸ”Ή [id: 2 | heter: 0.20 | inst: 50 | w: 0.50]

Parameters:

Name Type Description Default
scale_x_list

A list of dictionaries with the mean and standard deviation for each feature. - Example: [{"mean": 3, "std":2}, {"mean": 3, "std":2}, ...]

None
Source code in effector/tree.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def show_full_tree(self, scale_x_list=None):
    """Print the full tree structure.

    Examples:
        >>> tree.show_full_tree()
        🌳 Full Tree Structure:
        ───────────────────────
        x1 πŸ”Ή [id: 0 | heter: 0.50 | inst: 100 | w: 1.00]
            x2 β‰₯ 3.00 πŸ”Ή [id: 1 | heter: 0.30 | inst: 50 | w: 0.50]
            x2 < 3.00 πŸ”Ή [id: 2 | heter: 0.20 | inst: 50 | w: 0.50]

        >>> tree.show_full_tree({"mean": 3, "std":2}, {"mean": 3, "std":3}, {"mean": 3, "std":2})
        🌳 Full Tree Structure:
        ───────────────────────
        x1 πŸ”Ή [id: 0 | heter: 0.50 | inst: 100 | w: 1.00]
            x2 β‰₯ 12.00 πŸ”Ή [id: 1 | heter: 0.30 | inst: 50 | w: 0.50]
            x2 < 12.00 πŸ”Ή [id: 2 | heter: 0.20 | inst: 50 | w: 0.50]

    Args:
        scale_x_list: A list of dictionaries with the mean and standard deviation for each feature.
                           - Example: [{"mean": 3, "std":2}, {"mean": 3, "std":2}, ...]


    """
    self.update_display_names(scale_x_list)
    root = self.get_root()
    if root is None:
        print("Tree is empty.")
        return

    print("🌳 Full Tree Structure:")
    print("───────────────────────")
    self._recursive_print_full_tree(root)

show_level_stats()

Print the heterogeneity drop at each level of the tree.

Examples:

>>> tree.show_level_stats()
🌳 Tree Summary:
─────────────────
Level 0πŸ”Ήheter: 0.50
    Level 1πŸ”Ήheter: 0.25 | πŸ”»0.25 (50.00%)
Source code in effector/tree.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def show_level_stats(self):
    """Print the heterogeneity drop at each level of the tree.

    Examples:
        >>> tree.show_level_stats()
        🌳 Tree Summary:
        ─────────────────
        Level 0πŸ”Ήheter: 0.50
            Level 1πŸ”Ήheter: 0.25 | πŸ”»0.25 (50.00%)
    """

    max_level = max((node.info["level"] for node in self.nodes), default=0)
    prev_heter = 0
    print("🌳 Tree Summary:")
    print("─────────────────")
    for lev in range(max_level + 1):
        indent = "    " * lev
        stats = self.get_level_stats(lev)
        if lev == 0:
            print(f"Level {lev}πŸ”Ήheter: {stats['heterogeneity']:.2f}")
        else:
            drop = prev_heter - stats["heterogeneity"]
            perc = 100 * drop / prev_heter if prev_heter else 0
            print(f"{indent}Level {lev}πŸ”Ήheter: {stats['heterogeneity']:.2f} | πŸ”»{drop:.2f} ({perc:.2f}%)")
        prev_heter = stats["heterogeneity"]