Aggregator
muppet.components.aggregator.base
Base aggregator component for MUPPET XAI.
This module defines the abstract base class for all aggregators in the MUPPET XAI framework. Aggregators are responsible for combining individual feature attributions calculated by attributors to produce the final local explanations (heatmaps, feature importance scores, etc.).
The aggregation process is the final step in the four-step perturbation-based XAI approach: generate masks → apply perturbations → calculate attributions → aggregate results.
Classes:
-
Aggregator–Abstract base class defining the interface for all aggregator components.
Classes
Aggregator
Bases: ABC
Abstract base class for aggregator components in MUPPET XAI.
A global aggregator component that is responsible for aggregating the attribution calculated by the attributor in order to generate the final heatmap.
Attributes:
-
device–The used device. Will get updated from the main explainer after initialization.
-
convention–The attribution convention (constructive or destructive).
-
allowed_conventions–The allowed convention types from AttributionConvention.
Example
Typical usage involves subclassing the Aggregator base class:
Initialize the aggregator component.
Sets up the default device and attribution convention for the aggregator. If no convention is set, defaults to 'destructive' with a warning.
Source code in muppet/components/aggregator/base.py
Attributes
Functions
Reset the aggregator to its initial state.
This method restores the aggregator to its original configuration, clearing any internal state or accumulated data that may affect subsequent aggregation operations.
Source code in muppet/components/aggregator/base.py
abstractmethod
A custom method that calculates the final explanations as a heatmap of the same shape as the input. To do so, it uses the stored attributions in memory.
Parameters:
-
memory(Memory) –The used memory structure where premises are stored.
Returns:
-
Tensor–torch.Tensor: The final explanation in the form of a heatmap of shape input.
Source code in muppet/components/aggregator/base.py
muppet.components.aggregator.distribution
Distribution-based aggregator for time series classification explanations.
This module provides aggregators that work with probability distributions over class predictions in time series classification tasks. It implements Monte Carlo aggregation methods to compute feature importance scores from KL divergence measurements between original and perturbed predictions.
The aggregator groups Monte Carlo samples by time steps and features, calculating final attribution scores through statistical aggregation of the KL divergences.
Classes:
-
MonteCarloKLAggregator–Aggregates attributions using Monte Carlo sampling and KL divergence for time series classification tasks.
Classes
MonteCarloKLAggregator
Bases: Aggregator
Monte Carlo aggregator using KL divergence for time series classification.
This aggregator works on probability distributions over classes for classification tasks. It aggregates attributions using Monte Carlo sampling and KL divergence measurements to compute feature importance scores for time series data.
The aggregator groups Monte Carlo samples by time steps and features, calculating final attribution scores through statistical aggregation of KL divergences between original and perturbed predictions.
Initialize the Monte Carlo KL divergence aggregator.
Parameters:
-
num_sampling(int) –The number of Monte-Carlo sampling iterations.
Source code in muppet/components/aggregator/distribution.py
Functions
Calculates the final heatmap by grouping the Monte-Carlo samples premises of the same time-step, then aggregating over their attributions in order to calculate the final time-step's score.
Parameters:
-
memory(Premiselist) –List of premises.
Returns:
-
Tensor–torch.Tensor: The final heatmap where at every timestep the score \(score(t, S)\) is calculated. Shape (b, f, t)
Source code in muppet/components/aggregator/distribution.py
muppet.components.aggregator.mask
Mask-based aggregators for image and spatial data explanations.
This module provides aggregators that work with mask-based perturbations for generating explanation for image classification models. It implements weighted aggregation methods that combine multiple perturbation masks with their corresponding attribution weights to produce final saliency maps.
The module supports both weighted sum aggregation (for methods like RISE) and learned mask aggregation (for gradient-based optimization methods) with support for different attribution conventions (constructive vs destructive).
Classes:
-
WeightedSumAggregator–Aggregates masks using weighted sum based on model predictions.
-
LearntMaskAggregator–Returns normalized learned masks from optimization-based methods.
Classes
WeightedSumAggregator
Bases: Aggregator
Weighted sum aggregator for mask-based explanations.
This aggregator multiplies the weight of every perturbation by its mask and sums up all the masks. The weight is equal to the model's class probability. It is commonly used in methods like RISE.
Initialize the weighted sum aggregator.
Parameters:
-
post_proc(Union[Callable[[Tensor], Tensor], None], default:None) –Apply the post_proc function to the calculated heatmap (example: ReLU). None by default, meaning no post processing is done.
-
convention(Union[AttributionConvention, str], default:'destructive') –The attribution convention to use, either "constructive" or "destructive".
Source code in muppet/components/aggregator/mask.py
Functions
Calculate final heatmap by multiplying the weight of every perturbation by its mask and sum up all the masks.
Parameters:
-
memory(Premiselist) –A simple list where premises are saved. Every premise provides the attribution where mask's weight is stored.
Returns:
-
Tensor–torch.Tensor: Final heatmap map of same shape as input x (b=1, c, w, h) highlighting the most important parts of the input example. Where - b is batch dimension, expected to be set to 1 as only one example is being explained for the moment, - w is the width, - h is the height.
Source code in muppet/components/aggregator/mask.py
LearntMaskAggregator
Bases: Aggregator
Learned mask aggregator for optimization-based explanation methods.
This aggregator returns the normalized learned mask from gradient-based optimization methods. It normalizes the mask between 0 and 1 and applies convention-based transformations as needed.
Initialize the learnt mask aggregator.
Parameters:
-
convention(str, default:'destructive') –The attribution convention, either 'constructive' or 'destructive'. If "constructive", the heatmap is reversed using 1-heatmap.
Source code in muppet/components/aggregator/mask.py
Functions
Returns the learnt mask.
Parameters:
-
memory(Premiselist) –List of one premise with the mask to be optimized.
Returns:
-
Tensor–torch.Tensor: The final heatmap. Shape (b=1, *x.shape[1:]).
Source code in muppet/components/aggregator/mask.py
muppet.components.aggregator.model
Model-based aggregators using surrogate models for local explanations.
This module provides aggregators that use surrogate models (like Ridge regression) to fit local linear approximations of the complex model's behavior. This approach is fundamental to LIME-style explanations, where interpretable models explain individual predictions by learning from perturbations in the local neighborhood.
The aggregators support both tabular data (returning coefficients directly) and segmented image data (mapping coefficients back to pixel space using superpixels as in LIME-image). The surrogate models are fitted using weighted samples based on similarity to the original input.
Classes:
-
ModelAggregator–Base aggregator using surrogate models for local explanations.
-
SegmentedImageModelAggregator–Specialized aggregator for image data using superpixels.
Classes
ModelAggregator
Bases: Aggregator
Base aggregator using surrogate models for local explanations.
This aggregator fits a surrogate model to provide local linear approximations of the complex model's behavior. It is fundamental to LIME-style explanations, where interpretable models explain individual predictions by learning from perturbations in the local neighborhood.
Initialize the model aggregator.
Parameters:
-
surrogate_model–The model to use in explanation. Defaults to Ridge regression. Must have model_regressor.coef_ and 'sample_weight' as a parameter to model_regressor.fit(). It must be an inherently interpretable model, specifically a ((regularized)(Linear|Logistic)regression) model.
Source code in muppet/components/aggregator/model.py
Functions
Fits the surrogate model with premises.
Source code in muppet/components/aggregator/model.py
Method which fits a linear model with the data contained in premises and returns the learned coefficients of the surrogate model
Source code in muppet/components/aggregator/model.py
Calculate final heatmap.
This method is meant to be overridden by subclasses to handle different types of data.
Parameters:
-
memory(Premiselist) –A Premiselist where premises are saved.
Returns:
-
Tensor–torch.Tensor: The heatmap or coefficients depending on the data type.
Source code in muppet/components/aggregator/model.py
SegmentedImageModelAggregator
Bases: ModelAggregator
Specialized aggregator for image data using superpixels and surrogate models.
This aggregator extends ModelAggregator to handle segmented image data by mapping surrogate model coefficients back to pixel space using superpixels. It transforms the learned feature importance values from the superpixel level back to a spatial heatmap that highlights important image regions.
Source code in muppet/components/aggregator/model.py
Functions
Calculate final heatmap for segmented image data.
Parameters:
-
memory(Premiselist) –A Premiselist where premises are saved. Every premise provides the attribution where mask's weight is stored.
Returns:
-
Tensor–torch.Tensor: Final heatmap map of same shape as input x (b=1, c=1, h, w) highlighting the most important parts of the input example.
-
Tensor–Where
-
Tensor–b is batch dimension, expected to be set to 1 as only one example is being explained for the moment,
-
Tensor–w is the width,
-
Tensor–h is the height.