class_docstr: A CoreSaliency class that computes saliency masks using the XRAI method.
class_docstr_tok: ["A","CoreSaliency","class","that","computes","saliency","masks","using","the","XRAI","method","."]
docstr: Run approximate XRAI saliency given attributions and segments.
  
  Note: This version does not consider mask overlap during importance ranking,
     significantly speeding up the algorithm for less accurate results.
  
  Args:
      attr: Source attributions for XRAI. XRAI attributions will be same size
        as the input attr.
      segs: Input segments as a list of boolean masks. XRAI uses these to
        compute attribution sums.
      gain_fun: The function that computes XRAI area attribution from source
        attributions. Defaults to _gain_density, which calculates the
        density of attributions in a mask.
      area_perc_th: This parameter is ignored. Fast version always computes
        to 1.0. It is left here for API compatibility.
      min_pixel_diff: Do not consider masks that have difference less than
        this number compared to the current mask. Set it to 1
        to remove masks that completely overlap with the
        current mask.
      integer_segments: See XRAIParameters. Defaults to True.
  
  Returns:
      tuple: saliency heatmap and list of masks or an integer image with
        area ranks depending on the parameter integer_segments.
docstr_tok: ["Run","approximate","XRAI","saliency","given","attributions","and","segments",".","Note",":","This","version","does","not","consider","mask","overlap","during","importance","ranking",",","significantly","speeding","up","the","algorithm","for","less","accurate","results",".","Args",":","attr",":","Source","attributions","for","XRAI",".","XRAI","attributions","will","be","same","size","as","the","input","attr",".","segs",":","Input","segments","as","a","list","of","boolean","masks",".","XRAI","uses","these","to","compute","attribution","sums",".","gain_fun",":","The","function","that","computes","XRAI","area","attribution","from","source","attributions",".","Defaults","to","_gain_density",",","which","calculates","the","density","of","attributions","in","a","mask",".","area_perc_th",":","This","parameter","is","ignored",".","Fast","version","always","computes","to","1.0",".","It","is","left","here","for","API","compatibility",".","min_pixel_diff",":","Do","not","consider","masks","that","have","difference","less","than","this","number","compared","to","the","current","mask",".","Set","it","to","1","to","remove","masks","that","completely","overlap","with","the","current","mask",".","integer_segments",":","See","XRAIParameters",".","Defaults","to","True",".","Returns",":","tuple",":","saliency","heatmap","and","list","of","masks","or","an","integer","image","with","area","ranks","depending","on","the","parameter","integer_segments","."]
code: def _xrai_fast(
      attr,
      segs,
      gain_fun=_gain_density,
      area_perc_th=1.0,
      min_pixel_diff=50,
      integer_segments=True,
  ):
      """Run approximate XRAI saliency given attributions and segments.
  
      Note: This version does not consider mask overlap during importance ranking,
         significantly speeding up the algorithm for less accurate results.
  
      Args:
          attr: Source attributions for XRAI. XRAI attributions will be same size
            as the input attr.
          segs: Input segments as a list of boolean masks. XRAI uses these to
            compute attribution sums.
          gain_fun: The function that computes XRAI area attribution from source
            attributions. Defaults to _gain_density, which calculates the
            density of attributions in a mask.
          area_perc_th: This parameter is ignored. Fast version always computes
            to 1.0. It is left here for API compatibility.
          min_pixel_diff: Do not consider masks that have difference less than
            this number compared to the current mask. Set it to 1
            to remove masks that completely overlap with the
            current mask.
          integer_segments: See XRAIParameters. Defaults to True.
  
      Returns:
          tuple: saliency heatmap and list of masks or an integer image with
            area ranks depending on the parameter integer_segments.
      """
      output_attr = -np.inf * np.ones(shape=attr.shape, dtype=float)
  
      n_masks = len(segs)
      current_mask = np.zeros(attr.shape, dtype=bool)
  
      masks_trace = []
  
      # Sort all masks based on gain, ignore overlaps
      seg_attrs = [gain_fun(seg_mask, attr) for seg_mask in segs]
      segs, seg_attrs = list(
          zip(*sorted(zip(segs, seg_attrs), key=lambda x: -x[1]))
      )
  
      for i, added_mask in enumerate(segs):
          mask_diff = _get_diff_mask(added_mask, current_mask)
          # If mask does not add more than min_pixel_diff to current mask, skip
          mask_pixel_diff = _get_diff_cnt(added_mask, current_mask)
          if mask_pixel_diff < min_pixel_diff:
              if _logger.isEnabledFor(logging.DEBUG):
                  _logger.debug(
                      "Skipping mask with pixel difference: {:.3g},".format(
                          mask_pixel_diff
                      )
                  )
              continue
          mask_gain = gain_fun(mask_diff, attr)
          masks_trace.append((mask_diff, mask_gain))
          output_attr[mask_diff] = mask_gain
          current_mask = np.logical_or(current_mask, added_mask)
          if _logger.isEnabledFor(logging.DEBUG):
              current_attr_sum = np.sum(attr[current_mask])
              current_area_perc = np.mean(current_mask)
              _logger.debug(
                  "{} of {} masks processed,"
                  "attr_sum: {}, area: {:.3g}/{:.3g}".format(
                      i + 1,
                      n_masks,
                      current_attr_sum,
                      current_area_perc,
                      area_perc_th,
                  )
              )
      uncomputed_mask = output_attr == -np.inf
      # Assign the uncomputed areas a value such that sum is same as ig
      output_attr[uncomputed_mask] = gain_fun(uncomputed_mask, attr)
      masks_trace = [
          v[0] for v in sorted(masks_trace, key=lambda x: -x[1])
      ]
      if np.any(uncomputed_mask):
          masks_trace.append(uncomputed_mask)
      if integer_segments:
          attr_ranks = np.zeros(shape=attr.shape, dtype=int)
          for i, mask in enumerate(masks_trace):
              attr_ranks[mask] = i + 1
          return output_attr, attr_ranks
      else:
          return output_attr, masks_trace
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import logging
  from .base import CoreSaliency
  from .integrated_gradients import IntegratedGradients
  import numpy
  from skimage import segmentation
  from skimage.morphology import dilation
  from skimage.morphology import disk
  from skimage.transform import resize
label_desc: Use image node_id 8 for calling the XRAI obj's underlying member method code with example usage: obj._xrai_fast(attr, segs, gain_fun, area_perc_th, min_pixel_diff, integer_segments) and returns: output_attr,  attr_ranks, output_attr,  masks_trace