class_docstr: Implementation of Infidelity by Yeh et al., 2019.
  
  Explanation infidelity represents the expected mean square error
  between 1) a dot product of an attribution and input perturbation and
  2) difference in model output after significant perturbation.
  
  Assumptions:
      - The original implementation (https://github.com/chihkuanyeh/saliency_evaluation/
      blob/master/infid_sen_utils.py) supports perturbation of Gaussian noise and squared patches.
      In this implementation, we use squared patches as the default option.
      - Since we use squared patches in this implementation, the metric is only applicable
      to 3-dimensional (image) data. To extend the applicability to other data domains, adjustments
      to the current implementation might be necessary.
  
  References:
      1) Chih-Kuan Yeh et al.:
      "On the (In)fidelity and Sensitivity of Explanations."
      33rd Conference on Neural Information Processing Systems (NeurIPS 2019), Vancouver, Canada.
  
  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","Infidelity","by","Yeh","et","al.",",","2019",".","Explanation","infidelity","represents","the","expected","mean","square","error","between","1",")","a","dot","product","of","an","attribution","and","input","perturbation","and","2",")","difference","in","model","output","after","significant","perturbation",".","Assumptions",":","-","The","original","implementation","(","https",":","\/\/github.com\/chihkuanyeh\/saliency_evaluation\/","blob\/master\/infid_sen_utils.py",")","supports","perturbation","of","Gaussian","noise","and","squared","patches",".","In","this","implementation",",","we","use","squared","patches","as","the","default","option",".","-","Since","we","use","squared","patches","in","this","implementation",",","the","metric","is","only","applicable","to","3-dimensional","(","image",")","data",".","To","extend","the","applicability","to","other","data","domains",",","adjustments","to","the","current","implementation","might","be","necessary",".","References",":","1",")","Chih-Kuan","Yeh","et","al",".",":","``","On","the","(","In",")","fidelity","and","Sensitivity","of","Explanations",".","''","33rd","Conference","on","Neural","Information","Processing","Systems","(","NeurIPS","2019",")",",","Vancouver",",","Canada",".","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: Parameters
  ----------
  loss_func: string
      Loss function, default="mse".
  perturb_patch_sizes: list
      List of patch sizes to be perturbed. If None, the defaul is used, default=[4].
  features_in_step: integer
      The size of the step, default=1.
  n_perturb_samples: integer
      The number of samples to be perturbed, default=10.
  abs: boolean
      Indicates whether absolute operation is applied on the attribution, default=False.
  normalise: boolean
      Indicates whether normalise operation is applied on the attribution, default=False.
  normalise_func: callable
      Attribution normalisation function applied in case normalise=False.
      If normalise_func=None, the default value is used, default=normalise_by_max.
  normalise_func_kwargs: dict
      Keyword arguments to be passed to normalise_func on call, default={}.
  perturb_func: callable
      Input perturbation function. If None, the default value is used,
      default=baseline_replacement_by_indices.
  perturb_baseline: string
      Indicates the type of baseline: "mean", "random", "uniform", "black" or "white",
      default="black".
  perturb_func_kwargs: dict
      Keyword arguments to be passed to perturb_func, default={}.
  return_aggregate: boolean
      Indicates if an aggregated score should be computed over all instances.
  aggregate_func: callable
      Callable that aggregates the scores given an evaluation call.
  default_plot_func: callable
      Callable that plots the metrics result.
  disable_warnings: boolean
      Indicates whether the warnings are printed, default=False.
  display_progressbar: boolean
      Indicates whether a tqdm-progress-bar is printed, default=False.
  kwargs: optional
      Keyword arguments.
