code: def _purify_once(
      mat, densities=None, m1=None, m2=None, randomize=False
  ):
      # Purify ecah row and column of the matrix mat.
      if densities is None:
          densities = np.ones_like(mat)
      # m1 along first axis
      if m1 is None:
          m1 = np.zeros((mat.shape[0], 1))
  
      # m2 along second axis
      if m2 is None:
          m2 = np.zeros((mat.shape[1], 1))
  
      if (
          randomize
      ):  # randomize each row/col selection rather than in-order
          nonzero_rows = set(list(range(mat.shape[0])))
          nonzero_cols = set(list(range(mat.shape[1])))
          for iteration in range(mat.shape[0] + mat.shape[1]):
              if np.random.binomial(1, 0.5) and len(nonzero_rows) > 0:
                  i = np.random.choice(
                      list(nonzero_rows)
                  )  # todo: maybe slow
                  nonzero_rows.remove(i)
                  mat, m1 = _purify_row(mat, m1, densities, i)
              elif len(nonzero_cols) > 0:
                  j = np.random.choice(list(nonzero_cols))
                  nonzero_cols.remove(j)
                  mat, m2 = _purify_col(mat, m2, densities, j)
  
      # Fix each row mean
      for i in range(mat.shape[0]):
          mat, m1 = _purify_row(mat, m1, densities, i)
      # Fix each col mean
      for j in range(mat.shape[1]):
          mat, m2 = _purify_col(mat, m2, densities, j)
  
      return np.squeeze(m1), np.squeeze(m2), mat
  
imports: import numpy
label_desc: Use image node_id 3 for calling a global function with example usage: _purify_once(mat, densities, m1, m2, randomize) and returns: np,  np,  mat