code: def test_explain_kernel(
      monkeypatch,
      mock_kernel_shap_explainer,
      use_groups,
      summarise_result,
      data_type,
  ):
      """
      Integration tests, runs .explain method to check output dimensions are as expected.
      """
  
      # create fake data and records to explain
      seed = 0
      n_feats, n_samples, n_instances = 15, 20, 2
      background_data = get_data(
          data_type, n_rows=n_samples, n_cols=n_feats, seed=seed
      )
      instances = get_data(
          data_type, n_rows=n_instances, n_cols=n_feats, seed=seed + 1
      )
  
      # create groups
      if use_groups:
          group_names, groups, _ = setup_groups_and_weights(
              (n_feats, n_samples),
              b_group_names=True,
              b_groups=True,
              b_weights=False,
              seed=seed,
          )
  
      else:
          groups, group_names = None, None
  
      # create arrays with categorical variables start indices and encodings dim
      if summarise_result:
          if use_groups:
              cat_vars_start_idx, cat_vars_enc_dim = [], []
              start_idx = 0
              for group in groups:
                  if len(group) > 1:
                      cat_vars_start_idx.append(start_idx)
                      cat_vars_enc_dim.append(len(group))
                      start_idx += len(group)
                  else:
                      start_idx += 1
              if cat_vars_start_idx is None:
                  grp = itertools.chain(groups[-2:])
                  groups = groups[:-2]
                  groups.append(grp)
                  cat_vars_start_idx.append(n_feats - 2)
                  cat_vars_enc_dim.append(2)
          else:
              cat_vars_start_idx, cat_vars_enc_dim = [0], [2]
      else:
          cat_vars_start_idx, cat_vars_enc_dim = None, None
  
      # initialise and fit explainer
      explainer = mock_kernel_shap_explainer
      monkeypatch.setattr(shap, "kmeans", KMeansMock())
      explainer.use_groups = use_groups
      explainer.fit(
          background_data, group_names=group_names, groups=groups
      )
  
      # Explain some instances. Note that we disable the default 'auto' regularization which causes sklearn to throw
      # ValueError in LassoLARSIC. The noise variance estimate used in the AIC criterion is obtained by solving an
      # Ordinary Least Squares problem: ||Ax - b||^2, where matrix A has nsamples(4) rows. The error is thrown when the
      # number of rows in A is less than its number of columns. Note that when the number of rows is less than the
      # number of columns, the solution is not unique, and depending on the rank(A), the solution might be exact
      # (i.e. variance of zero). An alternative would be to increase the number of rows which corresponds to increasing
      # nsamples. Increasing the number of rows will also bypass the warning message returned by KernelSHAP. KernelSHAP
      # solves a Weighted Least Squares problem, and uses the pseudo-inverse when inversion is not possible. If the
      # pseudo-inverse is used, KernalSHAP throws a warning along with some suggestions to overcome obtaining a
      # singular matrix.
      explanation = explainer.explain(
          instances,
          summarise_result=summarise_result,
          cat_vars_enc_dim=cat_vars_enc_dim,
          cat_vars_start_idx=cat_vars_start_idx,
          nsamples=4,
          l1_reg=False,
      )
  
      # check that explanation metadata and data keys are as expected
      assert explanation.meta.keys() == DEFAULT_META_KERNEL_SHAP.keys()
      assert explanation.data.keys() == DEFAULT_DATA_KERNEL_SHAP.keys()
  
      # parameter keys
      assert set(explanation.meta["params"]) == set(KERNEL_SHAP_PARAMS)
  
      # check the output has expected shapes given the inputs
      n_outs = explainer.predictor.out_dim
      shap_values = [val for val in explanation["shap_values"]]
      n_shap_values = [arr.shape[1] for arr in shap_values]
      inst_explained = [arr.shape[0] for arr in shap_values]
      assert len(set(inst_explained)) == 1
      shap_dims = set(n_shap_values)
      assert len(shap_dims) == 1
      assert inst_explained[0] == n_instances
      assert len(shap_values) == n_outs
      assert explanation.raw["raw_prediction"].shape == (
          n_instances,
          n_outs,
      )
      assert explanation.raw["instances"].shape == (
          n_instances,
          n_feats,
      )
      assert len(explanation.raw["prediction"].squeeze()) == n_instances
  
      # check dimensions of shap value arrays returned
      if use_groups:
          assert not explainer.summarise_result
          assert len(groups) in shap_dims
      else:
          if summarise_result:
              assert explainer.summarise_result
              assert (
                  n_feats
                  - sum(cat_vars_enc_dim)
                  + len(cat_vars_start_idx)
                  in shap_dims
              )
          else:
              assert n_feats in shap_dims
  
imports: import itertools
  import logging
  import unittest
  from copy import copy
  from itertools import chain
  from unittest.mock import MagicMock
  from typing import Any, List
  import catboost
  import pandas
  import pytest
  import scipy.sparse
  import sklearn
  import shap
  import numpy
  import pandas
  import shap.utils._legacy
  from numpy.testing import assert_allclose, assert_almost_equal
  from scipy.special import expit
  from alibi.api.defaults import DEFAULT_META_KERNEL_SHAP, DEFAULT_DATA_KERNEL_SHAP, DEFAULT_META_TREE_SHAP, DEFAULT_DATA_TREE_SHAP, KERNEL_SHAP_PARAMS, TREE_SHAP_PARAMS
  from alibi.explainers.shap_wrappers import sum_categories, rank_by_importance, KernelExplainerWrapper
  from alibi.explainers.shap_wrappers import KERNEL_SHAP_BACKGROUND_THRESHOLD, TREE_SHAP_BACKGROUND_WARNING_THRESHOLD
  from alibi.explainers.tests.utils import get_random_matrix
  from alibi.tests.utils import assert_message_in_logs, not_raises
  from alibi.utils import DistributedExplainer
label_desc: Use image node_id 19 for calling a global function with example usage: test_explain_kernel(monkeypatch, mock_kernel_shap_explainer, use_groups, summarise_result, data_type) without return types