docstr: Initialize anchor image explainer.
  
  Parameters
  ----------
  predictor
      A callable that takes a `numpy` array of `N` data points as inputs and returns `N` outputs.
  image_shape
      Shape of the image to be explained. The channel axis is expected to be last.
  dtype
      A `numpy` scalar type that corresponds to the type of input array expected by `predictor`. This may be
      used to construct arrays of the given type to be passed through the `predictor`. For most use cases
      this argument should have no effect, but it is exposed for use with predictors that would break when
      called with an array of unsupported type.
  segmentation_fn
      Any of the built in segmentation function strings: ``'felzenszwalb'``, ``'slic'`` or ``'quickshift'`` or
      a custom segmentation function (callable) which returns an image mask with labels for each superpixel.
      The segmentation function is expected to return a segmentation mask containing all integer values
      from `0` to `K-1`, where `K` is the number of image segments (superpixels).
      See http://scikit-image.org/docs/dev/api/skimage.segmentation.html for more info.
  segmentation_kwargs
      Keyword arguments for the built in segmentation functions.
  images_background
      Images to overlay superpixels on.
  seed
      If set, ensures different runs with the same input will yield same explanation.
  
  Raises
  ------
  :py:class:`alibi.exceptions.PredictorCallError`
      If calling `predictor` fails at runtime.
  :py:class:`alibi.exceptions.PredictorReturnTypeError`
      If the return type of `predictor` is not `np.ndarray`.
