alibi.utils.visualization module

class alibi.utils.visualization.ImageVisualizationMethod(value)[source]

Bases: Enum

An enumeration.

alpha_scaling = 5
blended_heat_map = 2
heat_map = 1
masked_image = 4
original_image = 3
class alibi.utils.visualization.VisualizeSign(value)[source]

Bases: Enum

An enumeration.

absolute_value = 2
all = 4
negative = 3
positive = 1
alibi.utils.visualization.heatmap(data, xticklabels, yticklabels, vmin=None, vmax=None, cmap='magma', robust=False, annot=True, linewidths=3, linecolor='w', cbar=True, cbar_label='', cbar_ax=None, cbar_kws=None, fmt='{x:.2f}', textcolors=('white', 'black'), threshold=None, text_kws=None, ax=None, **kwargs)[source]

Constructs a heatmap with annotation.

Parameters:
  • data (ndarray) – A 2D numpy array of shape M x N.

  • yticklabels (List[str]) – A list or array of length M with the labels for the rows.

  • xticklabels (List[str]) – A list or array of length N with the labels for the columns.

  • vmin (Optional[float]) – When using scalar data and no explicit norm, vmin and vmax define the data range that the colormap covers. By default, the colormap covers the complete value range of the supplied data. It is an error to use vmin/vmax when norm is given. When using RGB(A) data, parameters vmin/vmax are ignored.

  • vmax (Optional[float]) – When using scalar data and no explicit norm, vmin and vmax define the data range that the colormap covers. By default, the colormap covers the complete value range of the supplied data. It is an error to use vmin/vmax when norm is given. When using RGB(A) data, parameters vmin/vmax are ignored.

  • cmap (Union[str, Colormap]) – The Colormap instance or registered colormap name used to map scalar data to colors. This parameter is ignored for RGB(A) data.

  • robust (Optional[bool]) –

    If True and vmin or vmax are absent, the colormap range is computed with robust quantiles instead of the extreme values. Uses numpy.nanpercentile with q values set to 2 and 98, respectively.

  • annot (Optional[bool]) – Boolean flag whether to annotate the heatmap. Default True.

  • linewidths (float) – Width of the lines that will divide each cell. Default 3.

  • linecolor (str) – Color of the lines that will divide each cell. Default "w".

  • cbar (bool) – Boolean flag whether to draw a colorbar.

  • cbar_label (str) – Optional label for the colorbar.

  • cbar_ax (Optional[Axes]) – Optional axes in which to draw the colorbar, otherwise take space from the main axes.

  • cbar_kws (Optional[dict]) –

    An optional dictionary with arguments to matplotlib.figure.Figure.colorbar.

  • fmt (Union[str, Formatter]) –

    Format of the annotations inside the heatmap. This should either use the string format method, e.g. "{x:.2f}", or be a matplotlib.ticker.Formatter. Default "{x:.2f}".

  • textcolors (Tuple[str, str]) – A tuple of matplotlib colors. The first is used for values below a threshold, the second for those above. Default ("black", "white").

  • threshold (Optional[float]) – Optional value in data units according to which the colors from textcolors are applied. If None (the default) uses the middle of the colormap as separation.

  • text_kws (Optional[dict]) –

    An optional dictionary with arguments to matplotlib.axes.Axes.text.

  • ax (Optional[Axes]) – Axes in which to draw the plot, otherwise use the currently-active axes.

  • kwargs

    All other keyword arguments are passed to matplotlib.axes.Axes.imshow.

Return type:

Axes

Returns:

Axes object with the heatmap.

alibi.utils.visualization.visualize_image_attr(attr, original_image=None, method='heat_map', sign='absolute_value', plt_fig_axis=None, outlier_perc=2, cmap=None, alpha_overlay=0.5, show_colorbar=False, title=None, fig_size=(6, 6), use_pyplot=True)[source]

Visualizes attribution for a given image by normalizing attribution values of the desired sign ('positive' | 'negative' | 'absolute_value' | 'all') and displaying them using the desired mode in a matplotlib figure.

Parameters:
  • attr (ndarray) – Numpy array corresponding to attributions to be visualized. Shape must be in the form (H, W, C), with channels as last dimension. Shape must also match that of the original image if provided.

  • original_image (Optional[ndarray]) – Numpy array corresponding to original image. Shape must be in the form (H, W, C), with channels as the last dimension. Image can be provided either with float values in range 0-1 or int values between 0-255. This is a necessary argument for any visualization method which utilizes the original image.

  • method (str) –

    Chosen method for visualizing attribution. Supported options are:

    • 'heat_map' - Display heat map of chosen attributions

    • 'blended_heat_map' - Overlay heat map over greyscale version of original image. Parameter alpha_overlay corresponds to alpha of heat map.

    • 'original_image' - Only display original image.

    • 'masked_image’ - Mask image (pixel-wise multiply) by normalized attribution values.

    • 'alpha_scaling' - Sets alpha channel of each pixel to be equal to normalized attribution value.

    Default: 'heat_map'.

  • sign (str) –

    Chosen sign of attributions to visualize. Supported options are:

    • 'positive' - Displays only positive pixel attributions.

    • 'absolute_value' - Displays absolute value of attributions.

    • 'negative' - Displays only negative pixel attributions.

    • 'all' - Displays both positive and negative attribution values. This is not supported for 'masked_image' or 'alpha_scaling' modes, since signed information cannot be represented in these modes.

  • plt_fig_axis (Optional[Tuple[Figure, Axes]]) – Tuple of matplotlib.pyplot.figure and axis on which to visualize. If None is provided, then a new figure and axis are created.

  • outlier_perc (Union[int, float]) – Top attribution values which correspond to a total of outlier_perc percentage of the total attribution are set to 1 and scaling is performed using the minimum of these values. For sign='all', outliers and scale value are computed using absolute value of attributions.

  • cmap (Optional[str]) – String corresponding to desired colormap for heatmap visualization. This defaults to 'Reds' for negative sign, 'Blues' for absolute value, 'Greens' for positive sign, and a spectrum from red to green for all. Note that this argument is only used for visualizations displaying heatmaps.

  • alpha_overlay (float) – Visualizes attribution for a given image by normalizing attribution values of the desired sign (positive, negative, absolute value, or all) and displaying them using the desired mode in a matplotlib figure.

  • show_colorbar (bool) – Displays colorbar for heatmap below the visualization. If given method does not use a heatmap, then a colormap axis is created and hidden. This is necessary for appropriate alignment when visualizing multiple plots, some with colorbars and some without.

  • title (Optional[str]) – The title for the plot. If None, no title is set.

  • fig_size (Tuple[int, int]) – Size of figure created.

  • use_pyplot (bool) – If True, uses pyplot to create and show figure and displays the figure after creating. If False, uses matplotlib object-oriented API and simply returns a figure object without showing.

Return type:

Tuple[Figure, Axes]

Returns:

2-element tuple of consisting of

  • figure : matplotlib.pyplot.Figure - Figure object on which visualization is created. If plt_fig_axis argument is given, this is the same figure provided.

  • axis : matplotlib.pyplot.Axes - Axes object on which visualization is created. If plt_fig_axis argument is given, this is the same axis provided.