quantus.functions.perturb_func module

This modules holds a collection of perturbation functions i.e., ways to perturb an input or an explanation.

quantus.functions.perturb_func.baseline_replacement_by_blur(arr: array, indices: Tuple[array], indexed_axes: Sequence[int], blur_kernel_size: int | Sequence[int] = 15, **kwargs) array

Replace array at indices by a blurred version, performed via convolution.

Parameters:
arr: np.ndarray

Array to be perturbed.

indices: int, sequence, tuple

Array-like, with a subset shape of arr.

indexed_axes: sequence
The dimensions of arr that are indexed. These need to be consecutive,

and either include the first or last dimension of array.

blur_kernel_size: int, sequence

Controls the kernel-size of that convolution (Default is 15).

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.baseline_replacement_by_indices(arr: array, indices: Tuple[slice, ...], indexed_axes: Sequence[int], perturb_baseline: float | int | str | array, **kwargs) array

Replace indices in an array by a given baseline.

Parameters:
arr: np.ndarray

Array to be perturbed.

indices: int, sequence, tuple

Array-like, with a subset shape of arr.

indexed_axes: sequence
The dimensions of arr that are indexed. These need to be consecutive,

and either include the first or last dimension of array.

perturb_baseline: float, int, str, np.ndarray

The baseline values to replace arr at indices with.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.baseline_replacement_by_mask(arr: array, mask: array, perturb_baseline: float | int | str | array, **kwargs) array

Replace indices in an array by a given baseline.

Parameters:
arr: np.ndarray

Array to be perturbed. Shape is arbitrary.

mask: np.ndarray

Boolean mask of the array to perturb. Shape must be the same as the arr.

perturb_baseline: float, int, str, np.ndarray

The baseline values to replace arr at indices with.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.baseline_replacement_by_shift(arr: array, indices: Tuple[slice, ...], indexed_axes: Sequence[int], input_shift: float | int | str | array, **kwargs) array

Shift values at indices in an image.

Parameters:
arr: np.ndarray

Array to be perturbed.

indices: int, sequence, tuple

Array-like, with a subset shape of arr.

indexed_axes: sequence

The dimensions of arr that are indexed. These need to be consecutive, and either include the first or last dimension of array.

input_shift: float, int, str, np.ndarray

Value to shift arr at indices with.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.batch_baseline_replacement_by_indices(arr: array, indices: array, perturb_baseline: float | int | str | array, **kwargs) array

Replace indices in an array by a given baseline.

Parameters:
arr: np.ndarray

Array to be perturbed. Shape N x F, where N is batch size and F is number of features.

indices: np.ndarray

Indices of the array to perturb. Shape N x I, where N is batch size and I is the number of indices to perturb.

perturb_baseline: float, int, str, np.ndarray

The baseline values to replace arr at indices with.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.batch_gaussian_noise(arr: array, indices: array, perturb_mean: float = 0.0, perturb_std: float = 0.01, **kwargs) array

Add gaussian noise to the input at indices.

Parameters:
arr: np.ndarray

Array to be perturbed.

indices: int, sequence, tuple

Array-like, with a subset shape of arr.

perturb_mean (float):

The mean for gaussian noise.

perturb_std (float):

The standard deviation for gaussian noise.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.batch_uniform_noise(arr: array, indices: array, lower_bound: float = 0.02, upper_bound: None | float = None, **kwargs) array

Add noise to the input at indices as sampled uniformly random from [-lower_bound, lower_bound]. if upper_bound is None, and [lower_bound, upper_bound] otherwise.

Parameters:
arr: np.ndarray

Array to be perturbed.

indices: int, sequence, tuple

Array-like, with a subset shape of arr.

lower_bound: float

The lower bound for uniform sampling.

upper_bound: float, optional

The upper bound for uniform sampling.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.batched_translation(arr: array, perturb_baseline: float | int | str | array, perturb_dx: int = 10, direction: str = 'x', **kwargs) array

Translate array by some given value in the x or y direction, assumes image type data and channel first layout.

Parameters:
arr: np.ndarray

Array to be perturbed.

