class_docstr: Defines the ExplainerManager for explaining an image-based model.
class_docstr_tok: ["Defines","the","ExplainerManager","for","explaining","an","image-based","model","."]
docstr: Creates an explanation for a single image in the dataset
  
  :param index: The index of the image to create the explanation for
  :type index: int
  :param max_evals: the maximum number of evalutions
  :type max_evals: int
  :param object_index: The index of the object within the image we are
  looking to create the explanation for. Note that saliency maps are
  created one object per image. The default value for this is 0 to
  ensure a modular development process (so this function won't fail
  without an updated frontend). This parameter is only for the object
  detection scenario using the DRISE functionality.
  :type object_index: Optional[int]
  :return: The explanation for the image, which is a saliency map.
      For object detection, this can be a list of saliency maps if
      object index is not specified.
  :rtype: str or list[str]
docstr_tok: ["Creates","an","explanation","for","a","single","image","in","the","dataset",":","param","index",":","The","index","of","the","image","to","create","the","explanation","for",":","type","index",":","int",":","param","max_evals",":","the","maximum","number","of","evalutions",":","type","max_evals",":","int",":","param","object_index",":","The","index","of","the","object","within","the","image","we","are","looking","to","create","the","explanation","for",".","Note","that","saliency","maps","are","created","one","object","per","image",".","The","default","value","for","this","is","0","to","ensure","a","modular","development","process","(","so","this","function","wo","n't","fail","without","an","updated","frontend",")",".","This","parameter","is","only","for","the","object","detection","scenario","using","the","DRISE","functionality",".",":","type","object_index",":","Optional","[","int","]",":","return",":","The","explanation","for","the","image",",","which","is","a","saliency","map",".","For","object","detection",",","this","can","be","a","list","of","saliency","maps","if","object","index","is","not","specified",".",":","rtype",":","str","or","list","[","str","]"]
code: def compute_single_explanation(
      self, index, max_evals=None, object_index=0, **kwargs
  ):
      """Creates an explanation for a single image in the dataset
  
      :param index: The index of the image to create the explanation for
      :type index: int
      :param max_evals: the maximum number of evalutions
      :type max_evals: int
      :param object_index: The index of the object within the image we are
      looking to create the explanation for. Note that saliency maps are
      created one object per image. The default value for this is 0 to
      ensure a modular development process (so this function won't fail
      without an updated frontend). This parameter is only for the object
      detection scenario using the DRISE functionality.
      :type object_index: Optional[int]
      :return: The explanation for the image, which is a saliency map.
          For object detection, this can be a list of saliency maps if
          object index is not specified.
      :rtype: str or list[str]
      """
      if max_evals is None:
          # if not specified use global max_evals value
          max_evals = self._max_evals
      self.automl_image_model = is_automl_image_model(self._model)
      if self.automl_image_model:
          # get xai algorithm name
          xai_algo_name = kwargs.get(
              ExplainabilityLiterals.XAI_ALGORITHM,
              ExplainabilityDefaults.XAI_ALGORITHM,
          )
      else:
          xai_algo_name = ExplainabilityLiterals.SHAP
  
      if not self._is_added:
          self.add()
      if index < 0 or index > len(self._evaluation_examples) - 1:
          raise ValueError("Index out of range")
      if self._explanation is not None and index < len(
          self._explanation
      ):
          if self._task_type == ModelTask.OBJECT_DETECTION:
              return self._explanation[index][object_index]
          else:
              return self._explanation[index]
      if self._is_classification_task:
          ex = self._evaluation_examples
          image = ex.iloc[index : index + 1, 0].values[0]
          if isinstance(image, str):
              if not self.automl_image_model:
                  image = get_image_from_path(image, self._image_mode)
          if xai_algo_name == ExplainabilityLiterals.SHAP:
              if not self.automl_image_model:
                  explanation = self.get_shap_explanations(
                      image, max_evals
                  )
                  return self.image(explanation, 0)
              else:
                  raise ValueError(
                      "{} is not supported for the model type: {}".format(
                          xai_algo_name, type(self._model)
                      )
                  )
          else:
              if self.automl_image_model:
                  visualization = self.get_automl_explanations(
                      image, xai_algo_name, **kwargs
                  )
                  return visualization
              else:
                  raise ValueError(
                      "{} is not supported for the model type: {}".format(
                          xai_algo_name, type(self._model)
                      )
                  )
      if self._is_object_detection_task:
          ex = self._evaluation_examples
          img = ex.iloc[index : index + 1, 0].values[0]
          try:
              if (
                  type(self._model) is not MLflowDRiseWrapper
                  and type(self._model) is not PytorchDRiseWrapper
              ):
                  if is_automl_image_model(self._model):
                      self._model = MLflowDRiseWrapper(
                          self._model._model, self._classes
                      )
                  else:
                      self._model = PytorchDRiseWrapper(
                          self._model._model,
                          len(self._classes),
                          self._device,
                      )
  
              # calling DRISE to generate saliency maps for all objects
              mask_res_tuple = (self._mask_res, self._mask_res)
              device = self._device
              # get_drise_saliency_map only recognizes GPU and CPU
              if device == Device.AUTO.value:
                  device = None
              try:
                  (
                      fl,
                      _,
                      _,
                  ) = get_drise_saliency_map(
                      img,
                      self._model,
                      len(self._classes),
                      savename=str(index),
                      nummasks=self._num_masks,
                      maskres=mask_res_tuple,
                      devicechoice=device,
                      max_figures=5000,
                  )
              except ValueError as ve:
                  if str(ve) == "No detections found":
                      raise UserConfigValidationException(
                          "No detections found in image. "
                          "Please increase num_masks or mask_res."
                      )
                  else:
                      raise ve
              if object_index is None:
                  return fl
              b64_string = fl[object_index]
          except BaseException:
              tb = traceback.format_exc()
              warnings.warn(
                  (
                      "Could not generate saliency map. "
                      + "Inner exception: {}"
                  ).format(tb),
                  UserWarning,
              )
              if object_index is None:
                  return [self._get_fail_str()]
              b64_string = self._get_fail_str()
          return b64_string
      else:
          raise ValueError(
              "Unknown task type: {}".format(self._task_type)
          )
  
