Skip to content

InternalNode

InternalNode

Bases: BaseNode, ABC

Abstract base for all internal nodes.

Attributes:

Name Type Description
pivot_idx int

Index of the feature this node acts on.

Source code in imote/nodes/internal_node.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class InternalNode(BaseNode, ABC):
    """Abstract base for all internal nodes.

    Attributes:
        pivot_idx: Index of the feature this node acts on.
    """

    pivot_idx: int

    def __init__(self, indices: np.ndarray, y_res: np.ndarray, pivot_idx: int):
        """
        Args:
            indices: Row indices of the samples belonging to this node.
            y_res: Residuals of the target variable at this node.
            pivot_idx: Index of the feature this node acts on.
        """
        super().__init__(indices, y_res)
        self.pivot_idx = pivot_idx

    def get_minimal_label(self, fmt: LabelFormatter) -> str:
        """Returns a short display label for this node.

        Returns:
            String of minimal label, used when space is limited.
        """
        return fmt.feature(self.pivot_idx)

    @abstractmethod
    def set_children(self, children: List[BaseNode]):
        """Replaces this node's children.

        Args:
            children: New child node(s), in the same order as
                returned by get_children().
        """
        pass

__init__(indices: np.ndarray, y_res: np.ndarray, pivot_idx: int)

Parameters:

Name Type Description Default
indices ndarray

Row indices of the samples belonging to this node.

required
y_res ndarray

Residuals of the target variable at this node.

required
pivot_idx int

Index of the feature this node acts on.

required
Source code in imote/nodes/internal_node.py
18
19
20
21
22
23
24
25
26
def __init__(self, indices: np.ndarray, y_res: np.ndarray, pivot_idx: int):
    """
    Args:
        indices: Row indices of the samples belonging to this node.
        y_res: Residuals of the target variable at this node.
        pivot_idx: Index of the feature this node acts on.
    """
    super().__init__(indices, y_res)
    self.pivot_idx = pivot_idx

get_minimal_label(fmt: LabelFormatter) -> str

Returns a short display label for this node.

Returns:

Type Description
str

String of minimal label, used when space is limited.

Source code in imote/nodes/internal_node.py
28
29
30
31
32
33
34
def get_minimal_label(self, fmt: LabelFormatter) -> str:
    """Returns a short display label for this node.

    Returns:
        String of minimal label, used when space is limited.
    """
    return fmt.feature(self.pivot_idx)

set_children(children: List[BaseNode]) abstractmethod

Replaces this node's children.

Parameters:

Name Type Description Default
children List[BaseNode]

New child node(s), in the same order as returned by get_children().

required
Source code in imote/nodes/internal_node.py
36
37
38
39
40
41
42
43
44
@abstractmethod
def set_children(self, children: List[BaseNode]):
    """Replaces this node's children.

    Args:
        children: New child node(s), in the same order as
            returned by get_children().
    """
    pass

LinearNode

Bases: InternalNode

LIN: Single child node that applies a linear model..

Attributes:

Name Type Description
linear_model SimpleLinearNodeModel

Single-feature linear model applied at this node.

child BaseNode

The node's single child.

Source code in imote/nodes/internal_node.py
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 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
 99
100
101
102
103
104
105
106
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
132
@BaseNode.register
class LinearNode(InternalNode):
    """LIN: Single child node that applies a linear model..

    Attributes:
        linear_model: Single-feature linear model applied at this node.
        child: The node's single child.
    """

    linear_model: SimpleLinearNodeModel
    child: BaseNode

    def __init__(self, indices: np.ndarray, y_res: np.ndarray, pivot_idx: int,
                 linear_model: SimpleLinearNodeModel, child: BaseNode):
        """
        Args:
            indices: Row indices of the samples associated with this node.
            y_res: Residuals of the target variable at this node.
            pivot_idx: Index of the feature the linear term is based on.
            linear_model: One dimensional linear model applied at this node.
            child: The node's single child.
        """
        super().__init__(indices, y_res, pivot_idx)
        self.linear_model = linear_model
        self.child = child

    def get_children(self) -> List[BaseNode]:
        """Returns this node's single child.

        Returns:
            List containing the child node.
        """
        return [self.child]

    def set_children(self, children: List[BaseNode]):
        """Replaces this node's child.

        Args:
            children: List containing exactly one new child node.
        """
        self.child = children[0]

    def get_label(self, fmt: LabelFormatter) -> str:
        """Returns the full display label for this node.

        Returns:
            Label of the form "LIN\\n<feature>".
        """
        return f"LIN\n{fmt.feature(self.pivot_idx)}"

    def get_description(self, fmt: LabelFormatter) -> str:
        """Returns the linear model of this node."""
        return self.linear_model.get_label(fmt)

    def to_dict(self) -> dict:
        """Serializes the node to a dictionary.

        Returns:
            Dictionary with base attributes plus pivot_idx, linear_model,
            and the child's class name and serialized form.
        """
        node_dict = super().to_dict()
        node_dict.update({
            "pivot_idx": self.pivot_idx,
            "linear_model": self.linear_model.to_dict(),
            "child_class": self.child.__class__.__name__,
            "child_dict": self.child.to_dict()
        })
        return node_dict

    @classmethod
    def from_dict(cls, dic: dict) -> "LinearNode":
        """Reconstructs a LinearNode instance, including its child.

        Args:
            dic: Dictionary previously produced by to_dict().

        Returns:
            A new LinearNode instance with its child restored.
        """
        child_class = cls.class_registry[dic["child_class"]]

        obj = super().from_dict(dic)
        obj.pivot_idx = dic["pivot_idx"]
        obj.linear_model = SimpleLinearNodeModel.from_dict(dic["linear_model"])
        obj.child = child_class.from_dict(dic["child_dict"])
        return obj