docstr_tok: ["Parameters","--","--","--","--","--","loss_func",":","string","Loss","function",",","default=","''","mse","''",".","perturb_patch_sizes",":","list","List","of","patch","sizes","to","be","perturbed",".","If","None",",","the","defaul","is","used",",","default=","[","4","]",".","features_in_step",":","integer","The","size","of","the","step",",","default=1",".","n_perturb_samples",":","integer","The","number","of","samples","to","be","perturbed",",","default=10",".","abs",":","boolean","Indicates","whether","absolute","operation","is","applied","on","the","attribution",",","default=False",".","normalise",":","boolean","Indicates","whether","normalise","operation","is","applied","on","the","attribution",",","default=False",".","normalise_func",":","callable","Attribution","normalisation","function","applied","in","case","normalise=False",".","If","normalise_func=None",",","the","default","value","is","used",",","default=normalise_by_max",".","normalise_func_kwargs",":","dict","Keyword","arguments","to","be","passed","to","normalise_func","on","call",",","default=","{","}",".","perturb_func",":","callable","Input","perturbation","function",".","If","None",",","the","default","value","is","used",",","default=baseline_replacement_by_indices",".","perturb_baseline",":","string","Indicates","the","type","of","baseline",":","``","mean","''",",","``","random","''",",","``","uniform","''",",","``","black","''","or","``","white","''",",","default=","''","black","''",".","perturb_func_kwargs",":","dict","Keyword","arguments","to","be","passed","to","perturb_func",",","default=","{","}",".","return_aggregate",":","boolean","Indicates","if","an","aggregated","score","should","be","computed","over","all","instances",".","aggregate_func",":","callable","Callable","that","aggregates","the","scores","given","an","evaluation","call",".","default_plot_func",":","callable","Callable","that","plots","the","metrics","result",".","disable_warnings",":","boolean","Indicates","whether","the","warnings","are","printed",",","default=False",".","display_progressbar",":","boolean","Indicates","whether","a","tqdm-progress-bar","is","printed",",","default=False",".","kwargs",":","optional","Keyword","arguments","."]
code: def __init__(
      self,
      loss_func: Union[str, Callable] = "mse",
      perturb_patch_sizes: List[int] = None,
      n_perturb_samples: int = 10,
      abs: bool = False,
      normalise: bool = False,
      normalise_func: Optional[
          Callable[[np.ndarray], np.ndarray]
      ] = None,
      normalise_func_kwargs: Optional[Dict[str, Any]] = None,
      perturb_func: Optional[Callable] = None,
      perturb_baseline: str = "black",
      perturb_func_kwargs: Optional[Dict[str, Any]] = None,
      return_aggregate: bool = False,
      aggregate_func: Optional[Callable] = None,
      default_plot_func: Optional[Callable] = None,
      disable_warnings: bool = False,
      display_progressbar: bool = False,
      **kwargs,
  ):
      """
      Parameters
      ----------
      loss_func: string
          Loss function, default="mse".
      perturb_patch_sizes: list
          List of patch sizes to be perturbed. If None, the defaul is used, default=[4].
      features_in_step: integer
          The size of the step, default=1.
      n_perturb_samples: integer
          The number of samples to be perturbed, default=10.
      abs: boolean
          Indicates whether absolute operation is applied on the attribution, default=False.
      normalise: boolean
          Indicates whether normalise operation is applied on the attribution, default=False.
      normalise_func: callable
          Attribution normalisation function applied in case normalise=False.
          If normalise_func=None, the default value is used, default=normalise_by_max.
      normalise_func_kwargs: dict
          Keyword arguments to be passed to normalise_func on call, default={}.
      perturb_func: callable
          Input perturbation function. If None, the default value is used,
          default=baseline_replacement_by_indices.
      perturb_baseline: string
          Indicates the type of baseline: "mean", "random", "uniform", "black" or "white",
          default="black".
      perturb_func_kwargs: dict
          Keyword arguments to be passed to perturb_func, default={}.
      return_aggregate: boolean
          Indicates if an aggregated score should be computed over all instances.
      aggregate_func: callable
          Callable that aggregates the scores given an evaluation call.
      default_plot_func: callable
          Callable that plots the metrics result.
      disable_warnings: boolean
          Indicates whether the warnings are printed, default=False.
      display_progressbar: boolean
          Indicates whether a tqdm-progress-bar is printed, default=False.
      kwargs: optional
          Keyword arguments.
      """
  
      super().__init__(
          abs=abs,
          normalise=normalise,
          normalise_func=normalise_func,
          normalise_func_kwargs=normalise_func_kwargs,
          return_aggregate=return_aggregate,
          aggregate_func=aggregate_func,
          default_plot_func=default_plot_func,
          display_progressbar=display_progressbar,
          disable_warnings=disable_warnings,
          **kwargs,
      )
  
      # Save metric-specific attributes.
      if isinstance(loss_func, str):
          if loss_func == "mse":
              loss_func = mse
          else:
              raise ValueError(
                  f"loss_func must be in ['mse'] but is: {loss_func}"
              )
      self.loss_func = loss_func
  
      if perturb_func is None:
          perturb_func = baseline_replacement_by_indices
  
      if perturb_patch_sizes is None:
          perturb_patch_sizes = [4]
      self.perturb_patch_sizes = perturb_patch_sizes
      self.n_perturb_samples = n_perturb_samples
      self.nr_channels = None
      self.perturb_func = make_perturb_func(
          perturb_func,
          perturb_func_kwargs,
          perturb_baseline=perturb_baseline,
      )
  
      # Asserts and warnings.
      if not self.disable_warnings:
          warn.warn_parameterisation(
              metric_name=self.__class__.__name__,
              sensitive_params=(
                  "baseline value 'perturb_baseline', perturbation function 'perturb_func',"
                  "number of perturbed samples 'n_perturb_samples', the loss function 'loss_func' "
                  "aggregation boolean 'aggregate'"
              ),
              data_domain_applicability=(
                  f"Also, the current implementation only works for 3-dimensional (image) data."
              ),
              citation=(
                  "Chih-Kuan, Yeh, et al. 'On the (In)fidelity and Sensitivity of Explanations'"
                  "arXiv:1901.09392 (2019)"
              ),
          )
  
file_path: Quantus/quantus/metrics/faithfulness/infidelity.py
imports: import sys
  from typing import Any, Callable, Dict, List, Optional, Union
  import numpy
  from quantus.functions.loss_func import mse
  from quantus.functions.perturb_func import baseline_replacement_by_indices
  from quantus.helpers import 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 1 to create a new Infidelity object from inherited base classes: Metric with example: obj = Infidelity(loss_func, perturb_patch_sizes, n_perturb_samples, abs, normalise, normalise_func, normalise_func_kwargs, perturb_func, perturb_baseline, perturb_func_kwargs, return_aggregate, aggregate_func, default_plot_func, disable_warnings, display_progressbar)