Skip to content

BaseAdapter

ADAPTERS_REGISTRY: dict[str, type[BaseAdapter]] = {} module-attribute

Maps adapter names to adapter classes, populated via register_adapter().

BaseAdapter

Bases: ABC

Abstract base class for adapters that build a VizTree from a model.

An adapter translates a fitted model from a specific source into the BaseNode linked tree structure that VizTree expects.

Source code in imote/adapters/base_adapter.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
class BaseAdapter(ABC):
    """Abstract base class for adapters that build a VizTree from a model.

    An adapter translates a fitted model from a specific source
    into the BaseNode linked tree structure that VizTree expects.
    """

    @staticmethod
    @abstractmethod
    def build_root_node(X_train: np.ndarray, y_train: np.ndarray, model) -> BaseNode:
        """Builds the BaseNode linked tree representing a fitted model.

        Args:
            X_train: Training feature matrix the model was fit on.
            y_train: Training target values the model was fit on.
            model: The fitted model, in whatever format this adapter
                supports (as returned by load_model()).

        Returns:
            Root BaseNode of the reconstructed linked tree.
        """
        pass

    @staticmethod
    @abstractmethod
    def load_model(model_path: str):
        """Loads a fitted model from disk.

        Args:
            model_path: Path to the serialized model file.

        Returns:
            The loaded model, in the format this adapter's
            build_root_node() expects.
        """
        pass

    @staticmethod
    def predict(X: np.ndarray, model) -> np.ndarray | None:
        """Predicts target values directly from the underlying model, if possible.

        This is optional: an adapter can override it to use the
        original model's own prediction logic (e.g. for speed or
        numerical parity). If not overridden, VizTree instead computes
        predictions by traversing the built BaseNode linked tree.

        Args:
            X: Feature matrix to predict on.
            model: The fitted model, as returned by load_model().

        Returns:
            Array of predicted values, or None if not implemented.
        """
        return None

build_root_node(X_train: np.ndarray, y_train: np.ndarray, model) -> BaseNode abstractmethod staticmethod

Builds the BaseNode linked tree representing a fitted model.

Parameters:

Name Type Description Default
X_train ndarray

Training feature matrix the model was fit on.

required
y_train ndarray

Training target values the model was fit on.

required
model

The fitted model, in whatever format this adapter supports (as returned by load_model()).

required

Returns:

Type Description
BaseNode

Root BaseNode of the reconstructed linked tree.

Source code in imote/adapters/base_adapter.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@staticmethod
@abstractmethod
def build_root_node(X_train: np.ndarray, y_train: np.ndarray, model) -> BaseNode:
    """Builds the BaseNode linked tree representing a fitted model.

    Args:
        X_train: Training feature matrix the model was fit on.
        y_train: Training target values the model was fit on.
        model: The fitted model, in whatever format this adapter
            supports (as returned by load_model()).

    Returns:
        Root BaseNode of the reconstructed linked tree.
    """
    pass

load_model(model_path: str) abstractmethod staticmethod

Loads a fitted model from disk.

Parameters:

Name Type Description Default
model_path str

Path to the serialized model file.

required

Returns:

Type Description

The loaded model, in the format this adapter's

build_root_node() expects.

Source code in imote/adapters/base_adapter.py
46
47
48
49
50
51
52
53
54
55
56
57
58
@staticmethod
@abstractmethod
def load_model(model_path: str):
    """Loads a fitted model from disk.

    Args:
        model_path: Path to the serialized model file.

    Returns:
        The loaded model, in the format this adapter's
        build_root_node() expects.
    """
    pass

predict(X: np.ndarray, model) -> np.ndarray | None staticmethod

Predicts target values directly from the underlying model, if possible.

This is optional: an adapter can override it to use the original model's own prediction logic (e.g. for speed or numerical parity). If not overridden, VizTree instead computes predictions by traversing the built BaseNode linked tree.

Parameters:

Name Type Description Default
X ndarray

Feature matrix to predict on.

required
model

The fitted model, as returned by load_model().

required

Returns:

Type Description
ndarray | None

Array of predicted values, or None if not implemented.

Source code in imote/adapters/base_adapter.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@staticmethod
def predict(X: np.ndarray, model) -> np.ndarray | None:
    """Predicts target values directly from the underlying model, if possible.

    This is optional: an adapter can override it to use the
    original model's own prediction logic (e.g. for speed or
    numerical parity). If not overridden, VizTree instead computes
    predictions by traversing the built BaseNode linked tree.

    Args:
        X: Feature matrix to predict on.
        model: The fitted model, as returned by load_model().

    Returns:
        Array of predicted values, or None if not implemented.
    """
    return None

register_adapter(name: str)

Class decorator that registers an adapter under a given name.

Parameters:

Name Type Description Default
name str

Name to register the adapter under.

required

Returns:

Type Description

A decorator that registers the decorated class and returns it

unchanged.

Source code in imote/adapters/base_adapter.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def register_adapter(name: str):
    """Class decorator that registers an adapter under a given name.

    Args:
        name: Name to register the adapter under.

    Returns:
        A decorator that registers the decorated class and returns it
        unchanged.
    """
    def decorator(adapter_cls):
        ADAPTERS_REGISTRY[name] = adapter_cls
        return adapter_cls
    return decorator