code: def soft_cluster_factor(factor):
      
  
      # copy factor of interest
      f = np.copy(factor)
  
      # cluster based on score of maximum absolute value
      cluster_ids = np.argmax(np.abs(f), axis=1)
      scores = f[range(f.shape[0]), cluster_ids]
  
      # resort within each cluster
      perm = []
      for cluster in np.unique(cluster_ids):
          idx = np.where(cluster_ids == cluster)[0]
          perm += list(idx[np.argsort(scores[idx])][::-1])
  
      return cluster_ids, perm
docs: Returns soft-clustering of data based on CP decomposition results.
  
      Parameters
      ----------
      data : ndarray, N x R matrix of nonnegative data
          Datapoints are held in rows, features are held in columns
  
      Returns
      -------
      cluster_ids : ndarray, vector of N integers in range(0, R)
          List of soft cluster assignments for each row of data matrix
      perm : ndarray, vector of N integers
          Permutation / ordering of the rows of data induced by the soft
          clustering.