docstr: Explain instance and return anchor with metadata.
  
  Parameters
  ----------
  image
      Image to be explained.
  p_sample
      The probability of simulating the absence of a superpixel. If the `images_background` is not provided,
      the absent superpixels will be replaced by the average value of their constituent pixels. Otherwise,
      the synthetic instances are created by fixing the present superpixels and superimposing another image
      from the `images_background` over the rest of the absent superpixels.
  threshold
      Minimum anchor precision threshold. The algorithm tries to find an anchor that maximizes the coverage
      under precision constraint. The precision constraint is formally defined as
      :math:`P(prec(A) \ge t) \ge 1 - \delta`, where :math:`A` is an anchor, :math:`t` is the `threshold`
      parameter, :math:`\delta` is the `delta` parameter, and :math:`prec(\cdot)` denotes the precision
      of an anchor. In other words, we are seeking for an anchor having its precision greater or equal than
      the given `threshold` with a confidence of `(1 - delta)`. A higher value guarantees that the anchors are
      faithful to the model, but also leads to more computation time. Note that there are cases in which the
      precision constraint cannot be satisfied due to the quantile-based discretisation of the numerical
      features. If that is the case, the best (i.e. highest coverage) non-eligible anchor is returned.
  delta
      Significance threshold. `1 - delta` represents the confidence threshold for the anchor precision
      (see `threshold`) and the selection of the best anchor candidate in each iteration (see `tau`).
  tau
      Multi-armed bandit parameter used to select candidate anchors in each iteration. The multi-armed bandit
      algorithm tries to find within a tolerance `tau` the most promising (i.e. according to the precision)
      `beam_size` candidate anchor(s) from a list of proposed anchors. Formally, when the `beam_size=1`,
      the multi-armed bandit algorithm seeks to find an anchor :math:`A` such that
      :math:`P(prec(A) \ge prec(A^\star) - \tau) \ge 1 - \delta`, where :math:`A^\star` is the anchor
      with the highest true precision (which we don't know), :math:`\tau` is the `tau` parameter,
      :math:`\delta` is the `delta` parameter, and :math:`prec(\cdot)` denotes the precision of an anchor.
      In other words, in each iteration, the algorithm returns with a probability of at least `1 - delta` an
      anchor :math:`A` with a precision within an error tolerance of `tau` from the precision of the
      highest true precision anchor :math:`A^\star`. A bigger value for `tau` means faster convergence but also
      looser anchor conditions.
  batch_size
      Batch size used for sampling. The Anchor algorithm will query the black-box model in batches of size
      `batch_size`. A larger `batch_size` gives more confidence in the anchor, again at the expense of
      computation time since it involves more model prediction calls.
  coverage_samples
      Number of samples used to estimate coverage from during result search.
  beam_size
      Number of candidate anchors selected by the multi-armed bandit algorithm in each iteration from a list of
      proposed anchors. A bigger beam  width can lead to a better overall anchor (i.e. prevents the algorithm
      of getting stuck in a local maximum) at the expense of more computation time.
  stop_on_first
      If ``True``, the beam search algorithm will return the first anchor that has satisfies the
      probability constraint.
  max_anchor_size
      Maximum number of features in result.
  min_samples_start
      Min number of initial samples.
  n_covered_ex
      How many examples where anchors apply to store for each anchor sampled during search
      (both examples where prediction on samples agrees/disagrees with `desired_label` are stored).
  binary_cache_size
      The result search pre-allocates `binary_cache_size` batches for storing the binary arrays
      returned during sampling.
  cache_margin
      When only ``max(cache_margin, batch_size)`` positions in the binary cache remain empty, a new cache
      of the same size is pre-allocated to continue buffering samples.
  verbose
      Display updates during the anchor search iterations.
  verbose_every
      Frequency of displayed iterations during anchor search process.
  
  Returns
  -------
  explanation
      `Explanation` object containing the anchor explaining the instance with additional metadata as attributes.
      See usage at `AnchorImage examples`_ for details.
  
      .. _AnchorImage examples:
          https://docs.seldon.io/projects/alibi/en/stable/methods/Anchors.html
docstr_tok: ["Explain","instance","and","return","anchor","with","metadata",".","Parameters","--","--","--","--","--","image","Image","to","be","explained",".","p_sample","The","probability","of","simulating","the","absence","of","a","superpixel",".","If","the","`","images_background","`","is","not","provided",",","the","absent","superpixels","will","be","replaced","by","the","average","value","of","their","constituent","pixels",".","Otherwise",",","the","synthetic","instances","are","created","by","fixing","the","present","superpixels","and","superimposing","another","image","from","the","`","images_background","`","over","the","rest","of","the","absent","superpixels",".","threshold","Minimum","anchor","precision","threshold",".","The","algorithm","tries","to","find","an","anchor","that","maximizes","the","coverage","under","precision","constraint",".","The","precision","constraint","is","formally","defined","as",":","math",":","`","P","(","prec","(","A",")","\\ge","t",")","\\ge","1","-","\\delta","`",",","where",":","math",":","`","A","`","is","an","anchor",",",":","math",":","`","t","`","is","the","`","threshold","`","parameter",",",":","math",":","`","\\delta","`","is","the","`","delta","`","parameter",",","and",":","math",":","`","prec","(","\\cdot",")","`","denotes","the","precision","of","an","anchor",".","In","other","words",",","we","are","seeking","for","an","anchor","having","its","precision","greater","or","equal","than","the","given","`","threshold","`","with","a","confidence","of","`","(","1","-","delta",")","`",".","A","higher","value","guarantees","that","the","anchors","are","faithful","to","the","model",",","but","also","leads","to","more","computation","time",".","Note","that","there","are","cases","in","which","the","precision","constraint","can","not","be","satisfied","due","to","the","quantile-based","discretisation","of","the","numerical","features",".","If","that","is","the","case",",","the","best","(","i.e",".","highest","coverage",")","non-eligible","anchor","is","returned",".","delta","Significance","threshold",".","`","1","-","delta","`","represents","the","confidence","threshold","for","the","anchor","precision","(","see","`","threshold","`",")","and","the","selection","of","the","best","anchor","candidate","in","each","iteration","(","see","`","tau","`",")",".","tau","Multi-armed","bandit","parameter","used","to","select","candidate","anchors","in","each","iteration",".","The","multi-armed","bandit","algorithm","tries","to","find","within","a","tolerance","`","tau","`","the","most","promising","(","i.e",".","according","to","the","precision",")","`","beam_size","`","candidate","anchor","(","s",")","from","a","list","of","proposed","anchors",".","Formally",",","when","the","`","beam_size=1","`",",","the","multi-armed","bandit","algorithm","seeks","to","find","an","anchor",":","math",":","`","A","`","such","that",":","math",":","`","P","(","prec","(","A",")","\\ge","prec","(","A^\\star",")","-","\\tau",")","\\ge","1","-","\\delta","`",",","where",":","math",":","`","A^\\star","`","is","the","anchor","with","the","highest","true","precision","(","which","we","do","n't","know",")",",",":","math",":","`","\\tau","`","is","the","`","tau","`","parameter",",",":","math",":","`","\\delta","`","is","the","`","delta","`","parameter",",","and",":","math",":","`","prec","(","\\cdot",")","`","denotes","the","precision","of","an","anchor",".","In","other","words",",","in","each","iteration",",","the","algorithm","returns","with","a","probability","of","at","least","`","1","-","delta","`","an","anchor",":","math",":","`","A","`","with","a","precision","within","an","error","tolerance","of","`","tau","`","from","the","precision","of","the","highest","true","precision","anchor",":","math",":","`","A^\\star","`",".","A","bigger","value","for","`","tau","`","means","faster","convergence","but","also","looser","anchor","conditions",".","batch_size","Batch","size","used","for","sampling",".","The","Anchor","algorithm","will","query","the","black-box","model","in","batches","of","size","`","batch_size","`",".","A","larger","`","batch_size","`","gives","more","confidence","in","the","anchor",",","again","at","the","expense","of","computation","time","since","it","involves","more","model","prediction","calls",".","coverage_samples","Number","of","samples","used","to","estimate","coverage","from","during","result","search",".","beam_size","Number","of","candidate","anchors","selected","by","the","multi-armed","bandit","algorithm","in","each","iteration","from","a","list","of","proposed","anchors",".","A","bigger","beam","width","can","lead","to","a","better","overall","anchor","(","i.e",".","prevents","the","algorithm","of","getting","stuck","in","a","local","maximum",")","at","the","expense","of","more","computation","time",".","stop_on_first","If","``","True","``",",","the","beam","search","algorithm","will","return","the","first","anchor","that","has","satisfies","the","probability","constraint",".","max_anchor_size","Maximum","number","of","features","in","result",".","min_samples_start","Min","number","of","initial","samples",".","n_covered_ex","How","many","examples","where","anchors","apply","to","store","for","each","anchor","sampled","during","search","(","both","examples","where","prediction","on","samples","agrees\/disagrees","with","`","desired_label","`","are","stored",")",".","binary_cache_size","The","result","search","pre-allocates","`","binary_cache_size","`","batches","for","storing","the","binary","arrays","returned","during","sampling",".","cache_margin","When","only","``","max","(","cache_margin",",","batch_size",")","``","positions","in","the","binary","cache","remain","empty",",","a","new","cache","of","the","same","size","is","pre-allocated","to","continue","buffering","samples",".","verbose","Display","updates","during","the","anchor","search","iterations",".","verbose_every","Frequency","of","displayed","iterations","during","anchor","search","process",".","Returns","--","--","--","-","explanation","`","Explanation","`","object","containing","the","anchor","explaining","the","instance","with","additional","metadata","as","attributes",".","See","usage","at","`","AnchorImage","examples","`","_","for","details",".","..","_AnchorImage","examples",":","https",":","\/\/docs.seldon.io\/projects\/alibi\/en\/stable\/methods\/Anchors.html"]
code: def explain(
      self,  # type: ignore[override]
      image: np.ndarray,
      p_sample: float = 0.5,
      threshold: float = 0.95,
      delta: float = 0.1,
      tau: float = 0.15,
      batch_size: int = 100,
      coverage_samples: int = 10000,
      beam_size: int = 1,
      stop_on_first: bool = False,
      max_anchor_size: Optional[int] = None,
      min_samples_start: int = 100,
      n_covered_ex: int = 10,
      binary_cache_size: int = 10000,
      cache_margin: int = 1000,
      verbose: bool = False,
      verbose_every: int = 1,
      **kwargs: Any,
  ) -> Explanation:
      """
      Explain instance and return anchor with metadata.
  
      Parameters
      ----------
      image
          Image to be explained.
      p_sample
          The probability of simulating the absence of a superpixel. If the `images_background` is not provided,
          the absent superpixels will be replaced by the average value of their constituent pixels. Otherwise,
          the synthetic instances are created by fixing the present superpixels and superimposing another image
          from the `images_background` over the rest of the absent superpixels.
      threshold
          Minimum anchor precision threshold. The algorithm tries to find an anchor that maximizes the coverage
          under precision constraint. The precision constraint is formally defined as
          :math:`P(prec(A) \\ge t) \\ge 1 - \\delta`, where :math:`A` is an anchor, :math:`t` is the `threshold`
          parameter, :math:`\\delta` is the `delta` parameter, and :math:`prec(\\cdot)` denotes the precision
          of an anchor. In other words, we are seeking for an anchor having its precision greater or equal than
          the given `threshold` with a confidence of `(1 - delta)`. A higher value guarantees that the anchors are
          faithful to the model, but also leads to more computation time. Note that there are cases in which the
          precision constraint cannot be satisfied due to the quantile-based discretisation of the numerical
          features. If that is the case, the best (i.e. highest coverage) non-eligible anchor is returned.
      delta
          Significance threshold. `1 - delta` represents the confidence threshold for the anchor precision
          (see `threshold`) and the selection of the best anchor candidate in each iteration (see `tau`).
      tau
          Multi-armed bandit parameter used to select candidate anchors in each iteration. The multi-armed bandit
          algorithm tries to find within a tolerance `tau` the most promising (i.e. according to the precision)
          `beam_size` candidate anchor(s) from a list of proposed anchors. Formally, when the `beam_size=1`,
          the multi-armed bandit algorithm seeks to find an anchor :math:`A` such that
          :math:`P(prec(A) \\ge prec(A^\\star) - \\tau) \\ge 1 - \\delta`, where :math:`A^\\star` is the anchor
          with the highest true precision (which we don't know), :math:`\\tau` is the `tau` parameter,
          :math:`\\delta` is the `delta` parameter, and :math:`prec(\\cdot)` denotes the precision of an anchor.
          In other words, in each iteration, the algorithm returns with a probability of at least `1 - delta` an
          anchor :math:`A` with a precision within an error tolerance of `tau` from the precision of the
          highest true precision anchor :math:`A^\\star`. A bigger value for `tau` means faster convergence but also
          looser anchor conditions.
      batch_size
          Batch size used for sampling. The Anchor algorithm will query the black-box model in batches of size
          `batch_size`. A larger `batch_size` gives more confidence in the anchor, again at the expense of
          computation time since it involves more model prediction calls.
      coverage_samples
          Number of samples used to estimate coverage from during result search.
      beam_size
          Number of candidate anchors selected by the multi-armed bandit algorithm in each iteration from a list of
          proposed anchors. A bigger beam  width can lead to a better overall anchor (i.e. prevents the algorithm
          of getting stuck in a local maximum) at the expense of more computation time.
      stop_on_first
          If ``True``, the beam search algorithm will return the first anchor that has satisfies the
          probability constraint.
      max_anchor_size
          Maximum number of features in result.
      min_samples_start
          Min number of initial samples.
      n_covered_ex
          How many examples where anchors apply to store for each anchor sampled during search
          (both examples where prediction on samples agrees/disagrees with `desired_label` are stored).
      binary_cache_size
          The result search pre-allocates `binary_cache_size` batches for storing the binary arrays
          returned during sampling.
      cache_margin
          When only ``max(cache_margin, batch_size)`` positions in the binary cache remain empty, a new cache
          of the same size is pre-allocated to continue buffering samples.
      verbose
          Display updates during the anchor search iterations.
      verbose_every
          Frequency of displayed iterations during anchor search process.
  
      Returns
      -------
      explanation
          `Explanation` object containing the anchor explaining the instance with additional metadata as attributes.
          See usage at `AnchorImage examples`_ for details.
  
          .. _AnchorImage examples:
              https://docs.seldon.io/projects/alibi/en/stable/methods/Anchors.html
      """
      # get params for storage in meta
      params = locals()
      remove = ["image", "self"]
      for key in remove:
          params.pop(key)
  
      sampler = AnchorImageSampler(
          predictor=self.predictor,
          segmentation_fn=self.segmentation_fn,
          custom_segmentation=self.custom_segmentation,
          image=image,
          images_background=self.images_background,
          p_sample=p_sample,
          n_covered_ex=n_covered_ex,
      )
  
      # get anchors and add metadata
      mab = AnchorBaseBeam(
          samplers=[sampler],
          sample_cache_size=binary_cache_size,
          cache_margin=cache_margin,
          **kwargs,
      )
      result: Any = mab.anchor_beam(
          desired_confidence=threshold,
          delta=delta,
          epsilon=tau,
          batch_size=batch_size,
          coverage_samples=coverage_samples,
          beam_size=beam_size,
          stop_on_first=stop_on_first,
          max_anchor_size=max_anchor_size,
          min_samples_start=min_samples_start,
          verbose=verbose,
          verbose_every=verbose_every,
          **kwargs,
      )
  
      return self._build_explanation(
          image, result, sampler.instance_label, params, sampler
      )
  
imports: import copy
  import logging
  from functools import partial
  from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
  import numpy
  from skimage.segmentation import felzenszwalb, quickshift, slic
  from alibi.api.defaults import DEFAULT_DATA_ANCHOR_IMG, DEFAULT_META_ANCHOR
  from alibi.api.interfaces import Explainer, Explanation
  from alibi.exceptions import PredictorCallError, PredictorReturnTypeError
  from alibi.utils.wrappers import ArgmaxTransformer
  from .anchor_base import AnchorBaseBeam
  from .anchor_explanation import AnchorExplanation
label_desc: Use image node_id 4 for calling the AnchorImage obj's underlying member method code with example usage: obj.explain(image, p_sample, threshold, delta, tau, batch_size, coverage_samples, beam_size, stop_on_first, max_anchor_size, min_samples_start, n_covered_ex, binary_cache_size, cache_margin, verbose, verbose_every) and returns: self