docstr: Maximum Mean Discrepancy (MMD) base data drift detector using a permutation test.
  
  Parameters
  ----------
  x_ref
      Data used as reference distribution.
  p_val
      p-value used for the significance of the permutation test.
  x_ref_preprocessed
      Whether the given reference data `x_ref` has been preprocessed yet. If `x_ref_preprocessed=True`, only
      the test data `x` will be preprocessed at prediction time. If `x_ref_preprocessed=False`, the reference
      data will also be preprocessed.
  preprocess_at_init
      Whether to preprocess the reference data when the detector is instantiated. Otherwise, the reference
      data will be preprocessed at prediction time. Only applies if `x_ref_preprocessed=False`.
  update_x_ref
      Reference data can optionally be updated to the last n instances seen by the detector
      or via reservoir sampling with size n. For the former, the parameter equals {'last': n} while
      for reservoir sampling {'reservoir_sampling': n} is passed.
  preprocess_fn
      Function to preprocess the data before computing the data drift metrics.
  sigma
      Optionally set the Gaussian RBF kernel bandwidth. Can also pass multiple bandwidth values as an array.
      The kernel evaluation is then averaged over those bandwidths.
  configure_kernel_from_x_ref
      Whether to already configure the kernel bandwidth from the reference data.
  n_permutations
      Number of permutations used in the permutation test.
  input_shape
      Shape of input data.
  data_type
      Optionally specify the data type (tabular, image or time-series). Added to metadata.
