code: def test_tf_keras_iris_explainer(
      disable_tf2, iris_data, tf_keras_iris_explainer, use_kdtree, k
  ):
      model, cf = tf_keras_iris_explainer
      X_train = iris_data["X_train"]
  
      # instance to be explained
      x = X_train[0].reshape(1, -1)
      pred_class = np.argmax(model.predict(x))
      not_pred_class = np.argmin(model.predict(x))
  
      # test fit
      cf.fit(X_train)
      if use_kdtree:  # k-d trees
          assert (
              len(cf.kdtrees) == cf.classes
          )  # each class has a k-d tree
          n_by_class = 0
          for c in range(cf.classes):
              n_by_class += cf.X_by_class[c].shape[0]
          assert (
              n_by_class == X_train.shape[0]
          )  # all training instances are stored in the trees
          assert (
              cf.kdtrees[pred_class].query(x, k=1)[0] == 0.0
          )  # nearest distance to own class equals 0
          assert (
              cf.score(x, not_pred_class, pred_class) == 0.0
          )  # test score fn
      else:  # encoder
          assert len(list(cf.class_proto.keys())) == cf.classes
          assert [True for _ in range(cf.classes)] == [
              v.shape == (1, 2) for _, v in cf.class_proto.items()
          ]
          n_by_class = 0
          for c in range(cf.classes):
              n_by_class += cf.class_enc[c].shape[0]
          assert (
              n_by_class == X_train.shape[0]
          )  # all training instances encoded
  
      # test explanation
      explanation = cf.explain(x, k=k)
      assert cf.id_proto != pred_class
      assert (
          np.argmax(model.predict(explanation.cf["X"]))
          == explanation.cf["class"]
      )
      assert (
          explanation.cf["grads_num"].shape
          == explanation.cf["grads_graph"].shape
          == x.shape
      )
      assert explanation.meta.keys() == DEFAULT_META_CFP.keys()
      assert explanation.data.keys() == DEFAULT_DATA_CFP.keys()
  
      # test gradient shapes
      y = np.zeros((1, cf.classes))
      np.put(y, pred_class, 1)
      cf.predict = cf.predict.predict  # make model black box
      grads = cf.get_gradients(x, y, x.shape[1:], cf.cat_vars_ord)
      assert grads.shape == x.shape
  
file_path: alibi/alibi/explainers/tests/test_cfproto.py
imports: import numpy
  import pytest
  import tensorflow
  from alibi.api.defaults import DEFAULT_META_CFP, DEFAULT_DATA_CFP
  from alibi.explainers import CounterfactualProto
  from alibi.utils.mapping import ohe_to_ord, ord_to_num
label_desc: Use image node_id 2 for calling a global function with example usage: test_tf_keras_iris_explainer(disable_tf2, iris_data, tf_keras_iris_explainer, use_kdtree, k) without return types