code: def test_anchor_image(
      predict_fn, models, mnist_data, images_background
  ):
      x_train = mnist_data["X_train"]
      image = x_train[0]
  
      segmentation_fn = "slic"
      segmentation_kwargs = {
          "n_segments": 10,
          "compactness": 10,
          "sigma": 0.5,
      }
      image_shape = (28, 28, 1)
      images_background = x_train[:10] if images_background else None
  
      explainer = AnchorImage(
          predict_fn,
          image_shape,
          segmentation_fn=segmentation_fn,
          segmentation_kwargs=segmentation_kwargs,
          images_background=images_background,
      )
  
      p_sample = 0.5  # probability of perturbing a superpixel
      n_covered_ex = (
          3  # nb of examples where the anchor applies that are saved
      )
      sampler = AnchorImageSampler(
          predictor=explainer.predictor,
          segmentation_fn=explainer.segmentation_fn,
          custom_segmentation=explainer.custom_segmentation,
          image=image,
          images_background=explainer.images_background,
          p_sample=p_sample,
          n_covered_ex=n_covered_ex,
      )
  
      # test explainer initialization
      # dtype=np.float32 should be safe here (default behaviour when calling _transform_predictor
      assert explainer.predictor(
          np.zeros((1,) + image_shape, dtype=np.float32)
      ).shape == (1,)
      assert explainer.custom_segmentation is False
  
      # test explanation
      threshold = 0.95
  
      before_explain = explainer.__dict__
      explanation = explainer.explain(
          image, threshold=threshold, n_covered_ex=3
      )
      after_explain = explainer.__dict__
  
      # Ensure that explainer's internal state doesn't change
      assert before_explain == after_explain
      if explanation.raw["feature"]:
          assert (
              len(explanation.raw["examples"][-1]["covered_true"])
              <= sampler.n_covered_ex
          )
          assert (
              len(explanation.raw["examples"][-1]["covered_false"])
              <= sampler.n_covered_ex
          )
      else:
          assert not explanation.raw["examples"]
      assert explanation.anchor.shape == image_shape
      assert explanation.precision >= threshold
      assert len(np.unique(explanation.segments)) == len(
          np.unique(sampler.segments)
      )
      assert explanation.meta.keys() == DEFAULT_META_ANCHOR.keys()
      assert explanation.data.keys() == DEFAULT_DATA_ANCHOR_IMG.keys()
  
file_path: alibi/alibi/explainers/tests/test_anchor_image.py
imports: import pytest
  from pytest_lazyfixture import lazy_fixture
  import numpy
  import tensorflow
  import torch
  from alibi.api.defaults import DEFAULT_META_ANCHOR, DEFAULT_DATA_ANCHOR_IMG
  from alibi.exceptions import PredictorCallError, PredictorReturnTypeError
  from alibi.explainers.anchors.anchor_image import AnchorImage, AnchorImageSampler, scale_image
label_desc: Use image node_id 4 for calling a global function with example usage: test_anchor_image(predict_fn, models, mnist_data, images_background) without return types