docstr_tok: ["Initialize","anchor","image","explainer",".","Parameters","--","--","--","--","--","predictor","A","callable","that","takes","a","`","numpy","`","array","of","`","N","`","data","points","as","inputs","and","returns","`","N","`","outputs",".","image_shape","Shape","of","the","image","to","be","explained",".","The","channel","axis","is","expected","to","be","last",".","dtype","A","`","numpy","`","scalar","type","that","corresponds","to","the","type","of","input","array","expected","by","`","predictor","`",".","This","may","be","used","to","construct","arrays","of","the","given","type","to","be","passed","through","the","`","predictor","`",".","For","most","use","cases","this","argument","should","have","no","effect",",","but","it","is","exposed","for","use","with","predictors","that","would","break","when","called","with","an","array","of","unsupported","type",".","segmentation_fn","Any","of","the","built","in","segmentation","function","strings",":","``","'felzenszwalb","'","``",",","``","'slic","'","``","or","``","'quickshift","'","``","or","a","custom","segmentation","function","(","callable",")","which","returns","an","image","mask","with","labels","for","each","superpixel",".","The","segmentation","function","is","expected","to","return","a","segmentation","mask","containing","all","integer","values","from","`","0","`","to","`","K-1","`",",","where","`","K","`","is","the","number","of","image","segments","(","superpixels",")",".","See","http",":","\/\/scikit-image.org\/docs\/dev\/api\/skimage.segmentation.html","for","more","info",".","segmentation_kwargs","Keyword","arguments","for","the","built","in","segmentation","functions",".","images_background","Images","to","overlay","superpixels","on",".","seed","If","set",",","ensures","different","runs","with","the","same","input","will","yield","same","explanation",".","Raises","--","--","--",":","py",":","class",":","`","alibi.exceptions.PredictorCallError","`","If","calling","`","predictor","`","fails","at","runtime",".",":","py",":","class",":","`","alibi.exceptions.PredictorReturnTypeError","`","If","the","return","type","of","`","predictor","`","is","not","`","np.ndarray","`","."]
code: def __init__(
      self,
      predictor: Callable[[np.ndarray], np.ndarray],
      image_shape: tuple,
      dtype: Type[np.generic] = np.float32,
      segmentation_fn: Any = "slic",
      segmentation_kwargs: Optional[dict] = None,
      images_background: Optional[np.ndarray] = None,
      seed: Optional[int] = None,
  ) -> None:
      """
      Initialize anchor image explainer.
  
      Parameters
      ----------
      predictor
          A callable that takes a `numpy` array of `N` data points as inputs and returns `N` outputs.
      image_shape
          Shape of the image to be explained. The channel axis is expected to be last.
      dtype
          A `numpy` scalar type that corresponds to the type of input array expected by `predictor`. This may be
          used to construct arrays of the given type to be passed through the `predictor`. For most use cases
          this argument should have no effect, but it is exposed for use with predictors that would break when
          called with an array of unsupported type.
      segmentation_fn
          Any of the built in segmentation function strings: ``'felzenszwalb'``, ``'slic'`` or ``'quickshift'`` or
          a custom segmentation function (callable) which returns an image mask with labels for each superpixel.
          The segmentation function is expected to return a segmentation mask containing all integer values
          from `0` to `K-1`, where `K` is the number of image segments (superpixels).
          See http://scikit-image.org/docs/dev/api/skimage.segmentation.html for more info.
      segmentation_kwargs
          Keyword arguments for the built in segmentation functions.
      images_background
          Images to overlay superpixels on.
      seed
          If set, ensures different runs with the same input will yield same explanation.
  
      Raises
      ------
      :py:class:`alibi.exceptions.PredictorCallError`
          If calling `predictor` fails at runtime.
      :py:class:`alibi.exceptions.PredictorReturnTypeError`
          If the return type of `predictor` is not `np.ndarray`.
      """
      super().__init__(meta=copy.deepcopy(DEFAULT_META_ANCHOR))
      np.random.seed(seed)
  
      # TODO: this logic needs improvement. We should check against a fixed set of strings
      # for built-ins instead of any `str`.
      if (
          isinstance(segmentation_fn, str)
          and segmentation_kwargs is None
      ):
          try:
              segmentation_kwargs = DEFAULT_SEGMENTATION_KWARGS[
                  segmentation_fn
              ]
          except KeyError:
              logger.warning(
                  "DEFAULT_SEGMENTATION_KWARGS did not contain any entry"
                  "for segmentation method {}. No kwargs will be passed to"
                  "the segmentation function!".format(segmentation_fn)
              )
              segmentation_kwargs = {}
      elif callable(segmentation_fn) and segmentation_kwargs:
          logger.warning(
              "Specified both a segmentation function to create superpixels and "
              "keyword arguments for built-in segmentation functions. By default "
              "the specified segmentation function will be used."
          )
  
      # set the predictor
      self.image_shape = tuple(image_shape)  # coerce lists
      self.dtype = dtype
      self.predictor = self._transform_predictor(predictor)
  
      # segmentation function is either a user-defined function or one of the values in
      fn_options = {
          "felzenszwalb": felzenszwalb,
          "slic": slic,
          "quickshift": quickshift,
      }
      if callable(segmentation_fn):
          self.custom_segmentation = True
          self.segmentation_fn = segmentation_fn
      else:
          self.custom_segmentation = False
          self.segmentation_fn = partial(fn_options[segmentation_fn], **segmentation_kwargs)  # type: ignore[arg-type]
  
      self.images_background = images_background
      # a superpixel is perturbed with prob 1 - p_sample
      self.p_sample: float = 0.5
  
      # update metadata
      self.meta["params"].update(
          custom_segmentation=self.custom_segmentation,
          segmentation_kwargs=segmentation_kwargs,
          p_sample=self.p_sample,
          seed=seed,
          image_shape=self.image_shape,
          images_background=self.images_background,
      )
      if not self.custom_segmentation:
          self.meta["params"].update(segmentation_fn=segmentation_fn)
      else:
          self.meta["params"].update(segmentation_fn="custom")
  
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 1 to create a new AnchorImage object from inherited base classes: Explainer with example: obj = AnchorImage(predictor, image_shape, dtype, segmentation_fn, segmentation_kwargs, images_background, seed)