def ranking_metric_tensor(exprs, method, permutation_num, pos, neg, classes,<EOL>ascending, rs=np.random.RandomState()):
<EOL>G, S = exprs.shape<EOL>expr_mat = exprs.values.T<EOL>perm_cor_tensor = np.tile(expr_mat, (permutation_num+<NUM_LIT:1>,<NUM_LIT:1>,<NUM_LIT:1>))<EOL>for arr in perm_cor_tensor[:-<NUM_LIT:1>]: rs.shuffle(arr)<EOL>classes = np.array(classes)<EOL>pos = classes == pos<EOL>neg = classes == neg<EOL>pos_cor_mean = perm_cor_tensor[:,pos,:].mean(axis=<NUM_LIT:1>)<EOL>neg_cor_mean = perm_cor_tensor[:,neg,:].mean(axis=<NUM_LIT:1>)<EOL>pos_cor_std = perm_cor_tensor[:,pos,:].std(axis=<NUM_LIT:1>, ddof=<NUM_LIT:1>)<EOL>neg_cor_std = perm_cor_tensor[:,neg,:].std(axis=<NUM_LIT:1>, ddof=<NUM_LIT:1>)<EOL>if method == '<STR_LIT>':<EOL><INDENT>cor_mat = (pos_cor_mean - neg_cor_mean)/(pos_cor_std + neg_cor_std)<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>denom = <NUM_LIT:1.0>/G<EOL>cor_mat = (pos_cor_mean - neg_cor_mean)/ np.sqrt(denom*pos_cor_std**<NUM_LIT:2> + denom*neg_cor_std**<NUM_LIT:2>)<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>cor_mat = pos_cor_mean / neg_cor_mean<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>cor_mat  = pos_cor_mean - neg_cor_mean<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>cor_mat  =  np.log2(pos_cor_mean / neg_cor_mean)<EOL><DEDENT>else:<EOL><INDENT>logging.error("<STR_LIT>")<EOL>sys.exit(<NUM_LIT:0>)<EOL><DEDENT>cor_mat_ind = cor_mat.argsort()<EOL>cor_mat.sort()<EOL>if ascending: return cor_mat_ind, cor_mat<EOL>return cor_mat_ind[:, ::-<NUM_LIT:1>], cor_mat[:, ::-<NUM_LIT:1>]<EOL>
Build shuffled ranking matrix when permutation_type eq to phenotype.

       :param exprs:   gene_expression DataFrame, gene_name indexed.
       :param str method:  calculate correlation or ranking. methods including:
                           1. 'signal_to_noise'.
                           2. 't_test'.
                           3. 'ratio_of_classes' (also referred to as fold change).
                           4. 'diff_of_classes'.
                           5. 'log2_ratio_of_classes'.
       :param int permuation_num: how many times of classes is being shuffled
       :param str pos: one of labels of phenotype's names.
       :param str neg: one of labels of phenotype's names.
       :param list classes:  a list of phenotype labels, to specify which column of
                             dataframe belongs to what class of phenotype.
       :param bool ascending:  bool. Sort ascending vs. descending.

       :return:
                returns two 2d ndarray with shape (nperm, gene_num).

                | cor_mat_indices: the indices of sorted and permutated (exclude last row) ranking matrix.
                | cor_mat: sorted and permutated (exclude last row) ranking matrix.