code: def test_fast__Perturbation():
      if kbackend.image_data_format() == "channels_first":
          input_shape = (1, 1, 4, 4)
      else:
          input_shape = (1, 4, 4, 1)
      x = np.arange(1 * 4 * 4).reshape(input_shape)
  
      perturbation = innvestigate.tools.perturbate.Perturbation(
          "zeros", region_shape=(2, 2), in_place=False
      )
  
      analysis = np.zeros((4, 4))
      analysis[:2, 2:] = 1
      analysis[2:, :2] = 2
      analysis[2:, 2:] = 3
      analysis = analysis.reshape(input_shape)
  
      if kbackend.image_data_format() == "channels_last":
          x = np.moveaxis(x, 3, 1)
          analysis = np.moveaxis(analysis, 3, 1)
  
      analysis = perturbation.reduce_function(
          analysis, axis=1, keepdims=True
      )
  
      aggregated_regions = perturbation._aggregate_regions(analysis)
      assert np.all(
          np.isclose(
              aggregated_regions[0, 0, :, :], np.array([[0, 1], [2, 3]])
          )
      )
  
      ranks = perturbation._compute_region_ordering(aggregated_regions)
      assert np.all(
          np.isclose(ranks[0, 0, :, :], np.array([[3, 2], [1, 0]]))
      )
  
      perturbation_mask_regions = (
          perturbation._compute_perturbation_mask(ranks, 1)
      )
      assert np.all(
          perturbation_mask_regions == np.array([[0, 0], [0, 1]])
      )
  
      perturbation_mask_regions = (
          perturbation._compute_perturbation_mask(ranks, 4)
      )
      assert np.all(
          perturbation_mask_regions == np.array([[1, 1], [1, 1]])
      )
  
      perturbation_mask_regions = (
          perturbation._compute_perturbation_mask(ranks, 0)
      )
      assert np.all(
          perturbation_mask_regions == np.array([[0, 0], [0, 0]])
      )
  
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 2 for calling a global function with example usage: test_fast__Perturbation() without return types