__init__(indices: np.ndarray, y_res: np.ndarray, pivot_idx: int, linear_model: SimpleLinearNodeModel, child: BaseNode)

Parameters:

Name Type Description Default
indices ndarray

Row indices of the samples associated with this node.

required
y_res ndarray

Residuals of the target variable at this node.

required
pivot_idx int

Index of the feature the linear term is based on.

required
linear_model SimpleLinearNodeModel

One dimensional linear model applied at this node.

required
child BaseNode

The node's single child.

required
Source code in imote/nodes/internal_node.py
58
59
60
61
62
63
64
65
66
67
68
69
70
def __init__(self, indices: np.ndarray, y_res: np.ndarray, pivot_idx: int,
             linear_model: SimpleLinearNodeModel, child: BaseNode):
    """
    Args:
        indices: Row indices of the samples associated with this node.
        y_res: Residuals of the target variable at this node.
        pivot_idx: Index of the feature the linear term is based on.
        linear_model: One dimensional linear model applied at this node.
        child: The node's single child.
    """
    super().__init__(indices, y_res, pivot_idx)
    self.linear_model = linear_model
    self.child = child

get_children() -> List[BaseNode]

Returns this node's single child.

Returns:

Type Description
List[BaseNode]

List containing the child node.

Source code in imote/nodes/internal_node.py
72
73
74
75
76
77
78
def get_children(self) -> List[BaseNode]:
    """Returns this node's single child.

    Returns:
        List containing the child node.
    """
    return [self.child]

set_children(children: List[BaseNode])

Replaces this node's child.

Parameters:

Name Type Description Default
children List[BaseNode]

List containing exactly one new child node.

required
Source code in imote/nodes/internal_node.py
80
81
82
83
84
85
86
def set_children(self, children: List[BaseNode]):
    """Replaces this node's child.

    Args:
        children: List containing exactly one new child node.
    """
    self.child = children[0]

get_label(fmt: LabelFormatter) -> str

Returns the full display label for this node.

Returns:

Type Description
str

Label of the form "LIN\n".

Source code in imote/nodes/internal_node.py
88
89
90
91
92
93
94
def get_label(self, fmt: LabelFormatter) -> str:
    """Returns the full display label for this node.

    Returns:
        Label of the form "LIN\\n<feature>".
    """
    return f"LIN\n{fmt.feature(self.pivot_idx)}"

get_description(fmt: LabelFormatter) -> str

Returns the linear model of this node.

Source code in imote/nodes/internal_node.py
96
97
98
def get_description(self, fmt: LabelFormatter) -> str:
    """Returns the linear model of this node."""
    return self.linear_model.get_label(fmt)

to_dict() -> dict

Serializes the node to a dictionary.

Returns:

Type Description
dict

Dictionary with base attributes plus pivot_idx, linear_model,

dict

and the child's class name and serialized form.

Source code in imote/nodes/internal_node.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def to_dict(self) -> dict:
    """Serializes the node to a dictionary.

    Returns:
        Dictionary with base attributes plus pivot_idx, linear_model,
        and the child's class name and serialized form.
    """
    node_dict = super().to_dict()
    node_dict.update({
        "pivot_idx": self.pivot_idx,
        "linear_model": self.linear_model.to_dict(),
        "child_class": self.child.__class__.__name__,
        "child_dict": self.child.to_dict()
    })
    return node_dict

from_dict(dic: dict) -> LinearNode classmethod

Reconstructs a LinearNode instance, including its child.

Parameters:

Name Type Description Default
dic dict

Dictionary previously produced by to_dict().

required

Returns:

Type Description
LinearNode

A new LinearNode instance with its child restored.

Source code in imote/nodes/internal_node.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@classmethod
def from_dict(cls, dic: dict) -> "LinearNode":
    """Reconstructs a LinearNode instance, including its child.

    Args:
        dic: Dictionary previously produced by to_dict().

    Returns:
        A new LinearNode instance with its child restored.
    """
    child_class = cls.class_registry[dic["child_class"]]

    obj = super().from_dict(dic)
    obj.pivot_idx = dic["pivot_idx"]
    obj.linear_model = SimpleLinearNodeModel.from_dict(dic["linear_model"])
    obj.child = child_class.from_dict(dic["child_dict"])
    return obj