docstr: Maximum Mean Discrepancy (MMD) based context aware drift detector.
  
  Parameters
  ----------
  x_ref
      Data used as reference distribution.
  c_ref
      Context for the 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_ref
      Reference data can optionally be updated to the last N instances seen by the detector.
      The parameter should be passed as a dictionary *{'last': N}*.
  preprocess_fn
      Function to preprocess the data before computing the data drift metrics.
  x_kernel
      Kernel defined on the input data, defaults to Gaussian RBF kernel.
  c_kernel
      Kernel defined on the context data, defaults to Gaussian RBF kernel.
  n_permutations
      Number of permutations used in the permutation test.
  prop_c_held
      Proportion of contexts held out to condition on.
  n_folds
      Number of cross-validation folds used when tuning the regularisation parameters.
  batch_size
      If not None, then compute batches of MMDs at a time (rather than all at once).
  input_shape
      Shape of input data.
  data_type
      Optionally specify the data type (tabular, image or time-series). Added to metadata.
  verbose
      Whether or not to print progress during configuration.
docstr_tok: ["Maximum","Mean","Discrepancy","(","MMD",")","based","context","aware","drift","detector",".","Parameters","--","--","--","--","--","x_ref","Data","used","as","reference","distribution",".","c_ref","Context","for","the","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_ref","Reference","data","can","optionally","be","updated","to","the","last","N","instances","seen","by","the","detector",".","The","parameter","should","be","passed","as","a","dictionary","*","{","'last","'",":","N","}","*",".","preprocess_fn","Function","to","preprocess","the","data","before","computing","the","data","drift","metrics",".","x_kernel","Kernel","defined","on","the","input","data",",","defaults","to","Gaussian","RBF","kernel",".","c_kernel","Kernel","defined","on","the","context","data",",","defaults","to","Gaussian","RBF","kernel",".","n_permutations","Number","of","permutations","used","in","the","permutation","test",".","prop_c_held","Proportion","of","contexts","held","out","to","condition","on",".","n_folds","Number","of","cross-validation","folds","used","when","tuning","the","regularisation","parameters",".","batch_size","If","not","None",",","then","compute","batches","of","MMDs","at","a","time","(","rather","than","all","at","once",")",".","input_shape","Shape","of","input","data",".","data_type","Optionally","specify","the","data","type","(","tabular",",","image","or","time-series",")",".","Added","to","metadata",".","verbose","Whether","or","not","to","print","progress","during","configuration","."]
code: def __init__(
      self,
      x_ref: Union[np.ndarray, list],
      c_ref: np.ndarray,
      p_val: float = 0.05,
      x_ref_preprocessed: bool = False,
      preprocess_at_init: bool = True,
      update_ref: Optional[Dict[str, int]] = None,
      preprocess_fn: Optional[Callable] = None,
      x_kernel: Callable = None,
      c_kernel: Callable = None,
      n_permutations: int = 1000,
      prop_c_held: float = 0.25,
      n_folds: int = 5,
      batch_size: Optional[int] = 256,
      input_shape: Optional[tuple] = None,
      data_type: Optional[str] = None,
      verbose: bool = False,
  ) -> None:
      """
      Maximum Mean Discrepancy (MMD) based context aware drift detector.
  
      Parameters
      ----------
      x_ref
          Data used as reference distribution.
      c_ref
          Context for the 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_ref
          Reference data can optionally be updated to the last N instances seen by the detector.
          The parameter should be passed as a dictionary *{'last': N}*.
      preprocess_fn
          Function to preprocess the data before computing the data drift metrics.
      x_kernel
          Kernel defined on the input data, defaults to Gaussian RBF kernel.
      c_kernel
          Kernel defined on the context data, defaults to Gaussian RBF kernel.
      n_permutations
          Number of permutations used in the permutation test.
      prop_c_held
          Proportion of contexts held out to condition on.
      n_folds
          Number of cross-validation folds used when tuning the regularisation parameters.
      batch_size
          If not None, then compute batches of MMDs at a time (rather than all at once).
      input_shape
          Shape of input data.
      data_type
          Optionally specify the data type (tabular, image or time-series). Added to metadata.
      verbose
          Whether or not to print progress during configuration.
      """
      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."
          )
  
      # 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.preprocess_fn = preprocess_fn
      self.n = len(x_ref)
      self.n_permutations = (
          n_permutations  # nb of iterations through permutation test
      )
      self.x_kernel = x_kernel
      self.c_kernel = c_kernel
      if len(c_ref) == self.n:
          self.c_ref = c_ref
      else:
          raise ValueError(
              "x_ref and c_ref should contain the same number of instances."
          )
  
      # store input shape for save and load functionality
      self.input_shape = get_input_shape(input_shape, x_ref)
  
      # Regularisation parameter tuning settings
      if n_folds > 1:
          self.n_folds = n_folds
      else:
          raise ValueError("The `n_folds` parameter must be > 1.")
      self.lams = None
  
      # Update ref attribute. Disallow res
      self.update_ref = update_ref
      if update_ref is not None:
          if "reservoir_sampling" in update_ref.keys():
              raise ValueError(
                  "The BaseContextMMDDrift detector doesn't currently support the `reservoir_sampling` "
                  "option in `update_ref`."
              )
  
      # Other attributes
      self.prop_c_held = prop_c_held
      self.batch_size = batch_size
      self.verbose = verbose
  
      # 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 BaseContextMMDDrift object from inherited base classes: BaseDetector with example: obj = BaseContextMMDDrift(x_ref, c_ref, p_val, x_ref_preprocessed, preprocess_at_init, update_ref, preprocess_fn, x_kernel, c_kernel, n_permutations, prop_c_held, n_folds, batch_size, input_shape, data_type, verbose)