alibi_detect.utils.perturbation module
- alibi_detect.utils.perturbation.apply_mask(X, mask_size=(4, 4), n_masks=1, coord=None, channels=[0, 1, 2], mask_type='uniform', noise_distr=(0, 1), noise_rng=(0, 1), clip_rng=(0, 1))[source]
Mask images. Can zero out image patches or add normal or uniformly distributed noise.
- Parameters:
X (
ndarray
) – Batch of instances to be masked.mask_size (
tuple
) – Tuple with the size of the mask.n_masks (
int
) – Number of masks applied for each instance in the batch X.coord (
Optional
[tuple
]) – Upper left (x,y)-coordinates for the mask.channels (
list
) – Channels of the image to apply the mask to.mask_type (
str
) – Type of mask. One of ‘uniform’, ‘random’ (both additive noise) or ‘zero’ (zero values for mask).noise_distr (
tuple
) – Mean and standard deviation for noise of ‘random’ mask type.noise_rng (
tuple
) – Min and max value for noise of ‘uniform’ type.clip_rng (
tuple
) – Min and max values for the masked instances.
- Return type:
Tuple
[ndarray
,ndarray
]- Returns:
Tuple with masked instances and the masks.
- alibi_detect.utils.perturbation.brightness(x, strength, xrange=None)[source]
Change brightness of image.
- alibi_detect.utils.perturbation.clipped_zoom(x, zoom_factor)[source]
Helper function for zoom blur.
- Parameters:
x (
ndarray
) – Instance to be perturbed.zoom_factor (
float
) – Zoom strength.
- Return type:
ndarray
- Returns:
Cropped and zoomed instance.
- alibi_detect.utils.perturbation.contrast(x, strength, xrange=None)[source]
Change contrast of image.
- alibi_detect.utils.perturbation.defocus_blur(x, radius, alias_blur, xrange=None)[source]
Apply defocus blur.
- Parameters:
- Return type:
ndarray
- Returns:
Perturbed instance.
- alibi_detect.utils.perturbation.disk(radius, alias_blur=0.1, dtype=<class 'numpy.float32'>)[source]
Helper function for defocus blur.
- alibi_detect.utils.perturbation.elastic_transform(x, mult_dxdy, sigma, rnd_rng, xrange=None)[source]
Apply elastic transformation to instance.
- Parameters:
x (
ndarray
) – Instance to be perturbed.mult_dxdy (
float
) – Multiplier for the Gaussian noise in x and y directions.sigma (
float
) – Standard deviation determining the strength of the Gaussian perturbation.rnd_rng (
float
) – Range for random uniform noise.xrange (
Optional
[tuple
]) – Tuple with min and max data range.
- Return type:
ndarray
- Returns:
Perturbed instance.
- alibi_detect.utils.perturbation.fog(x, fractal_mult, wibbledecay, xrange=None)[source]
Apply fog to instance.
- alibi_detect.utils.perturbation.gaussian_blur(x, sigma, channel_axis=-1, xrange=None)[source]
Apply Gaussian blur.
- Parameters:
- Return type:
ndarray
- Returns:
Perturbed instance.
- alibi_detect.utils.perturbation.gaussian_noise(x, stdev, xrange=None)[source]
Inject Gaussian noise.
- alibi_detect.utils.perturbation.glass_blur(x, sigma, max_delta, iterations, xrange=None)[source]
Apply glass blur.
- Parameters:
- Return type:
ndarray
- Returns:
Perturbed instance.
- alibi_detect.utils.perturbation.impulse_noise(x, amount, xrange=None)[source]
Inject salt & pepper noise.
- alibi_detect.utils.perturbation.inject_outlier_categorical(X, cols, perc_outlier, y=None, cat_perturb=None, X_fit=None, disc_perc=[25, 50, 75], smooth=1.0)[source]
Inject outliers in categorical variables of tabular data.
- Parameters:
X (
ndarray
) – Tabular data with categorical variables to perturb (inject outliers).cols (
List
[int
]) – Columns of X that are categorical and can be perturbed.perc_outlier (
int
) – Percentage of observations which are perturbed to outliers. For multiple numerical features, the percentage is evenly split across the features.y (
Optional
[ndarray
]) – Outlier labels.cat_perturb (
Optional
[dict
]) – Dictionary mapping each category in the categorical variables to their furthest neighbour.X_fit (
Optional
[ndarray
]) – Optional data used to infer pairwise distances from.disc_perc (
list
) – List with percentiles used in binning of numerical features used for the ‘abdm’ pairwise distance measure.smooth (
float
) – Smoothing exponent between 0 and 1 for the distances. Lower values will smooth the difference in distance metric between different features.
- Return type:
- Returns:
Bunch object with the perturbed tabular data, outlier labels and a dictionary used to map categories to their furthest neighbour.
- alibi_detect.utils.perturbation.inject_outlier_tabular(X, cols, perc_outlier, y=None, n_std=2.0, min_std=1.0)[source]
Inject outliers in numerical tabular data.
- Parameters:
X (
ndarray
) – Tabular data to perturb (inject outliers).cols (
List
[int
]) – Columns of X that are numerical and can be perturbed.perc_outlier (
int
) – Percentage of observations which are perturbed to outliers. For multiple numerical features, the percentage is evenly split across the features.y (
Optional
[ndarray
]) – Outlier labels.n_std (
float
) – Number of feature-wise standard deviations used to perturb the original data.min_std (
float
) – Minimum number of standard deviations away from the current observation. This is included because of the stochastic nature of the perturbation which could lead to minimal perturbations without a floor.
- Return type:
- Returns:
Bunch object with the perturbed tabular data and the outlier labels.
- alibi_detect.utils.perturbation.inject_outlier_ts(X, perc_outlier, perc_window=10, n_std=2.0, min_std=1.0)[source]
Inject outliers in both univariate and multivariate time series data.
- Parameters:
X (
ndarray
) – Time series data to perturb (inject outliers).perc_outlier (
int
) – Percentage of observations which are perturbed to outliers. For multivariate data, the percentage is evenly split across the individual time series.perc_window (
int
) – Percentage of the observations used to compute the standard deviation used in the perturbation.n_std (
float
) – Number of standard deviations in the window used to perturb the original data.min_std (
float
) – Minimum number of standard deviations away from the current observation. This is included because of the stochastic nature of the perturbation which could lead to minimal perturbations without a floor.
- Return type:
- Returns:
Bunch object with the perturbed time series and the outlier labels.
- alibi_detect.utils.perturbation.jpeg_compression(x, strength, xrange=None)[source]
Simulate changes due to JPEG compression for an image.
- alibi_detect.utils.perturbation.pixelate(x, strength, xrange=None)[source]
Change coarseness of pixels for an image.
- alibi_detect.utils.perturbation.plasma_fractal(mapsize=256, wibbledecay=3.0)[source]
Helper function to apply fog to instance. Generates a heightmap using diamond-square algorithm. Returns a square 2d array, side length ‘mapsize’, of floats in range 0-255. ‘mapsize’ must be a power of two.
- Return type:
ndarray
- alibi_detect.utils.perturbation.saturate(x, strength, xrange=None)[source]
Change colour saturation of image.