class_docstr: Interface defining Metrics' API.
docstr: Prepares all necessary variables for evaluation.
  
      - Reshapes data to channel first layout.
      - Wraps model into ModelInterface.
      - Creates attributions if necessary.
      - Expands attributions to data shape (adds channel dimension).
      - Calls custom_preprocess().
      - Normalises attributions if desired.
      - Takes absolute of attributions if desired.
      - If no segmentation s_batch given, creates list of Nones with as many
        elements as there are data instances.
      - If no custom_batch given, creates list of Nones with as many
        elements as there are data instances.
  
  Parameters
  ----------
  
  model: torch.nn.Module, tf.keras.Model
      A torch or tensorflow model e.g., torchvision.models that is subject to explanation.
  x_batch: np.ndarray
      A np.ndarray which contains the input data that are explained.
  y_batch: np.ndarray
      A np.ndarray which contains the output labels that are explained.
  a_batch: np.ndarray, optional
      A np.ndarray which contains pre-computed attributions i.e., explanations.
  s_batch: np.ndarray, optional
      A np.ndarray which contains segmentation masks that matches the input.
  channel_first: boolean, optional
      Indicates of the image dimensions are channel first, or channel last.
      Inferred from the input shape if None.
  explain_func: callable
      Callable generating attributions.
  explain_func_kwargs: dict, optional
      Keyword arguments to be passed to explain_func on call.
  model_predict_kwargs: dict, optional
      Keyword arguments to be passed to the model's predict method.
  softmax: boolean
      Indicates whether to use softmax probabilities or logits in model prediction.
      This is used for this __call__ only and won't be saved as attribute. If None, self.softmax is used.
  device: string
      Indicated the device on which a torch.Tensor is or will be allocated: "cpu" or "gpu".
  custom_batch: any
      Gives flexibility ot the user to use for evaluation, can hold any variable.
  
  Returns
  -------
  tuple
      A general preprocess.
