code: def test_purify():
      # Test Base Case of all Zeros.
      np.random.seed(0)
      raw_mat = np.array(
          [np.array([0, 0]), np.array([0, 0])], dtype=np.float64
      )
      densities = np.ones_like(raw_mat)
      intercept, pure_marg1, pure_marg2, pure_mat, n_iter = purify(
          raw_mat.copy(),
          densities=densities,
          tol=1e-10,
          randomize=False,
      )
      assert np.abs(intercept) < 1e-10
      assert np.all(np.isclose(pure_mat, raw_mat, atol=1e-10))
      assert np.all(np.isclose(pure_marg1, 0, atol=1e-10))
      assert np.all(np.isclose(pure_marg2, 0, atol=1e-10))
  
      # Test with random matrix and uniform density.
      raw_mat = np.random.uniform(-1, 1, size=(n_rows, n_cols))
      densities = np.ones_like(raw_mat)
      intercept, pure_marg1, pure_marg2, pure_mat, n_iter = purify(
          raw_mat.copy(),
          densities=densities,
          tol=1e-10,
          randomize=False,
      )
      for i in range(n_rows):
          assert (
              np.abs(
                  np.average(pure_mat[i, :], weights=densities[i, :])
              )
              < 1e-10
          )
          assert np.all(
              np.isclose(
                  pure_marg1[i] + intercept,
                  np.average(raw_mat[i, :], weights=densities[i, :]),
                  atol=1e-10,
              )
          )
      for j in range(n_cols):
          assert (
              np.abs(
                  np.average(pure_mat[:, j], weights=densities[:, j])
              )
              < 1e-10
          )
          assert np.all(
              np.isclose(
                  pure_marg2[j] + intercept,
                  np.average(raw_mat[:, j], weights=densities[:, j]),
                  atol=1e-10,
              )
          )
      assert (
          n_iter == 1
      )  # Always takes a single iteration with uniform density.
  
      # Test with random matrix and random density.
      # Test with random matrix and uniform density.
      raw_mat = np.random.uniform(-1, 1, size=(n_rows, n_cols))
      densities = np.random.uniform(0, 100, size=raw_mat.shape)
      intercept, pure_marg1, pure_marg2, pure_mat, n_iter = purify(
          raw_mat.copy(),
          densities=densities,
          tol=1e-10,
          randomize=False,
      )
      for i in range(n_rows):
          assert (
              np.abs(
                  np.average(pure_mat[i, :], weights=densities[i, :])
              )
              < 1e-10
          )
      for j in range(n_cols):
          assert (
              np.abs(
                  np.average(pure_mat[:, j], weights=densities[:, j])
              )
              < 1e-10
          )
  
file_path: interpret/python/interpret-core/tests/glassbox/ebm/research/test_purify.py
imports: import numpy
  from interpret.glassbox._ebm._research._purify import _purify_row, _purify_col
  from interpret.glassbox._ebm._research import purify
label_desc: Use image node_id 3 for calling a global function with example usage: test_purify() without return types