code: def test_sampler(predict_fn, models, mnist_data):
      eps = 0.0001  # tolerance for tensor comparisons
      num_samples = 10
  
      x_train = mnist_data["X_train"]
      segmentation_fn = "slic"
      segmentation_kwargs = {
          "n_segments": 10,
          "compactness": 10,
          "sigma": 0.5,
      }
      image_shape = (28, 28, 1)
      explainer = AnchorImage(
          predict_fn,
          image_shape,
          segmentation_fn=segmentation_fn,
          segmentation_kwargs=segmentation_kwargs,
      )
  
      image = x_train[0]
      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,
      )
  
      image_preproc = sampler._preprocess_img(image)
      superpixels_mask = sampler._choose_superpixels(
          num_samples=num_samples
      )
  
      # grayscale image should be replicated across channel dim before segmentation
      assert image_preproc.shape[-1] == 3
      for channel in range(image_preproc.shape[-1]):
          assert (
              image.squeeze() - image_preproc[..., channel] <= eps
          ).all()
      # check superpixels mask
      assert superpixels_mask.shape[0] == num_samples
      assert superpixels_mask.shape[1] == len(
          list(np.unique(sampler.segments))
      )
      assert (
          superpixels_mask.sum(axis=1).any()
          <= segmentation_kwargs["n_segments"]
      )
      assert superpixels_mask.any() <= 1
  
      cov_true, cov_false, labels, data, coverage, _ = sampler(
          (0, ()), num_samples
      )
      assert data.shape[0] == labels.shape[0]
      assert data.shape[1] == len(np.unique(sampler.segments))
      assert coverage == -1
  
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 3 for calling a global function with example usage: test_sampler(predict_fn, models, mnist_data) without return types