class_docstr: Implementation of the Continuity test by Montavon et al., 2018.
  
  The test measures the strongest variation of the explanation in the input domain i.e.,
  ||R(x) - R(x')||_1 / ||x - x'||_2
  where R(x) is the explanation for input x and x' is the perturbed input.
  
  Assumptions:
      - The original metric definition relies on perturbation functionality suited only for images.
      Therefore, only apply the metric to 3-dimensional (image) data. To extend the applicablity
      to other data domains, adjustments to the current implementation might be necessary.
  
  References:
      1) Grégoire Montavon et al.: "Methods for interpreting and
      understanding deep neural networks." Digital Signal Processing 73 (2018): 1-15.
  
  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","the","Continuity","test","by","Montavon","et","al.",",","2018",".","The","test","measures","the","strongest","variation","of","the","explanation","in","the","input","domain","i.e.",",","||R","(","x",")","-","R","(","x","'",")","||_1","\/","||x","-","x'||_2","where","R","(","x",")","is","the","explanation","for","input","x","and","x","'","is","the","perturbed","input",".","Assumptions",":","-","The","original","metric","definition","relies","on","perturbation","functionality","suited","only","for","images",".","Therefore",",","only","apply","the","metric","to","3-dimensional","(","image",")","data",".","To","extend","the","applicablity","to","other","data","domains",",","adjustments","to","the","current","implementation","might","be","necessary",".","References",":","1",")","Gr\u00e9goire","Montavon","et","al",".",":","``","Methods","for","interpreting","and","understanding","deep","neural","networks",".","''","Digital","Signal","Processing","73","(","2018",")",":","1-15",".","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: Evaluate instance gets model and data for a single instance as input and returns the evaluation result.
  
  Parameters
  ----------
  model: ModelInterface
      A ModelInteface that is subject to explanation.
  x: np.ndarray
      The input to be evaluated on an instance-basis.
  y: np.ndarray
      The output to be evaluated on an instance-basis.
  
  Returns
  -------
  dict
      The evaluation results.
docstr_tok: ["Evaluate","instance","gets","model","and","data","for","a","single","instance","as","input","and","returns","the","evaluation","result",".","Parameters","--","--","--","--","--","model",":","ModelInterface","A","ModelInteface","that","is","subject","to","explanation",".","x",":","np.ndarray","The","input","to","be","evaluated","on","an","instance-basis",".","y",":","np.ndarray","The","output","to","be","evaluated","on","an","instance-basis",".","Returns","--","--","--","-","dict","The","evaluation","results","."]
code: def evaluate_instance(
      self, model: ModelInterface, x: np.ndarray, y: np.ndarray
  ) -> Dict[int, List[Any]]:
      """
      Evaluate instance gets model and data for a single instance as input and returns the evaluation result.
  
      Parameters
      ----------
      model: ModelInterface
          A ModelInteface that is subject to explanation.
      x: np.ndarray
          The input to be evaluated on an instance-basis.
      y: np.ndarray
          The output to be evaluated on an instance-basis.
  
      Returns
      -------
      dict
          The evaluation results.
      """
      results: Dict[int, list] = {
          k: [] for k in range(self.nr_patches + 1)
      }
  
      for step in range(self.nr_steps):
          # Generate explanation based on perturbed input x.
          dx_step = (step + 1) * self.dx
          x_perturbed = self.perturb_func(
              arr=x,
              indices=np.arange(0, x.size),
              indexed_axes=np.arange(0, x.ndim),
              perturb_dx=dx_step,
          )
          x_input = model.shape_input(
              x_perturbed, x.shape, channel_first=True
          )
  
          prediction_changed = (
              self.return_nan_when_prediction_changes
              and model.predict(np.expand_dims(x, 0)).argmax(axis=-1)[0]
              != model.predict(x_input).argmax(axis=-1)[0]
          )
          # Taking the first element, since a_perturbed will be expanded to a batch dimension
          # not expected by the current index management functions.
          a_perturbed = self.explain_batch(
              model, x_input, np.expand_dims(y, 0)
          )[0]
  
          # Store the prediction score as the last element of the sub_self.evaluation_scores dictionary.
          y_pred = float(model.predict(x_input)[:, y])
  
          results[self.nr_patches].append(y_pred)
  
          # Create patches by splitting input into grid. Take x_input[0] to avoid batch axis,
          # which a_axes is not tuned for
          axis_iterators = [
              range(0, x_input[0].shape[axis], self.patch_size)
              for axis in self.a_axes
          ]
  
          for ix_patch, top_left_coords in enumerate(
              itertools.product(*axis_iterators)
          ):
              if prediction_changed:
                  results[ix_patch].append(np.nan)
                  continue
  
              # Create slice for patch.
              patch_slice = utils.create_patch_slice(
                  patch_size=self.patch_size,
                  coords=top_left_coords,
              )
  
              a_perturbed_patch = a_perturbed[
                  utils.expand_indices(
                      a_perturbed, patch_slice, self.a_axes
                  )
              ]
  
              # Taking the first element, since a_perturbed will be expanded to a batch dimension
              # not expected by the current index management functions.
              # a_perturbed = utils.expand_attribution_channel(a_perturbed, x_input)[0]
  
              if self.normalise:
                  a_perturbed_patch = self.normalise_func(
                      a_perturbed_patch.flatten()
                  )
  
              if self.abs:
                  a_perturbed_patch = np.abs(
                      a_perturbed_patch.flatten()
                  )
  
              # Sum attributions for patch.
              patch_sum = float(sum(a_perturbed_patch))
              results[ix_patch].append(patch_sum)
  
      return results
  
file_path: Quantus/quantus/metrics/robustness/continuity.py
imports: import itertools
  import sys
  from typing import Any, Callable, Dict, List, Optional
  import numpy
  from quantus.functions.perturb_func import translation_x_direction
  from quantus.functions.similarity_func import lipschitz_constant
  from quantus.helpers import asserts, utils, 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_perturb_func
  from quantus.metrics.base import Metric
label_desc: Use image node_id 3 for calling the Continuity obj's underlying member method code with example usage: obj.evaluate_instance(model, x, y) and returns: results