perturb_baseline: float, int, str, np.ndarray

The baseline values to replace arr at indices with.

perturb_dy: integer

The translation length in features, e.g., pixels.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.gaussian_noise(arr: array, indices: Tuple[slice, ...], indexed_axes: Sequence[int], perturb_mean: float = 0.0, perturb_std: float = 0.01, **kwargs) array

Add gaussian noise to the input at indices.

Parameters:
arr: np.ndarray

Array to be perturbed.

indices: int, sequence, tuple

Array-like, with a subset shape of arr.

indexed_axes: sequence

The dimensions of arr that are indexed. These need to be consecutive, and either include the first or last dimension of array.

perturb_mean (float):

The mean for gaussian noise.

perturb_std (float):

The standard deviation for gaussian noise.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.no_perturbation(arr: array, **kwargs) array

Apply no perturbation to input.

Parameters:
arr: np.ndarray

Array to be perturbed.

kwargs: optional

Keyword arguments.

Returns:
arr: np.ndarray

Array unperturbed.

quantus.functions.perturb_func.noisy_linear_imputation(arr: array, indices: Sequence[int] | Tuple[array], noise: float = 0.01, **kwargs) array

Calculates noisy linear imputation for the given array and a list of indices indicating which elements are not included in the mask.

Parameters:
arr: np.ndarray

Array to be perturbed.

indices: int, sequence, tuple

Array-like, with a subset shape of arr.

indexed_axes: sequence
The dimensions of arr that are indexed. These need to be consecutive,

and either include the first or last dimension of array.

noise: float

The amount of noise added.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.perturb_batch(perturb_func: Callable, arr: ndarray, indices: ndarray | None = None, inplace: bool = False, **kwargs) ndarray | None

Use a perturb function and make perturbation on the full batch.

Parameters:
perturb_func: callable

Input perturbation function.

arr: np.ndarray

Array to be perturbed.

indices: int, sequence, tuple

Array-like, with a subset shape of arr.

inplace: boolean

Indicates if the array should be copied or not.

kwargs: optional

Keyword arguments.

Returns:
None, array
quantus.functions.perturb_func.rotation(arr: array, perturb_angle: float = 10, **kwargs) array

Rotate array by some given angle, assumes image type data and channel first layout.

Parameters:
arr: np.ndarray

Array to be perturbed.

perturb_angle: float

The angle to perturb.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.translation_x_direction(arr: array, perturb_baseline: float | int | str | array, perturb_dx: int = 10, **kwargs) array

Translate array by some given value in the x-direction, assumes image type data and channel first layout.

Parameters:
arr: np.ndarray

Array to be perturbed.

perturb_baseline: float, int, str, np.ndarray

The baseline values to replace arr at indices with.

perturb_dx: integer

The translation length in features, e.g., pixels.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.translation_y_direction(arr: array, perturb_baseline: float | int | str | array, perturb_dy: int = 10, **kwargs) array

Translate array by some given value in the y-direction, assumes image type data and channel first layout.

Parameters:
arr: np.ndarray

Array to be perturbed.

perturb_baseline: float, int, str, np.ndarray

The baseline values to replace arr at indices with.

perturb_dy: integer

The translation length in features, e.g., pixels.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.

quantus.functions.perturb_func.uniform_noise(arr: array, indices: Tuple[slice, ...], indexed_axes: Sequence[int], lower_bound: float = 0.02, upper_bound: None | float = None, **kwargs) array

Add noise to the input at indices as sampled uniformly random from [-lower_bound, lower_bound]. if upper_bound is None, and [lower_bound, upper_bound] otherwise.

Parameters:
arr: np.ndarray

Array to be perturbed.

indices: int, sequence, tuple

Array-like, with a subset shape of arr.

indexed_axes: sequence
The dimensions of arr that are indexed. These need to be consecutive,

and either include the first or last dimension of array.

lower_bound: float

The lower bound for uniform sampling.

upper_bound: float, optional

The upper bound for uniform sampling.

kwargs: optional

Keyword arguments.

Returns:
arr_perturbed: np.ndarray

The array which some of its indices have been perturbed.