code: def test_explainer(
      n_explainer_runs,
      at_defaults,
      rf_classifier,
      explainer,
      test_instance_idx,
      caplog,
  ):
      """
      Convergence test on Adult and Iris datasets.
      """
  
      # fixture returns a fitted AnchorTabular explainer
      X_test, explainer, predict_fn, predict_type = explainer
      if predict_type == "proba":
          instance_label = np.argmax(
              predict_fn(X_test[test_instance_idx, :].reshape(1, -1)),
              axis=1,
          )
      else:
          instance_label = predict_fn(
              X_test[test_instance_idx, :].reshape(1, -1)
          )[0]
  
      explainer.instance_label = instance_label
  
      explain_defaults = at_defaults
      threshold = explain_defaults["desired_confidence"]
      n_covered_ex = explain_defaults["n_covered_ex"]
  
      run_precisions = []
      for _ in range(n_explainer_runs):
          explanation = explainer.explain(
              X_test[test_instance_idx],
              threshold=threshold,
              **explain_defaults,
          )
          assert explainer.instance_label == instance_label
          if "Could not find" not in caplog.text:
              assert explanation.precision >= threshold
          assert explanation.coverage >= 0.001
          assert explanation.meta.keys() == DEFAULT_META_ANCHOR.keys()
          assert explanation.data.keys() == DEFAULT_DATA_ANCHOR.keys()
          run_precisions.append(explanation.precision)
  
      # check that 80% of runs returned a valid anchor
      # removed as the algorithm is too stochastic to pass for the fixed instance
      # assert ((np.asarray(run_precisions) > threshold).sum()) / n_explainer_runs >= 0.80
  
      sampler = explainer.samplers[0]
      assert sampler.instance_label == instance_label
      assert sampler.n_covered_ex == n_covered_ex
  
file_path: alibi/alibi/explainers/tests/test_anchor_tabular.py
imports: from collections import OrderedDict, defaultdict
  from copy import deepcopy
  import numpy
  import pytest
  from pytest_lazyfixture import lazy_fixture
  from alibi.api.defaults import DEFAULT_META_ANCHOR, DEFAULT_DATA_ANCHOR
  from alibi.exceptions import NotFittedError, PredictorCallError, PredictorReturnTypeError
  from alibi.explainers import AnchorTabular, DistributedAnchorTabular
  from alibi.explainers.tests.utils import predict_fcn
label_desc: Use image node_id 2 for calling a global function with example usage: test_explainer(n_explainer_runs, at_defaults, rf_classifier, explainer, test_instance_idx, caplog) without return types