class_docstr: To run: "python -m saliency.tf1.xrai_test" from top-level saliency directory.
class_docstr_tok: ["To","run",":","``","python","-m","saliency.tf1.xrai_test","''","from","top-level","saliency","directory","."]
docstr: Test that the model is called and used to calculate the XRAI mask.
docstr_tok: ["Test","that","the","model","is","called","and","used","to","calculate","the","XRAI","mask","."]
code: def testXraiFunctional(self):
      """Test that the model is called and used to calculate the XRAI mask."""
      self.xrai_instance = xrai.XRAI(
          self.graph, self.sess_spy, self.y, self.x
      )
      self.xrai_instance.validate_xy_tensor_shape = mock.MagicMock()
      feed_dict = {}
      baselines = np.array([[0, 0, 3], [0, 0, 0]])
      segments = [[True, True, False], [False, False, True]]
      batch_size = 9
      steps = 100
      extra_parameters = xrai.XRAIParameters(
          steps=steps,
          return_ig_attributions=True,
          return_xrai_segments=True,
      )
      # Reducing min_pixel_diff lets us split up this very small 1x3 "image"
      extra_parameters.experimental_params["min_pixel_diff"] = 0
      expected_segments = np.array([1, 1, 2])
      x_value = np.array([3, 2, 3])
      # equation is 5x + y^2 + 2z, but first baseline has z_baseline=z_input
      expected_ig = np.array(
          [
              [x_value[0] * 5, x_value[1] * x_value[1], 0],
              [x_value[0] * 5, x_value[1] * x_value[1], x_value[2] * 2],
          ]
      )
      expected_mask = np.mean(expected_ig, axis=0)
      # segment masks have the first two inputs together
      expected_mask[0:2] = np.mean(expected_mask[0:2])  # [9.5, 9.5, 3]
      expected_calls = 24  # batch size is 9, 2*ceil(100/9)=24
      expected_validate_args = (steps, batch_size)
  
      mask_details = self.xrai_instance.GetMaskWithDetails(
          x_value=x_value,
          feed_dict=feed_dict,
          baselines=baselines,
          segments=segments,
          batch_size=batch_size,
          extra_parameters=extra_parameters,
      )
      validate_args = (
          self.xrai_instance.validate_xy_tensor_shape.call_args[0]
      )
  
      # Verify the result.
      self.assertEqual(validate_args, expected_validate_args)
      np.testing.assert_almost_equal(
          mask_details.attribution_mask, expected_mask, decimal=4
      )
      np.testing.assert_almost_equal(
          mask_details.ig_attribution, expected_ig, decimal=4
      )
      np.testing.assert_almost_equal(
          mask_details.segments, expected_segments, decimal=4
      )
      np.testing.assert_almost_equal(
          mask_details.baselines, baselines, decimal=4
      )
      self.assertEqual(self.sess_spy.run.call_count, expected_calls)
  
file_path: saliency/saliency/tf1/xrai_test.py
imports: import unittest
  import unittest.mock
  from .None import xrai
  import numpy
  import tensorflow.compat.v1
label_desc: Use image node_id 4 for calling the XRAITest obj's underlying member method code with example usage: obj.testXraiFunctional() without return types