code: def test_additivity(explainer_type, model, masker, data, **kwargs):
      """Test explainer and masker for additivity on a single output prediction problem."""
      explainer = explainer_type(model, masker, **kwargs)
      shap_values = explainer(data)
  
      # a multi-output additivity check
      if len(shap_values.shape) == 3:
          # this works with ragged arrays and for models that we can't call directly (they get auto-wrapped)
          for i in range(shap_values.shape[0]):
              row = shap_values[i]
              if callable(explainer.masker.shape):
                  all_on_masked = explainer.masker(
                      np.ones(
                          explainer.masker.shape(data[i])[1], dtype=bool
                      ),
                      data[i],
                  )
              else:
                  all_on_masked = explainer.masker(
                      np.ones(explainer.masker.shape[1], dtype=bool),
                      data[i],
                  )
              if not isinstance(all_on_masked, tuple):
                  all_on_masked = (all_on_masked,)
              out = explainer.model(*all_on_masked)
              assert np.max(
                  np.abs(row.base_values + row.values.sum(0) - out)
                  < 1e6
              )
      else:
          assert np.max(
              np.abs(
                  shap_values.base_values
                  + shap_values.values.sum(1)
                  - model(data)
              )
              < 1e6
          )
  
file_path: shap/tests/explainers/common.py
imports: import tempfile
  import numpy
  import pytest
  import shap
label_desc: Use image node_id 2 for calling a global function with example usage: test_additivity(explainer_type, model, masker, data) without return types