code: def test_serialization(
      explainer_type,
      model,
      masker,
      data,
      rtol=1e-05,
      atol=1e-8,
      **kwargs,
  ):
      """Test serialization with a given explainer algorithm."""
  
      explainer_kwargs = {
          k: v for k, v in kwargs.items() if k in ["algorithm"]
      }
      explainer_original = explainer_type(
          model, masker, **explainer_kwargs
      )
      shap_values_original = explainer_original(data[:1])
  
      # Serialization
      with tempfile.TemporaryFile() as temp_serialization_file:
          save_kwargs = {
              k: v
              for k, v in kwargs.items()
              if k in ["model_saver", "masker_saver"]
          }
          explainer_original.save(
              temp_serialization_file, **save_kwargs
          )
  
          # Deserialization
          temp_serialization_file.seek(0)
          load_kwargs = {
              k: v
              for k, v in kwargs.items()
              if k in ["model_loader", "masker_loader"]
          }
          explainer_new = explainer_type.load(
              temp_serialization_file, **load_kwargs
          )
  
      call_kwargs = {
          k: v for k, v in kwargs.items() if k in ["max_evals"]
      }
      shap_values_new = explainer_new(data[:1], **call_kwargs)
  
      assert np.allclose(
          shap_values_original.base_values,
          shap_values_new.base_values,
          rtol=rtol,
          atol=atol,
      )
      assert np.allclose(
          shap_values_original[0].values,
          shap_values_new[0].values,
          rtol=rtol,
          atol=atol,
      )
      assert isinstance(explainer_original, type(explainer_new))
      assert isinstance(
          explainer_original.masker, type(explainer_new.masker)
      )
  
file_path: shap/tests/explainers/common.py
imports: import tempfile
  import numpy
  import pytest
  import shap
label_desc: Use image node_id 4 for calling a global function with example usage: test_serialization(explainer_type, model, masker, data, rtol, atol) without return types