code: def test_fast__PerturbationAnalysis():
      # Some test data
      if kbackend.image_data_format() == "channels_first":
          input_shape = (2, 1, 4, 4)
      else:
          input_shape = (2, 4, 4, 1)
      x = np.arange(2 * 4 * 4).reshape(input_shape)
      generator = isequence.BatchSequence(
          [x, np.zeros(x.shape[0])], batch_size=x.shape[0]
      )
  
      # Simple model
      model = kmodels.Sequential(
          [
              klayers.Flatten(input_shape=x.shape[1:]),
              klayers.Dense(1, use_bias=False),
          ]
      )
  
      weights = np.arange(4 * 4 * 1).reshape((4 * 4, 1))
      model.layers[-1].set_weights([weights])
      model.compile(loss="mean_squared_error", optimizer="sgd")
  
      expected_output = np.array([[1240.0], [3160.0]])
      assert np.all(np.isclose(model.predict(x), expected_output))
  
      # Analyzer
      analyzer = innvestigate.create_analyzer(
          "gradient", model, postprocess="abs"
      )
  
      # Run perturbation analysis
      perturbation = innvestigate.tools.perturbate.Perturbation(
          "zeros", region_shape=(2, 2), in_place=False
      )
  
      perturbation_analysis = (
          innvestigate.tools.perturbate.PerturbationAnalysis(
              analyzer,
              model,
              generator,
              perturbation,
              recompute_analysis=False,
              steps=3,
              regions_per_step=1,
              verbose=False,
          )
      )
  
      scores = perturbation_analysis.compute_perturbation_analysis()
  
      expected_scores = np.array(
          [5761600.0, 1654564.0, 182672.0, 21284.0]
      )
      assert np.all(np.isclose(scores, expected_scores))
  
file_path: innvestigate/tests/tools/test_perturbate.py
imports: from __future__ import annotations
  import numpy
  import pytest
  import tensorflow.keras.backend
  import tensorflow.keras.layers
  import tensorflow.keras.models
  import innvestigate.tools.perturbate
  import innvestigate.utils.sequence
label_desc: Use image node_id 1 for calling a global function with example usage: test_fast__PerturbationAnalysis() without return types