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.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • strength (float) – Strength of brightness change.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

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.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • strength (float) – Strength of contrast change. Lower is actually more contrast.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

alibi_detect.utils.perturbation.defocus_blur(x, radius, alias_blur, xrange=None)[source]

Apply defocus blur.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • radius (int) – Radius for the Gaussian kernel.

  • alias_blur (float) – Standard deviation for the Gaussian kernel in both X and Y directions.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

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.

Parameters:
  • radius (float) – Radius for the Gaussian kernel.

  • alias_blur (float) – Standard deviation for the Gaussian kernel in both X and Y directions.

  • dtype – Data type.

Return type:

ndarray

Returns:

Kernel used for Gaussian blurring.

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.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • fractal_mult (float) – Strength applied to plasma_fractal output.

  • wibbledecay (float) – Decay factor for size of noise that is applied.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

alibi_detect.utils.perturbation.gaussian_blur(x, sigma, channel_axis=-1, xrange=None)[source]

Apply Gaussian blur.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • sigma (float) – Standard deviation determining the strength of the blur.

  • channel_axis (int) – Denotes the axis of the colour channel. If None the image is assumed to be grayscale.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

alibi_detect.utils.perturbation.gaussian_noise(x, stdev, xrange=None)[source]

Inject Gaussian noise.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • stdev (float) – Standard deviation of noise.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

alibi_detect.utils.perturbation.glass_blur(x, sigma, max_delta, iterations, xrange=None)[source]

Apply glass blur.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • sigma (float) – Standard deviation determining the strength of the Gaussian perturbation.

  • max_delta (int) – Maximum pixel range for the blurring.

  • iterations (int) – Number of blurring iterations.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

alibi_detect.utils.perturbation.impulse_noise(x, amount, xrange=None)[source]

Inject salt & pepper noise.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • amount (float) – Proportion of pixels to replace with noise.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

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:

Bunch

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:

Bunch

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:

Bunch

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.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • strength (float) – Strength of compression (>1). Lower is actually more compressed.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

alibi_detect.utils.perturbation.pixelate(x, strength, xrange=None)[source]

Change coarseness of pixels for an image.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • strength (float) – Strength of pixelation (<1). Lower is actually more pixelated.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

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.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • strength (tuple) – Strength of saturation change. Tuple consists of (multiplier, shift) of the perturbation.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

alibi_detect.utils.perturbation.scale_minmax(x, xrange=None)[source]

Minmax scaling to [0,1].

Parameters:
  • x (ndarray) – Numpy array to be scaled.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

Tuple[ndarray, bool]

Returns:

Scaled array and boolean whether the array is actually scaled.

alibi_detect.utils.perturbation.shot_noise(x, lam, xrange=None)[source]

Inject Poisson noise.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • lam (float) – Scalar for the lambda parameter determining the expectation of the interval.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

alibi_detect.utils.perturbation.speckle_noise(x, stdev, xrange=None)[source]

Inject speckle noise.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • stdev (float) – Standard deviation of noise.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.

alibi_detect.utils.perturbation.zoom_blur(x, max_zoom, step_zoom, xrange=None)[source]

Apply zoom blur.

Parameters:
  • x (ndarray) – Instance to be perturbed.

  • max_zoom (float) – Max zoom strength.

  • step_zoom (float) – Step size to go from 1 to max_zoom strength.

  • xrange (Optional[tuple]) – Tuple with min and max data range.

Return type:

ndarray

Returns:

Perturbed instance.