class_docstr: Implementation of Max-Sensitivity by Yeh at el., 2019.
  
  Using Monte Carlo sampling-based approximation while measuring how explanations
  change under slight perturbation - the maximum sensitivity is captured.
  
  References:
      1) Chih-Kuan Yeh et al. "On the (in) fidelity and sensitivity for explanations."
      NeurIPS (2019): 10965-10976.
      2) Umang Bhatt et al.: "Evaluating and aggregating
      feature-based model explanations."  IJCAI (2020): 3016-3022.
  
  Attributes:
      -  _name: The name of the metric.
      - _data_applicability: The data types that the metric implementation currently supports.
      - _models: The model types that this metric can work with.
      - score_direction: How to interpret the scores, whether higher/ lower values are considered better.
      - evaluation_category: What property/ explanation quality that this metric measures.
class_docstr_tok: ["Implementation","of","Max-Sensitivity","by","Yeh","at","el.",",","2019",".","Using","Monte","Carlo","sampling-based","approximation","while","measuring","how","explanations","change","under","slight","perturbation","-","the","maximum","sensitivity","is","captured",".","References",":","1",")","Chih-Kuan","Yeh","et","al",".","``","On","the","(","in",")","fidelity","and","sensitivity","for","explanations",".","''","NeurIPS","(","2019",")",":","10965-10976",".","2",")","Umang","Bhatt","et","al",".",":","``","Evaluating","and","aggregating","feature-based","model","explanations",".","''","IJCAI","(","2020",")",":","3016-3022",".","Attributes",":","-","_name",":","The","name","of","the","metric",".","-","_data_applicability",":","The","data","types","that","the","metric","implementation","currently","supports",".","-","_models",":","The","model","types","that","this","metric","can","work","with",".","-","score_direction",":","How","to","interpret","the","scores",",","whether","higher\/","lower","values","are","considered","better",".","-","evaluation_category",":","What","property\/","explanation","quality","that","this","metric","measures","."]
docstr: Evaluates model and attributes on a single data batch and returns the batched evaluation result.
  
  Parameters
  ----------
  model: ModelInterface
      A ModelInteface that is subject to explanation.
  x_batch: np.ndarray
      The input to be evaluated on an instance-basis.
  y_batch: np.ndarray
      The output to be evaluated on an instance-basis.
  a_batch: np.ndarray
      The explanation to be evaluated on an instance-basis.
  kwargs:
      Unused.
  
  Returns
  -------
  scores_batch: np.ndarray
      The batched evaluation results.
docstr_tok: ["Evaluates","model","and","attributes","on","a","single","data","batch","and","returns","the","batched","evaluation","result",".","Parameters","--","--","--","--","--","model",":","ModelInterface","A","ModelInteface","that","is","subject","to","explanation",".","x_batch",":","np.ndarray","The","input","to","be","evaluated","on","an","instance-basis",".","y_batch",":","np.ndarray","The","output","to","be","evaluated","on","an","instance-basis",".","a_batch",":","np.ndarray","The","explanation","to","be","evaluated","on","an","instance-basis",".","kwargs",":","Unused",".","Returns","--","--","--","-","scores_batch",":","np.ndarray","The","batched","evaluation","results","."]
code: def evaluate_batch(
      self,
      model: ModelInterface,
      x_batch: np.ndarray,
      y_batch: np.ndarray,
      a_batch: np.ndarray,
      **kwargs,
  ) -> np.ndarray:
      """
      Evaluates model and attributes on a single data batch and returns the batched evaluation result.
  
      Parameters
      ----------
      model: ModelInterface
          A ModelInteface that is subject to explanation.
      x_batch: np.ndarray
          The input to be evaluated on an instance-basis.
      y_batch: np.ndarray
          The output to be evaluated on an instance-basis.
      a_batch: np.ndarray
          The explanation to be evaluated on an instance-basis.
      kwargs:
          Unused.
  
      Returns
      -------
      scores_batch: np.ndarray
          The batched evaluation results.
      """
      batch_size = x_batch.shape[0]
      similarities = np.zeros((batch_size, self.nr_samples)) * np.nan
  
      for step_id in range(self.nr_samples):
          # Perturb input.
          x_perturbed = perturb_batch(
              perturb_func=self.perturb_func,
              indices=np.tile(
                  np.arange(0, x_batch[0].size), (batch_size, 1)
              ),
              indexed_axes=np.arange(0, x_batch[0].ndim),
              arr=x_batch,
          )
  
          changed_prediction_indices = (
              self.changed_prediction_indices_func(
                  model, x_batch, x_perturbed
              )
          )
  
          for x_instance, x_instance_perturbed in zip(
              x_batch, x_perturbed
          ):
              warn.warn_perturbation_caused_no_change(
                  x=x_instance,
                  x_perturbed=x_instance_perturbed,
              )
  
          # Generate explanation based on perturbed input x.
          a_perturbed = self.explain_batch(model, x_perturbed, y_batch)
  
          # Measure similarity for each instance separately.
          for instance_id in range(batch_size):
              if instance_id in changed_prediction_indices:
                  similarities[instance_id, step_id] = np.nan
                  continue
  
              sensitivities = self.similarity_func(
                  a=a_batch[instance_id].flatten(),
                  b=a_perturbed[instance_id].flatten(),
              )
              numerator = self.norm_numerator(a=sensitivities)
              denominator = self.norm_denominator(
                  a=a_batch[instance_id].flatten()
              )
              sensitivities_norm = numerator / denominator
              similarities[instance_id, step_id] = sensitivities_norm
  
      return self.max_func(similarities, axis=1)
  
file_path: Quantus/quantus/metrics/robustness/max_sensitivity.py
imports: import sys
  from typing import Any, Callable, Dict, List, Optional
  import numpy
  from quantus.functions import norm_func
  from quantus.functions.perturb_func import perturb_batch, uniform_noise
  from quantus.functions.similarity_func import difference
  from quantus.helpers import asserts, warn
  from quantus.helpers.enums import DataType, EvaluationCategory, ModelType, ScoreDirection
  from quantus.helpers.model.model_interface import ModelInterface
  from quantus.helpers.perturbation_utils import make_changed_prediction_indices_func, make_perturb_func
  from quantus.metrics.base import Metric
label_desc: Use image node_id 3 for calling the MaxSensitivity obj's underlying member method code with example usage: obj.evaluate_batch(model, x_batch, y_batch, a_batch) and returns: self