docstr: A context-aware drift detector based on a conditional analogue of the maximum mean discrepancy (MMD).
  Only detects differences between samples that can not be attributed to differences between associated
  sets of contexts. p-values are computed using a conditional permutation test.
  
  Parameters
  ----------
  x_ref
      Data used as reference distribution.
  c_ref
      Context for the reference distribution.
  backend
      Backend used for the MMD implementation.
  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).
  device
      Device type used. The default tries to use the GPU and falls back on CPU if needed.
      Can be specified by passing either ``'cuda'``, ``'gpu'``, ``'cpu'`` or an instance of
      ``torch.device``. Only relevant for 'pytorch' backend.
  input_shape
      Shape of input data.
  data_type
      Optionally specify the data type (tabular, image or time-series). Added to metadata.
  verbose
      Whether to print progress messages.
docstr_tok: ["A","context-aware","drift","detector","based","on","a","conditional","analogue","of","the","maximum","mean","discrepancy","(","MMD",")",".","Only","detects","differences","between","samples","that","can","not","be","attributed","to","differences","between","associated","sets","of","contexts",".","p-values","are","computed","using","a","conditional","permutation","test",".","Parameters","--","--","--","--","--","x_ref","Data","used","as","reference","distribution",".","c_ref","Context","for","the","reference","distribution",".","backend","Backend","used","for","the","MMD","implementation",".","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",")",".","device","Device","type","used",".","The","default","tries","to","use","the","GPU","and","falls","back","on","CPU","if","needed",".","Can","be","specified","by","passing","either","``","'cuda","'","``",",","``","'gpu","'","``",",","``","'cpu","'","``","or","an","instance","of","``","torch.device","``",".","Only","relevant","for","'pytorch","'","backend",".","input_shape","Shape","of","input","data",".","data_type","Optionally","specify","the","data","type","(","tabular",",","image","or","time-series",")",".","Added","to","metadata",".","verbose","Whether","to","print","progress","messages","."]
code: def __init__(
      self,
      x_ref: Union[np.ndarray, list],
      c_ref: np.ndarray,
      backend: str = "tensorflow",
      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,
      device: TorchDeviceType = None,
      input_shape: Optional[tuple] = None,
      data_type: Optional[str] = None,
      verbose: bool = False,
  ) -> None:
      """
      A context-aware drift detector based on a conditional analogue of the maximum mean discrepancy (MMD).
      Only detects differences between samples that can not be attributed to differences between associated
      sets of contexts. p-values are computed using a conditional permutation test.
  
      Parameters
      ----------
      x_ref
          Data used as reference distribution.
      c_ref
          Context for the reference distribution.
      backend
          Backend used for the MMD implementation.
      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).
      device
          Device type used. The default tries to use the GPU and falls back on CPU if needed.
          Can be specified by passing either ``'cuda'``, ``'gpu'``, ``'cpu'`` or an instance of
          ``torch.device``. Only relevant for 'pytorch' backend.
      input_shape
          Shape of input data.
      data_type
          Optionally specify the data type (tabular, image or time-series). Added to metadata.
      verbose
          Whether to print progress messages.
      """
      super().__init__()
  
      # Set config
      self._set_config(locals())
  
      backend = backend.lower()
      BackendValidator(
          backend_options={
              Framework.TENSORFLOW: [Framework.TENSORFLOW],
              Framework.PYTORCH: [Framework.PYTORCH],
          },
          construct_name=self.__class__.__name__,
      ).verify_backend(backend)
  
      kwargs = locals()
      args = [kwargs["x_ref"], kwargs["c_ref"]]
      pop_kwargs = ["self", "x_ref", "c_ref", "backend", "__class__"]
      [kwargs.pop(k, None) for k in pop_kwargs]
  
      if x_kernel is None or c_kernel is None:
          if backend == Framework.TENSORFLOW:
              from alibi_detect.utils.tensorflow.kernels import (
                  GaussianRBF,
              )
          else:
              from alibi_detect.utils.pytorch.kernels import GaussianRBF  # type: ignore[assignment]
          if x_kernel is None:
              kwargs.update({"x_kernel": GaussianRBF})
          if c_kernel is None:
              kwargs.update({"c_kernel": GaussianRBF})
  
      if backend == Framework.TENSORFLOW:
          kwargs.pop("device", None)
          self._detector = ContextMMDDriftTF(*args, **kwargs)
      else:
          self._detector = ContextMMDDriftTorch(*args, **kwargs)
      self.meta = self._detector.meta
  
file_path: alibi-detect/alibi_detect/cd/context_aware.py
imports: import logging
  import numpy
  from typing import Callable, Dict, Optional, Union, Tuple
  from alibi_detect.utils.frameworks import has_pytorch, has_tensorflow, BackendValidator, Framework
  from alibi_detect.utils.warnings import deprecated_alias
  from alibi_detect.base import DriftConfigMixin
  from alibi_detect.utils._types import TorchDeviceType
label_desc: Use image node_id 1 to create a new ContextMMDDrift object from inherited base classes: DriftConfigMixin with example: obj = ContextMMDDrift(x_ref, c_ref, backend, 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, device, input_shape, data_type, verbose)