docstr: Compute the p-value resulting from a permutation test using the maximum mean discrepancy
  as a distance measure between the reference data and the data to be tested. The kernel
  used within the MMD is first trained to maximise an estimate of the resulting test power.
  
  Parameters
  ----------
  x
      Batch of instances.
  
  Returns
  -------
  p-value obtained from the permutation test, the MMD^2 between the reference and test set,         and the MMD^2 threshold above which drift is flagged.
docstr_tok: ["Compute","the","p-value","resulting","from","a","permutation","test","using","the","maximum","mean","discrepancy","as","a","distance","measure","between","the","reference","data","and","the","data","to","be","tested",".","The","kernel","used","within","the","MMD","is","first","trained","to","maximise","an","estimate","of","the","resulting","test","power",".","Parameters","--","--","--","--","--","x","Batch","of","instances",".","Returns","--","--","--","-","p-value","obtained","from","the","permutation","test",",","the","MMD^2","between","the","reference","and","test","set",",","and","the","MMD^2","threshold","above","which","drift","is","flagged","."]
code: def score(
      self, x: Union[np.ndarray, list]
  ) -> Tuple[float, float, float]:
      """
          Compute the p-value resulting from a permutation test using the maximum mean discrepancy
          as a distance measure between the reference data and the data to be tested. The kernel
          used within the MMD is first trained to maximise an estimate of the resulting test power.
  
          Parameters
          ----------
          x
              Batch of instances.
  
          Returns
          -------
          p-value obtained from the permutation test, the MMD^2 between the reference and test set, \
          and the MMD^2 threshold above which drift is flagged.
          """
      x_ref, x_cur = self.preprocess(x)
      (x_ref_tr, x_cur_tr), (x_ref_te, x_cur_te) = self.get_splits(
          x_ref, x_cur
      )
      ds_ref_tr, ds_cur_tr = self.dataset(x_ref_tr), self.dataset(
          x_cur_tr
      )
  
      self.kernel = (
          clone_model(self.original_kernel)
          if self.retrain_from_scratch
          else self.kernel
      )
      train_args = [self.j_hat, (ds_ref_tr, ds_cur_tr)]
      LearnedKernelDriftTF.trainer(*train_args, **self.train_kwargs)
  
      if isinstance(x_ref_te, np.ndarray) and isinstance(
          x_cur_te, np.ndarray
      ):
          x_all = np.concatenate([x_ref_te, x_cur_te], axis=0)
      else:
          x_all = x_ref_te + x_cur_te
      kernel_mat = self.kernel_mat_fn(x_all, x_all, self.kernel)
      kernel_mat = kernel_mat - tf.linalg.diag(
          tf.linalg.diag_part(kernel_mat)
      )  # zero diagonal
      mmd2 = mmd2_from_kernel_matrix(
          kernel_mat, len(x_cur_te), permute=False, zero_diag=False
      ).numpy()
      mmd2_permuted = np.array(
          [
              mmd2_from_kernel_matrix(
                  kernel_mat,
                  len(x_cur_te),
                  permute=True,
                  zero_diag=False,
              ).numpy()
              for _ in range(self.n_permutations)
          ]
      )
      p_val = (mmd2 <= mmd2_permuted).mean()
  
      idx_threshold = int(self.p_val * len(mmd2_permuted))
      distance_threshold = np.sort(mmd2_permuted)[::-1][idx_threshold]
      return p_val, mmd2, distance_threshold
  
file_path: alibi-detect/alibi_detect/cd/tensorflow/learned_kernel.py
imports: from functools import partial
  import numpy
  import tensorflow
  from typing import Callable, Dict, Optional, Tuple, Union
  from alibi_detect.cd.base import BaseLearnedKernelDrift
  from alibi_detect.utils.tensorflow.data import TFDataset
  from alibi_detect.utils.tensorflow.misc import clone_model
  from alibi_detect.utils.tensorflow.distance import mmd2_from_kernel_matrix, batch_compute_kernel_matrix
  from alibi_detect.utils.warnings import deprecated_alias
  from alibi_detect.utils.frameworks import Framework
  from alibi_detect.utils._types import OptimizerTF
label_desc: Use image node_id 2 for calling the LearnedKernelDriftTF obj's underlying member method code with example usage: obj.score(x) and returns: p_val,  mmd2,  distance_threshold