Skip to content

xwhy.core.explainer

Base class for all xwhy explainers.

BaseExplainer

Bases: ABC

Abstract base class for all xwhy explainers.

This class provides the common interface shared by all explainers. Concrete explainers are responsible for initializing their own runtime state (models, providers, embeddings, etc.).

Source code in src/xwhy/core/explainer.py
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
45
46
47
48
49
50
51
52
class BaseExplainer(ABC):
    """Abstract base class for all xwhy explainers.

    This class provides the common interface shared by all explainers.
    Concrete explainers are responsible for initializing their own runtime
    state (models, providers, embeddings, etc.).
    """

    def __init__(
        self,
        config: ExplainerConfig | None = None,
    ) -> None:
        """Initialize the explainer.

        Args:
            config: Explainer configuration object.

        """
        self.config = config

    @abstractmethod
    def explain(
        self,
        instance: Any,  # noqa: ANN401
        **kwargs: Any,  # noqa: ANN401
    ) -> BaseXWhyResult:
        """Generate an explanation for the given input instance.

        Args:
            instance: Input instance to explain.
            **kwargs: Additional explainer-specific arguments.

        Returns:
            Structured explanation result.

        Raises:
            NotImplementedError:
                If the subclass does not implement this method.

        """
        raise NotImplementedError("Subclasses must implement explain method.")

__init__(config=None)

Initialize the explainer.

Parameters:

Name Type Description Default
config ExplainerConfig | None

Explainer configuration object.

None
Source code in src/xwhy/core/explainer.py
20
21
22
23
24
25
26
27
28
29
30
def __init__(
    self,
    config: ExplainerConfig | None = None,
) -> None:
    """Initialize the explainer.

    Args:
        config: Explainer configuration object.

    """
    self.config = config

explain(instance, **kwargs) abstractmethod

Generate an explanation for the given input instance.

Parameters:

Name Type Description Default
instance Any

Input instance to explain.

required
**kwargs Any

Additional explainer-specific arguments.

{}

Returns:

Type Description
BaseXWhyResult

Structured explanation result.

Raises:

Type Description
NotImplementedError

If the subclass does not implement this method.

Source code in src/xwhy/core/explainer.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@abstractmethod
def explain(
    self,
    instance: Any,  # noqa: ANN401
    **kwargs: Any,  # noqa: ANN401
) -> BaseXWhyResult:
    """Generate an explanation for the given input instance.

    Args:
        instance: Input instance to explain.
        **kwargs: Additional explainer-specific arguments.

    Returns:
        Structured explanation result.

    Raises:
        NotImplementedError:
            If the subclass does not implement this method.

    """
    raise NotImplementedError("Subclasses must implement explain method.")