docstr_tok: ["Maximum","Mean","Discrepancy","(","MMD",")","base","data","drift","detector","using","a","permutation","test",".","Parameters","--","--","--","--","--","x_ref","Data","used","as","reference","distribution",".","p_val","p-value","used","for","the","significance","of","the","permutation","test",".","x_ref_preprocessed","Whether","the","given","reference","data","`","x_ref","`","has","been","preprocessed","yet",".","If","`","x_ref_preprocessed=True","`",",","only","the","test","data","`","x","`","will","be","preprocessed","at","prediction","time",".","If","`","x_ref_preprocessed=False","`",",","the","reference","data","will","also","be","preprocessed",".","preprocess_at_init","Whether","to","preprocess","the","reference","data","when","the","detector","is","instantiated",".","Otherwise",",","the","reference","data","will","be","preprocessed","at","prediction","time",".","Only","applies","if","`","x_ref_preprocessed=False","`",".","update_x_ref","Reference","data","can","optionally","be","updated","to","the","last","n","instances","seen","by","the","detector","or","via","reservoir","sampling","with","size","n.","For","the","former",",","the","parameter","equals","{","'last","'",":","n","}","while","for","reservoir","sampling","{","'reservoir_sampling","'",":","n","}","is","passed",".","preprocess_fn","Function","to","preprocess","the","data","before","computing","the","data","drift","metrics",".","sigma","Optionally","set","the","Gaussian","RBF","kernel","bandwidth",".","Can","also","pass","multiple","bandwidth","values","as","an","array",".","The","kernel","evaluation","is","then","averaged","over","those","bandwidths",".","configure_kernel_from_x_ref","Whether","to","already","configure","the","kernel","bandwidth","from","the","reference","data",".","n_permutations","Number","of","permutations","used","in","the","permutation","test",".","input_shape","Shape","of","input","data",".","data_type","Optionally","specify","the","data","type","(","tabular",",","image","or","time-series",")",".","Added","to","metadata","."]
code: def __init__(
      self,
      x_ref: Union[np.ndarray, list],
      p_val: float = 0.05,
      x_ref_preprocessed: bool = False,
      preprocess_at_init: bool = True,
      update_x_ref: Optional[Dict[str, int]] = None,
      preprocess_fn: Optional[Callable] = None,
      sigma: Optional[np.ndarray] = None,
      configure_kernel_from_x_ref: bool = True,
      n_permutations: int = 100,
      input_shape: Optional[tuple] = None,
      data_type: Optional[str] = None,
  ) -> None:
      """
      Maximum Mean Discrepancy (MMD) base data drift detector using a permutation test.
  
      Parameters
      ----------
      x_ref
          Data used as reference distribution.
      p_val
          p-value used for the significance of the permutation test.
      x_ref_preprocessed
          Whether the given reference data `x_ref` has been preprocessed yet. If `x_ref_preprocessed=True`, only
          the test data `x` will be preprocessed at prediction time. If `x_ref_preprocessed=False`, the reference
          data will also be preprocessed.
      preprocess_at_init
          Whether to preprocess the reference data when the detector is instantiated. Otherwise, the reference
          data will be preprocessed at prediction time. Only applies if `x_ref_preprocessed=False`.
      update_x_ref
          Reference data can optionally be updated to the last n instances seen by the detector
          or via reservoir sampling with size n. For the former, the parameter equals {'last': n} while
          for reservoir sampling {'reservoir_sampling': n} is passed.
      preprocess_fn
          Function to preprocess the data before computing the data drift metrics.
      sigma
          Optionally set the Gaussian RBF kernel bandwidth. Can also pass multiple bandwidth values as an array.
          The kernel evaluation is then averaged over those bandwidths.
      configure_kernel_from_x_ref
          Whether to already configure the kernel bandwidth from the reference data.
      n_permutations
          Number of permutations used in the permutation test.
      input_shape
          Shape of input data.
      data_type
          Optionally specify the data type (tabular, image or time-series). Added to metadata.
      """
      super().__init__()
  
      if p_val is None:
          logger.warning(
              "No p-value set for the drift threshold. Need to set it to detect data drift."
          )
  
      self.infer_sigma = configure_kernel_from_x_ref
      if configure_kernel_from_x_ref and isinstance(sigma, np.ndarray):
          self.infer_sigma = False
          logger.warning(
              "`sigma` is specified for the kernel and `configure_kernel_from_x_ref` "
              "is set to True. `sigma` argument takes priority over "
              "`configure_kernel_from_x_ref` (set to False)."
          )
  
      # x_ref preprocessing
      self.preprocess_at_init = preprocess_at_init
      self.x_ref_preprocessed = x_ref_preprocessed
      if preprocess_fn is not None and not isinstance(preprocess_fn, Callable):  # type: ignore[arg-type]
          raise ValueError("`preprocess_fn` is not a valid Callable.")
      if (
          self.preprocess_at_init
          and not self.x_ref_preprocessed
          and preprocess_fn is not None
      ):
          self.x_ref = preprocess_fn(x_ref)
      else:
          self.x_ref = x_ref
  
      # Other attributes
      self.p_val = p_val
      self.update_x_ref = update_x_ref
      self.preprocess_fn = preprocess_fn
      self.n = len(x_ref)
      self.n_permutations = (
          n_permutations  # nb of iterations through permutation test
      )
  
      # store input shape for save and load functionality
      self.input_shape = get_input_shape(input_shape, x_ref)
  
      # set metadata
      self.meta.update(
          {
              "detector_type": "drift",
              "online": False,
              "data_type": data_type,
          }
      )
  
imports: import logging
  from abc import abstractmethod
  from typing import Callable, Dict, List, Optional, Tuple, Union, Any
  import numpy
  from alibi_detect.base import BaseDetector, concept_drift_dict, DriftConfigMixin
  from alibi_detect.cd.utils import get_input_shape, update_reference
  from alibi_detect.utils.frameworks import has_pytorch, has_tensorflow
  from alibi_detect.utils.statstest import fdr
  from scipy.stats import binom_test, ks_2samp
  from sklearn.model_selection import StratifiedKFold
label_desc: Use image node_id 1 to create a new BaseMMDDrift object from inherited base classes: BaseDetector with example: obj = BaseMMDDrift(x_ref, p_val, x_ref_preprocessed, preprocess_at_init, update_x_ref, preprocess_fn, sigma, configure_kernel_from_x_ref, n_permutations, input_shape, data_type)