code: def test_rank_by_importance(n_outputs, data_dimension):
      """
      Tests the feature effects ranking function.
      """
  
      def get_column_ranks(X, ascending=False):
          """
          Ranks the columns of X according to the average magnitude value
          and returns an array of ranking indices and a an array of
          sorted values according to the ranking.
          """
  
          avg_mag = np.mean(np.abs(X), axis=0)
          rank = np.argsort(avg_mag)
          if ascending:
              return rank, avg_mag[rank]
          else:
              return rank[::-1], avg_mag[rank][::-1]
  
      # setup explainer
      n_samples, n_features = data_dimension
      feature_names = gen_group_names(n_features)
  
      # create inputs
      (n_outs,) = n_outputs
      shap_values = [
          get_random_matrix(n_rows=n_samples, n_cols=n_features)
          for _ in range(n_outs)
      ]
  
      # compute desired values
      exp_ranked_effects_class = {}
      expected_feat_names_order = {}
      ranks_and_vals = [
          get_column_ranks(class_shap_vals)
          for class_shap_vals in shap_values
      ]
      ranks, vals = list(zip(*ranks_and_vals))
      for i, values in enumerate(vals):
          exp_ranked_effects_class[str(i)] = vals[i]
          expected_feat_names_order[str(i)] = [
              feature_names[k] for k in ranks[i]
          ]
      aggregate_shap = np.sum(shap_values, axis=0)
      (
          exp_aggregate_rank,
          exp_ranked_effects_aggregate,
      ) = get_column_ranks(aggregate_shap)
      exp_aggregate_names = [
          feature_names[k] for k in exp_aggregate_rank
      ]
  
      # check results
      importances = rank_by_importance(
          shap_values, feature_names=feature_names
      )
      assert len(importances.keys()) == n_outs + 1
      for key in importances:
          if key != "aggregated":
              assert_allclose(
                  importances[key]["ranked_effect"],
                  exp_ranked_effects_class[key],
              )
              assert (
                  importances[key]["names"]
                  == expected_feat_names_order[key]
              )
          else:
              assert_allclose(
                  importances[key]["ranked_effect"],
                  exp_ranked_effects_aggregate,
              )
              assert importances[key]["names"] == exp_aggregate_names
  
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 10 for calling a global function with example usage: test_rank_by_importance(n_outputs, data_dimension) and returns: rank,  avg_mag, rank,  avg_mag