pdstools.adm.trees._nodes

Tree primitives: split parsing, node dataclass, traversal helpers.

Attributes

Classes

Split

A parsed tree split condition.

Node

A single node in an AGB tree.

Functions

parse_split(→ Split)

Parse a tree-split string into a Split.

_iter_nodes(→ collections.abc.Iterator[Node])

Yield every node in a tree in pre-order, root first.

_traverse(→ Any)

Walk a nested dict/list using the given key/index path; raise KeyError on miss.

Module Contents

SplitOperator
_SPLIT_OPERATORS: frozenset[str]
_SPLIT_RE
class Split

A parsed tree split condition.

variable

Predictor name being split on.

Type:

str

operator

Comparison operator: "<" and ">" for numeric thresholds, "==" for single-category equality, "in" for set membership, "is" for missing-value checks.

Type:

SplitOperator

value

Right-hand side of the split. float for numeric thresholds, tuple[str, ...] for in-splits, str for ==/is.

Type:

float | str | tuple[str, …]

raw

Original split string, useful for diagnostics or display.

Type:

str

variable: str
operator: SplitOperator
value: float | str | tuple[str, Ellipsis]
raw: str
property is_numeric: bool
Return type:

bool

property is_symbolic: bool
Return type:

bool

parse_split(raw: str) Split

Parse a tree-split string into a Split.

Examples

>>> parse_split("Age < 42.5").operator
'<'
>>> sorted(parse_split("Color in { red, blue }").value)
['blue', 'red']
>>> parse_split("Status is Missing").value
'Missing'
Parameters:

raw (str)

Return type:

Split

class Node

A single node in an AGB tree.

All nodes carry a score (the leaf prediction or root prior). Internal nodes additionally carry a parsed Split and a gain. Leaves have split=None and gain=0.0.

depth: int
score: float
is_leaf: bool
split: Split | None
gain: float
_iter_nodes(tree: dict, depth: int = 1) collections.abc.Iterator[Node]

Yield every node in a tree in pre-order, root first.

Single source of truth for tree traversal. All metric computations consume this iterator instead of reimplementing recursion.

Parameters:
Return type:

collections.abc.Iterator[Node]

_BOOSTER_PATHS: tuple[tuple[str | int, Ellipsis], Ellipsis] = (('model', 'boosters', 0, 'trees'), ('model', 'model', 'boosters', 0, 'trees'), ('model',...
_traverse(d: Any, path: tuple) Any

Walk a nested dict/list using the given key/index path; raise KeyError on miss.

Parameters:
Return type:

Any