xai4tsc.xai.base
Explainer ABCs: ExplainerBase, GradientExplainer, and friends.
Classes
Base class for all XAI explainer methods. |
|
Base class for gradient-based attribution methods (Captum). |
|
Base class for perturbation-based attribution methods. |
|
Base class for surrogate / proxy model attribution methods. |
|
Base class for explainers that wrap and extend another explainer. |
Module Contents
- class xai4tsc.xai.base.ExplainerBase
Bases:
abc.ABCBase class for all XAI explainer methods.
Subclass one of the mid-level classes (
GradientExplainer,PerturbationExplainer, orSurrogateExplainer) to add a custom explainer. Register the subclass withxai4tsc.register_explainer()to make it available by name.Example:
class MyExplainer(GradientExplainer): explanation_type = "feature_attribution" def _get_captum_attribution(self, model): from captum.attr import IntegratedGradients return IntegratedGradients(model) xai4tsc.register_explainer("my_method", MyExplainer)
Use specific classes for explanation types, e.g.
"feature_attribution"or"counterfactual"explanations.- explanation_type: str
Type of explanation produced by this explainer.
- data_applicability: ClassVar[set[xai4tsc.xai._types.DataType]]
Data domains this explainer applies to. A set of
DataTypemembers —{DataType.AGNOSTIC}(any input) or{DataType.TIME_SERIES}.
- explanation_domains: ClassVar[set[xai4tsc.xai._types.Domain]]
Signal domains this explainer can produce explanations in. A set of
Domainmembers (capability declaration, mirroringdata_applicability). Checked statically by the runner’s config sanity check before any explainer is instantiated. Defaults to{Domain.TIME}; frequency/time-frequency explainers override it. The realized domain of a produced explanation (Explanation.explanation_domain) must be a member of this set.
- abstractmethod explain(model: torch.nn.Module, exp: xai4tsc.xai._types.Explanation, device: str | torch.device, targets: list | None, **kwargs: object) numpy.ndarray
Generate explanations for the samples in exp.
- Parameters:
model – The PyTorch model to explain (an
Module).exp –
Explanationdataclass holding the data, labels, and encoder for the samples to explain.device – Compute device (e.g.
"cpu"or"cuda").targets – Target class indices to explain, or
Nonefor all classes.**kwargs – Additional explainer-specific options.
- Returns:
Explanation values with the same leading dimensions as
exp.data.- Return type:
np.ndarray
- class xai4tsc.xai.base.GradientExplainer
Bases:
ExplainerBase,abc.ABCBase class for gradient-based attribution methods (Captum).
Provides a shared
explain()implementation that calls the abstract_get_captum_attribution()factory method. Subclasses only need to return the appropriate CaptumAttributionobject.Suitable for: Integrated Gradients, DeepLIFT, Deconvolution, Guided Backpropagation, and any other Captum gradient method.
Subclasses may set
_attribute_kwargsin their__init__to pass extra keyword arguments to Captum’sattribute()call.- explanation_type = 'feature_attribution'
Type of explanation produced by this explainer.
- explain(model: torch.nn.Module, exp: xai4tsc.xai._types.Explanation, device: str | torch.device, targets: list | None, **kwargs: object) numpy.ndarray
Compute Captum attributions for the samples in exp.
- class xai4tsc.xai.base.PerturbationExplainer
Bases:
ExplainerBase,abc.ABCBase class for perturbation-based attribution methods.
Suitable for: Occlusion, RISE, and any method that masks or replaces parts of the input to estimate feature importance.
- explanation_type = 'feature_attribution'
Type of explanation produced by this explainer.
- class xai4tsc.xai.base.SurrogateExplainer
Bases:
ExplainerBase,abc.ABCBase class for surrogate / proxy model attribution methods.
Suitable for: LIME, SHAP (KernelSHAP), and any method that fits a simpler interpretable model locally around the prediction.
Currently a placeholder — no concrete implementations yet.
- explanation_type = 'feature_attribution'
Type of explanation produced by this explainer.
- class xai4tsc.xai.base.WrapperExplainer(base: dict | str)
Bases:
ExplainerBase,abc.ABCBase class for explainers that wrap and extend another explainer.
A wrapper takes a base explainer (any entry in
EXPLAINERS) and modifies its computation: either at the final input-multiplication step (e.g. SIGN for gradient-family bases) or by re-invoking it on transformed inputs (e.g. Temporal Saliency Rescaling, Time Forward Tunnel). Use_run_base()to (re-)invoke the wrapped method. Subclasses needing a true in-pass modification (e.g. an LRP input-layer rule) may overrideexplain()without using_run_base()at all.The base is instantiated via
build_explainer(), so it is configured exactly as it would be when invoked directly by name.- Parameters:
base (dict or str) – The base explainer to wrap. Either a bare method name, or a config dict with a
"method"key plus that method’s hyperparameters.
- explanation_type
Type of explanation produced by this explainer.
- data_applicability
Data domains this explainer applies to. A set of
DataTypemembers —{DataType.AGNOSTIC}(any input) or{DataType.TIME_SERIES}.
- explanation_domains
Signal domains this explainer can produce explanations in. A set of
Domainmembers (capability declaration, mirroringdata_applicability). Checked statically by the runner’s config sanity check before any explainer is instantiated. Defaults to{Domain.TIME}; frequency/time-frequency explainers override it. The realized domain of a produced explanation (Explanation.explanation_domain) must be a member of this set.