class_docstr: Implementation of the Smooth MPRT by Hedström et al., 2023.
  
  The Smooth Model Parameter Randomisation adds a "denoising" preprocessing step to the original MPRT,
  where the explanations are averaged over N noisy samples before the similarity between the original-
  and fully random model's explanations is measured.
  
  References:
      1) Hedström, Anna, et al. "Sanity Checks Revisited: An Exploration to Repair the Model Parameter
      Randomisation Test." XAI in Action: Past, Present, and Future Applications. 2023.
  
  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","Smooth","MPRT","by","Hedstr\u00f6m","et","al.",",","2023",".","The","Smooth","Model","Parameter","Randomisation","adds","a","``","denoising","''","preprocessing","step","to","the","original","MPRT",",","where","the","explanations","are","averaged","over","N","noisy","samples","before","the","similarity","between","the","original-","and","fully","random","model","'s","explanations","is","measured",".","References",":","1",")","Hedstr\u00f6m",",","Anna",",","et","al",".","``","Sanity","Checks","Revisited",":","An","Exploration","to","Repair","the","Model","Parameter","Randomisation","Test",".","''","XAI","in","Action",":","Past",",","Present",",","and","Future","Applications",".","2023",".","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
  ----------
  similarity_func: callable
      Similarity function applied to compare input and perturbed input, default=correlation_spearman.
  layer_order: string
      Indicated whether the model is randomized cascadingly or independently.
      Set order=top_down for cascading randomization, set order=independent for independent randomization,
      default="independent".
  seed: integer
      Seed used for the random generator, default=42.
  nr_samples: integer
      The number of samples used to compute the average (denoised) explanations, default=50.
      The default value is set based on ImageNet experiment in the reference paper.
      Please update the value according to your use case.
  noise_magnitude: float
      The magnitude of the noise added to the input, default=0.1.
      The default value is set based on ImageNet experiment in the reference paper.
      Please update the value according to your use case.
  return_average_correlation: boolean
      Indicates whether to return one float per sample, computing the average
      correlation coefficient across the layers for a given sample.
  return_last_correlation: boolean
      Indicates whether to return one float per sample, computing the explanation
      correlation coefficient for the full model randomisation (not layer-wise) of a sample.
  skip_layers: boolean
      Indicates if explanation similarity should be computed only once; between the
      original and fully randomised model, instead of in a layer-by-layer basis.
  abs: boolean
      Indicates whether absolute operation is applied on the attribution, default=True.
  normalise: boolean
      Indicates whether normalise operation is applied on the attribution, default=True.
  normalise_func: callable
      Attribution normalisation function applied in case normalise=True.
      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={}.
  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","--","--","--","--","--","similarity_func",":","callable","Similarity","function","applied","to","compare","input","and","perturbed","input",",","default=correlation_spearman",".","layer_order",":","string","Indicated","whether","the","model","is","randomized","cascadingly","or","independently",".","Set","order=top_down","for","cascading","randomization",",","set","order=independent","for","independent","randomization",",","default=","''","independent","''",".","seed",":","integer","Seed","used","for","the","random","generator",",","default=42",".","nr_samples",":","integer","The","number","of","samples","used","to","compute","the","average","(","denoised",")","explanations",",","default=50",".","The","default","value","is","set","based","on","ImageNet","experiment","in","the","reference","paper",".","Please","update","the","value","according","to","your","use","case",".","noise_magnitude",":","float","The","magnitude","of","the","noise","added","to","the","input",",","default=0.1",".","The","default","value","is","set","based","on","ImageNet","experiment","in","the","reference","paper",".","Please","update","the","value","according","to","your","use","case",".","return_average_correlation",":","boolean","Indicates","whether","to","return","one","float","per","sample",",","computing","the","average","correlation","coefficient","across","the","layers","for","a","given","sample",".","return_last_correlation",":","boolean","Indicates","whether","to","return","one","float","per","sample",",","computing","the","explanation","correlation","coefficient","for","the","full","model","randomisation","(","not","layer-wise",")","of","a","sample",".","skip_layers",":","boolean","Indicates","if","explanation","similarity","should","be","computed","only","once",";","between","the","original","and","fully","randomised","model",",","instead","of","in","a","layer-by-layer","basis",".","abs",":","boolean","Indicates","whether","absolute","operation","is","applied","on","the","attribution",",","default=True",".","normalise",":","boolean","Indicates","whether","normalise","operation","is","applied","on","the","attribution",",","default=True",".","normalise_func",":","callable","Attribution","normalisation","function","applied","in","case","normalise=True",".","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=","{","}",".","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,
      similarity_func: Optional[Callable] = None,
      layer_order: str = "bottom_up",
      seed: int = 42,
      nr_samples: int = 50,
      noise_magnitude: float = 0.1,
      return_average_correlation: bool = False,
      return_last_correlation: bool = False,
      skip_layers: bool = False,
      abs: bool = True,
      normalise: bool = True,
      normalise_func: Optional[
          Callable[[np.ndarray], np.ndarray]
      ] = None,
      normalise_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
      ----------
      similarity_func: callable
          Similarity function applied to compare input and perturbed input, default=correlation_spearman.
      layer_order: string
          Indicated whether the model is randomized cascadingly or independently.
          Set order=top_down for cascading randomization, set order=independent for independent randomization,
          default="independent".
      seed: integer
          Seed used for the random generator, default=42.
      nr_samples: integer
          The number of samples used to compute the average (denoised) explanations, default=50.
          The default value is set based on ImageNet experiment in the reference paper.
          Please update the value according to your use case.
      noise_magnitude: float
          The magnitude of the noise added to the input, default=0.1.
          The default value is set based on ImageNet experiment in the reference paper.
          Please update the value according to your use case.
      return_average_correlation: boolean
          Indicates whether to return one float per sample, computing the average
          correlation coefficient across the layers for a given sample.
      return_last_correlation: boolean
          Indicates whether to return one float per sample, computing the explanation
          correlation coefficient for the full model randomisation (not layer-wise) of a sample.
      skip_layers: boolean
          Indicates if explanation similarity should be computed only once; between the
          original and fully randomised model, instead of in a layer-by-layer basis.
      abs: boolean
          Indicates whether absolute operation is applied on the attribution, default=True.
      normalise: boolean
          Indicates whether normalise operation is applied on the attribution, default=True.
      normalise_func: callable
          Attribution normalisation function applied in case normalise=True.
          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={}.
      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 similarity_func is None:
          similarity_func = correlation_spearman
      self.similarity_func = similarity_func
      self.layer_order = layer_order
      self.seed = seed
      self.nr_samples = nr_samples
      self.noise_magnitude = noise_magnitude
      self.return_average_correlation = return_average_correlation
      self.return_last_correlation = return_last_correlation
      self.skip_layers = skip_layers
  
      # Results are returned/saved as a dictionary not like in the super-class as a list.
      self.evaluation_scores = {}
  
      # Asserts and warnings.
      if (
          self.return_average_correlation
          and self.return_last_correlation
      ):
          raise ValueError(
              f"Both 'return_average_correlation' and 'return_last_correlation' cannot be set to 'True'. "
              f"Set both to 'False' or one of the attributes to 'True'."
          )
      if self.return_average_correlation and self.skip_layers:
          raise ValueError(
              f"Both 'return_average_correlation' and 'skip_layers' cannot be set to 'True'. "
              f"You need to calculate the explanation correlation at all layers in order "
              f"to compute the average correlation coefficient on all layers."
          )
      asserts.assert_layer_order(layer_order=self.layer_order)
      if not self.disable_warnings:
          warn.warn_parameterisation(
              metric_name=self.__class__.__name__,
              sensitive_params=(
                  "similarity metric 'similarity_func' and the order of "
                  "the layer randomisation 'layer_order', the number of samples 'nr_samples', and"
                  " the magnitude of noise 'noise_magnitude' "
              ),
              citation=(
                  'Hedström, Anna, et al. "Sanity Checks Revisited: An Exploration to Repair'
                  ' the Model Parameter Randomisation Test." XAI in Action: Past, Present, '
                  "and Future Applications. 2023."
              ),
          )
  
imports: import sys
  import warnings
  from typing import Any, Callable, Collection, Dict, List, Optional, Union, Tuple, Generator, Iterable
  from importlib import util
  import numpy
  import quantus
  from tqdm.auto import tqdm
  from sklearn.utils import gen_batches
  from scipy import stats
  from quantus.functions.similarity_func import correlation_spearman
  from quantus.helpers import asserts, warn, utils
  from quantus.helpers.enums import DataType, EvaluationCategory, ModelType, ScoreDirection
  from quantus.helpers.model.model_interface import ModelInterface
  from quantus.metrics.base import Metric
label_desc: Use image node_id 1 to create a new SmoothMPRT object from inherited base classes: Metric with example: obj = SmoothMPRT(similarity_func, layer_order, seed, nr_samples, noise_magnitude, return_average_correlation, return_last_correlation, skip_layers, abs, normalise, normalise_func, normalise_func_kwargs, return_aggregate, aggregate_func, default_plot_func, disable_warnings, display_progressbar)