code: def purify(
      mat, densities=None, verbose=False, tol=1e-6, randomize=False
  ):
      # Move the means of the rows and columns into the marginal effects,
      # respecting sample density.
      # If randomize is True, then the order of row and columns is randomly
      # selected; otherwise they are processed in order.
  
      if densities is None:  # Use a uniform density
          densities = np.ones_like(mat)
      i = 1
      m1, m2, mat = _purify_once(mat, densities)
      row_means = _calc_row_means(mat, densities)
      col_means = _calc_col_means(mat, densities)
      max_row = np.max(np.abs(row_means))
      max_col = np.max(np.abs(col_means))
      while max_row > tol or max_col > tol:
          i += 1
          m1, m2, mat = _purify_once(mat, densities, m1, m2, randomize)
          row_means = _calc_row_means(mat, densities)
          col_means = _calc_col_means(mat, densities)
          max_row = np.max(np.abs(row_means))
          max_col = np.max(np.abs(col_means))
      # Center m1 and m2
      intercept = 0.0
      intercept += np.average(m1, weights=np.sum(densities, axis=1))
      m1 -= np.average(m1, weights=np.sum(densities, axis=1))
      intercept += np.average(m2, weights=np.sum(densities, axis=0))
      m2 -= np.average(m2, weights=np.sum(densities, axis=0))
      return intercept, m1, m2, mat, i
  
imports: import numpy
label_desc: Use image node_id 6 for calling a global function with example usage: purify(mat, densities, verbose, tol, randomize) and returns: intercept,  m1,  m2,  mat,  i