pertubators
muppet.components.perturbator.base
Base perturbator classes for applying masks to create perturbed inputs.
This module provides the foundational infrastructure for perturbators in the MUPPET XAI framework. Perturbators implement the second step of the four-step perturbation-based explanation process: generate masks → apply perturbations → calculate attributions → aggregate results.
Perturbators take the original input data and binary masks from explorers, then create perturbed versions of the input by modifying specific regions according to the masks. The perturbation strategy depends on the data modality and explanation method requirements.
The module contains
Perturbator: Abstract base class defining the perturbation interface with automatic batch processing and memory management capabilities. TrainablePerturbator: Extended base class for perturbators that use trainable generators for more sophisticated perturbation strategies.
Mask Convention
All premise types follow the MUPPET binary mask convention: - 0: Preserve the original input value (no perturbation) - 1: Perturb the input value (apply perturbation strategy)
Note
Perturbators work closely with explorers (mask generation) and are consumed by attributors (feature importance calculation). The choice of perturbation strategy significantly impacts explanation quality.
Classes
Perturbator
Bases: ABC
Abstract base class for perturbation strategies in XAI methods.
Perturbators apply masks to input data to create perturbed versions for explanation purposes. They define how the original input should be modified based on the perturbation masks generated by explorers.
Perturbators implement the second step of the four-step perturbation-based explanation process: generate masks → apply perturbations → calculate attributions → aggregate results.
The perturbators automatically handle
- Batch processing with configurable memory limits
- Out-of-memory error recovery with smaller batch sizes
- Integration with trainable generators when needed
Initialize the Perturbator with batch processing configuration.
Parameters:
-
max_batch_size(int) –Maximum batch size for processing perturbations.
Source code in muppet/components/perturbator/base.py
Functions
Reset the perturbator to its initial state.
Clears any internal state or cached data that may affect subsequent perturbation operations.
abstractmethod
Apply perturbations to input data using provided masks.
Mask Convention
All perturbators follow the MUPPET perturbation convention: - 0: Preserve the original input value (no perturbation) - 1: Perturb the input value (apply perturbation strategy)
Parameters:
-
x(Tensor) –The original input example. Shape (b, *x.shape[1:]).
-
masks(Tensor) –Binary masks for perturbation. Shape (N, *mask_shape).
Returns:
-
Tensor–torch.Tensor: Perturbed examples. Shape (N, *x.shape).
Source code in muppet/components/perturbator/base.py
staticmethod
A method that acts as a decorator factory. It has access to the instance 'self'.
Source code in muppet/components/perturbator/base.py
TrainablePerturbator
Bases: Perturbator
Perturbator with trainable generator capabilities.
Extends the basic Perturbator to include a trainable generator component that can learn perturbation strategies from training data. These trainable perturbators are designed for perturbation strategies that create plausible perturbations.
It automatically handles generator training if needed and integrates seamlessly with the MUPPET explanation pipeline.
Technical Features
- Automatic generator training with validation splits and early stopping
- Seamless integration with various trainable generators
Initialize the TrainablePerturbator with a generator and training data.
Parameters:
-
generator(TrainableGenerator) –The generator for creating perturbations.
-
train_loader(DataLoader) –Training data for generator training.
-
max_batch_size(int, default:100) –Maximum batch size for processing. Defaults to 100.
Source code in muppet/components/perturbator/base.py
muppet.components.perturbator.scale_feature_generator
Scale-based perturbators for tabular data using statistical generators.
This module implements perturbators that use statistical generators to create scaled perturbations of tabular data. These perturbators are designed for the MUPPET XAI framework's perturbation step, where input features are selectively replaced with generated values based on learned or observed data distributions.
The perturbators in this module specialize in tabular data explanations by leveraging sophisticated generators that understand feature distributions, correlations, and data types. This enables more realistic perturbations compared to simple masking approaches, leading to better explanation quality for tabular models.
The module contains
ScaleFeaturePerturbator: Uses Gaussian-based generators for continuous tabular features with statistical scaling and normalization RandomSamplePerturbator: Employs frequency-based sampling from training data distributions for mixed categorical/numerical features
Key Features
- Statistical distribution preservation through generator training
- Mixed data type handling (numerical and categorical)
- Instance-centered perturbations for local explanations
- Configurable sampling strategies and scaling approaches
- Memory-efficient batch processing with automatic size adjustment
These perturbators are essential for tabular explanation methods like LIME and SHAP, where the quality of counterfactual examples directly impacts explanation fidelity and interpretability. They enable realistic "what-if" scenarios by generating plausible alternative feature values.
Classes
ScaleFeaturePerturbator
Bases: Perturbator
Perturbator for tabular data using Gaussian generators.
Specializes in perturbing tabular data by applying statistical generators that maintain feature distributions. Designed for structured data explanations where preserving data realism is crucial.
Perturbator for tabular data based on a Gaussian generator.
This perturbator is designed to modify tabular data by generating controlled variations using a Gaussian-based generator, which has been trained on continuous input features.
Parameters:
-
generator(StandardGaussianTabularGenerator) –An instance of a Gaussian generator for tabular data, which, once trained on the input data, generates continuous values based on a Gaussian distribution.
-
max_batch_size(int, default:100) –Max Batch size to use. Default to 100.
Source code in muppet/components/perturbator/scale_feature_generator.py
Functions
Perturbs the input tensor x using the provided masks.
This method applies perturbations to the input x based on the given masks.
The masks determine which features of the input should be perturbed (1 for
perturbed, 0 for not perturbed). The generator is used to produce substitute
values for the masked features.
Parameters:
-
x(Tensor) –The input tensor to be perturbed, with shape (1, f), where
fis the number of features in the data. -
masks(Tensor) –A tensor of masks containing 0s and 1s to determine which features in
xwill be perturbed. Its shape is (b, *shape), whereshapecorresponds to the shape ofx.
Returns:
-
Tensor–torch.Tensor: A tensor containing the generated perturbed values,
-
Tensor–with shape (number_of_masks, *x.shape).
Source code in muppet/components/perturbator/scale_feature_generator.py
RandomSamplePerturbator
Bases: Perturbator
A class to perturb tabular data using generated samples and binary masks.
Employs frequency-based sampling from training data distributions for mixed categorical/numerical features
Initializes the RandomSamplePerturbator with a generator.
Parameters:
-
generator(RandomSampleTabularGenerator) –An instance of RandomSampleTabularGenerator that will be used to generate random samples.
Source code in muppet/components/perturbator/scale_feature_generator.py
Functions
Perturb the input tensor using generated samples and binary masks.
Parameters:
-
x(Tensor) –The input tensor to be perturbed. Should have shape (1, num_features).
-
masks(Tensor) –A tensor containing binary masks with shape (number_masks, 1, num_features). Each mask is used to determine where to apply the perturbation.
Returns:
-
Tensor–torch.Tensor: A tensor containing the perturbed samples with shape (number_masks, 1, num_features). Perturbations are applied according to the masks: where masks are 1, the value from the generated tensor is used; where masks are 0, the original value
xis preserved.
Source code in muppet/components/perturbator/scale_feature_generator.py
muppet.components.perturbator.simple
Simple perturbation strategies for basic masking operations.
Provides simple perturbator implementations for the MUPPET XAI framework, implementing the second step of the perturbation process. These modules apply straightforward transformations to input data based on binary masks from explorers.
Simple perturbators are fast, interpretable, and computationally efficient.
Classes:
-
SetToZeroPerturbator–Masks features by setting them to zero.
-
BlurPerturbator–Applies Gaussian blur to masked regions in image data, simulating information removal while maintaining spatial structure
These simple perturbators are building blocks for many explanation methods including RISE. They provide baseline perturbation strategies that can be compared against more sophisticated approaches or used when interpretability and speed are prioritized over realism.
Classes
SetToZeroPerturbator
Bases: Perturbator
Simple perturbator that sets masked features to zero.
Provides basic perturbation by multiplying input features with the complement of the mask (1-mask). This creates binary perturbations where features are either preserved or zeroed out.
Initialize the SetToZeroPerturbator.
Parameters:
-
max_batch_size(int, default:100) –Maximum batch size for processing. Defaults to 100.
Source code in muppet/components/perturbator/simple.py
Functions
Set masked features to zero by multiplying with (1-mask).
Parameters:
-
x(Tensor) –The input example. Shape (b, *input_dims).
-
masks(Tensor) –The perturbation masks. Shape (N, *mask_shape).
Returns:
-
Tensor–torch.Tensor: Perturbed examples with masked features set to zero.
Source code in muppet/components/perturbator/simple.py
BlurPerturbator
Bases: Perturbator
Perturbator that applies Gaussian blur to masked regions.
Creates perturbations by blurring masked areas instead of zeroing them. Maintains spatial information while reducing detail, useful for image explanations where complete occlusion is too harsh.
This perturbator simulates information removal while maintaining spatial structure by applying Gaussian blur to masked regions in image data. It provides a more realistic perturbation than simple masking for image explanations.
Initialize the BlurPerturbator.
Parameters:
-
add_noise(bool, default:False) –Whether to add normal noise to perturbations. Defaults to False.
-
kernel_size(tuple[int, int], default:(11, 11)) –Gaussian kernel size for blurring. Defaults to (11, 11).
-
sigma(int, default:5) –Gaussian sigma parameter. Defaults to 5.
-
max_batch_size(int, default:100) –Maximum batch size for processing. Defaults to 100.
Source code in muppet/components/perturbator/simple.py
Functions
Calculates the input perturbations by $Input*(1-Mask) + BluredInput*Mask$
Parameters:
-
x(Tensor) –The input examples. Shape (b=1, *x.shape[1:]) E.g (b=1, c, w, h)
-
masks(Tensor) –The generated masks to use for perturbing x. Shape (N, *mask_shape), len(mask_shape)==x.dim(). E.g mask_shape =(b=1, c=1, w, h) - b is batch dimension, expected to be set to 1 as only one example is being explained for the moment, - c is the channel dimensions, - w is the width, - h is the height, - N the number of perturbation masks.
Returns:
-
Tensor–x' (torch.Tensor): Perturbed version of x. Shape (N, *x.shape)
Source code in muppet/components/perturbator/simple.py
muppet.components.perturbator.timestep_generator
Time series perturbators using trainable generators for temporal explanations.
This module implements specialized perturbators for time series data that leverage trainable generators to create realistic temporal perturbations. These perturbators are essential for explaining sequential models where temporal dependencies and patterns are crucial for understanding model behavior.
In the MUPPET framework's perturbation step, these perturbators go beyond simple masking by using learned generative models to impute missing values in time series data. This preserves temporal coherence and realistic data characteristics, leading to more meaningful explanations for sequential models.
The module contains
GeneratorSamplingPerturbator: Uses trainable generators to impute values at multiple timesteps simultaneously, suitable for complex temporal patterns ConditionalSamplingGeneratorPerturbator: Performs conditional sampling for single-timestep perturbations, ideal for feature-specific temporal explanations
Key Technical Features
- Automatic generator training with validation splits and early stopping
- Temporal dependency preservation through learned representations
- Feature-wise conditional sampling for fine-grained explanations
- NaN handling for padding when model cannot handle unequal length signals
These perturbators are designed for explaining sequential models in domains like
- Financial time series forecasting
- Sensor data analysis and anomaly detection
- Medical signal processing and diagnosis
The generators learn to capture complex temporal patterns during training, enabling realistic counterfactual scenarios that maintain temporal consistency while revealing feature importance across time.
Classes
GeneratorSamplingPertubator
Bases: TrainablePerturbator
Perturbator using generative models for time series imputation.
Employs trainable generators to create realistic substitutes for perturbed time series segments. Learns temporal patterns from training data to generate contextually appropriate perturbations.
Perturbator that uses a generative model - generator - in order to impute the missing measurements.
Parameters:
-
generator(TrainableGenerator) –The generator to be used to impute the perturbed values through its inference method 'call'.
-
train_loader(DataLoader) –Training data.
-
max_batch_size(int, default:100) –Max Batch size to use. Default to 100.
Source code in muppet/components/perturbator/timestep_generator.py
Functions
Perturbate the input x according to the masks. The generator will be used to perturbate each position covered by the mask.
Parameters:
-
x(Tensor) –The input example. Shape (b=1, f, t)
-
masks(Tensor) –Encodes what parts of x to be perturbed. Shape (N, x.shape)
Returns:
-
Tensor–torch.Tensor: Perturbed version of x. (N, x.shape)
Source code in muppet/components/perturbator/timestep_generator.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
ConditionalSamplingGeneratorPertubator
Bases: TrainablePerturbator
Conditional perturbator for advanced time series explanations.
Uses conditional generators to create perturbations that respect feature dependencies and temporal relationships. Enables sophisticated perturbations for time series models.
Perturbator that uses a GAN model - generator - in order to impute the missing measurements.
Parameters:
-
generator(TrainableGenerator) –The generator to be used to impute the perturbed values through its inference method 'call'.
-
train_loader(DataLoader) –Training data.
-
max_batch_size(int, default:100) –Max Batch size to use. Default to 100.
Source code in muppet/components/perturbator/timestep_generator.py
Functions
Perturbate the input x according to the masks. The generator will be used to perturbate each position covered by the mask.
Parameters:
-
x(Tensor) –The input example. Shape (b=1, f, t)
-
masks(Tensor) –Encodes what parts of x to be perturbed. Shape (N, x.shape)
Returns:
-
Tensor–torch.Tensor: Perturbed version of x. (N, x.shape)