docstr_tok: ["Prepares","all","necessary","variables","for","evaluation",".","-","Reshapes","data","to","channel","first","layout",".","-","Wraps","model","into","ModelInterface",".","-","Creates","attributions","if","necessary",".","-","Expands","attributions","to","data","shape","(","adds","channel","dimension",")",".","-","Calls","custom_preprocess","(",")",".","-","Normalises","attributions","if","desired",".","-","Takes","absolute","of","attributions","if","desired",".","-","If","no","segmentation","s_batch","given",",","creates","list","of","Nones","with","as","many","elements","as","there","are","data","instances",".","-","If","no","custom_batch","given",",","creates","list","of","Nones","with","as","many","elements","as","there","are","data","instances",".","Parameters","--","--","--","--","--","model",":","torch.nn.Module",",","tf.keras.Model","A","torch","or","tensorflow","model","e.g.",",","torchvision.models","that","is","subject","to","explanation",".","x_batch",":","np.ndarray","A","np.ndarray","which","contains","the","input","data","that","are","explained",".","y_batch",":","np.ndarray","A","np.ndarray","which","contains","the","output","labels","that","are","explained",".","a_batch",":","np.ndarray",",","optional","A","np.ndarray","which","contains","pre-computed","attributions","i.e.",",","explanations",".","s_batch",":","np.ndarray",",","optional","A","np.ndarray","which","contains","segmentation","masks","that","matches","the","input",".","channel_first",":","boolean",",","optional","Indicates","of","the","image","dimensions","are","channel","first",",","or","channel","last",".","Inferred","from","the","input","shape","if","None",".","explain_func",":","callable","Callable","generating","attributions",".","explain_func_kwargs",":","dict",",","optional","Keyword","arguments","to","be","passed","to","explain_func","on","call",".","model_predict_kwargs",":","dict",",","optional","Keyword","arguments","to","be","passed","to","the","model","'s","predict","method",".","softmax",":","boolean","Indicates","whether","to","use","softmax","probabilities","or","logits","in","model","prediction",".","This","is","used","for","this","__call__","only","and","wo","n't","be","saved","as","attribute",".","If","None",",","self.softmax","is","used",".","device",":","string","Indicated","the","device","on","which","a","torch.Tensor","is","or","will","be","allocated",":","``","cpu","''","or","``","gpu","''",".","custom_batch",":","any","Gives","flexibility","ot","the","user","to","use","for","evaluation",",","can","hold","any","variable",".","Returns","--","--","--","-","tuple","A","general","preprocess","."]
code: def general_preprocess(
      self,
      model: Union[keras.Model, nn.Module, None],
      x_batch: np.ndarray,
      y_batch: np.ndarray,
      a_batch: Optional[np.ndarray],
      s_batch: Optional[np.ndarray],
      channel_first: Optional[bool],
      explain_func: Callable,
      explain_func_kwargs: Optional[Dict[str, Any]],
      model_predict_kwargs: Optional[Dict[str, Any]],
      softmax: bool,
      device: Optional[str],
      custom_batch: Optional[np.ndarray],
  ) -> Dict[str, Any]:
      """
      Prepares all necessary variables for evaluation.
  
          - Reshapes data to channel first layout.
          - Wraps model into ModelInterface.
          - Creates attributions if necessary.
          - Expands attributions to data shape (adds channel dimension).
          - Calls custom_preprocess().
          - Normalises attributions if desired.
          - Takes absolute of attributions if desired.
          - If no segmentation s_batch given, creates list of Nones with as many
            elements as there are data instances.
          - If no custom_batch given, creates list of Nones with as many
            elements as there are data instances.
  
      Parameters
      ----------
  
      model: torch.nn.Module, tf.keras.Model
          A torch or tensorflow model e.g., torchvision.models that is subject to explanation.
      x_batch: np.ndarray
          A np.ndarray which contains the input data that are explained.
      y_batch: np.ndarray
          A np.ndarray which contains the output labels that are explained.
      a_batch: np.ndarray, optional
          A np.ndarray which contains pre-computed attributions i.e., explanations.
      s_batch: np.ndarray, optional
          A np.ndarray which contains segmentation masks that matches the input.
      channel_first: boolean, optional
          Indicates of the image dimensions are channel first, or channel last.
          Inferred from the input shape if None.
      explain_func: callable
          Callable generating attributions.
      explain_func_kwargs: dict, optional
          Keyword arguments to be passed to explain_func on call.
      model_predict_kwargs: dict, optional
          Keyword arguments to be passed to the model's predict method.
      softmax: boolean
          Indicates whether to use softmax probabilities or logits in model prediction.
          This is used for this __call__ only and won't be saved as attribute. If None, self.softmax is used.
      device: string
          Indicated the device on which a torch.Tensor is or will be allocated: "cpu" or "gpu".
      custom_batch: any
          Gives flexibility ot the user to use for evaluation, can hold any variable.
  
      Returns
      -------
      tuple
          A general preprocess.
  
      """
  
      # Reshape input batch to channel first order:
      if not isinstance(
          channel_first, bool
      ):  # None is not a boolean instance.
          channel_first = utils.infer_channel_first(x_batch)
      x_batch = utils.make_channel_first(x_batch, channel_first)
  
      if model is not None:
          # Use attribute value if not passed explicitly.
          model = utils.get_wrapped_model(
              model=model,
              channel_first=channel_first,
              softmax=softmax,
              device=device,
              model_predict_kwargs=model_predict_kwargs,
          )
  
      # Save as attribute, some metrics need it during processing.
      self.explain_func = explain_func
      self.explain_func_kwargs = explain_func_kwargs or {}
  
      # Include device in explain_func_kwargs.
      if (
          device is not None
          and "device" not in self.explain_func_kwargs
      ):
          self.explain_func_kwargs["device"] = device
  
      if a_batch is not None:
          a_batch = utils.expand_attribution_channel(a_batch, x_batch)
          asserts.assert_attributions(x_batch=x_batch, a_batch=a_batch)
          self.a_axes = utils.infer_attribution_axes(a_batch, x_batch)
  
          # Normalise with specified keyword arguments if requested.
          if self.normalise:
              a_batch = self.normalise_func(a_batch)
  
          # Take absolute if requested.
          if self.abs:
              a_batch = np.abs(a_batch)
  
      else:
          # If no explanations provided, we will compute them on batch-level to avoid OOM.
          asserts.assert_explain_func(explain_func=self.explain_func)
  
      # Initialize data dictionary.
      data = {
          "model": model,
          "x_batch": x_batch,
          "y_batch": y_batch,
          "a_batch": a_batch,
          "s_batch": s_batch,
          "custom_batch": custom_batch,
      }
  
      # Call custom pre-processing from inheriting class.
      custom_preprocess_dict = self.custom_preprocess(**data)
      # Save data coming from custom preprocess to data dict.
      if custom_preprocess_dict is not None:
          data.update(custom_preprocess_dict)
  
      # Remove custom_batch if not used.
      if data["custom_batch"] is None:
          del data["custom_batch"]
  
      return data
  
imports: from __future__ import annotations
  import functools
  import logging
  import os
  import sys
  from abc import abstractmethod
  from typing import Any, Callable, ClassVar, Dict, Generator, Generic, Sequence, Set, TypeVar, Optional, Union, TYPE_CHECKING, no_type_check
  import matplotlib.pyplot
  import numpy
  from sklearn.utils import gen_batches
  from tqdm.auto import tqdm
  from quantus.helpers import asserts, utils, warn
  from quantus.functions.normalise_func import normalise_by_max
  from quantus.helpers.enums import DataType, EvaluationCategory, ModelType, ScoreDirection
  from quantus.helpers.model.model_interface import ModelInterface
label_desc: Use image node_id 4 for calling the Metric obj's underlying member method code with example usage: obj.general_preprocess(model, x_batch, y_batch, a_batch, s_batch, channel_first, explain_func, explain_func_kwargs, model_predict_kwargs, softmax, device, custom_batch) and returns: data