imports: import base64
  import io
  import json
  import pickle
  import traceback
  import warnings
  from pathlib import Path
  from typing import Any, List, Optional
  import cv2
  import matplotlib.pyplot
  import numpy
  import pandas
  import shap
  from ml_wrappers import wrap_model
  from ml_wrappers.common.constants import Device
  from ml_wrappers.model.image_model_wrapper import MLflowDRiseWrapper, PytorchDRiseWrapper
  from PIL import Image, ImageDraw, ImageFont
  from shap.plots import colors
  from shap.utils._legacy import kmeans
  from vision_explanation_methods.DRISE_runner import get_drise_saliency_map
  from responsibleai._interfaces import ModelExplanationData
  from responsibleai._internal.constants import ExplainerManagerKeys
  from responsibleai._internal.constants import ListProperties, ManagerNames, Metadata
  from responsibleai._tools.shared.state_directory_management import DirectoryManager
  from responsibleai.exceptions import UserConfigValidationException
  from responsibleai.managers.base_manager import BaseManager
  from responsibleai_vision.common.constants import CommonTags, ExplainabilityDefaults, ExplainabilityLiterals, MLFlowSchemaLiterals, ModelTask, XAIPredictionLiterals
  from responsibleai_vision.utils.image_reader import get_base64_string_from_path, get_image_from_path, is_automl_image_model
label_desc: Use image node_id 4 for calling the ExplainerManager obj's underlying member method code with example usage: obj.compute_single_explanation(index, max_evals, object_index) and returns: b64_string, self, self, self, visualization, fl, list